~indicator-multiload/indicator-multiload/trunk

« back to all changes in this revision

Viewing changes to src/diskicondata.vala

  • Committer: Michael Hofmann
  • Date: 2011-05-07 21:31:34 UTC
  • Revision ID: mh21@piware.de-20110507213134-81xrb5jjkkwju1h2
Fix disk usage plot by using /sys directly instead of libgtop.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
        base("diskload", 2, 10, 1000);
26
26
    }
27
27
 
 
28
    private string[] split(string val) {
 
29
        string[] result = null;
 
30
        char *last = null;
 
31
        char *current = val;
 
32
        for (; *current != '\0'; current = current + 1) {
 
33
            if (*current == ' ' || *current == '\n') {
 
34
                if (last != null) {
 
35
                    result += strndup(last, current - last);
 
36
                    last = null;
 
37
                }
 
38
            } else {
 
39
                if (last == null)
 
40
                    last = current;
 
41
            }
 
42
        }
 
43
        if (last != null)
 
44
            result += strndup(last, current - last);
 
45
        return result;
 
46
    }
 
47
 
28
48
    public override void update() {
29
49
        uint64[] newdata = new uint64[3];
30
50
        uint64 newtime = get_monotonic_time();
31
51
 
32
 
        // TODO: This does not work for LVM or virtual fs
33
 
        // may give weird results anyway if there are two partitions on the same drive?
34
 
        // on Linux, maybe copy the code from iotop?
35
 
        GTop.MountEntry[] mountentries;
36
 
        GTop.MountList mountlist;
37
 
        mountentries = GTop.get_mountlist (out mountlist, false);
38
 
 
39
 
        for (uint i = 0; i < mountlist.number; ++i) {
40
 
            GTop.FSUsage fsusage;
41
 
            GTop.get_fsusage(out fsusage, mountentries[i].mountdir);
42
 
            newdata[0] += fsusage.read;
43
 
            newdata[1] += fsusage.write;
 
52
        try {
 
53
            // Accounts for io for everything that has an associated device
 
54
            // TODO: will jump on unmount
 
55
            Dir directory = Dir.open("/sys/block");
 
56
            string entry;
 
57
            while ((entry = directory.read_name()) != null) {
 
58
                if (!FileUtils.test(@"/sys/block/$entry/device", FileTest.EXISTS))
 
59
                    continue;
 
60
                string stat;
 
61
                try {
 
62
                    FileUtils.get_contents(@"/sys/block/$entry/stat", out stat);
 
63
                } catch (Error e) {
 
64
                    continue;
 
65
                }
 
66
                string[] stats = this.split(stat);
 
67
                if (stats.length < 8)
 
68
                    continue;
 
69
                newdata[0] += 512 * uint64.parse(stats[2]);
 
70
                newdata[1] += 512 * uint64.parse(stats[6]);
 
71
            }
 
72
        } catch (Error e) {
 
73
            // Fall back to libgtop if we have no /sys
 
74
            GTop.MountEntry[] mountentries;
 
75
            GTop.MountList mountlist;
 
76
            mountentries = GTop.get_mountlist (out mountlist, false);
 
77
            for (uint i = 0; i < mountlist.number; ++i) {
 
78
                GTop.FSUsage fsusage;
 
79
                GTop.get_fsusage(out fsusage, mountentries[i].mountdir);
 
80
                newdata[0] += fsusage.block_size * fsusage.read;
 
81
                newdata[1] += fsusage.block_size * fsusage.write;
 
82
            }
44
83
        }
45
84
 
46
85
        double read = 0, write = 0;