diff --git a/Source/Engine/Platform/Android/AndroidPlatform.cpp b/Source/Engine/Platform/Android/AndroidPlatform.cpp index c4ef0379c..404cf5476 100644 --- a/Source/Engine/Platform/Android/AndroidPlatform.cpp +++ b/Source/Engine/Platform/Android/AndroidPlatform.cpp @@ -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); diff --git a/Source/Engine/Platform/Base/PlatformBase.cpp b/Source/Engine/Platform/Base/PlatformBase.cpp index 860f72bc2..40c017c6e 100644 --- a/Source/Engine/Platform/Base/PlatformBase.cpp +++ b/Source/Engine/Platform/Base/PlatformBase.cpp @@ -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)); } diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.cpp b/Source/Engine/Platform/Linux/LinuxPlatform.cpp index 681b60c37..8c03fa164 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.cpp +++ b/Source/Engine/Platform/Linux/LinuxPlatform.cpp @@ -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); diff --git a/Source/Engine/Platform/MemoryStats.h b/Source/Engine/Platform/MemoryStats.h index b673c5138..75788d5db 100644 --- a/Source/Engine/Platform/MemoryStats.h +++ b/Source/Engine/Platform/MemoryStats.h @@ -7,45 +7,50 @@ /// /// Contains information about current memory usage and capacity. /// -API_STRUCT() struct MemoryStats +API_STRUCT(NoDefault) struct MemoryStats { -DECLARE_SCRIPTING_TYPE_MINIMAL(MemoryStats); + DECLARE_SCRIPTING_TYPE_MINIMAL(MemoryStats); /// /// Total amount of physical memory in bytes. /// - API_FIELD() uint64 TotalPhysicalMemory; + API_FIELD() uint64 TotalPhysicalMemory = 0; /// /// Amount of used physical memory in bytes. /// - API_FIELD() uint64 UsedPhysicalMemory; + API_FIELD() uint64 UsedPhysicalMemory = 0; /// /// Total amount of virtual memory in bytes. /// - API_FIELD() uint64 TotalVirtualMemory; + API_FIELD() uint64 TotalVirtualMemory = 0; /// /// Amount of used virtual memory in bytes. /// - API_FIELD() uint64 UsedVirtualMemory; + API_FIELD() uint64 UsedVirtualMemory = 0; + + /// + /// Amount of memory used initially by the program data (executable code, exclusive shared libraries and global static data sections). + /// + API_FIELD() uint64 ProgramSizeMemory = 0; }; /// /// Contains information about current memory usage by the process. /// -API_STRUCT() struct ProcessMemoryStats +API_STRUCT(NoDefault) struct ProcessMemoryStats { -DECLARE_SCRIPTING_TYPE_MINIMAL(ProcessMemoryStats); + DECLARE_SCRIPTING_TYPE_MINIMAL(ProcessMemoryStats); /// /// Amount of used physical memory in bytes. /// - API_FIELD() uint64 UsedPhysicalMemory; + API_FIELD() uint64 UsedPhysicalMemory = 0; /// /// Amount of used virtual memory in bytes. /// - API_FIELD() uint64 UsedVirtualMemory; + API_FIELD() uint64 UsedVirtualMemory = 0; }; diff --git a/Source/Engine/Platform/Win32/Win32Platform.cpp b/Source/Engine/Platform/Win32/Win32Platform.cpp index 5650a5839..49548eadf 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.cpp +++ b/Source/Engine/Platform/Win32/Win32Platform.cpp @@ -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(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; }