Use long long types to avoid overflow

This commit is contained in:
Hisham Muhammad 2006-06-05 21:28:54 +00:00
parent d0325cfec5
commit 8c643f5f89
4 changed files with 72 additions and 63 deletions

View File

@ -1,6 +1,8 @@
What's new in version 0.6.3 What's new in version 0.6.3
* Use 64-bit values when storing processor times to
avoid overflow.
* Internal change: rename TypedVector to Vector and * Internal change: rename TypedVector to Vector and
ListBox (and related classes) to Panel. ListBox (and related classes) to Panel.

View File

@ -66,26 +66,26 @@ typedef struct ProcessList_ {
int totalTasks; int totalTasks;
int runningTasks; int runningTasks;
long int* totalTime; unsigned long long int* totalTime;
long int* userTime; unsigned long long int* userTime;
long int* systemTime; unsigned long long int* systemTime;
long int* idleTime; unsigned long long int* idleTime;
long int* niceTime; unsigned long long int* niceTime;
long int* totalPeriod; unsigned long long int* totalPeriod;
long int* userPeriod; unsigned long long int* userPeriod;
long int* systemPeriod; unsigned long long int* systemPeriod;
long int* idlePeriod; unsigned long long int* idlePeriod;
long int* nicePeriod; unsigned long long int* nicePeriod;
long int totalMem; unsigned long long int totalMem;
long int usedMem; unsigned long long int usedMem;
long int freeMem; unsigned long long int freeMem;
long int sharedMem; unsigned long long int sharedMem;
long int buffersMem; unsigned long long int buffersMem;
long int cachedMem; unsigned long long int cachedMem;
long int totalSwap; unsigned long long int totalSwap;
long int usedSwap; unsigned long long int usedSwap;
long int freeSwap; unsigned long long int freeSwap;
ProcessField* fields; ProcessField* fields;
ProcessField sortKey; ProcessField sortKey;
@ -130,7 +130,10 @@ static inline int ProcessList_xread(ProcessList* this, vxscanf fn, void* buffer,
va_start(ap, format); va_start(ap, format);
while (*format) { while (*format) {
char ch = *format; char ch = *format;
char* c; int* d; long int* ld; unsigned long int* lu; char** s; char* c; int* d;
long int* ld; unsigned long int* lu;
long long int* lld; unsigned long long int* llu;
char** s;
if (ch != '%') { if (ch != '%') {
fprintf(this->traceFile, "%c", ch); fprintf(this->traceFile, "%c", ch);
format++; format++;
@ -146,6 +149,12 @@ static inline int ProcessList_xread(ProcessList* this, vxscanf fn, void* buffer,
switch (*format) { switch (*format) {
case 'd': ld = va_arg(ap, long int*); fprintf(this->traceFile, "%ld", *ld); break; case 'd': ld = va_arg(ap, long int*); fprintf(this->traceFile, "%ld", *ld); break;
case 'u': lu = va_arg(ap, unsigned long int*); fprintf(this->traceFile, "%lu", *lu); break; case 'u': lu = va_arg(ap, unsigned long int*); fprintf(this->traceFile, "%lu", *lu); break;
case 'l':
format++;
switch (*format) {
case 'd': lld = va_arg(ap, long long int*); fprintf(this->traceFile, "%lld", *lld); break;
case 'u': llu = va_arg(ap, unsigned long long int*); fprintf(this->traceFile, "%llu", *llu); break;
}
} }
} }
format++; format++;
@ -190,16 +199,16 @@ ProcessList* ProcessList_new(UsersTable* usersTable) {
} while (String_startsWith(buffer, "cpu")); } while (String_startsWith(buffer, "cpu"));
fclose(status); fclose(status);
this->processorCount = procs - 1; this->processorCount = procs - 1;
this->totalTime = calloc(procs, sizeof(long int)); this->totalTime = calloc(procs, sizeof(long long int));
this->userTime = calloc(procs, sizeof(long int)); this->userTime = calloc(procs, sizeof(long long int));
this->systemTime = calloc(procs, sizeof(long int)); this->systemTime = calloc(procs, sizeof(long long int));
this->niceTime = calloc(procs, sizeof(long int)); this->niceTime = calloc(procs, sizeof(long long int));
this->idleTime = calloc(procs, sizeof(long int)); this->idleTime = calloc(procs, sizeof(long long int));
this->totalPeriod = calloc(procs, sizeof(long int)); this->totalPeriod = calloc(procs, sizeof(long long int));
this->userPeriod = calloc(procs, sizeof(long int)); this->userPeriod = calloc(procs, sizeof(long long int));
this->systemPeriod = calloc(procs, sizeof(long int)); this->systemPeriod = calloc(procs, sizeof(long long int));
this->nicePeriod = calloc(procs, sizeof(long int)); this->nicePeriod = calloc(procs, sizeof(long long int));
this->idlePeriod = calloc(procs, sizeof(long int)); this->idlePeriod = calloc(procs, sizeof(long long int));
for (int i = 0; i < procs; i++) { for (int i = 0; i < procs; i++) {
this->totalTime[i] = 1; this->totalTime[i] = 1;
this->totalPeriod[i] = 1; this->totalPeriod[i] = 1;
@ -560,8 +569,8 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl
} }
void ProcessList_scan(ProcessList* this) { void ProcessList_scan(ProcessList* this) {
long int usertime, nicetime, systemtime, idletime, totaltime; unsigned long long int usertime, nicetime, systemtime, idletime, totaltime;
long int swapFree; unsigned long long int swapFree;
FILE* status; FILE* status;
char buffer[128]; char buffer[128];
@ -573,25 +582,25 @@ void ProcessList_scan(ProcessList* this) {
switch (buffer[0]) { switch (buffer[0]) {
case 'M': case 'M':
if (String_startsWith(buffer, "MemTotal:")) if (String_startsWith(buffer, "MemTotal:"))
ProcessList_read(this, buffer, "MemTotal: %ld kB", &this->totalMem); ProcessList_read(this, buffer, "MemTotal: %llu kB", &this->totalMem);
else if (String_startsWith(buffer, "MemFree:")) else if (String_startsWith(buffer, "MemFree:"))
ProcessList_read(this, buffer, "MemFree: %ld kB", &this->freeMem); ProcessList_read(this, buffer, "MemFree: %llu kB", &this->freeMem);
else if (String_startsWith(buffer, "MemShared:")) else if (String_startsWith(buffer, "MemShared:"))
ProcessList_read(this, buffer, "MemShared: %ld kB", &this->sharedMem); ProcessList_read(this, buffer, "MemShared: %llu kB", &this->sharedMem);
break; break;
case 'B': case 'B':
if (String_startsWith(buffer, "Buffers:")) if (String_startsWith(buffer, "Buffers:"))
ProcessList_read(this, buffer, "Buffers: %ld kB", &this->buffersMem); ProcessList_read(this, buffer, "Buffers: %llu kB", &this->buffersMem);
break; break;
case 'C': case 'C':
if (String_startsWith(buffer, "Cached:")) if (String_startsWith(buffer, "Cached:"))
ProcessList_read(this, buffer, "Cached: %ld kB", &this->cachedMem); ProcessList_read(this, buffer, "Cached: %llu kB", &this->cachedMem);
break; break;
case 'S': case 'S':
if (String_startsWith(buffer, "SwapTotal:")) if (String_startsWith(buffer, "SwapTotal:"))
ProcessList_read(this, buffer, "SwapTotal: %ld kB", &this->totalSwap); ProcessList_read(this, buffer, "SwapTotal: %llu kB", &this->totalSwap);
if (String_startsWith(buffer, "SwapFree:")) if (String_startsWith(buffer, "SwapFree:"))
ProcessList_read(this, buffer, "SwapFree: %ld kB", &swapFree); ProcessList_read(this, buffer, "SwapFree: %llu kB", &swapFree);
break; break;
} }
} }
@ -606,16 +615,16 @@ void ProcessList_scan(ProcessList* this) {
for (int i = 0; i <= this->processorCount; i++) { for (int i = 0; i <= this->processorCount; i++) {
char buffer[256]; char buffer[256];
int cpuid; int cpuid;
long int ioWait, irq, softIrq, steal; unsigned long long int ioWait, irq, softIrq, steal;
ioWait = irq = softIrq = steal = 0; ioWait = irq = softIrq = steal = 0;
// Dependending on your kernel version, // Dependending on your kernel version,
// 5, 7 or 8 of these fields will be set. // 5, 7 or 8 of these fields will be set.
// The rest will remain at zero. // The rest will remain at zero.
fgets(buffer, 255, status); fgets(buffer, 255, status);
if (i == 0) if (i == 0)
ProcessList_read(this, buffer, "cpu %ld %ld %ld %ld %ld %ld %ld %ld", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal); ProcessList_read(this, buffer, "cpu %llu %llu %llu %llu %llu %llu %llu %llu", &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal);
else { else {
ProcessList_read(this, buffer, "cpu%d %ld %ld %ld %ld %ld %ld %ld %ld", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal); ProcessList_read(this, buffer, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu", &cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal);
assert(cpuid == i - 1); assert(cpuid == i - 1);
} }
// Fields existing on kernels >= 2.6 // Fields existing on kernels >= 2.6

View File

@ -66,26 +66,26 @@ typedef struct ProcessList_ {
int totalTasks; int totalTasks;
int runningTasks; int runningTasks;
long int* totalTime; unsigned long long int* totalTime;
long int* userTime; unsigned long long int* userTime;
long int* systemTime; unsigned long long int* systemTime;
long int* idleTime; unsigned long long int* idleTime;
long int* niceTime; unsigned long long int* niceTime;
long int* totalPeriod; unsigned long long int* totalPeriod;
long int* userPeriod; unsigned long long int* userPeriod;
long int* systemPeriod; unsigned long long int* systemPeriod;
long int* idlePeriod; unsigned long long int* idlePeriod;
long int* nicePeriod; unsigned long long int* nicePeriod;
long int totalMem; unsigned long long int totalMem;
long int usedMem; unsigned long long int usedMem;
long int freeMem; unsigned long long int freeMem;
long int sharedMem; unsigned long long int sharedMem;
long int buffersMem; unsigned long long int buffersMem;
long int cachedMem; unsigned long long int cachedMem;
long int totalSwap; unsigned long long int totalSwap;
long int usedSwap; unsigned long long int usedSwap;
long int freeSwap; unsigned long long int freeSwap;
ProcessField* fields; ProcessField* fields;
ProcessField sortKey; ProcessField sortKey;
@ -151,6 +151,4 @@ void ProcessList_processEntries(ProcessList* this, char* dirname, int parent, fl
void ProcessList_scan(ProcessList* this); void ProcessList_scan(ProcessList* this);
void ProcessList_dontCrash(int signal);
#endif #endif

0
scripts/MakeHeader.py Normal file → Executable file
View File