File: sys_linux.c

package info (click to toggle)
wmbubble 1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 280 kB
  • ctags: 185
  • sloc: ansic: 2,824; makefile: 90; sh: 2
file content (131 lines) | stat: -rw-r--r-- 3,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*  BubbleMon dockapp 1.2 - Linux specific code
 *  Copyright 2000, 2001 timecop@japan.co.jp
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

#include <stdio.h>
#include "include/bubblemon.h"
#include "include/sys_include.h"

extern BubbleMonData bm;

/* returns current CPU load in percent, 0 to 100 */
int system_cpu(void)
{
    unsigned int cpuload;
    u_int64_t load, total, oload, ototal;
    u_int64_t ab, ac, ad, ae;
    int i;
    FILE *stat;

    stat = fopen("/proc/stat", "r");
    fscanf(stat, "%*s %Ld %Ld %Ld %Ld", &ab, &ac, &ad, &ae);
    fclose(stat);

    /* Find out the CPU load */
    /* user + sys = load
     * total = total */
    load = ab + ac + ad;	/* cpu.user + cpu.sys; */
    total = ab + ac + ad + ae;	/* cpu.total; */

    /* "i" is an index into a load history */
    i = bm.loadIndex;
    oload = bm.load[i];
    ototal = bm.total[i];

    bm.load[i] = load;
    bm.total[i] = total;
    bm.loadIndex = (i + 1) % bm.samples;

    /*
       Because the load returned from libgtop is a value accumulated
       over time, and not the current load, the current load percentage
       is calculated as the extra amount of work that has been performed
       since the last sample. yah, right, what the fuck does that mean?
     */
    if (ototal == 0)		/* ototal == 0 means that this is the first time
				   we get here */
	cpuload = 0;
    else
	cpuload = (100 * (load - oload)) / (total - ototal);

    return cpuload;
}

int system_memory(void)
{
    u_int64_t my_mem_used, my_mem_max;
    u_int64_t my_swap_used, my_swap_max;

    static int mem_delay = 0;
    FILE *mem;
    static u_int64_t aa, ab, ac, ad, ae, af, ag, ah;
    /* put this in permanent storage instead of stack */
    static char shit[2048];

    /* we might as well get both swap and memory at the same time.
     * sure beats opening the same file twice */
    if (mem_delay-- <= 0) {
	mem = fopen("/proc/meminfo", "r");
	fgets(shit, 2048, mem);
	
	fscanf(mem, "%*s %Ld %Ld %Ld %Ld %Ld %Ld", &aa, &ab, &ac,
	       &ad, &ae, &af);
	fscanf(mem, "%*s %Ld %Ld", &ag, &ah);
	fclose(mem);
	mem_delay = 25;

	/* calculate it */
	my_mem_max = aa;	/* memory.total; */
	my_swap_max = ag;	/* swap.total; */

	my_mem_used = ah + ab - af - ae;	/* swap.used + memory.used - memory.cached - memory.buffer; */

	if (my_mem_used > my_mem_max) {
	    my_swap_used = my_mem_used - my_mem_max;
	    my_mem_used = my_mem_max;
	} else {
	    my_swap_used = 0;
	}

	bm.mem_used = my_mem_used;
	bm.mem_max = my_mem_max;
	bm.swap_used = my_swap_used;
	bm.swap_max = my_swap_max;

	/* memory info changed - update things */
	return 1;
    }
    /* nothing new */
    return 0;
}

#ifdef ENABLE_MEMSCREEN
void system_loadavg(void)
{
    FILE *avg;
    static int avg_delay;
    if (avg_delay-- <= 0) {
	avg = fopen("/proc/loadavg", "r");
	fscanf(avg, "%d.%d %d.%d %d.%d", &bm.loadavg[0].i, &bm.loadavg[0].f,
		&bm.loadavg[1].i, &bm.loadavg[1].f,
		&bm.loadavg[2].i, &bm.loadavg[2].f);
	fclose(avg);
	avg_delay = ROLLVALUE;
    }
}
#endif				/* ENABLE_MEMSCREEN */