Skip to content

Commit

Permalink
[WIN] fix reporting system memory > 4GB. Need to use GlobalMemoryEx/M…
Browse files Browse the repository at this point in the history
…EMORYSTATUSEX instead of GlobalMemory
  • Loading branch information
CrystalP authored and CrystalP committed Jan 2, 2012
1 parent 396fc2d commit ee1a1e2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 44 deletions.
13 changes: 7 additions & 6 deletions xbmc/GUIInfoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,21 +1393,22 @@ CStdString CGUIInfoManager::GetLabel(int info, int contextWindow)
case SYSTEM_USED_MEMORY_PERCENT:
case SYSTEM_TOTAL_MEMORY:
{
MEMORYSTATUS stat;
GlobalMemoryStatus(&stat);
int iMemPercentFree = 100 - ((int)( 100.0f* (stat.dwTotalPhys - stat.dwAvailPhys)/stat.dwTotalPhys + 0.5f ));
MEMORYSTATUSEX stat;
stat.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&stat);
int iMemPercentFree = 100 - ((int)( 100.0f* (stat.ullTotalPhys - stat.ullAvailPhys)/stat.ullTotalPhys + 0.5f ));
int iMemPercentUsed = 100 - iMemPercentFree;

if (info == SYSTEM_FREE_MEMORY)
strLabel.Format("%luMB", (ULONG)(stat.dwAvailPhys/MB));
strLabel.Format("%luMB", (ULONG)(stat.ullAvailPhys/MB));
else if (info == SYSTEM_FREE_MEMORY_PERCENT)
strLabel.Format("%i%%", iMemPercentFree);
else if (info == SYSTEM_USED_MEMORY)
strLabel.Format("%luMB", (ULONG)((stat.dwTotalPhys - stat.dwAvailPhys)/MB));
strLabel.Format("%luMB", (ULONG)((stat.ullTotalPhys - stat.ullAvailPhys)/MB));
else if (info == SYSTEM_USED_MEMORY_PERCENT)
strLabel.Format("%i%%", iMemPercentUsed);
else if (info == SYSTEM_TOTAL_MEMORY)
strLabel.Format("%luMB", (ULONG)(stat.dwTotalPhys/MB));
strLabel.Format("%luMB", (ULONG)(stat.ullTotalPhys/MB));
}
break;
case SYSTEM_SCREEN_MODE:
Expand Down
9 changes: 5 additions & 4 deletions xbmc/guilib/TextureBundleXPR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,11 @@ bool CTextureBundleXPR::LoadFile(const CStdString& Filename, CAutoTexBuffer& Unp
if (!buffer || !UnpackedBuf.Set((BYTE*)XPhysicalAlloc(file->second.UnpackedSize, MAXULONG_PTR, 128, PAGE_READWRITE)))
{ // failed due to lack of memory
#ifndef _LINUX
MEMORYSTATUS stat;
GlobalMemoryStatus(&stat);
CLog::Log(LOGERROR, "Out of memory loading texture: %s (need %lu bytes, have %lu bytes)", name.c_str(),
file->second.UnpackedSize + file->second.PackedSize, stat.dwAvailPhys);
MEMORYSTATUSEX stat;
stat.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&stat);
CLog::Log(LOGERROR, "Out of memory loading texture: %s (need %lu bytes, have %"PRIu64" bytes)", name.c_str(),
file->second.UnpackedSize + file->second.PackedSize, stat.ullAvailPhys);
#elif defined(__APPLE__) || defined(__FreeBSD__)
CLog::Log(LOGERROR, "Out of memory loading texture: %s (need %d bytes)", name.c_str(),
file->second.UnpackedSize + file->second.PackedSize);
Expand Down
7 changes: 4 additions & 3 deletions xbmc/interfaces/python/xbmcmodule/xbmcmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,10 @@ namespace PYXBMC

PyObject* XBMC_GetFreeMem(PyObject *self, PyObject *args)
{
MEMORYSTATUS stat;
GlobalMemoryStatus(&stat);
return PyInt_FromLong( stat.dwAvailPhys / ( 1024 * 1024 ) );
MEMORYSTATUSEX stat;
stat.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&stat);
return PyInt_FromLong( stat.ullAvailPhys / ( 1024 * 1024 ) );
}

