1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
$NetBSD: patch-fh,v 1.5 2006/10/08 07:49:27 markd Exp $
--- ksysguard/ksysguardd/NetBSD/CPU.c.orig 2005-10-11 04:04:31.000000000 +1300
+++ ksysguard/ksysguardd/NetBSD/CPU.c
@@ -19,6 +19,8 @@
*/
+#include <sys/param.h>
+#include <sys/sysctl.h>
#include <sys/dkstat.h>
#include <sys/sched.h> /* CPUSTATES */
#include <fcntl.h>
@@ -33,20 +35,13 @@
#include "Command.h"
#include "ksysguardd.h"
-long percentages(int cnt, int *out, long *new, long *old, long *diffs);
-
-struct nlist my_nlist[] = {
- {"_cp_time"},
- { 0 }
-};
+void percentages(int, int *, u_int64_t *, u_int64_t *, u_int64_t *);
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 +56,7 @@ 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;
+ kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
updateCpuInfo();
}
@@ -77,7 +70,13 @@ exitCpuInfo(void)
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 +151,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 +173,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 +189,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);
}
|