2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <sys/param.h>
22
#include <sys/sysctl.h>
23
#include <sys/dkstat.h>
28
#include <sys/types.h>
32
#include "ksysguardd.h"
34
long percentages(int cnt, int *out, long *new, long *old, long *diffs);
36
unsigned long cp_time_offset;
38
long cp_time[CPUSTATES];
39
long cp_old[CPUSTATES];
40
long cp_diff[CPUSTATES];
41
int cpu_states[CPUSTATES];
44
initCpuInfo(struct SensorModul* sm)
47
registerMonitor("cpu/system/user", "integer", printCPUUser,
48
printCPUUserInfo, sm);
49
registerMonitor("cpu/system/nice", "integer", printCPUNice,
50
printCPUNiceInfo, sm);
51
registerMonitor("cpu/system/sys", "integer", printCPUSys,
53
registerMonitor("cpu/system/idle", "integer", printCPUIdle,
54
printCPUIdleInfo, sm);
55
registerMonitor("cpu/interrupt", "integer", printCPUInterrupt,
56
printCPUInterruptInfo, sm);
69
static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
72
sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0);
73
percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
78
printCPUUser(const char* cmd)
80
fprintf(CurrentClient, "%d\n", cpu_states[CP_USER]/10);
84
printCPUUserInfo(const char* cmd)
86
fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
90
printCPUNice(const char* cmd)
92
fprintf(CurrentClient, "%d\n", cpu_states[CP_NICE]/10);
96
printCPUNiceInfo(const char* cmd)
98
fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n");
102
printCPUSys(const char* cmd)
104
fprintf(CurrentClient, "%d\n", cpu_states[CP_SYS]/10);
108
printCPUSysInfo(const char* cmd)
110
fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
114
printCPUIdle(const char* cmd)
116
fprintf(CurrentClient, "%d\n", cpu_states[CP_IDLE]/10);
120
printCPUIdleInfo(const char* cmd)
122
fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
126
printCPUInterrupt(const char* cmd)
128
fprintf(CurrentClient, "%d\n", cpu_states[CP_INTR]/10);
132
printCPUInterruptInfo(const char* cmd)
134
fprintf(CurrentClient, "CPU Interrupt Load\t0\t100\t%%\n");
137
/* The part ripped from top... */
139
* Top users/processes display for Unix
142
* This program may be freely redistributed,
143
* but this entire comment MUST remain intact.
145
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
146
* Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
150
* percentages(cnt, out, new, old, diffs) - calculate percentage change
151
* between array "old" and "new", putting the percentages i "out".
152
* "cnt" is size of each array and "diffs" is used for scratch space.
153
* The array "old" is updated on each call.
154
* The routine assumes modulo arithmetic. This function is especially
155
* useful on BSD mchines for calculating cpu state percentages.
158
long percentages(cnt, out, new, old, diffs)
168
register long change;
169
register long total_change;
177
/* calculate changes for each state and the overall change */
178
for (i = 0; i < cnt; i++)
180
if ((change = *new - *old) < 0)
182
/* this only happens when the counter wraps */
184
((unsigned long)*new-(unsigned long)*old);
186
total_change += (*dp++ = change);
190
/* avoid divide by zero potential */
191
if (total_change == 0)
196
/* calculate percentages based on overall change, rounding up */
197
half_total = total_change / 2l;
199
/* Do not divide by 0. Causes Floating point exception */
201
for (i = 0; i < cnt; i++)
203
*out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
207
/* return the total in case the caller wants to use it */
208
return(total_change);