Add Program Size Memory access and log on start

This commit is contained in:
Wojtek Figat
2025-03-05 12:10:17 +01:00
parent 7809007a13
commit 06729f6b62
5 changed files with 41 additions and 30 deletions

View File

@@ -283,6 +283,7 @@ namespace
bool IsPaused = true;
bool IsVibrating = false;
int32 ScreenWidth = 0, ScreenHeight = 0;
uint64 ProgramSizeMemory = 0;
Guid DeviceId;
String AppPackageName, DeviceManufacturer, DeviceModel, DeviceBuildNumber;
String SystemVersion, SystemLanguage, CacheDir, ExecutablePath;
@@ -724,6 +725,7 @@ MemoryStats AndroidPlatform::GetMemoryStats()
result.UsedPhysicalMemory = (totalPages - availablePages) * pageSize;
result.TotalVirtualMemory = result.TotalPhysicalMemory;
result.UsedVirtualMemory = result.UsedPhysicalMemory;
result.ProgramSizeMemory = ProgramSizeMemory;
return result;
}
@@ -826,6 +828,9 @@ bool AndroidPlatform::Init()
ClockSource = CLOCK_MONOTONIC;
}
// Estimate program size by checking physical memory usage on start
ProgramSizeMemory = Platform::GetProcessMemoryStats().UsedPhysicalMemory;
// Set info about the CPU
cpu_set_t cpus;
CPU_ZERO(&cpus);

View File

