$NetBSD: patch-fh,v 1.6 2008/01/04 21:05:58 markd Exp $ --- ksysguard/ksysguardd/NetBSD/CPU.c.orig 2005-10-11 04:04:31.000000000 +1300 +++ ksysguard/ksysguardd/NetBSD/CPU.c @@ -19,10 +19,11 @@ */ +#include +#include #include #include /* CPUSTATES */ #include -#include #include #include #include @@ -33,20 +34,11 @@ #include "Command.h" #include "ksysguardd.h" -long percentages(int cnt, int *out, long *new, long *old, long *diffs); +void percentages(int, int *, u_int64_t *, u_int64_t *, u_int64_t *); -struct nlist my_nlist[] = { - {"_cp_time"}, - { 0 } -}; - -kvm_t *kd; - -unsigned long cp_time_offset; - -long cp_time[CPUSTATES]; -long cp_old[CPUSTATES]; -long cp_diff[CPUSTATES]; +u_int64_t cp_time[CPUSTATES]; +u_int64_t cp_old[CPUSTATES]; +u_int64_t cp_diff[CPUSTATES]; int cpu_states[CPUSTATES]; void @@ -61,9 +53,6 @@ initCpuInfo(struct SensorModul* sm) printCPUSysInfo, sm); registerMonitor("cpu/idle", "integer", printCPUIdle, printCPUIdleInfo, sm); - kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open"); - kvm_nlist(kd, my_nlist); - cp_time_offset = my_nlist[0].n_value; updateCpuInfo(); } @@ -71,13 +60,18 @@ initCpuInfo(struct SensorModul* sm) void exitCpuInfo(void) { - kvm_close(kd); } int updateCpuInfo(void) { - kvm_read(kd, cp_time_offset, (char *)cp_time, sizeof(cp_time)); + int mib[2]; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_CP_TIME; + size = sizeof(cp_time[0]) * CPUSTATES; + sysctl(mib, 2, cp_time, &size, NULL, 0); percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); return (0); } @@ -152,20 +146,20 @@ printCPUIdleInfo(const char* cmd) * useful on BSD mchines for calculating cpu state percentages. */ -long percentages(cnt, out, new, old, diffs) +void percentages(cnt, out, new, old, diffs) int cnt; int *out; -register long *new; -register long *old; -long *diffs; +u_int64_t *new; +u_int64_t *old; +u_int64_t *diffs; { - register int i; - register long change; - register long total_change; - register long *dp; - long half_total; + int i; + u_int64_t change; + u_int64_t total_change; + u_int64_t *dp; + u_int64_t half_total; /* initialization */ total_change = 0; @@ -174,12 +168,11 @@ long *diffs; /* calculate changes for each state and the overall change */ for (i = 0; i < cnt; i++) { - if ((change = *new - *old) < 0) - { - /* this only happens when the counter wraps */ - change = (int) - ((unsigned long)*new-(unsigned long)*old); - } + /* + * Don't worry about wrapping - even at hz=1GHz, a + * u_int64_t will last at least 544 years. + */ + change = *new - *old; total_change += (*dp++ = change); *old++ = *new++; } @@ -191,16 +184,9 @@ long *diffs; } /* calculate percentages based on overall change, rounding up */ - half_total = total_change / 2l; - - /* Do not divide by 0. Causes Floating point exception */ - if(total_change) { - for (i = 0; i < cnt; i++) - { - *out++ = (int)((*diffs++ * 1000 + half_total) / total_change); - } + half_total = total_change / 2; + for (i = 0; i < cnt; i++) + { + *out++ = (int)((*diffs++ * 1000 + half_total) / total_change); } - - /* return the total in case the caller wants to use it */ - return(total_change); }