~ubuntu-branches/ubuntu/vivid/xfce4-systemload-plugin/vivid

« back to all changes in this revision

Viewing changes to panel-plugin/cpu.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez, Evgeni Golov, Yves-Alexis Perez
  • Date: 2011-04-19 23:30:56 UTC
  • mfrom: (2.5.2 upstream)
  • mto: (1.1.11 vivid-proposed)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: james.westby@ubuntu.com-20110419233056-bn3kriiqwolmcdij
Tags: 1.0.0-1
[ Evgeni Golov ]
* Fix Vcs-* fields, they were missing 'trunk' in the path.

[ Yves-Alexis Perez ]
* New upstream release.
* debian/control:
  - remove Simon and Emanuele from uploaders, thanks to them.
  - add build-dep on libxfcegui4-dev.
  - update standards version to 3.9.2.
  - add build-dep on hardening-includes.
  - add build-dep on intltool.
  - bump xfce4-panel-dev b-dep to (>= 4.8.0).
* Switch to 3.0 (quilt) source format.
* debian/rules:
  - pick {C,LD}FLAGS from dpkg-buildflags.
  - add -O1, -z,defs and --as-needed to LDFLAGS.
  - add hardening flags to {C,LD}FLAGS.
* debian/watch edited to track Xfce archive reorganisation
* debian/patches:
  - 01_fix-ftbfs-kfreebsd dropped, included upstream.
  - 02_fix-tooltip-gtk2.12 dropped as well. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (c) 2003 Riccardo Persichetti <riccardo.persichetti@tin.it>
 
3
 * Copyright (c) 2010 Florian Rivoal <frivoal@xfce.org>
3
4
 *
4
5
 * Redistribution and use in source and binary forms, with or without
5
6
 * modification, are permitted provided that the following conditions
37
38
#include <libxfcegui4/dialogs.h>
38
39
#include "cpu.h" 
39
40
 
40
 
#if defined(__linux__)
 
41
#if defined(__linux__) || defined(__FreeBSD_kernel__)
 
42
 
 
43
#include <stdint.h>
 
44
 
41
45
#define PROC_STAT "/proc/stat"
42
46
 
43
47
/* user, nice, system, interrupt(BSD specific), idle */
45
49
    gulong load[5];
46
50
};
47
51
 
48
 
struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
49
52
gulong cpu_used, oldtotal, oldused;
50
53
 
51
54
gulong read_cpuload()
52
55
{
53
56
    FILE *fd;
 
57
    uint64_t user, nice, system, idle, iowait, irq, softirq, guest;
54
58
    gulong used, total;
 
59
    int nb_read;
55
60
 
56
61
    fd = fopen(PROC_STAT, "r");
57
62
    if (!fd) {
58
63
        g_warning(_("File /proc/stat not found!"));
59
64
        return 0;
60
65
    }
61
 
    fscanf(fd, "%*s %ld %ld %ld %ld", &fresh.load[0], &fresh.load[1],
62
 
           &fresh.load[2], &fresh.load[3]);
 
66
 
 
67
    /* Don't count steal time. It is neither busy nor free tiime. */
 
68
    nb_read = fscanf (fd, "%*s " "%llu %llu %llu %llu %llu %llu %llu %*llu %llu",
 
69
            &user, &nice, &system, &idle, &iowait, &irq, &softirq, &guest);
63
70
    fclose(fd);
64
 
 
65
 
    used = fresh.load[0] + fresh.load[1] + fresh.load[2];
66
 
    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
 
71
    switch (nb_read) /* fall through intentional */
 
72
    {
 
73
            case 4:
 
74
                    iowait = 0;
 
75
            case 5:
 
76
                    irq = 0;
 
77
            case 6:
 
78
                    softirq = 0;
 
79
            case 7:
 
80
                    guest = 0;
 
81
    }
 
82
 
 
83
    used = user + nice + system + irq + softirq + guest;
 
84
    total = used + idle + iowait;
 
85
 
67
86
    if ((total - oldtotal) != 0)
68
87
    {
69
88
        cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
97
116
    gulong load[5];
98
117
};
99
118
 
100
 
struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
101
119
gulong cpu_used, oldtotal, oldused;
102
120
 
103
121
gulong read_cpuload()
111
129
        return 0;
112
130
    }
113
131
 
114
 
    fresh.load[0] = cp_time[CP_USER];
115
 
    fresh.load[1] = cp_time[CP_NICE];
116
 
    fresh.load[2] = cp_time[CP_SYS];
117
 
    fresh.load[3] = cp_time[CP_IDLE];
118
 
    fresh.load[4] = cp_time[CP_IDLE];
 
132
    used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
 
133
    total = used + cp_time[CP_IDLE];
119
134
 
120
 
    used = fresh.load[0] + fresh.load[1] + fresh.load[2];
121
 
    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
122
135
    if ((total - oldtotal) != 0)
123
136
    {
124
137
        cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
155
168
    gulong load[5];
156
169
};
157
170
 
158
 
struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
159
171
gulong cpu_used, oldtotal, oldused;
160
172
 
161
173
gulong read_cpuload()
170
182
            return 0;
171
183
    }
172
184
 
173
 
    fresh.load[0] = cp_time[CP_USER];
174
 
    fresh.load[1] = cp_time[CP_NICE];
175
 
    fresh.load[2] = cp_time[CP_SYS];
176
 
    fresh.load[3] = cp_time[CP_IDLE];
177
 
    fresh.load[4] = cp_time[CP_IDLE];
 
185
    used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
 
186
    total = used + cp_time[CP_IDLE];
178
187
 
179
 
    used = fresh.load[0] + fresh.load[1] + fresh.load[2];
180
 
    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
181
188
    if ((total - oldtotal) != 0)
182
189
    {
183
190
        cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
215
222
    gulong load[5];
216
223
};
217
224
 
218
 
struct cpu_load_struct fresh = {{0, 0, 0, 0, 0}};
219
225
gulong cpu_used, oldtotal, oldused;
220
226
 
221
227
gulong read_cpuload()
230
236
            return 0;
231
237
    }
232
238
 
233
 
    fresh.load[0] = cp_time[CP_USER];
234
 
    fresh.load[1] = cp_time[CP_NICE];
235
 
    fresh.load[2] = cp_time[CP_SYS];
236
 
    fresh.load[3] = cp_time[CP_INTR];
237
 
    fresh.load[4] = cp_time[CP_IDLE];
 
239
    used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] + cp_time[CP_INTR];
 
240
    total = used + cp_time[CP_IDLE];
238
241
 
239
 
    used = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
240
 
    total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3] +
241
 
            fresh.load[4];
242
242
    if ((total - oldtotal) != 0)
243
243
    {
244
244
        cpu_used = (100 * (double)(used - oldused)) / (double)(total - oldtotal);
254
254
}
255
255
 
256
256
#else
257
 
#error "Your plattform is not yet supported"
 
257
#error "Your platform is not yet supported"
258
258
#endif