Fixed integer overflow in QEMU guest CPU time

This commit is contained in:
Daniel P. Berrange 2007-04-15 19:58:44 +00:00
parent 9e4d606303
commit cc05eaf803
2 changed files with 9 additions and 4 deletions

View File

@ -1,3 +1,8 @@
Sun Apr 15 15:57:04 EST 2007 Daniel P. Berrange <berrange@redhat.com>
* qemud/driver.c: Fixed integer overflow in calculating CPU time
for qemu guests - use long long throughout.
Fri Apr 13 10:07:04 EST 2007 Daniel P. Berrange <berrange@redhat.com> Fri Apr 13 10:07:04 EST 2007 Daniel P. Berrange <berrange@redhat.com>
* src/xend_internal.c: Back out accidental commit of code which * src/xend_internal.c: Back out accidental commit of code which

View File

@ -197,7 +197,7 @@ int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) { static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
char proc[PATH_MAX]; char proc[PATH_MAX];
FILE *pidinfo; FILE *pidinfo;
unsigned long usertime, systime; unsigned long long usertime, systime;
if (snprintf(proc, sizeof(proc), "/proc/%d/stat", pid) >= (int)sizeof(proc)) { if (snprintf(proc, sizeof(proc), "/proc/%d/stat", pid) >= (int)sizeof(proc)) {
return -1; return -1;
@ -210,7 +210,7 @@ static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
return 0; return 0;
} }
if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &usertime, &systime) != 2) { if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &usertime, &systime) != 2) {
qemudDebug("not enough arg"); qemudDebug("not enough arg");
return -1; return -1;
} }
@ -220,9 +220,9 @@ static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
* _SC_CLK_TCK is jiffies per second * _SC_CLK_TCK is jiffies per second
* So calulate thus.... * So calulate thus....
*/ */
*cpuTime = 1000 * 1000 * 1000 * (usertime + systime) / sysconf(_SC_CLK_TCK); *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime) / (unsigned long long)sysconf(_SC_CLK_TCK);
qemudDebug("Got %lu %lu %lld", usertime, systime, *cpuTime); qemudDebug("Got %llu %llu %llu", usertime, systime, *cpuTime);
fclose(pidinfo); fclose(pidinfo);