~indicator-multiload/indicator-multiload/trunk

1 by Michael Hofmann
Initial public version.
1
/******************************************************************************
2
 * Copyright (C) 2011  Michael Hofmann <mh21@piware.de>                       *
3
 *                                                                            *
4
 * This program is free software; you can redistribute it and/or modify       *
5
 * it under the terms of the GNU General Public License as published by       *
6
 * the Free Software Foundation; either version 3 of the License, or          *
7
 * (at your option) any later version.                                        *
8
 *                                                                            *
9
 * This program is distributed in the hope that it will be useful,            *
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
12
 * GNU General Public License for more details.                               *
13
 *                                                                            *
14
 * You should have received a copy of the GNU General Public License along    *
15
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
17
 ******************************************************************************/
18
19
public class CpuIconData : IconData {
20
    private uint64[] lastdata;
21
22
    public CpuIconData() {
23
        base("cpuload", 4, 1, 1);
24
    }
25
26
    public override void update_traces() {
27
        GTop.Cpu cpu;
28
        GTop.get_cpu(out cpu);
29
30
        uint64[] newdata = new uint64[5];
31
        newdata[0] = cpu.user;
32
        newdata[1] = cpu.nice;
33
        newdata[2] = cpu.sys;
34
        newdata[3] = cpu.iowait + cpu.irq + cpu.softirq;
35
        newdata[4] = cpu.idle;
36
37
        if (this.lastdata.length == 0) {
38
            foreach (unowned IconTraceData trace in this.traces)
39
                trace.add_value(0);
40
        } else {
41
            double total = 0;
42
            for (uint i = 0, isize = newdata.length; i < isize; ++i)
43
            	total += newdata[i] - this.lastdata[i];
44
            for (uint i = 0, isize = this.traces.length; i < isize; ++i)
45
                this.traces[i].add_value((newdata[i] - this.lastdata[i]) / total);
46
        }
47
        this.lastdata = newdata;
48
    }
49
}
50