Fix a ton of small bugs in 64bit memory value handling

This commit is contained in:
Rye Mutt
2019-10-09 16:52:46 -04:00
parent bcadee8575
commit a559a09217
9 changed files with 99 additions and 206 deletions

View File

@@ -102,39 +102,47 @@ void LLMemory::initMaxHeapSizeGB(F32Gigabytes max_heap_size, BOOL prevent_heap_f
//static
void LLMemory::updateMemoryInfo()
{
#if LL_WINDOWS
HANDLE self = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS counters;
if (!GetProcessMemoryInfo(self, &counters, sizeof(counters)))
#if LL_WINDOWS
PROCESS_MEMORY_COUNTERS_EX counters;
counters.cb = sizeof(counters);
if (!GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*) &counters, sizeof(counters)))
{
LL_WARNS() << "GetProcessMemoryInfo failed" << LL_ENDL;
return ;
return;
}
sAllocatedMemInKB = (U32Bytes)(counters.WorkingSetSize) ;
sAllocatedPageSizeInKB = (U32Bytes)(counters.PagefileUsage) ;
sAllocatedMemInKB = U64Bytes(counters.WorkingSetSize);
sAllocatedPageSizeInKB = (counters.PagefileUsage != 0) ? U64Bytes(counters.PagefileUsage) : U64Bytes(counters.PrivateUsage);
U32Kilobytes avail_phys, avail_virtual;
LLMemoryInfo::getAvailableMemoryKB(avail_phys, avail_virtual) ;
sMaxPhysicalMemInKB = llmin(avail_phys + sAllocatedMemInKB, sMaxHeapSizeInKB);
if(sMaxPhysicalMemInKB > sAllocatedMemInKB)
MEMORYSTATUSEX memorystat;
memorystat.dwLength = sizeof(memorystat);
if (!GlobalMemoryStatusEx(&memorystat))
{
sAvailPhysicalMemInKB = sMaxPhysicalMemInKB - sAllocatedMemInKB ;
LL_WARNS() << "GlobalMemoryStatusEx failed" << LL_ENDL;
return;
}
#if (defined(_WIN64) || defined(__amd64__) || defined(__x86_64__))
sMaxPhysicalMemInKB = U64Bytes(memorystat.ullTotalPhys);
sAvailPhysicalMemInKB = U64Bytes(memorystat.ullAvailPhys);
#else
sMaxPhysicalMemInKB = llmin(U32Kilobytes(U64Bytes(memorystat.ullTotalPhys)), sMaxHeapSizeInKB);
if (sMaxPhysicalMemInKB > sAllocatedMemInKB)
{
sAvailPhysicalMemInKB = U64Bytes(memorystat.ullAvailPhys);;
}
else
{
sAvailPhysicalMemInKB = U32Kilobytes(0);
}
#else
//not valid for other systems for now.
sAllocatedMemInKB = (U32Bytes)LLMemory::getCurrentRSS();
sMaxPhysicalMemInKB = (U32Bytes)U32_MAX ;
sAvailPhysicalMemInKB = (U32Bytes)U32_MAX ;
#endif
return ;
#else
//not valid for other systems for now.
sAllocatedMemInKB = U64Bytes(LLMemory::getCurrentRSS());
sMaxPhysicalMemInKB = U64Bytes(U32_MAX);
sAvailPhysicalMemInKB = U64Bytes(U32_MAX);
#endif
}
//
@@ -158,7 +166,7 @@ void* LLMemory::tryToAlloc(void* address, U32 size)
return address ;
#else
return (void*)0x01 ; //skip checking
#endif
#endif
}
//static
@@ -233,31 +241,31 @@ bool LLMemory::isMemoryPoolLow()
//static
U32Kilobytes LLMemory::getAvailableMemKB()
{
return sAvailPhysicalMemInKB ;
return sAvailPhysicalMemInKB;
}
//static
U32Kilobytes LLMemory::getMaxMemKB()
{
return sMaxPhysicalMemInKB ;
return sMaxPhysicalMemInKB;
}
//static
U32Kilobytes LLMemory::getAllocatedMemKB()
{
return sAllocatedMemInKB ;
return sAllocatedMemInKB;
}
//----------------------------------------------------------------------------
#if defined(LL_WINDOWS)
//static
U64 LLMemory::getCurrentRSS()
{
HANDLE self = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS counters;
if (!GetProcessMemoryInfo(self, &counters, sizeof(counters)))
if (!GetProcessMemoryInfo(GetCurrentProcess(), &counters, sizeof(counters)))
{
LL_WARNS() << "GetProcessMemoryInfo failed" << LL_ENDL;
return 0;
@@ -266,35 +274,7 @@ U64 LLMemory::getCurrentRSS()
return counters.WorkingSetSize;
}
//static
U32 LLMemory::getWorkingSetSize()
{
PROCESS_MEMORY_COUNTERS pmc ;
U32 ret = 0 ;
if (GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof(pmc)) )
{
ret = pmc.WorkingSetSize ;
}
return ret ;
}
#elif defined(LL_DARWIN)
/*
The API used here is not capable of dealing with 64-bit memory sizes, but is available before 10.4.
Once we start requiring 10.4, we can use the updated API, which looks like this:
task_basic_info_64_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = TASK_BASIC_INFO_64_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO_64, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
Of course, this doesn't gain us anything unless we start building the viewer as a 64-bit executable, since that's the only way
for our memory allocation to exceed 2^32.
*/
// if (sysctl(ctl, 2, &page_size, &size, NULL, 0) == -1)
// {
// LL_WARNS() << "Couldn't get page size" << LL_ENDL;
@@ -307,16 +287,15 @@ U32 LLMemory::getWorkingSetSize()
U64 LLMemory::getCurrentRSS()
{
U64 residentSize = 0;
task_basic_info_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
mach_task_basic_info_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
{
residentSize = basicInfo.resident_size;
// If we ever wanted it, the process virtual size is also available as:
// virtualSize = basicInfo.virtual_size;
// LL_INFOS() << "resident size is " << residentSize << LL_ENDL;
// residentSize = basicInfo.resident_size;
// Although this method is defined to return the "resident set size,"
// in fact what callers want from it is the total virtual memory
// consumed by the application.
residentSize = basicInfo.virtual_size;
}
else
{
@@ -326,11 +305,6 @@ U64 LLMemory::getCurrentRSS()
return residentSize;
}
U32 LLMemory::getWorkingSetSize()
{
return 0 ;
}
#elif defined(LL_LINUX)
U64 LLMemory::getCurrentRSS()
@@ -342,7 +316,7 @@ U64 LLMemory::getCurrentRSS()
if (fp == NULL)
{
LL_WARNS() << "couldn't open " << statPath << LL_ENDL;
goto bail;
return rss;
}
// Eee-yew! See Documentation/filesystems/proc.txt in your
@@ -361,49 +335,8 @@ U64 LLMemory::getCurrentRSS()
fclose(fp);
bail:
return rss;
}
U32 LLMemory::getWorkingSetSize()
{
return 0 ;
}
#elif LL_SOLARIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _STRUCTURED_PROC 1
#include <sys/procfs.h>
U64 LLMemory::getCurrentRSS()
{
char path [LL_MAX_PATH]; /* Flawfinder: ignore */
sprintf(path, "/proc/%d/psinfo", (int)getpid());
int proc_fd = -1;
if((proc_fd = open(path, O_RDONLY)) == -1){
LL_WARNS() << "LLmemory::getCurrentRSS() unable to open " << path << ". Returning 0 RSS!" << LL_ENDL;
return 0;
}
psinfo_t proc_psinfo;
if(read(proc_fd, &proc_psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)){
LL_WARNS() << "LLmemory::getCurrentRSS() Unable to read from " << path << ". Returning 0 RSS!" << LL_ENDL;
close(proc_fd);
return 0;
}
close(proc_fd);
return((U64)proc_psinfo.pr_rssize * 1024);
}
U32 LLMemory::getWorkingSetSize()
{
return 0 ;
}
#else
U64 LLMemory::getCurrentRSS()
@@ -411,11 +344,6 @@ U64 LLMemory::getCurrentRSS()
return 0;
}
U32 LLMemory::getWorkingSetSize()
{
return 0;
}
#endif
//--------------------------------------------------------------------------------------------------
@@ -427,7 +355,7 @@ LLMemTracker* LLMemTracker::sInstance = NULL ;
LLMemTracker::LLMemTracker()
{
mLastAllocatedMem = LLMemory::getWorkingSetSize() ;
mLastAllocatedMem = LLMemory::getCurrentRSS() ;
mCapacity = 128 ;
mCurIndex = 0 ;
mCounter = 0 ;
@@ -480,7 +408,7 @@ void LLMemTracker::track(const char* function, const int line)
return ;
}
U32 allocated_mem = LLMemory::getWorkingSetSize() ;
U64 allocated_mem = LLMemory::getCurrentRSS() ;
LLMutexLock lock(mMutexp) ;