~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_proc_stat.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_proc_stat.c 1079 2010-01-15 21:44:04Z volker $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_proc_stat.c $
 
3
 *
 
4
 * plugin for /proc/stat parsing
 
5
 *
 
6
 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
11
 * LCD4Linux is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2, or (at your option)
 
14
 * any later version.
 
15
 *
 
16
 * LCD4Linux is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
24
 *
 
25
 */
 
26
 
 
27
/* 
 
28
 * exported functions:
 
29
 *
 
30
 * int plugin_init_proc_stat (void)
 
31
 *  adds functions to access /proc/stat
 
32
 *
 
33
 */
 
34
 
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <stdlib.h>
 
39
#include <stdio.h>
 
40
#include <string.h>
 
41
#include <ctype.h>
 
42
#include <errno.h>
 
43
 
 
44
#ifdef __MAC_OS_X_VERSION_10_3
 
45
#include <mach/mach_host.h>
 
46
#include <mach/host_info.h>
 
47
#endif
 
48
 
 
49
#include "debug.h"
 
50
#include "plugin.h"
 
51
#include "qprintf.h"
 
52
#include "hash.h"
 
53
 
 
54
 
 
55
static HASH Stat;
 
56
static FILE *stream = NULL;
 
57
 
 
58
 
 
59
static void hash_put1(const char *key1, const char *val)
 
60
{
 
61
    hash_put_delta(&Stat, key1, val);
 
62
}
 
63
 
 
64
 
 
65
static void hash_put2(const char *key1, const char *key2, const char *val)
 
66
{
 
67
    char key[32];
 
68
 
 
69
    qprintf(key, sizeof(key), "%s.%s", key1, key2);
 
70
    hash_put1(key, val);
 
71
}
 
72
 
 
73
 
 
74
static void hash_put3(const char *key1, const char *key2, const char *key3, const char *val)
 
75
{
 
76
    char key[32];
 
77
 
 
78
    qprintf(key, sizeof(key), "%s.%s.%s", key1, key2, key3);
 
79
    hash_put1(key, val);
 
80
}
 
81
 
 
82
 
 
83
static int parse_proc_stat(void)
 
