~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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
        Irix support by Carsten Kroll <ckroll@pinnaclesys.com>
 
7
 
 
8
    This program is free software; you can redistribute it and/or modify
 
9
    it under the terms of the GNU General Public License as published by
 
10
    the Free Software Foundation; either version 2 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    This program is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU General Public License
 
19
    along with this program; if not, write to the Free Software
 
20
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
21
 
 
22
*/
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <sys/types.h>
 
28
#include <sys/sysmp.h>
 
29
#include <sys/sysinfo.h>
 
30
 
 
31
#include "cpu.h"
 
32
#include "Command.h"
 
33
#include "ksysguardd.h"
 
34
 
 
35
#define CPUSTATES 6
 
36
 
 
37
long percentages(int cnt, int *out, long *new, long *old, long *diffs);
 
38
 
 
39
static int nCPUs=0;
 
40
 
 
41
 
 
42
long cp_time[CPUSTATES];
 
43
long cp_old[CPUSTATES];
 
44
long cp_diff[CPUSTATES];
 
45
int  cpu_states[CPUSTATES];
 
46
 
 
47
struct cpu_info{
 
48
        long cp_time[CPUSTATES];
 
49
        long cp_old[CPUSTATES];
 
50
        long cp_diff[CPUSTATES];
 
51
        int  cpu_states[CPUSTATES];
 
52
};
 
53
 
 
54
static struct cpu_info *g_ci;
 
55
 
 
56
/* returns the requested cpu number starting at 0*/
 
57
int getID(const char *cmd){
 
58
        int id;
 
59
        sscanf(cmd + 7, "%d", &id);
 
60
        return id-1;
 
61
}
 
62
 
 
63
void
 
64
initCpuInfo(struct SensorModul* sm)
 
65
{
 
66
        char mname[50];
 
67
        int i;
 
68
        if (sysmp(MP_NPROCS,&nCPUs) < 0) nCPUs=0;
 
69
        nCPUs++;
 
70
        g_ci = malloc(sizeof(struct cpu_info) * nCPUs);
 
71
        memset(g_ci,0,sizeof(struct cpu_info) * nCPUs);
 
72
 
 
73
        registerMonitor("cpu/system/user", "integer", printCPUUser, printCPUUserInfo, sm);
 
74
        registerMonitor("cpu/system/sys",  "integer", printCPUSys, printCPUSysInfo, sm);
 
75
        registerMonitor("cpu/system/idle", "integer", printCPUIdle, printCPUIdleInfo, sm);
 
76
        
 
77
        /* Monitor names changed from kde3 => kde4. Remain compatible with legacy requests when possible. */
 
78
        registerLegacyMonitor("cpu/user", "integer", printCPUUser, printCPUUserInfo, sm);
 
79
        registerLegacyMonitor("cpu/sys",  "integer", printCPUSys, printCPUSysInfo, sm);
 
80
        registerLegacyMonitor("cpu/idle", "integer", printCPUIdle, printCPUIdleInfo, sm);
 
81
 
 
82
        if (nCPUs > 1) for (i=0;i<nCPUs;i++){
 
83
                /* indidividual CPU load */
 
84
                sprintf(mname,"cpu/cpu%d/user",i+1);
 
85
                registerMonitor(mname, "integer", printCPUxUser,
 
86
                                printCPUUserInfo, sm);
 
87
                sprintf(mname,"cpu/cpu%d/sys",i+1);
 
88
                registerMonitor(mname, "integer", printCPUxSys,
 
89
                                printCPUSysInfo, sm);
 
90
                sprintf(mname,"cpu/cpu%d/idle",i+1);
 
91
                registerMonitor(mname, "integer", printCPUxIdle,
 
92
                                printCPUIdleInfo, sm);
 
93
        }
 
94
 
 
95
        updateCpuInfo();
 
96
}
 
97
 
 
98
void
 
99
exitCpuInfo(void)
 
100
{
 
101
        removeMonitor("cpu/system/user");
 
102
        removeMonitor("cpu/system/nice");
 
103
        removeMonitor("cpu/system/sys");
 
104
        removeMonitor("cpu/system/idle");
 
105
        
 
106
        /* Todo: Dynamically registered monitors (per cpu, per disk) are not removed yet) */
 
107
        
 
108
        /* These were registered as legacy monitors */
 
109
        removeMonitor("cpu/user");
 
110
        removeMonitor("cpu/nice");
 
111
        removeMonitor("cpu/sys");
 
112
        removeMonitor("cpu/idle");
 
113
        
 
114
 
 
115
        free(g_ci);
 
116
}
 
117
 
 
118
int
 
119
updateCpuInfo(void)
 
