-
Notifications
You must be signed in to change notification settings - Fork 2
/
load.c
167 lines (139 loc) · 4.16 KB
/
load.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
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
/*
* Shows CPU load and some other information.
* Copyright (c) 2005,2007 by Michal Nazareicz <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* This is part of Tiny Applications Collection
* -> http://tinyapps.sourceforge.net/
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**** Defines ****/
#define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE];
#ifdef ULLONG_MAX
typedef unsigned long long bigint;
#define FMT "llu"
#else
typedef unsigned long bigint;
#define FMT "lu"
#endif
/**** Structire with all information ****/
typedef struct {
struct {
bigint usr, nice, sys, idle, load, total;
} cpu;
struct {
bigint rec, send;
} traf;
struct {
unsigned long total, free;
} mem;
} State;
/**** Updates CPU load info ****/
static void update_cpu(State *state) {
FILE *file = fopen("/proc/stat", "r");
int i;
if (!file) return;
i = fscanf(file, "%*s %" FMT " %" FMT " %" FMT " %" FMT ,
&state->cpu.usr, &state->cpu.nice,
&state->cpu.sys, &state->cpu.idle);
fclose(file);
switch (i) {
case 0:
state->cpu.usr = 0;
case 1:
state->cpu.nice = 0;
case 2:
state->cpu.sys = 0;
case 3:
state->cpu.idle = 0;
}
state->cpu.load = state->cpu.usr + state->cpu.sys + state->cpu.nice;
state->cpu.total = state->cpu.load + state->cpu.idle;
}
/**** Updates traffic info ****/
static void update_traf(State *state) {
FILE *file = fopen("/proc/net/dev", "r");
if (!file) return;
while (fgets(buffer, BUFFER_SIZE, file)) {
if (sscanf(buffer, " eth0:%" FMT " %*d %*d %*d %*d %*d %*d %*d %" FMT,
&state->traf.rec, &state->traf.send)) {
break;
}
}
fclose(file);
}
/**** Updates memory info ****/
static void update_mem(State *state) {
FILE *file = fopen("/proc/meminfo", "r");
unsigned long val;
int flags = 0;
if (!file) return;
while (fgets(buffer, BUFFER_SIZE, file) && flags!=3) {
if (sscanf(buffer, " MemTotal: %lu", &val)==1) {;
flags |= 1;
state->mem.total = val;
} else if (sscanf(buffer, " MemFree: %lu", &val)==1) {;
flags |= 2;
state->mem.free = val;
}
}
fclose(file);
}
/**** Calculates delta between two states ****/
static void make_delta(State *delta, const State *prev, const State *now) {
delta->cpu.usr = now->cpu.usr - prev->cpu.usr ;
delta->cpu.nice = now->cpu.nice - prev->cpu.nice ;
delta->cpu.sys = now->cpu.sys - prev->cpu.sys ;
delta->cpu.idle = now->cpu.idle - prev->cpu.idle ;
delta->cpu.load = now->cpu.load - prev->cpu.load ;
delta->cpu.total = now->cpu.total - prev->cpu.total;
delta->traf.rec = now->traf.rec - prev->traf.rec ;
delta->traf.send = now->traf.send - prev->traf.send;
delta->mem.total = now->mem.total;
delta->mem.free = now->mem.free ;
}
/**** Prints state ****/
static void print_state(const State *s) {
printf("%4" FMT " %4" FMT " %4" FMT " %4" FMT " %4" FMT " %4" FMT
" %3d%% %7ld %7ld %7" FMT " %7" FMT "\n",
s->cpu.usr, s->cpu.nice, s->cpu.sys, s->cpu.idle,
s->cpu.load, s->cpu.total,
(int)(s->cpu.total ? 100 * s->cpu.load / s->cpu.total : 0),
s->mem.free, s->mem.total,
s->traf.rec, s->traf.send);
}
/**** Main ****/
int main(void) {
State states[3];
int num = 0;
update_cpu(states + 1);
update_traf(states + 1);
update_mem(states + 1);
puts("user nice sys idle | load ttl | cpu | memfree mem-ttl "
"| rec send");
for(;;) {
sleep(1);
update_cpu(states + num);
update_traf(states + num);
update_mem(states + num);
make_delta(states + 2, states + (num^1), states + num);
print_state(states + 2);
num ^= 1;
}
}