~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/OpenBSD/cpu.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
19
*/
 
20
 
 
21
#include <sys/param.h>
 
22
#include <sys/sysctl.h>
 
23
#include <sys/dkstat.h>
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <sys/types.h>
 
29
 
 
30
#include "cpu.h"
 
31
#include "Command.h"
 
32
#include "ksysguardd.h"
 
33
 
 
34
long percentages(int cnt, int *out, long *new, long *old, long *diffs);
 
35
 
 
36
unsigned long cp_time_offset;
 
37
 
 
38
long cp_time[CPUSTATES];
 
39
long cp_old[CPUSTATES];
 
40
long cp_diff[CPUSTATES];
 
41
int cpu_states[CPUSTATES];
 
42
 
 
43
void
 
44
initCpuInfo(struct SensorModul* sm)
 
45
{
 
46
        /* Total CPU load */
 
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,
 
52
                        printCPUSysInfo, sm);
 
53
        registerMonitor("cpu/system/idle", "integer", printCPUIdle,
 
54
                        printCPUIdleInfo, sm);
 
55
        registerMonitor("cpu/interrupt", "integer", printCPUInterrupt,
 
56
                        printCPUInterruptInfo, sm);
 
57
 
 
58
        updateCpuInfo();
 
59
}
 
60
 
 
61
void
 
62
exitCpuInfo(void)
 
63
{
 
64
}
 
65
 
 
66
int
 
67
updateCpuInfo(void)
 
68
{
 
69
        static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
 
70
        size_t size;
 
71
        size=sizeof(cp_time);
 
72
        sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0);
 
73
        percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
 
74
        return (0);
 
75
}
 
76
 
 
77
void
 
78
printCPUUser(const char* cmd)
 
79
{
 
80
        fprintf(CurrentClient, "%d\n", cpu_states[CP_USER]/10);
 
81
}
 
82
 
 
83
void
 
84
printCPUUserInfo(const char* cmd)
 
85
{
 
86
        fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
 
87
}
 
88
 
 
89
void
 
90
printCPUNice(const char* cmd)
 
91
{
 
92
        fprintf(CurrentClient, "%d\n", cpu_states[CP_NICE]/10);
 
93
}
 
94
 
 
95
void
 
96
printCPUNiceInfo(const char* cmd)
 
97
{
 
98
        fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n");
 
99
}
 
100
 
 
101
void
 
102
printCPUSys(const char* cmd)
 
103
{
 
104
        fprintf(CurrentClient, "%d\n", cpu_states[CP_SYS]/10);
 
105
}
 
106
 
 
107
void
 
108
printCPUSysInfo(const char* cmd)
 
109
{
 
110
        fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
 
111
}
 
112
 
 
113
void
 
114
printCPUIdle(const char* cmd)
 
115
{
 
116
        fprintf(CurrentClient, "%d\n", cpu_states[CP_IDLE]/10);
 
117
}
 
118
 
 
119
void
 
120
printCPUIdleInfo(const char* cmd)
 
121
{
 
122
        fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
 
123
}
 
124
 
 
125
void
 
126
printCPUInterrupt(const char* cmd)
 
127
{
 
128
        fprintf(CurrentClient, "%d\n", cpu_states[CP_INTR]/10);
 
129
}
 
130
 
 
131
void
 
132
printCPUInterruptInfo(const char* cmd)
 
133
{
 
134
        fprintf(CurrentClient, "CPU Interrupt Load\t0\t100\t%%\n");
 
135
}
 
136
 
 
137
/* The part ripped from top... */
 
138
/*
 
139
 *  Top users/processes display for Unix
 
140
 *  Version 3
 
141
 *
 
142
 *  This program may be freely redistributed,
 
143
 *  but this entire comment MUST remain intact.
 
144
 *
 
145
 *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
 
146
 *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
 
147
 */
 
148
 
 
149
/*
 
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.
 
156
 */
 
157
 
 
158
long percentages(cnt, out, new, old, diffs)
 
159
 
 
160
int cnt;
 
161
int *out;
 
162
register long *new;
 
163
register long *old;
 
164
long *diffs;
 
165
 
 
166
{
 
167
    register int i;
 
168
    register long change;
 
169
    register long total_change;
 
170
    register long *dp;
 
171
    long half_total;
 
172
 
 
173
    /* initialization */
 
174
    total_change = 0;
 
175
    dp = diffs;
 
176
 
 
177
    /* calculate changes for each state and the overall change */
 
178
    for (i = 0; i < cnt; i++)
 
179
    {
 
180
        if ((change = *new - *old) < 0)
 
181
        {
 
182
            /* this only happens when the counter wraps */
 
183
            change = (int)
 
184
                ((unsigned long)*new-(unsigned long)*old);
 
185
        }
 
186
        total_change += (*dp++ = change);
 
187
        *old++ = *new++;
 
188
    }
 
189
 
 
190
    /* avoid divide by zero potential */
 
191
    if (total_change == 0)
 
192
    {
 
193
        total_change = 1;
 
194
    }
 
195
 
 
196
    /* calculate percentages based on overall change, rounding up */
 
197
    half_total = total_change / 2l;
 
198
 
 
199
    /* Do not divide by 0. Causes Floating point exception */
 
200
    if(total_change) {
 
201
        for (i = 0; i < cnt; i++)
 
202
        {
 
203
          *out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
 
204
        }
 
205
    }
 
206
 
 
207
    /* return the total in case the caller wants to use it */
 
208
    return(total_change);
 
209
}