@@ -174,6 +174,7 @@ void PlatformBase::LogInfo()
const MemoryStats memStats = Platform::GetMemoryStats();
LOG(Info, "Physical Memory: {0} total, {1} used ({2}%)", Utilities::BytesToText(memStats.TotalPhysicalMemory), Utilities::BytesToText(memStats.UsedPhysicalMemory), Utilities::RoundTo2DecimalPlaces((float)memStats.UsedPhysicalMemory * 100.0f / (float)memStats.TotalPhysicalMemory));
LOG(Info, "Virtual Memory: {0} total, {1} used ({2}%)", Utilities::BytesToText(memStats.TotalVirtualMemory), Utilities::BytesToText(memStats.UsedVirtualMemory), Utilities::RoundTo2DecimalPlaces((float)memStats.UsedVirtualMemory * 100.0f / (float)memStats.TotalVirtualMemory));
LOG(Info, "Program Size: {0}", Utilities::BytesToText(memStats.ProgramSizeMemory));
LOG(Info, "Main thread id: 0x{0:x}, Process id: {1}", Globals::MainThreadID, Platform::GetCurrentProcessId());
LOG(Info, "Desktop size: {0}", Platform::GetDesktopSize());
@@ -345,15 +346,21 @@ void PlatformBase::Fatal(const StringView& msg, void* context, FatalErrorType er
// Log memory stats
{
const MemoryStats memoryStats = Platform::GetMemoryStats();
LOG(Error, "Total Used Physical Memory: {0} ({1}%)", Utilities::BytesToText(memoryStats.UsedPhysicalMemory), (int32)(100 * memoryStats.UsedPhysicalMemory / memoryStats.TotalPhysicalMemory));
LOG(Error, "Total Used Virtual Memory: {0} ({1}%)", Utilities::BytesToText(memoryStats.UsedVirtualMemory), (int32)(100 * memoryStats.UsedVirtualMemory / memoryStats.TotalVirtualMemory));
const ProcessMemoryStats processMemoryStats = Platform::GetProcessMemoryStats();
LOG(Error, "Process Used Physical Memory: {0} ({1}%)", Utilities::BytesToText(processMemoryStats.UsedPhysicalMemory), (int32)(100 * processMemoryStats.UsedPhysicalMemory / memoryStats.TotalPhysicalMemory));
LOG(Error, "Process Used Virtual Memory: {0} ({1}%)", Utilities::BytesToText(processMemoryStats.UsedVirtualMemory), (int32)(100 * processMemoryStats.UsedVirtualMemory / memoryStats.TotalVirtualMemory));
#define GET_MEM(totalUsed, processUsed) totalUsed > processUsed ? totalUsed - processUsed : 0
const uint64 externalUsedPhysical = GET_MEM(memoryStats.UsedPhysicalMemory, processMemoryStats.UsedPhysicalMemory);
const uint64 externalUsedVirtual = GET_MEM(memoryStats.UsedVirtualMemory, processMemoryStats.UsedVirtualMemory);
#undef GET_MEM
// Total memory usage
LOG(Error, "Total Used Physical Memory: {0} ({1}%)", Utilities::BytesToText(memoryStats.UsedPhysicalMemory), (int32)(100 * memoryStats.UsedPhysicalMemory / memoryStats.TotalPhysicalMemory));
LOG(Error, "Total Used Virtual Memory: {0} ({1}%)", Utilities::BytesToText(memoryStats.UsedVirtualMemory), (int32)(100 * memoryStats.UsedVirtualMemory / memoryStats.TotalVirtualMemory));
// Engine memory usage
LOG(Error, "Process Used Physical Memory: {0} ({1}%)", Utilities::BytesToText(processMemoryStats.UsedPhysicalMemory), (int32)(100 * processMemoryStats.UsedPhysicalMemory / memoryStats.TotalPhysicalMemory));
LOG(Error, "Process Used Virtual Memory: {0} ({1}%)", Utilities::BytesToText(processMemoryStats.UsedVirtualMemory), (int32)(100 * processMemoryStats.UsedVirtualMemory / memoryStats.TotalVirtualMemory));
// External apps memory usage
LOG(Error, "External Used Physical Memory: {0} ({1}%)", Utilities::BytesToText(externalUsedPhysical), (int32)(100 * externalUsedPhysical / memoryStats.TotalPhysicalMemory));
LOG(Error, "External Used Virtual Memory: {0} ({1}%)", Utilities::BytesToText(externalUsedVirtual), (int32)(100 * externalUsedVirtual / memoryStats.TotalVirtualMemory));
}

View File

@@ -57,6 +57,7 @@
CPUInfo UnixCpu;
int ClockSource;
uint64 ProgramSizeMemory;
Guid DeviceId;
String UserLocale, ComputerName, HomeDir;
byte MacAddress[6];
@@ -1773,32 +1774,25 @@ int32 LinuxPlatform::GetCacheLineSize()
MemoryStats LinuxPlatform::GetMemoryStats()
{
// Get memory usage
const uint64 pageSize = getpagesize();
const uint64 totalPages = get_phys_pages();
const uint64 availablePages = get_avphys_pages();
// Fill result data
MemoryStats result;
result.TotalPhysicalMemory = totalPages * pageSize;
result.UsedPhysicalMemory = (totalPages - availablePages) * pageSize;
result.TotalVirtualMemory = result.TotalPhysicalMemory;
result.UsedVirtualMemory = result.UsedPhysicalMemory;
result.ProgramSizeMemory = ProgramSizeMemory;
return result;
}
ProcessMemoryStats LinuxPlatform::GetProcessMemoryStats()
{
// Get memory usage
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
// Fill result data
ProcessMemoryStats result;
result.UsedPhysicalMemory = usage.ru_maxrss;
result.UsedVirtualMemory = result.UsedPhysicalMemory;
return result;
}
@@ -1930,6 +1924,9 @@ bool LinuxPlatform::Init()
ClockSource = CLOCK_MONOTONIC;
}
// Estimate program size by checking physical memory usage on start
ProgramSizeMemory = Platform::GetProcessMemoryStats().UsedPhysicalMemory;
// Set info about the CPU
cpu_set_t cpus;
CPU_ZERO(&cpus);

View File