// getCpuTemp() method
Expand Down
12 changes: 6 additions & 6 deletions xbmc/linux/PlatformDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,18 @@ typedef struct _SECURITY_ATTRIBUTES {
#define _stat stat

// Memory
typedef struct _MEMORYSTATUS
typedef struct _MEMORYSTATUSEX
{
DWORD dwLength;
DWORD dwMemoryLoad;

uint64_t dwTotalPhys;
uint64_t dwAvailPhys;
uint64_t dwTotalPageFile;
uint64_t dwAvailPageFile;
uint64_t dwTotalVirtual;
uint64_t dwAvailVirtual;
} MEMORYSTATUS, *LPMEMORYSTATUS;
uint64_t ullTotalPageFile;
uint64_t ullAvailPageFile;
uint64_t ullTotalVirtual;
uint64_t ullAvailVirtual;
} MEMORYSTATUSEX, *LPMEMORYSTATUSEX;

// Common HRESULT values
#ifndef NOERROR
Expand Down
36 changes: 17 additions & 19 deletions xbmc/linux/XSyncUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ using namespace XbmcThreads;
static FILE* procMeminfoFP = NULL;
#endif

void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
void GlobalMemoryStatusEx(LPMEMORYSTATUSEX lpBuffer)
{
if (!lpBuffer)
return;

memset(lpBuffer, 0, sizeof(MEMORYSTATUS));
lpBuffer->dwLength = sizeof(MEMORYSTATUS);
memset(lpBuffer, 0, sizeof(MEMORYSTATUSEX));

#ifdef __APPLE__
uint64_t physmem;
Expand All @@ -63,16 +62,16 @@ void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)

// Total physical memory.
if (sysctl(mib, miblen, &physmem, &len, NULL, 0) == 0 && len == sizeof (physmem))
lpBuffer->dwTotalPhys = physmem;
lpBuffer->ullTotalPhys = physmem;

// Virtual memory.
mib[0] = CTL_VM; mib[1] = VM_SWAPUSAGE;
struct xsw_usage swap;
len = sizeof(struct xsw_usage);
if (sysctl(mib, miblen, &swap, &len, NULL, 0) == 0)
{
lpBuffer->dwAvailPageFile = swap.xsu_avail;
lpBuffer->dwTotalVirtual = lpBuffer->dwTotalPhys + swap.xsu_total;
lpBuffer->ullAvailPageFile = swap.xsu_avail;
lpBuffer->ullTotalVirtual = lpBuffer->ullTotalPhys + swap.xsu_total;
}

// In use.
Expand All @@ -89,8 +88,8 @@ void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
{
uint64_t used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pageSize;

lpBuffer->dwAvailPhys = lpBuffer->dwTotalPhys - used;
lpBuffer->dwAvailVirtual = lpBuffer->dwAvailPhys; // FIXME.
lpBuffer->ullAvailPhys = lpBuffer->ullTotalPhys - used;
lpBuffer->ullAvailVirtual = lpBuffer->ullAvailPhys; // FIXME.
}
}
#elif defined(__FreeBSD__)
Expand All @@ -101,8 +100,8 @@ void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
/* physmem */
len = sizeof(physmem);
if (sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) == 0) {
lpBuffer->dwTotalPhys = physmem;
lpBuffer->dwTotalVirtual = physmem;
lpBuffer->ullTotalPhys = physmem;
lpBuffer->ullTotalVirtual = physmem;
}
/* pagesize */
len = sizeof(pagesize);
Expand All @@ -122,11 +121,11 @@ void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
mem_free *= pagesize;

/* mem_avail = mem_inactive + mem_cache + mem_free */
lpBuffer->dwAvailPhys = mem_inactive + mem_cache + mem_free;
lpBuffer->dwAvailVirtual = mem_inactive + mem_cache + mem_free;
lpBuffer->ullAvailPhys = mem_inactive + mem_cache + mem_free;
lpBuffer->ullAvailVirtual = mem_inactive + mem_cache + mem_free;

