convert cpu times to seconds

This commit is contained in:
Doug MacEachern 2005-05-11 23:17:39 +00:00
parent 083414a22a
commit 1146d10e26
2 changed files with 24 additions and 18 deletions

View File

@ -308,17 +308,17 @@ int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap)
return SIGAR_OK; return SIGAR_OK;
} }
static void get_cpu_metrics(sigar_cpu_t *cpu, char *line) static void get_cpu_metrics(sigar_t *sigar, sigar_cpu_t *cpu, char *line)
{ {
char *ptr = sigar_skip_token(line); /* "cpu%d" */ char *ptr = sigar_skip_token(line); /* "cpu%d" */
cpu->user += sigar_strtoul(ptr); cpu->user += sigar_strtoul(ptr) / sigar->ticks;
cpu->nice += sigar_strtoul(ptr); cpu->nice += sigar_strtoul(ptr) / sigar->ticks;
cpu->sys += sigar_strtoul(ptr); cpu->sys += sigar_strtoul(ptr) / sigar->ticks;
cpu->idle += sigar_strtoul(ptr); cpu->idle += sigar_strtoul(ptr) / sigar->ticks;
if (*ptr == ' ') { if (*ptr == ' ') {
/* 2.6+ kernels only */ /* 2.6+ kernels only */
cpu->wait += sigar_strtoul(ptr); cpu->wait += sigar_strtoul(ptr) / sigar->ticks;
} }
cpu->total += cpu->user + cpu->nice + cpu->sys + cpu->idle + cpu->wait; cpu->total += cpu->user + cpu->nice + cpu->sys + cpu->idle + cpu->wait;
} }
@ -333,7 +333,7 @@ int sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu)
} }
SIGAR_ZERO(cpu); SIGAR_ZERO(cpu);
get_cpu_metrics(cpu, buffer); get_cpu_metrics(sigar, cpu, buffer);
return SIGAR_OK; return SIGAR_OK;
} }
@ -370,7 +370,7 @@ int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist)
SIGAR_ZERO(cpu); SIGAR_ZERO(cpu);
} }
get_cpu_metrics(cpu, ptr); get_cpu_metrics(sigar, cpu, ptr);
i++; i++;
} }
@ -381,7 +381,7 @@ int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist)
/* likely older kernel where cpu\d is not present */ /* likely older kernel where cpu\d is not present */
cpu = &cpulist->data[cpulist->number++]; cpu = &cpulist->data[cpulist->number++];
SIGAR_ZERO(cpu); SIGAR_ZERO(cpu);
get_cpu_metrics(cpu, cpu_total); get_cpu_metrics(sigar, cpu, cpu_total);
} }
return SIGAR_OK; return SIGAR_OK;

View File

@ -86,6 +86,9 @@ typedef enum {
perf_offsets[ix] ? \ perf_offsets[ix] ? \
*((DWORD *)((BYTE *)counter_block + perf_offsets[ix])) : 0 *((DWORD *)((BYTE *)counter_block + perf_offsets[ix])) : 0
/* 1/100ns units to seconds */
#define NS100_2SEC(t) ((t) / 10000000)
static PERF_OBJECT_TYPE *get_perf_object(sigar_t *sigar, char *counter_key, static PERF_OBJECT_TYPE *get_perf_object(sigar_t *sigar, char *counter_key,
DWORD *err) DWORD *err)
{ {
@ -403,11 +406,11 @@ static int get_idle_cpu(sigar_t *sigar, sigar_cpu_t *cpu,
if (idx == -1) { if (idx == -1) {
int i; int i;
for (i=0; i<num; i++) { for (i=0; i<num; i++) {
cpu->idle += info[i].IdleTime.QuadPart; cpu->idle += NS100_2SEC(info[i].IdleTime.QuadPart);
} }
} }
else if (idx < num) { else if (idx < num) {
cpu->idle = info[idx].IdleTime.QuadPart; cpu->idle = NS100_2SEC(info[idx].IdleTime.QuadPart);
} }
else { else {
return ERROR_INVALID_DATA; return ERROR_INVALID_DATA;
@ -473,9 +476,11 @@ static int sigar_cpu_ntsys_get(sigar_t *sigar, sigar_cpu_t *cpu)
SIGAR_ZERO(cpu); SIGAR_ZERO(cpu);
for (i=0; i<num; i++) { for (i=0; i<num; i++) {
cpu->idle += info[i].IdleTime.QuadPart; cpu->idle += NS100_2SEC(info[i].IdleTime.QuadPart);
cpu->user += info[i].UserTime.QuadPart; cpu->user += NS100_2SEC(info[i].UserTime.QuadPart);
cpu->sys += info[i].KernelTime.QuadPart - info[i].IdleTime.QuadPart; cpu->sys += NS100_2SEC(info[i].KernelTime.QuadPart -
info[i].IdleTime.QuadPart);
cpu->total += cpu->idle + cpu->user + cpu->sys;
} }
return SIGAR_OK; return SIGAR_OK;
@ -599,9 +604,10 @@ static int sigar_cpu_list_ntsys_get(sigar_t *sigar,
SIGAR_ZERO(cpu); SIGAR_ZERO(cpu);
} }
idle = info[i].IdleTime.QuadPart; idle = NS100_2SEC(info[i].IdleTime.QuadPart);
user = info[i].UserTime.QuadPart; user = NS100_2SEC(info[i].UserTime.QuadPart);
sys = info[i].KernelTime.QuadPart - info[i].IdleTime.QuadPart; sys = NS100_2SEC(info[i].KernelTime.QuadPart -
info[i].IdleTime.QuadPart);
cpu->idle += idle; cpu->idle += idle;
cpu->user += user; cpu->user += user;
cpu->sys += sys; cpu->sys += sys;
@ -823,7 +829,7 @@ SIGAR_DECLARE(int) sigar_proc_cred_get(sigar_t *sigar, sigar_pid_t pid,
} }
#define FILETIME2SEC(ft) \ #define FILETIME2SEC(ft) \
(((ft.dwHighDateTime << 32) | ft.dwLowDateTime) / 10000000) NS100_2SEC(((ft.dwHighDateTime << 32) | ft.dwLowDateTime))
SIGAR_DECLARE(int) sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid, SIGAR_DECLARE(int) sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_time_t *proctime) sigar_proc_time_t *proctime)