-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsacf.c
353 lines (293 loc) · 7.6 KB
/
sacf.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <glob.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#if defined(__OpenBSD__) || defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/sysctl.h>
#endif
#ifdef __OpenBSD__
#include <sys/sched.h>
#endif
#ifdef __FreeBSD__
#include <devstat.h>
#endif
#include "util.h"
/* DONT CHANGE */
#define LOCK "/var/run/sacf.lock"
/* enums */
enum { INTEL, CPUFREQ, BROKEN };
enum { Always, Never, Auto };
/* globals */
static const char *turbopath[] = {
[INTEL] = "/sys/devices/system/cpu/intel_pstate/no_turbo",
[CPUFREQ] = "/sys/devices/system/cpu/cpufreq/boost",
[BROKEN] = "", /* no turbo boost support */
};
static size_t ti = BROKEN; /* turbo index */
static unsigned int cpus = 0;
#include "config.h"
static float
avgload(void)
{
double avg;
/* get the average load over 1 minute */
if (getloadavg(&avg, 1) < 0) {
fprintf(stderr, "getloadavg: Failed to obtain load average.\n");
return 0.0;
}
return avg;
}
static unsigned int
cpuperc(void)
{
#ifdef __linux__
static long double a[7];
long double b[7], sum;
memcpy(b, a, sizeof(b));
/* cpu user nice system idle iowait irq softirq */
/* 0 1 2 3 4 5 6 */
if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
&a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6])
!= 7)
return 0;
if (b[0] == 0)
return 0;
sum = (b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
(a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]);
if (sum == 0)
return 0;
return (int)(100 *
((b[0] + b[1] + b[2] + b[5] + b[6]) -
(a[0] + a[1] + a[2] + a[5] + a[6])) / sum);
#endif /* __linux__ */
#ifdef __OpenBSD__
//TODO openbsd support
#endif /* __OpenBSD__ */
#ifdef __FreeBSD__
//TODO freebsd support
#endif
}
//TODO remove this. `sacf &`, `setsid -f sacf`, `sacf & disown`, `nohup sacf`,
//... should do the trick
static void
daemonize(void)
{
/* child process */
pid_t id = fork();
if (id < 0)
die("fork failed.");
/* parent process */
if (id > 0)
exit(0); //kill the parent
/* unmask the file mode */
umask(0);
/* new session */
if (setsid() < 0)
die("setsid failed:");
/* close stdin, stdout and stderr */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
static int
ischarging()
{ //TODO handle multiple AC online ?
glob_t buf;
int online;
eglob("/sys/class/power_supply/A*/online", &buf);
if (pscanf(buf.gl_pathv[0], "%d", &online) != 1)
return -1;
globfree(&buf);
return online;
}
static unsigned int
nproc(void)
{
unsigned int ret;
#ifdef _SC_NPROCESSORS_ONLN
/* This works on glibc, Mac OS X 10.5, FreeBSD, AIX, OSF/1, Solaris,
* Cygwin, Haiku. */
ret = sysconf(_SC_NPROCESSORS_ONLN);
#else
glob_t buf;
eglob("/sys/devices/system/cpu/cpu[0-9]*", &buf);
ret = buf.gl_pathc;
globfree(&buf);
#endif
return ret;
}
static int
getturbo(void)
{
int state;
if (ti == BROKEN) /* no turbo boost support */
return -1;
if (pscanf(turbopath[ti], "%d", &state) != 1)
return -1;
return state;
}
static void
setgovernor(const char *governor)
{
const char path[] = "/sys/devices/system/cpu/cpu";
const char end[] = "/cpufreq/scaling_governor";
unsigned int i;
char tmp[sizeof(path) + sizeof(i) + sizeof(end) + 5];
for (i = 0; i < cpus; i++) {
/* store the path of cpu i on tmp */
snprintf(tmp, sizeof(tmp), "%s%u%s", path, i, end);
pprintf(tmp, "%s\n", governor);
}
}
static unsigned int
avgtemp(void)
{
#ifdef __linux__
//TODO on some systems, there could exist multiple paths, so get an avg
//by using glob
unsigned int temp;
if (pscanf(thermal, "%u", &temp) != 1)
return 0;
return temp / 1000; /* value in celsius */
#endif /* __linux__ */
#ifdef __OpenBSD__
//TODO openbsd support
#endif /* __OpenBSD__ */
#ifdef __FreeBSD__
//TODO freebsd support
#endif
}
static void
turbo(int on)
{
int i = getturbo();
/* do nothing if the turbo state is already as desired or turbo boost
* is not supported */
if (i == -1 || i == on)
return;
/* change state of turbo boost */
pprintf(turbopath[ti], "%d\n", on);
}
static void
run(void)
{
float threshold = (75 * cpus) / 100;
int charge = ischarging();
unsigned int tb = charge ? acturbo : batturbo;
setgovernor(charge ? acgovernor : batgovernor);
turbo(tb != Never
&& (tb == Always
|| cpuperc() >= mincpu
|| avgtemp() >= mintemp
|| avgload() >= threshold));
}
static void
info(void)
{
const char first[] = "/sys/devices/system/cpu/cpu";
const char scgov[] = "/cpufreq/scaling_governor";
const char scfreq[] = "/cpufreq/scaling_cur_freq";
const char scdvr[] = "/cpufreq/scaling_driver";
unsigned int i, freq;
char path[sizeof(first) + sizeof(i) + sizeof(scgov) + 5];
char governor[16], driver[16];
cpuperc(); /* workaround to make the static variable not 0 */
fprintf(stdout, "Cores: %u\n", cpus);
fprintf(stdout, "AC adapter status: %d\n", ischarging());
fprintf(stdout, "Average system load: %0.2f\n", avgload());
fprintf(stdout, "System temperature: %u °C\n", avgtemp());
if (ti != BROKEN) {
fprintf(stdout, "Turbo state: %d\n", getturbo());
fprintf(stdout, "Turbo path: %s\n", turbopath[ti]);
} else
fprintf(stdout, "CPU turbo boost is not available.\n");
fprintf(stdout, "Average CPU usage: %u%%\n", cpuperc());
/* per cpu info */
fprintf(stdout, "Core\tGovernor\tScaling Driver\tFrequency(kHz)\n");
for (i = 0; i < cpus; i++) {
/* governor */
snprintf(path, sizeof(path), "%s%u%s", first, i, scgov);
pscanf(path, "%16s", governor);
/* current frequency */
snprintf(path, sizeof(path), "%s%u%s", first, i, scfreq);
pscanf(path, "%u", &freq);
/* driver */
snprintf(path, sizeof(path), "%s%u%s", first, i, scdvr);
pscanf(path, "%16s", driver);
fprintf(stdout, "CPU%d\t%s\t%s\t%u\n", i, governor, driver, freq);
}
}
static void
usage(void)
{
die("usage: sacf [-blrtTv] [-g governor]");
}
static void
cleanup(int unused)
{
remove(LOCK); //unlink(2) it's async-signal-safe
_exit(EXIT_SUCCESS);
}
int
main(int argc, char *argv[])
{
int i;
/* setup */
if (access(turbopath[INTEL], F_OK) != -1)
ti = INTEL; /* intel driver support */
else if (access(turbopath[CPUFREQ], F_OK) != -1)
ti = CPUFREQ; /* cpufreq driver support */
cpus = nproc(); /* store the number of cpus */
for (i = 1; i < argc; i++)
/* these options take no arguments */
if (!strcmp(argv[i], "-v") /* prints version information */
|| !strcmp(argv[i], "--version")) {
puts("sacf-"VERSION);
exit(0);
} else if (!strcmp(argv[i], "-l") /* stats about the system */
|| !strcmp(argv[i], "--list")) {
info();
exit(0);
} else if (!strcmp(argv[i], "-t") /* turbo on */
|| !strcmp(argv[i], "--enable-turbo")) {
turbo(1);
exit(0);
} else if (!strcmp(argv[i], "-T") /* turbo off */
|| !strcmp(argv[i], "--disable-turbo")) {
turbo(0);
exit(0);
} else if (!strcmp(argv[i], "-r") /* run once */
|| !strcmp(argv[i], "--run-once")) {
run();
exit(0);
} else if (!strcmp(argv[i], "-b")
|| !strcmp(argv[i], "--daemon")) { /* daemon mode */
daemonize();
} else if (i + 1 == argc) {
usage();
/* these options take one argument */
} else if (!strcmp(argv[i], "-g") /* set governor */
|| !strcmp(argv[i], "--governor")) {
setgovernor(argv[++i]);
exit(0);
} else
usage();
signal(SIGINT, cleanup);
signal(SIGTERM, cleanup);
/* hardcoded lock file */
if (access(LOCK, F_OK) != -1)
die("-> " LOCK " is present.\n-> There may be another instance of sacf already running.");
else
pprintf(LOCK, "%d", getpid());
while (1) {
run();
sleep(interval);
}
remove(LOCK);
return EXIT_SUCCESS;
}