84
{
 
85
    int age;
 
86
 
 
87
    /* reread every 10 msec only */
 
88
    age = hash_age(&Stat, NULL);
 
89
    if (age > 0 && age <= 10)
 
90
        return 0;
 
91
 
 
92
#ifndef __MAC_OS_X_VERSION_10_3
 
93
 
 
94
    /* Linux Kernel, /proc-filesystem */
 
95
 
 
96
    if (stream == NULL)
 
97
        stream = fopen("/proc/stat", "r");
 
98
    if (stream == NULL) {
 
99
        error("fopen(/proc/stat) failed: %s", strerror(errno));
 
100
        return -1;
 
101
    }
 
102
 
 
103
    rewind(stream);
 
104
 
 
105
    while (!feof(stream)) {
 
106
        char buffer[1024];
 
107
        if (fgets(buffer, sizeof(buffer), stream) == NULL)
 
108
            break;
 
109
 
 
110
        if (strncmp(buffer, "cpu", 3) == 0) {
 
111
            char *key[] = { "user", "nice", "system", "idle", "iow", "irq", "sirq" };
 
112
            char delim[] = " \t\n";
 
113
            char *cpu, *beg, *end;
 
114
            int i;
 
115
 
 
116
            cpu = buffer;
 
117
 
 
118
            /* skip "cpu" or "cpu0" block */
 
119
            if ((end = strpbrk(buffer, delim)) != NULL)
 
120
                *end = '\0';
 
121
            beg = end ? end + 1 : NULL;
 
122
 
 
123
            for (i = 0; i < 7 && beg != NULL; i++) {
 
124
                while (strchr(delim, *beg))
 
125
                    beg++;
 
126
                if ((end = strpbrk(beg, delim)))
 
127
                    *end = '\0';
 
128
                hash_put2(cpu, key[i], beg);
 
129
                beg = end ? end + 1 : NULL;
 
130
            }
 
131
        }
 
132
 
 
133
        else if (strncmp(buffer, "page ", 5) == 0) {
 
134
            char *key[] = { "in", "out" };
 
135
            char delim[] = " \t\n";
 
136
            char *beg, *end;
 
137
            int i;
 
138
 
 
139
            for (i = 0, beg = buffer + 5; i < 2 && beg != NULL; i++) {
 
140
                while (strchr(delim, *beg))
 
141
                    beg++;
 
142
                if ((end = strpbrk(beg, delim)))
 
143
                    *end = '\0';
 
144
                hash_put2("page", key[i], beg);
 
145
                beg = end ? end + 1 : NULL;
 
146
            }
 
147
        }
 
148
 
 
149
        else if (strncmp(buffer, "swap ", 5) == 0) {
 
150
            char *key[] = { "in", "out" };
 
151
            char delim[] = " \t\n";
 
152
            char *beg, *end;
 
153
            int i;
 
154
 
 
155
            for (i = 0, beg = buffer + 5; i < 2 && beg != NULL; i++) {
 
156
                while (strchr(delim, *beg))
 
157
                    beg++;
 
158
                if ((end = strpbrk(beg, delim)))
 
159
                    *end = '\0';
 
160
                hash_put2("swap", key[i], beg);
 
161
                beg = end ? end + 1 : NULL;
 
162
            }
 
163
        }
 
164
 
 
165
        else if (strncmp(buffer, "intr ", 5) == 0) {
 
166
            char delim[] = " \t\n";
 
167
            char *beg, *end, num[4];
 
168
            int i;
 
169
 
 
170
            for (i = 0, beg = buffer + 5; i < 17 && beg != NULL; i++) {
 
171
                while (strchr(delim, *beg))
 
172
                    beg++;
 
173
                if ((end = strpbrk(beg, delim)))
 
174
                    *end = '\0';
 
175
                if (i == 0)
 
176
                    strcpy(num, "sum");
 
177
                else
 
178
                    qprintf(num, sizeof(num), "%d", i - 1);
 
179
                hash_put2("intr", num, beg);
 
180
                beg = end ? end + 1 : NULL;
 
181
            }
 
182
        }
 
183
 
 
184
        else if (strncmp(buffer, "disk_io:", 8) == 0) {
 
185
            char *key[] = { "io", "rio", "rblk", "wio", "wblk" };
 
186
            char delim[] = " ():,\t\n";
 
187
            char *dev, *beg, *end, *p;
 
188
            int i;
 
189
 
 
190
            dev = buffer + 8;
 
191
            while (dev != NULL) {
 
192
                while (strchr(delim, *dev))
 
193
                    dev++;
 
194
                if ((end = strchr(dev, ')')))
 
195
                    *end = '\0';
 
196
                while ((p = strchr(dev, ',')) != NULL)
 
197
                    *p = ':';
 
198
                beg = end ? end + 1 : NULL;
 
199
                for (i = 0; i < 5 && beg != NULL; i++) {
 
200
                    while (strchr(delim, *beg))
 
201
                        beg++;
 
202
                    if ((end = strpbrk(beg, delim)))
 
203
                        *end = '\0';
 
204
                    hash_put3("disk_io", dev, key[i], beg);
 
205
                    beg = end ? end + 1 : NULL;
 
206
                }
 
207
                dev = beg;
 
208
            }
 
209
        }
 
210
 
 
211
        else {
 
212
            char delim[] = " \t\n";
 
213
            char *beg, *end;
 
214
 
 
215
            beg = buffer;
 
216
            if ((end = strpbrk(beg, delim)))
 
217
                *end = '\0';
 
218
            beg = end ? end + 1 : NULL;
 
219
            if ((end = strpbrk(beg, delim)))
 
220
                *end = '\0';
 
221
            while (strchr(delim, *beg))
 
222
                beg++;
 
223
            hash_put1(buffer, beg);
 
224
        }
 
225
    }
 
226
 
 
227
#else
 
228
 
 
229
    /* MACH Kernel, MacOS X */
 
230
 
 
231
    kern_return_t err;
 
232
    mach_msg_type_number_t count;
 
233
    host_info_t r_load;
 
234
    host_cpu_load_info_data_t cpu_load;
 
235
    char s_val[8];
 
236
 
 
237
    r_load = &cpu_load;
 
238
    count = HOST_CPU_LOAD_INFO_COUNT;
 
239
    err = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, r_load, &count);
 
240
    if (KERN_SUCCESS != err) {
 
241
        error("Error getting cpu load");
 
242
        return -1;
 
243
    }
 
244
    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_USER]);
 
245
    hash_put2("cpu", "user", s_val);
 
246
    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_NICE]);
 
247
    hash_put2("cpu", "nice", s_val);
 
248
    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_SYSTEM]);
 
249
    hash_put2("cpu", "system", s_val);
 
250
    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_IDLE]);
 
251
    hash_put2("cpu", "idle", s_val);
 
252
 
 
253
#endif
 
254
 
 
255
    return 0;
 
256
}
 
257
 
 
258
 
 
259
static void my_proc_stat(RESULT * result, const int argc, RESULT * argv[])
 
