File: fireload_cpu.c

package info (click to toggle)
wmfire 0.0.3.8-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 308 kB
  • ctags: 90
  • sloc: ansic: 752; sh: 354; makefile: 50
file content (93 lines) | stat: -rw-r--r-- 2,307 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
/* fireload_cpu .. accessory to wmfire - Zinx Verituse */
/* e-mail @ zinx@linuxfreak.com */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

#ifdef __NetBSD__
/*********************************************************************************
 * This NetBSD load code is by Hubert Feyrer, not me.  Thank him, NetBSD fans ;) *
 *********************************************************************************/
int getload(int cpunum, int shownice) {
       double load;
       getloadavg(&load, 1);
       return (int)(load * 100);
}
#else /* use linux code */
int getload(int cpunum, int shownice) {
	static int lastloadstuff = 0, lastidle = 0, lastnice = 0;
	int loadline = 0, loadstuff = 0, nice = 0, idle = 0;
	FILE *stat = fopen("/proc/stat", "r");
	char temp[128], *p;

	if (!stat) return 100;

	while (fgets(temp, 128, stat)) {
		if (!strncmp(temp, "cpu", 3) && (temp[3] == ' ' && cpunum == 0) || (temp[3] != ' ' && atol(&temp[3]) == cpunum)) {
			p = strtok(temp, " \t\n");
			loadstuff = atol(p = strtok(NULL, " \t\n"));
			nice = atol(p = strtok(NULL, " \t\n"));
			loadstuff += atol(p = strtok(NULL, " \t\n"));
			idle = atol(p = strtok(NULL, " \t\n"));
			break;
		}
	}

	fclose(stat);

	if (!lastloadstuff && !lastidle && !lastnice) {
		lastloadstuff = loadstuff; lastidle = idle; lastnice = nice;
		return 0;
	}

	if (shownice) {
		loadline = (loadstuff-lastloadstuff)+(idle-lastidle);
	} else {
		loadline = (loadstuff-lastloadstuff)+(nice-lastnice)+(idle-lastidle);
	}
	if (loadline)
		loadline = ((loadstuff-lastloadstuff)*100)/loadline;
	else loadline = 100;

	lastloadstuff = loadstuff; lastidle = idle; lastnice = nice;

	return loadline;
}
#endif

void do_help(char *prgname) {
	printf("%s [-c<cpunum>] [-n] [-h]\n", prgname);
}

int main(int argc, char *argv[]) {
	int ch, cpunum = 0, shownice = 1;

	while ((ch = getopt(argc, argv, "c:hn")) != EOF) {
		switch (ch) {
			case 'c':
				cpunum = atoi(optarg);
				break;
			case 'n':
				shownice = 0;
				break;
			default: /* h is handled here */
				do_help(argv[0]);
				exit(0);
		}
	}

	printf("fireload\t0\t100\n"); fflush(stdout);

	for (;;) {
		ch = getload(cpunum, shownice);
		printf("%d\t%d\n", ch, ch); fflush(stdout);
		usleep(100000);
	}

	exit(0);
}