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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
KSysGuard, the KDE System Guard
Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <osreldate.h>
#include <sys/types.h>
#if defined(__DragonFly__)
#include <sys/param.h>
#include <kinfo.h>
#elif __FreeBSD_version < 500101
#include <sys/dkstat.h>
#else
#include <sys/resource.h>
#endif
#include <sys/sysctl.h>
#include <devstat.h>
#include <fcntl.h>
#include <nlist.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CPU.h"
#include "Command.h"
#include "ksysguardd.h"
#if defined(__DragonFly__)
static void cputime_percentages(int[4], struct kinfo_cputime *,
struct kinfo_cputime *);
static struct kinfo_cputime cp_time, cp_old;
#define CPUSTATES 4
#define CP_USER 0
#define CP_NICE 1
#define CP_SYS 2
#define CP_IDLE 3
#else
long percentages(int cnt, int *out, long *new, long *old, long *diffs);
unsigned long cp_time_offset;
long cp_time[CPUSTATES];
long cp_old[CPUSTATES];
long cp_diff[CPUSTATES];
#endif
int cpu_states[CPUSTATES];
void
initCpuInfo(struct SensorModul* sm)
{
/* Total CPU load */
registerMonitor("cpu/user", "integer", printCPUUser,
printCPUUserInfo, sm);
registerMonitor("cpu/nice", "integer", printCPUNice,
printCPUNiceInfo, sm);
registerMonitor("cpu/sys", "integer", printCPUSys,
printCPUSysInfo, sm);
registerMonitor("cpu/idle", "integer", printCPUIdle,
printCPUIdleInfo, sm);
updateCpuInfo();
}
void
exitCpuInfo(void)
{
}
int
updateCpuInfo(void)
{
#if defined(__DragonFly__)
kinfo_get_sched_cputime(&cp_time);
cputime_percentages(cpu_states, &cp_time, &cp_old);
#else
size_t len = sizeof(cp_time);
sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0);
percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
#endif
return (0);
}
void
printCPUUser(const char* cmd)
{
fprintf(CurrentClient, "%d\n", cpu_states[CP_USER]/10);
}
void
printCPUUserInfo(const char* cmd)
{
fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
}
void
printCPUNice(const char* cmd)
{
fprintf(CurrentClient, "%d\n", cpu_states[CP_NICE]/10);
}
void
printCPUNiceInfo(const char* cmd)
{
fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n");
}
void
printCPUSys(const char* cmd)
{
fprintf(CurrentClient, "%d\n", cpu_states[CP_SYS]/10);
}
void
printCPUSysInfo(const char* cmd)
{
fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
}
void
printCPUIdle(const char* cmd)
{
fprintf(CurrentClient, "%d\n", cpu_states[CP_IDLE]/10);
}
void
printCPUIdleInfo(const char* cmd)
{
fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
}
/* The part ripped from top... */
/*
* Top users/processes display for Unix
* Version 3
*
* This program may be freely redistributed,
* but this entire comment MUST remain intact.
*
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
* Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
*/
/*
* percentages(cnt, out, new, old, diffs) - calculate percentage change
* between array "old" and "new", putting the percentages i "out".
* "cnt" is size of each array and "diffs" is used for scratch space.
* The array "old" is updated on each call.
* The routine assumes modulo arithmetic. This function is especially
* useful on BSD mchines for calculating cpu state percentages.
*/
#if defined(__DragonFly__)
static void
cputime_percentages(int out[4], struct kinfo_cputime *new, struct kinfo_cputime * old)
{
struct kinfo_cputime diffs;
int i;
uint64_t total_change, half_total;
/* initialization */
total_change = 0;
diffs.cp_user = new->cp_user - old->cp_user;
diffs.cp_nice = new->cp_nice - old->cp_nice;
diffs.cp_sys = new->cp_sys - old->cp_sys;
diffs.cp_intr = new->cp_intr - old->cp_intr;
diffs.cp_idle = new->cp_idle - old->cp_idle;
total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys +
diffs.cp_intr + diffs.cp_idle;
old->cp_user = new->cp_user;
old->cp_nice = new->cp_nice;
old->cp_sys = new->cp_sys;
old->cp_intr = new->cp_intr;
old->cp_idle = new->cp_idle;
/* avoid divide by zero potential */
if (total_change == 0)
total_change = 1;
/* calculate percentages based on overall change, rounding up */
half_total = total_change >> 1;
out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change);
out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change);
out[2] = (((diffs.cp_sys + diffs.cp_intr) * 1000LL + half_total) / total_change);
out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change);
}
#else
long percentages(cnt, out, new, old, diffs)
int cnt;
int *out;
long *new;
long *old;
long *diffs;
{
int i;
long change;
long total_change;
long *dp;
long half_total;
/* initialization */
total_change = 0;
dp = 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);
}
total_change += (*dp++ = change);
*old++ = *new++;
}
/* avoid divide by zero potential */
if (total_change == 0)
{
total_change = 1;
}
/* 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);
}
}
/* return the total in case the caller wants to use it */
return(total_change);
}
#endif
|