-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcpuload.c
108 lines (88 loc) · 2.39 KB
/
cpuload.c
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
/* cpuload - a demo program for feeding data into dcled's tachometer mode.
* Copyright 2014 Jeff Jahr <[email protected]> */
/* This is free software. G'head, use it all you want. This program reads the
* first line out of Linux's /proc/stat file to get stats on the different cpu
* utilization states. It tallys up the data to come up with a CPU load-
* percentage of time spent out of idle mode. It prints the percentage as an
* integer, one sample per line. This program probably won't work on anything
* but Linux, and may not even parse all /proc/stat formats correctly. Its
* meant to be a demo of what dcled's tach mode can be used for.
* Sat Feb 8 18:04:06 PST 2014 -jsj */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float cpuload(int user, int nice, int sys, int idle) {
float t;
t=(user+nice+sys);
return( 100*t/(t+idle) );
}
int main(int argc, char **argv) {
FILE *in;
char filename[] = "/proc/stat";
unsigned long int stat[2][4];
char *tag=NULL;
int count;
int phase=0;
int i;
float load=0;
struct timespec snooze;
char line[8192];
float defperiod = 1.0;
float period;
period = defperiod;
if(argc == 2) {
period = atof(argv[1]);
if(period <= 0) {
fprintf(stderr,"%s takes one argument, the polling frequency in seconds.\n",argv[0]);
fprintf(stderr,"Default is %f\n",defperiod);
exit(EXIT_FAILURE);
}
}
snooze.tv_sec=(int)period;
snooze.tv_nsec=(1000000000*(period-(int)period));
/* init */
for(i=0;i<4;i++) {
stat[0][i]=0;
stat[1][i]=0;
}
/* main loop */
while (1) {
if(!(in=fopen(filename,"r"))) {
perror("Couldn't open stats file.");
exit(EXIT_FAILURE);
}
tag=NULL;
if(!fgets(line,8192,in)) {
continue;
}
count = sscanf(line,"%ms %lu %lu %lu %lu",
&tag,
&(stat[phase][0]),
&(stat[phase][1]),
&(stat[phase][2]),
&(stat[phase][3])
);
if(count!=5) {
fprintf(stderr,"Didn't read the right number of values, got %d out of\n%s\n",
count,line);
exit(EXIT_FAILURE);
}
if(!strcmp(tag,"cpu ")) {
fprintf(stderr,"Didn't read the right tag...\n");
exit(EXIT_FAILURE);
} else {
free(tag);
}
load = cpuload(
stat[phase][0] - stat[phase^1][0],
stat[phase][1] - stat[phase^1][1],
stat[phase][2] - stat[phase^1][2],
stat[phase][3] - stat[phase^1][3]
);
fprintf(stdout,"%d\n",(int)load);
fflush(stdout);
fclose(in);
nanosleep(&snooze,NULL);
phase = phase^1;
}
}