Merge pull request #313 from hishamhm/sreclaimable

Update calculation of used vs. free memory.
This commit is contained in:
Hisham Muhammad 2015-11-30 18:27:33 -02:00
commit 670a2de692
1 changed files with 22 additions and 14 deletions

View File

@ -621,6 +621,8 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char*
static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
unsigned long long int swapFree = 0; unsigned long long int swapFree = 0;
unsigned long long int shmem = 0;
unsigned long long int sreclaimable = 0;
FILE* file = fopen(PROCMEMINFOFILE, "r"); FILE* file = fopen(PROCMEMINFOFILE, "r");
if (file == NULL) { if (file == NULL) {
@ -629,33 +631,39 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) {
char buffer[128]; char buffer[128];
while (fgets(buffer, 128, file)) { while (fgets(buffer, 128, file)) {
#define tryRead(label, variable) (String_startsWith(buffer, label) && sscanf(buffer + strlen(label), " %32llu kB", variable))
switch (buffer[0]) { switch (buffer[0]) {
case 'M': case 'M':
if (String_startsWith(buffer, "MemTotal:")) if (tryRead("MemTotal:", &this->totalMem)) {}
sscanf(buffer, "MemTotal: %32llu kB", &this->totalMem); else if (tryRead("MemFree:", &this->freeMem)) {}
else if (String_startsWith(buffer, "MemFree:")) else if (tryRead("MemShared:", &this->sharedMem)) {}
sscanf(buffer, "MemFree: %32llu kB", &this->freeMem);
else if (String_startsWith(buffer, "MemShared:"))
sscanf(buffer, "MemShared: %32llu kB", &this->sharedMem);
break; break;
case 'B': case 'B':
if (String_startsWith(buffer, "Buffers:")) if (tryRead("Buffers:", &this->buffersMem)) {}
sscanf(buffer, "Buffers: %32llu kB", &this->buffersMem);
break; break;
case 'C': case 'C':
if (String_startsWith(buffer, "Cached:")) if (tryRead("Cached:", &this->cachedMem)) {}
sscanf(buffer, "Cached: %32llu kB", &this->cachedMem);
break; break;
case 'S': case 'S':
if (String_startsWith(buffer, "SwapTotal:")) switch (buffer[1]) {
sscanf(buffer, "SwapTotal: %32llu kB", &this->totalSwap); case 'w':
if (String_startsWith(buffer, "SwapFree:")) if (tryRead("SwapTotal:", &this->totalSwap)) {}
sscanf(buffer, "SwapFree: %32llu kB", &swapFree); else if (tryRead("SwapFree:", &swapFree)) {}
break;
case 'h':
if (tryRead("Shmem:", &shmem)) {}
break;
case 'R':
if (tryRead("SReclaimable:", &sreclaimable)) {}
break; break;
} }
break;
}
#undef tryRead
} }
this->usedMem = this->totalMem - this->freeMem; this->usedMem = this->totalMem - this->freeMem;
this->cachedMem = this->cachedMem + sreclaimable - shmem;
this->usedSwap = this->totalSwap - swapFree; this->usedSwap = this->totalSwap - swapFree;
fclose(file); fclose(file);
} }