260
{
 
261
    char *string;
 
262
    double number;
 
263
 
 
264
    if (parse_proc_stat() < 0) {
 
265
        SetResult(&result, R_STRING, "");
 
266
        return;
 
267
    }
 
268
 
 
269
    switch (argc) {
 
270
    case 1:
 
271
        string = hash_get(&Stat, R2S(argv[0]), NULL);
 
272
        if (string == NULL)
 
273
            string = "";
 
274
        SetResult(&result, R_STRING, string);
 
275
        break;
 
276
    case 2:
 
277
        number = hash_get_delta(&Stat, R2S(argv[0]), NULL, R2N(argv[1]));
 
278
        SetResult(&result, R_NUMBER, &number);
 
279
        break;
 
280
    default:
 
281
        error("proc_stat(): wrong number of parameters");
 
282
        SetResult(&result, R_STRING, "");
 
283
    }
 
284
}
 
285
 
 
286
 
 
287
static void my_cpu(RESULT * result, RESULT * arg1, RESULT * arg2)
 
288
{
 
289
    char *key;
 
290
    int delay;
 
291
    double value;
 
292
    double cpu_user, cpu_nice, cpu_system, cpu_idle, cpu_total;
 
293
    double cpu_iow, cpu_irq, cpu_sirq;
 
294
 
 
295
    if (parse_proc_stat() < 0) {
 
296
        SetResult(&result, R_STRING, "");
 
297
        return;
 
298
    }
 
299
 
 
300
    key = R2S(arg1);
 
301
    delay = R2N(arg2);
 
302
 
 
303
    cpu_user = hash_get_delta(&Stat, "cpu.user", NULL, delay);
 
304
    cpu_nice = hash_get_delta(&Stat, "cpu.nice", NULL, delay);
 
305
    cpu_system = hash_get_delta(&Stat, "cpu.system", NULL, delay);
 
306
    cpu_idle = hash_get_delta(&Stat, "cpu.idle", NULL, delay);
 
307
 
 
308
    /* new fields for kernel 2.6 */
 
309
    /* even if we dont have this param (ie kernel 2.4) */
 
310
    /* the return is 0.0 and not change the results */
 
311
    cpu_iow = hash_get_delta(&Stat, "cpu.iow", NULL, delay);
 
312
    cpu_irq = hash_get_delta(&Stat, "cpu.irq", NULL, delay);
 
313
    cpu_sirq = hash_get_delta(&Stat, "cpu.sirq", NULL, delay);
 
314
 
 
315
    cpu_total = cpu_user + cpu_nice + cpu_system + cpu_idle + cpu_iow + cpu_irq + cpu_sirq;
 
316
 
 
317
    if (strcasecmp(key, "user") == 0)
 
318
        value = cpu_user;
 
319
    else if (strcasecmp(key, "nice") == 0)
 
320
        value = cpu_nice;
 
321
    else if (strcasecmp(key, "system") == 0)
 
322
        value = cpu_system;
 
323
    else if (strcasecmp(key, "idle") == 0)
 
324
        value = cpu_idle;
 
325
    else if (strcasecmp(key, "iowait") == 0)
 
326
        value = cpu_iow;
 
327
    else if (strcasecmp(key, "irq") == 0)
 
328
        value = cpu_irq;
 
329
    else if (strcasecmp(key, "softirq") == 0)
 
330
        value = cpu_sirq;
 
331
    else if (strcasecmp(key, "busy") == 0)
 
332
        value = cpu_total - cpu_idle;
 
333
 
 
334
    if (cpu_total > 0.0)
 
335
        value = 100 * value / cpu_total;
 
336
    else
 
337
        value = 0.0;
 
338
 
 
339
    SetResult(&result, R_NUMBER, &value);
 
340
}
 
341
 
 
342
 
 
343
static void my_disk(RESULT * result, RESULT * arg1, RESULT * arg2, RESULT * arg3)
 
344
{
 
345
    char *dev, *key, buffer[32];
 
346
    int delay;
 
347
    double value;
 
348
 
 
349
    if (parse_proc_stat() < 0) {
 
350
        SetResult(&result, R_STRING, "");
 
351
        return;
 
352
    }
 
353
 
 
354
    dev = R2S(arg1);
 
355
    key = R2S(arg2);
 
356
    delay = R2N(arg3);
 
357
 
 
358
    qprintf(buffer, sizeof(buffer), "disk_io\\.%s\\.%s", dev, key);
 
359
    value = hash_get_regex(&Stat, buffer, NULL, delay);
 
360
 
 
361
    SetResult(&result, R_NUMBER, &value);
 
362
}
 
363
 
 
364
 
 
365
int plugin_init_proc_stat(void)
 
366
{
 
367
    hash_create(&Stat);
 
368
    AddFunction("proc_stat", -1, my_proc_stat);
 
369
    AddFunction("proc_stat::cpu", 2, my_cpu);
 
370
    AddFunction("proc_stat::disk", 3, my_disk);
 
371
    return 0;
 
372
}
 
373
 
 
374
void plugin_exit_proc_stat(void)
 
375
{
 
376
    if (stream != NULL) {
 
377
        fclose(stream);
 
378
        stream = NULL;
 
379
    }
 
380
    hash_destroy(&Stat);
 
381
}