summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/NetBSD/CPU.c
diff options
context:
space:
mode:
authorOBATA Akio <[email protected]>2019-04-06 16:49:26 +0900
committerOBATA Akio <[email protected]>2019-08-18 14:45:26 +0900
commit2a88ec3c02ab46c8b816cfa348ea53075f57a59c (patch)
treee7029145d265a0a6a31601a1eb476a719d6dd4b5 /ksysguard/ksysguardd/NetBSD/CPU.c
parentdf19ff6b7dc7adcdb88e86d50d9d88d622a09d88 (diff)
downloadtdebase-2a88ec3c02ab46c8b816cfa348ea53075f57a59c.tar.gz
tdebase-2a88ec3c02ab46c8b816cfa348ea53075f57a59c.zip
Revive NetBSD support
Catch up to TDE and OS changes Signed-off-by: OBATA Akio <[email protected]>
Diffstat (limited to 'ksysguard/ksysguardd/NetBSD/CPU.c')
-rw-r--r--ksysguard/ksysguardd/NetBSD/CPU.c76
1 files changed, 31 insertions, 45 deletions
diff --git a/ksysguard/ksysguardd/NetBSD/CPU.c b/ksysguard/ksysguardd/NetBSD/CPU.c
index 959924087..1d80d9bb6 100644
--- a/ksysguard/ksysguardd/NetBSD/CPU.c
+++ b/ksysguard/ksysguardd/NetBSD/CPU.c
@@ -19,10 +19,11 @@
*/
+#include <sys/param.h>
+#include <sys/sysctl.h>
#include <sys/dkstat.h>
#include <sys/sched.h> /* CPUSTATES */
#include <fcntl.h>
-#include <kvm.h>
#include <nlist.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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 cnt, int *out, u_int64_t *new, u_int64_t *old, u_int64_t *diffs);
-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);
}