@@ -7,45 +7,50 @@
/// <summary>
/// Contains information about current memory usage and capacity.
/// </summary>
API_STRUCT() struct MemoryStats
API_STRUCT(NoDefault) struct MemoryStats
{
DECLARE_SCRIPTING_TYPE_MINIMAL(MemoryStats);
DECLARE_SCRIPTING_TYPE_MINIMAL(MemoryStats);
/// <summary>
/// Total amount of physical memory in bytes.
/// </summary>
API_FIELD() uint64 TotalPhysicalMemory;
API_FIELD() uint64 TotalPhysicalMemory = 0;
/// <summary>
/// Amount of used physical memory in bytes.
/// </summary>
API_FIELD() uint64 UsedPhysicalMemory;
API_FIELD() uint64 UsedPhysicalMemory = 0;
/// <summary>
/// Total amount of virtual memory in bytes.
/// </summary>
API_FIELD() uint64 TotalVirtualMemory;
API_FIELD() uint64 TotalVirtualMemory = 0;
/// <summary>
/// Amount of used virtual memory in bytes.
/// </summary>
API_FIELD() uint64 UsedVirtualMemory;
API_FIELD() uint64 UsedVirtualMemory = 0;
/// <summary>
/// Amount of memory used initially by the program data (executable code, exclusive shared libraries and global static data sections).
/// </summary>
API_FIELD() uint64 ProgramSizeMemory = 0;
};
/// <summary>
/// Contains information about current memory usage by the process.
/// </summary>
API_STRUCT() struct ProcessMemoryStats
API_STRUCT(NoDefault) struct ProcessMemoryStats
{
DECLARE_SCRIPTING_TYPE_MINIMAL(ProcessMemoryStats);
DECLARE_SCRIPTING_TYPE_MINIMAL(ProcessMemoryStats);
/// <summary>
/// Amount of used physical memory in bytes.
/// </summary>
API_FIELD() uint64 UsedPhysicalMemory;
API_FIELD() uint64 UsedPhysicalMemory = 0;
/// <summary>
/// Amount of used virtual memory in bytes.
/// </summary>
API_FIELD() uint64 UsedVirtualMemory;
API_FIELD() uint64 UsedVirtualMemory = 0;
};

View File

@@ -26,6 +26,7 @@ namespace
{
Guid DeviceId;
CPUInfo CpuInfo;
uint64 ProgramSizeMemory;
uint64 ClockFrequency;
double CyclesToSeconds;
WSAData WsaData;
@@ -72,6 +73,9 @@ bool Win32Platform::Init()
ClockFrequency = frequency.QuadPart;
CyclesToSeconds = 1.0 / static_cast<double>(frequency.QuadPart);
// Estimate program size by checking physical memory usage on start
ProgramSizeMemory = Platform::GetProcessMemoryStats().UsedPhysicalMemory;
// Count CPUs
BOOL done = FALSE;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = nullptr;
@@ -313,33 +317,26 @@ int32 Win32Platform::GetCacheLineSize()
MemoryStats Win32Platform::GetMemoryStats()
{
// Get memory stats
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
// Fill result data
MemoryStats result;
result.TotalPhysicalMemory = statex.ullTotalPhys;
result.UsedPhysicalMemory = statex.ullTotalPhys - statex.ullAvailPhys;
result.TotalVirtualMemory = statex.ullTotalVirtual;
result.UsedVirtualMemory = statex.ullTotalVirtual - statex.ullAvailVirtual;
result.ProgramSizeMemory = ProgramSizeMemory;
return result;
}
ProcessMemoryStats Win32Platform::GetProcessMemoryStats()
{
// Get memory stats
PROCESS_MEMORY_COUNTERS_EX countersEx;
countersEx.cb = sizeof(countersEx);
GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&countersEx, sizeof(countersEx));
// Fill result data
ProcessMemoryStats result;
result.UsedPhysicalMemory = countersEx.WorkingSetSize;
result.UsedVirtualMemory = countersEx.PrivateUsage;
return result;
}