120
{
 
121
        struct sysinfo si;
 
122
        int rv=0;
 
123
        int i;
 
124
        /* overall summary */
 
125
        if (sysmp(MP_SAGET,MPSA_SINFO,&si,sizeof(struct sysinfo)) >=0){
 
126
                cp_time[CPU_IDLE]  =si.cpu[CPU_IDLE];
 
127
                cp_time[CPU_USER]  =si.cpu[CPU_USER];
 
128
                cp_time[CPU_KERNEL]=si.cpu[CPU_KERNEL];
 
129
                cp_time[CPU_SXBRK] =si.cpu[CPU_SXBRK];
 
130
                cp_time[CPU_INTR]  =si.cpu[CPU_INTR];
 
131
                cp_time[CPU_WAIT]  =si.cpu[CPU_WAIT];
 
132
                percentages(CPUSTATES,cpu_states,cp_time,cp_old,cp_diff);
 
133
        }
 
134
        /* individual CPU statistics*/
 
135
        if (nCPUs > 1) for (i=0;i<nCPUs;i++){
 
136
                if (sysmp(MP_SAGET1,MPSA_SINFO,&si,sizeof(struct sysinfo),i) >=0){
 
137
                        g_ci[i].cp_time[CPU_IDLE]  =si.cpu[CPU_IDLE];
 
138
                        g_ci[i].cp_time[CPU_USER]  =si.cpu[CPU_USER];
 
139
                        g_ci[i].cp_time[CPU_KERNEL]=si.cpu[CPU_KERNEL];
 
140
                        g_ci[i].cp_time[CPU_SXBRK] =si.cpu[CPU_SXBRK];
 
141
                        g_ci[i].cp_time[CPU_INTR]  =si.cpu[CPU_INTR];
 
142
                        g_ci[i].cp_time[CPU_WAIT]  =si.cpu[CPU_WAIT];
 
143
                        percentages(CPUSTATES, g_ci[i].cpu_states, g_ci[i].cp_time, g_ci[i].cp_old,g_ci[i].cp_diff);
 
144
                }else{
 
145
                        rv =-1;
 
146
                }
 
147
        }
 
148
        return (rv);
 
149
}
 
150
 
 
151
void
 
152
printCPUUser(const char* cmd)
 
153
{
 
154
        fprintf(CurrentClient, "%d\n", cpu_states[CPU_USER]/10);
 
155
}
 
156
 
 
157
void
 
158
printCPUUserInfo(const char* cmd)
 
159
{
 
160
        fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
 
161
}
 
162
 
 
163
void
 
164
printCPUSys(const char* cmd)
 
165
{
 
166
        fprintf(CurrentClient, "%d\n", cpu_states[CPU_KERNEL]/10);
 
167
}
 
168
 
 
169
void
 
170
printCPUSysInfo(const char* cmd)
 
171
{
 
172
        fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
 
173
}
 
174
 
 
175
void
 
176
printCPUIdle(const char* cmd)
 
177
{
 
178
        fprintf(CurrentClient, "%d\n", cpu_states[CPU_IDLE]/10);
 
179
}
 
180
 
 
181
void
 
182
printCPUIdleInfo(const char* cmd)
 
183
{
 
184
        fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
 
185
}
 
186
/* same as above but for individual CPUs */
 
187
void
 
188
printCPUxUser(const char* cmd)
 
189
{
 
190
        fprintf(CurrentClient, "%d\n", g_ci[getID(cmd)].cpu_states[CPU_USER]/10);
 
191
}
 
192
 
 
193
void
 
194
printCPUxSys(const char* cmd)
 
195
{
 
196
        fprintf(CurrentClient, "%d\n", g_ci[getID(cmd)].cpu_states[CPU_KERNEL]/10);
 
197
}
 
198
 
 
199
void
 
200
printCPUxIdle(const char* cmd)
 
201
{
 
202
        fprintf(CurrentClient, "%d\n", g_ci[getID(cmd)].cpu_states[CPU_IDLE]/10);
 
203
}
 
204
 
 
205
 
 
206
/* The part ripped from top... */
 
207
/*
 
208
 *  Top users/processes display for Unix
 
209
 *  Version 3
 
210
 *
 
211
 *  This program may be freely redistributed,
 
212
 *  but this entire comment MUST remain intact.
 
213
 *
 
214
 *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
 
215
 *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
 
216
 */
 
217
 
 
218
/*
 
219
 *  percentages(cnt, out, new, old, diffs) - calculate percentage change
 
220
 *      between array "old" and "new", putting the percentages in "out".
 
221
 *      "cnt" is size of each array and "diffs" is used for scratch space.
 
222
 *      The array "old" is updated on each call.
 
223
 *      The routine assumes modulo arithmetic.  This function is especially
 
224
 *      useful on BSD mchines for calculating cpu state percentages.
 
225
 */
 
226
 
 
227
long percentages(cnt, out, new, old, diffs)
 
228
 
 
229
int cnt;
 
230
int *out;
 
231
register long *new;
 
232
register long *old;
 
233
long *diffs;
 
234
 
 
235
{
 
236
    register int i;
 
237
    register long change;
 
238
    register long total_change;
 
239
    register long *dp;
 
240
    long half_total;
 
241
 
 
242
    /* initialization */
 
243
    total_change = 0;
 
244
    dp = diffs;
 
245
 
 
246
    /* calculate changes for each state and the overall change */
 
247
    for (i = 0; i < cnt; i++)
 
248
    {
 
249
        if ((change = *new - *old) < 0)
 
250
        {
 
251
            /* this only happens when the counter wraps */
 
252
            change = (int)
 
253
                ((unsigned long)*new-(unsigned long)*old);
 
254
        }
 
255
        total_change += (*dp++ = change);
 
256
        *old++ = *new++;
 
257
    }
 
258
 
 
259
    /* avoid divide by zero potential */
 
260
    if (total_change == 0)
 
261
    {
 
262
        total_change = 1;
 
263
    }
 
264
 
 
265
    /* calculate percentages based on overall change, rounding up */
 
266
    half_total = total_change / 2l;
 
267
 
 
268
    /* Do not divide by 0. Causes Floating point exception */
 
269
    if(total_change) {
 
270
        for (i = 0; i < cnt; i++)
 
271
        {
 
272
          *out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
 
273
        }
 
274
    }
 
275
 
 
276
    /* return the total in case the caller wants to use it */
 
277
    return(total_change);
 
278
}