CDVD: Adjust ram requirements when precaching on linux/Mac.

This commit is contained in:
lightningterror 2025-04-19 10:23:33 +02:00
parent 717775d9ab
commit 842190e15e
4 changed files with 32 additions and 19 deletions

View File

@ -50,6 +50,27 @@ u64 GetPhysicalMemory()
return getmem;
}
u64 GetAvailablePhysicalMemory()
{
const mach_port_t host_port = mach_host_self();
vm_size_t page_size;
if (host_page_size(host_port, &page_size) != KERN_SUCCESS)
return 0;
vm_statistics64_data_t vm_stat;
mach_msg_type_number_t host_size = sizeof(vm_statistics64_data_t) / sizeof(integer_t);
if (host_statistics64(host_port, HOST_VM_INFO, reinterpret_cast<host_info64_t>(&vm_stat), &host_size) != KERN_SUCCESS)
return 0;
const u64 free_pages = static_cast<u64>(vm_stat.free_count);
const u64 inactive_pages = static_cast<u64>(vm_stat.inactive_count);
const u64 get_available_mem = (free_pages + inactive_pages) * page_size;
return get_available_mem;
}
static mach_timebase_info_data_t s_timebase_info;
static const u64 tickfreq = []() {
if (mach_timebase_info(&s_timebase_info) != KERN_SUCCESS)

View File

@ -181,10 +181,7 @@ private:
extern u64 GetTickFrequency();
extern u64 GetCPUTicks();
extern u64 GetPhysicalMemory();
#ifdef _WIN32
// TODO: Someone do linux/mac.
extern u64 GetAvailablePhysicalMemory();
#endif
/// Spin for a short period of time (call while spinning waiting for a lock)
/// Returns the approximate number of ns that passed
extern u32 ShortSpin();

View File

@ -15,6 +15,7 @@
#include <dbus/dbus.h>
#include <spawn.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
@ -40,6 +41,15 @@ u64 GetPhysicalMemory()
return pages * getpagesize();
}
u64 GetAvailablePhysicalMemory()
{
struct sysinfo info;
if (sysinfo(&info) != 0)
return 0;
return static_cast<u64>(info.freeram) * info.mem_unit;
}
u64 GetTickFrequency()
{
return 1000000000; // unix measures in nanoseconds

View File

@ -266,12 +266,11 @@ bool ThreadedFileReader::Precache2(ProgressCallback* progress, Error* error)
bool ThreadedFileReader::CheckAvailableMemoryForPrecaching(u64 required_size, Error* error)
{
#ifdef _WIN32
// We want to check available physical memory instead of total.
const u64 memory_available = GetAvailablePhysicalMemory();
// Reserve 2GB of available memory for headroom.
constexpr u64 memory_reserve = 2147483648;
const u64 max_precache_size = std::max(0LL, static_cast<s64>(memory_available - memory_reserve));
const u64 max_precache_size = std::max(s64{0}, static_cast<s64>(memory_available - memory_reserve));
if (required_size > max_precache_size)
{
@ -280,20 +279,6 @@ bool ThreadedFileReader::CheckAvailableMemoryForPrecaching(u64 required_size, Er
(required_size + memory_reserve) / _1gb);
return false;
}
#else
// Don't allow precaching to use more than 50% of system memory.
// Hopefully nobody's running 2-4GB potatoes anymore....
const u64 memory_size = GetPhysicalMemory();
const u64 max_precache_size = memory_size / 2;
if (required_size > max_precache_size)
{
Error::SetStringFmt(error,
TRANSLATE_FS("CDVD", "Required memory ({}GB) is the above the maximum allowed ({}GB)."),
required_size / _1gb, max_precache_size / _1gb);
return false;
}
#endif
return true;
}