File: sys_linux.c

package info (click to toggle)
bfm 0.6.4-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 412 kB
  • sloc: ansic: 5,239; makefile: 155
file content (321 lines) | stat: -rw-r--r-- 7,414 bytes parent folder | download | duplicates (5)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*  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.
 *
 */
/* Huw Giddens 2003-10-27: Changed system_memory() so that it reports memory
 *     correctly on Linux 2.6 systems. Also works on 2.4 systems without
 *     recompile. Based on a patch by Marcelo E Magallon <mmagallo@debian.org>,
 *     see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=175140
 */

#include <stdio.h>
#include <string.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 if ((total - ototal) <= 0)
	cpuload = 100;
    else
	cpuload = (100 * (load - oload)) / (total - ototal);

    return cpuload;
}

int system_memory(void)
{
	u_int64_t mem_used, mem_max, swap_used, swap_max;
	u_int64_t value, mem_cache, mem_buffers, swap_cache;
	static int delay = 0;
	FILE *mem;
	char name[256];
	char line[256];

	if (delay-- <= 0) {
		mem = fopen("/proc/meminfo", "r"); 
		if (mem == NULL) return 0;
		while (!feof(mem)) {
			if (fgets(line, 256, mem) == NULL) break;
			if (sscanf(line, "%s %Ld", name, &value) != 2) continue;

			/* Before I calculate mem_used, I store MemFree in the mem_used
			 * variable. The same with swap_used/SwapFree */
			if (strcmp(name, "MemTotal:") == 0) mem_max = value;
		    else if (strcmp(name, "Cached:") == 0) mem_cache = value;
		    else if (strcmp(name, "Buffers:") == 0) mem_buffers = value;
		    else if (strcmp(name, "MemFree:") == 0) mem_used = value;
		    else if (strcmp(name, "SwapTotal:") == 0) swap_max = value;
		    else if (strcmp(name, "SwapFree:") == 0) swap_used = value;
		    else if (strcmp(name, "SwapCached:") == 0) swap_cache = value;
		}
		fclose(mem);
		
		mem_used = mem_max - ( mem_used + mem_cache + mem_buffers );

		if( mem_used > mem_max ) mem_used = mem_max;

		swap_used = swap_max - swap_used;

		/* proc reports usage in kb, bm wants it in bytes. */
		bm.mem_used  = 1024 * mem_used;
		bm.mem_max   = 1024 * mem_max;
		bm.swap_used = 1024 * swap_used;
		bm.swap_max  = 1024 * swap_max;

		return 1; /* update */
	}
	return 0; /* nothing changed, don't update */
}

#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 */


#ifdef ENABLE_FISH
#define FISH_MAX_SPEED	8
#define DIFF_MIN 10

/* The actual speed for the fish... */
int tx_speed;
int rx_speed;

u_int64_t tx_amount;
u_int64_t rx_amount;

/* Store the last one to compare */
u_int64_t last_tx_amount;
u_int64_t last_rx_amount;

/* Store the max for scaling, too. */
u_int64_t max_tx_diff = DIFF_MIN;
u_int64_t max_rx_diff = DIFF_MIN;


/* The cnt for scaling */
int tx_cnt;
int rx_cnt;
int delay;

extern int fish_traffic;
extern char *network_interface;
void get_traffic(void);

int net_tx_speed(void)
{
	get_traffic();
	return tx_speed;
}


int net_rx_speed(void)
{
	get_traffic();
	return rx_speed;
}


void get_traffic(void)
{
	FILE *dev;
	char buffer[256];
	u_int64_t diff;

	/* Have some delay in updating/sampling traffic... */
	if(delay++ < 5)
	{
		return;
	}
	else
	{
		delay = 0;
	}

	if((dev = fopen("/proc/net/dev", "r")) == NULL)
	{
		/* Sanity check */
		fish_traffic = 0;
	}
	else
	{
		/* Don't care about the first 2 lines */
		fgets(buffer, 256, dev);
		fgets(buffer, 256, dev);

		while(fgets(buffer, 256, dev))
		{
			char name[256];
			
			/* I love sscanf! :) */
			sscanf(buffer, "%*[ ]%[^:]:%*d %Ld %*d %*d %*d %*d %*d %*d %*d %Ld %*d %*d %*d %*d %*d %*d", name, &rx_amount, &tx_amount);

			if(!strcmp(name, network_interface))
			{
				/* Incoming traffic */
				if(rx_amount != last_rx_amount)
				{
					if(last_rx_amount == 0)
					{
						last_rx_amount = rx_amount;
					}

					diff = rx_amount - last_rx_amount;
					last_rx_amount = rx_amount;
					rx_speed = FISH_MAX_SPEED * diff / max_rx_diff;
					if(rx_speed == 0)
					{
						/* At least, make it move a bit, cos we know there's
						   traffic */
						rx_speed = 1;
					}

					/* Do something to max rate, to do proper (hopefully)
					   scaling */
					if(max_rx_diff < diff)
					{
						max_rx_diff = diff;
					}
					else
					{
						/* Slowly lower the scale */
						if(++rx_cnt > 5)
						{
							max_rx_diff = diff;
							if(max_rx_diff < DIFF_MIN)
							{
								/* And don't scale it too low */
								max_rx_diff = DIFF_MIN;
							}
							rx_cnt = 0;
						}
					}
				}
				else
				{
					rx_speed = 0;
				}


				/* Outgoing traffic */
				if(tx_amount != last_tx_amount)
				{
					if(last_tx_amount == 0)
					{
						last_tx_amount = tx_amount;
					}
					diff = tx_amount - last_tx_amount;
					last_tx_amount = tx_amount;
					tx_speed= FISH_MAX_SPEED * diff / max_tx_diff;
					if(tx_speed == 0)
					{
						/* At least, make it move a bit, cos we know there's
						   traffic */
						tx_speed = 1;
					}
					
					/* Do something to max rate, to do proper (hopefully)
					   scaling */
					if(max_tx_diff < diff)
					{
						max_tx_diff = diff;
					}
					else
					{
						/* Slowly lower the scale */
						if(++tx_cnt > 5)
						{
							max_tx_diff = diff;
							if(max_tx_diff < DIFF_MIN)
							{
								/* And don't scale it too low */
								max_tx_diff = DIFF_MIN;
							}
							tx_cnt = 0;
						}
					}
				}
				else
				{
					tx_speed = 0;
				}

			}

		}

	}
	fclose(dev);

}





#endif