if (sysctlbyname("vm.stats.vm.v_swappgsout", &swap_free, &len, NULL, 0) == 0)
lpBuffer->dwAvailPageFile = swap_free * pagesize;
lpBuffer->ullAvailPageFile = swap_free * pagesize;
#else
struct sysinfo info;
char name[32];
Expand Down Expand Up @@ -159,12 +158,11 @@ void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer)
rewind(procMeminfoFP);
fflush(procMeminfoFP);
}
lpBuffer->dwLength = sizeof(MEMORYSTATUS);
lpBuffer->dwAvailPageFile = (info.freeswap * info.mem_unit);
lpBuffer->dwAvailPhys = ((info.freeram + info.bufferram) * info.mem_unit);
lpBuffer->dwAvailVirtual = ((info.freeram + info.bufferram) * info.mem_unit);
lpBuffer->dwTotalPhys = (info.totalram * info.mem_unit);
lpBuffer->dwTotalVirtual = (info.totalram * info.mem_unit);
lpBuffer->ullAvailPageFile = (info.freeswap * info.mem_unit);
lpBuffer->ullAvailPhys = ((info.freeram + info.bufferram) * info.mem_unit);
lpBuffer->ullAvailVirtual = ((info.freeram + info.bufferram) * info.mem_unit);
lpBuffer->ullTotalPhys = (info.totalram * info.mem_unit);
lpBuffer->ullTotalVirtual = (info.totalram * info.mem_unit);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion xbmc/linux/XSyncUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#define WAIT_ABANDONED ((STATUS_ABANDONED_WAIT_0 ) + 0 )
#define WAIT_ABANDONED_0 ((STATUS_ABANDONED_WAIT_0 ) + 0 )

void GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer);
void GlobalMemoryStatusEx(LPMEMORYSTATUSEX lpBuffer);

#endif

Expand Down
11 changes: 6 additions & 5 deletions xbmc/windows/GUIWindowDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,18 @@ void CGUIWindowDebugInfo::Process(unsigned int currentTime, CDirtyRegionList &di
CStdString info;
if (LOG_LEVEL_DEBUG_FREEMEM <= g_advancedSettings.m_logLevel)
{
MEMORYSTATUS stat;
GlobalMemoryStatus(&stat);
MEMORYSTATUSEX stat;
stat.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&stat);
CStdString profiling = CGUIControlProfiler::IsRunning() ? " (profiling)" : "";
CStdString strCores = g_cpuInfo.GetCoresUsageString();
#if !defined(_LINUX)
info.Format("LOG: %sxbmc.log\nMEM: %d/%d KB - FPS: %2.1f fps\nCPU: %s%s", g_settings.m_logFolder.c_str(),
stat.dwAvailPhys/1024, stat.dwTotalPhys/1024, g_infoManager.GetFPS(), strCores.c_str(), profiling.c_str());
info.Format("LOG: %sxbmc.log\nMEM: %"PRIu64"/%"PRIu64" KB - FPS: %2.1f fps\nCPU: %s%s", g_settings.m_logFolder.c_str(),
stat.ullAvailPhys/1024, stat.ullTotalPhys/1024, g_infoManager.GetFPS(), strCores.c_str(), profiling.c_str());
#else
double dCPU = m_resourceCounter.GetCPUUsage();
info.Format("LOG: %sxbmc.log\nMEM: %"PRIu64"/%"PRIu64" KB - FPS: %2.1f fps\nCPU: %s (CPU-XBMC %4.2f%%%s)", g_settings.m_logFolder.c_str(),
stat.dwAvailPhys/1024, stat.dwTotalPhys/1024, g_infoManager.GetFPS(), strCores.c_str(), dCPU, profiling.c_str());
stat.ullAvailPhys/1024, stat.ullTotalPhys/1024, g_infoManager.GetFPS(), strCores.c_str(), dCPU, profiling.c_str());
#endif
}

Expand Down

0 comments on commit ee1a1e2

Please sign in to comment.