From 1146d10e2645ae550fe2919fb7e75dd7d10607a4 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 11 May 2005 23:17:39 +0000 Subject: [PATCH] convert cpu times to seconds --- src/os/linux/linux_sigar.c | 18 +++++++++--------- src/os/win32/win32_sigar.c | 24 +++++++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/os/linux/linux_sigar.c b/src/os/linux/linux_sigar.c index 7d886ed2..9741c2d8 100644 --- a/src/os/linux/linux_sigar.c +++ b/src/os/linux/linux_sigar.c @@ -308,17 +308,17 @@ int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap) 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" */ - cpu->user += sigar_strtoul(ptr); - cpu->nice += sigar_strtoul(ptr); - cpu->sys += sigar_strtoul(ptr); - cpu->idle += sigar_strtoul(ptr); + cpu->user += sigar_strtoul(ptr) / sigar->ticks; + cpu->nice += sigar_strtoul(ptr) / sigar->ticks; + cpu->sys += sigar_strtoul(ptr) / sigar->ticks; + cpu->idle += sigar_strtoul(ptr) / sigar->ticks; if (*ptr == ' ') { /* 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; } @@ -333,7 +333,7 @@ int sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu) } SIGAR_ZERO(cpu); - get_cpu_metrics(cpu, buffer); + get_cpu_metrics(sigar, cpu, buffer); return SIGAR_OK; } @@ -370,7 +370,7 @@ int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist) SIGAR_ZERO(cpu); } - get_cpu_metrics(cpu, ptr); + get_cpu_metrics(sigar, cpu, ptr); 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 */ cpu = &cpulist->data[cpulist->number++]; SIGAR_ZERO(cpu); - get_cpu_metrics(cpu, cpu_total); + get_cpu_metrics(sigar, cpu, cpu_total); } return SIGAR_OK; diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index 660fd0fc..17d66df5 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -86,6 +86,9 @@ typedef enum { perf_offsets[ix] ? \ *((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, DWORD *err) { @@ -403,11 +406,11 @@ static int get_idle_cpu(sigar_t *sigar, sigar_cpu_t *cpu, if (idx == -1) { int i; for (i=0; iidle += info[i].IdleTime.QuadPart; + cpu->idle += NS100_2SEC(info[i].IdleTime.QuadPart); } } else if (idx < num) { - cpu->idle = info[idx].IdleTime.QuadPart; + cpu->idle = NS100_2SEC(info[idx].IdleTime.QuadPart); } else { return ERROR_INVALID_DATA; @@ -473,9 +476,11 @@ static int sigar_cpu_ntsys_get(sigar_t *sigar, sigar_cpu_t *cpu) SIGAR_ZERO(cpu); for (i=0; iidle += info[i].IdleTime.QuadPart; - cpu->user += info[i].UserTime.QuadPart; - cpu->sys += info[i].KernelTime.QuadPart - info[i].IdleTime.QuadPart; + cpu->idle += NS100_2SEC(info[i].IdleTime.QuadPart); + cpu->user += NS100_2SEC(info[i].UserTime.QuadPart); + cpu->sys += NS100_2SEC(info[i].KernelTime.QuadPart - + info[i].IdleTime.QuadPart); + cpu->total += cpu->idle + cpu->user + cpu->sys; } return SIGAR_OK; @@ -599,9 +604,10 @@ static int sigar_cpu_list_ntsys_get(sigar_t *sigar, SIGAR_ZERO(cpu); } - idle = info[i].IdleTime.QuadPart; - user = info[i].UserTime.QuadPart; - sys = info[i].KernelTime.QuadPart - info[i].IdleTime.QuadPart; + idle = NS100_2SEC(info[i].IdleTime.QuadPart); + user = NS100_2SEC(info[i].UserTime.QuadPart); + sys = NS100_2SEC(info[i].KernelTime.QuadPart - + info[i].IdleTime.QuadPart); cpu->idle += idle; cpu->user += user; cpu->sys += sys; @@ -823,7 +829,7 @@ SIGAR_DECLARE(int) sigar_proc_cred_get(sigar_t *sigar, sigar_pid_t pid, } #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_proc_time_t *proctime)