From 6afacee50d9039e21a9dc40df360f718075da875 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 27 Nov 2014 16:28:32 -0200 Subject: [PATCH 1/2] A little refactoring --- linux/LinuxProcessList.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 7bad637d..2b0f3f0c 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -571,10 +571,7 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna return true; } -void ProcessList_scan(ProcessList* this) { - unsigned long long int usertime, nicetime, systemtime, idletime; - unsigned long long int swapFree = 0; - +static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { FILE* file = fopen(PROCMEMINFOFILE, "r"); if (file == NULL) { CRT_fatalError("Cannot open " PROCMEMINFOFILE); @@ -615,8 +612,10 @@ void ProcessList_scan(ProcessList* this) { this->usedMem = this->totalMem - this->freeMem; this->usedSwap = this->totalSwap - swapFree; fclose(file); +} - file = fopen(PROCSTATFILE, "r"); +static inline double LinuxProcessList_readCPUTime(ProcessList* this) { + FILE* file = fopen(PROCSTATFILE, "r"); if (file == NULL) { CRT_fatalError("Cannot open " PROCSTATFILE); } @@ -684,6 +683,16 @@ void ProcessList_scan(ProcessList* this) { cpuData->totalTime = totaltime; } double period = (double)this->cpus[0].totalPeriod / cpus; fclose(file); + return period; +} + +void ProcessList_scan(ProcessList* this) { + unsigned long long int usertime, nicetime, systemtime, idletime; + unsigned long long int swapFree = 0; + + LinuxProcessList_scanMemoryInfo(this); + + LinuxProcessList_scanCPUTime(this); // mark all process as "dirty" for (int i = 0; i < Vector_size(this->processes); i++) { From ff4d1b466fba116d0750a815a1d517dbc24b4b43 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 27 Nov 2014 16:31:42 -0200 Subject: [PATCH 2/2] Build fixes. --- linux/LinuxProcessList.c | 68 ++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 2b0f3f0c..c549667e 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -572,40 +572,38 @@ static bool LinuxProcessList_processEntries(ProcessList* this, const char* dirna } static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { + unsigned long long int swapFree = 0; + FILE* file = fopen(PROCMEMINFOFILE, "r"); if (file == NULL) { CRT_fatalError("Cannot open " PROCMEMINFOFILE); } - int cpus = this->cpuCount; - assert(cpus > 0); - { - char buffer[128]; - while (fgets(buffer, 128, file)) { - - switch (buffer[0]) { - case 'M': - if (String_startsWith(buffer, "MemTotal:")) - sscanf(buffer, "MemTotal: %32llu kB", &this->totalMem); - else if (String_startsWith(buffer, "MemFree:")) - sscanf(buffer, "MemFree: %32llu kB", &this->freeMem); - else if (String_startsWith(buffer, "MemShared:")) - sscanf(buffer, "MemShared: %32llu kB", &this->sharedMem); - break; - case 'B': - if (String_startsWith(buffer, "Buffers:")) - sscanf(buffer, "Buffers: %32llu kB", &this->buffersMem); - break; - case 'C': - if (String_startsWith(buffer, "Cached:")) - sscanf(buffer, "Cached: %32llu kB", &this->cachedMem); - break; - case 'S': - if (String_startsWith(buffer, "SwapTotal:")) - sscanf(buffer, "SwapTotal: %32llu kB", &this->totalSwap); - if (String_startsWith(buffer, "SwapFree:")) - sscanf(buffer, "SwapFree: %32llu kB", &swapFree); - break; - } + char buffer[128]; + while (fgets(buffer, 128, file)) { + + switch (buffer[0]) { + case 'M': + if (String_startsWith(buffer, "MemTotal:")) + sscanf(buffer, "MemTotal: %32llu kB", &this->totalMem); + else if (String_startsWith(buffer, "MemFree:")) + sscanf(buffer, "MemFree: %32llu kB", &this->freeMem); + else if (String_startsWith(buffer, "MemShared:")) + sscanf(buffer, "MemShared: %32llu kB", &this->sharedMem); + break; + case 'B': + if (String_startsWith(buffer, "Buffers:")) + sscanf(buffer, "Buffers: %32llu kB", &this->buffersMem); + break; + case 'C': + if (String_startsWith(buffer, "Cached:")) + sscanf(buffer, "Cached: %32llu kB", &this->cachedMem); + break; + case 'S': + if (String_startsWith(buffer, "SwapTotal:")) + sscanf(buffer, "SwapTotal: %32llu kB", &this->totalSwap); + if (String_startsWith(buffer, "SwapFree:")) + sscanf(buffer, "SwapFree: %32llu kB", &swapFree); + break; } } @@ -614,11 +612,15 @@ static inline void LinuxProcessList_scanMemoryInfo(ProcessList* this) { fclose(file); } -static inline double LinuxProcessList_readCPUTime(ProcessList* this) { +static inline double LinuxProcessList_scanCPUTime(ProcessList* this) { + unsigned long long int usertime, nicetime, systemtime, idletime; + FILE* file = fopen(PROCSTATFILE, "r"); if (file == NULL) { CRT_fatalError("Cannot open " PROCSTATFILE); } + int cpus = this->cpuCount; + assert(cpus > 0); for (int i = 0; i <= cpus; i++) { char buffer[256]; int cpuid; @@ -687,12 +689,10 @@ static inline double LinuxProcessList_readCPUTime(ProcessList* this) { } void ProcessList_scan(ProcessList* this) { - unsigned long long int usertime, nicetime, systemtime, idletime; - unsigned long long int swapFree = 0; LinuxProcessList_scanMemoryInfo(this); - LinuxProcessList_scanCPUTime(this); + double period = LinuxProcessList_scanCPUTime(this); // mark all process as "dirty" for (int i = 0; i < Vector_size(this->processes); i++) {