Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
5 #if defined(__OpenBSD__)
6 #include <sys/sysctl.h>
11 #if defined(__linux__)
17 return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
19 bprintf("%d", (freq
+ 500) / 1000) : NULL
;
26 static long double a
[7];
30 memcpy(b
, a
, sizeof(b
));
31 if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2],
32 &a
[3], &a
[4], &a
[5], &a
[6]) != 7) {
40 perc
= 100 * ((b
[0]+b
[1]+b
[2]+b
[5]+b
[6]) - (a
[0]+a
[1]+a
[2]+a
[5]+a
[6])) /
41 ((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]));
43 return bprintf("%d", perc
);
51 static long double a
[7];
54 memcpy(b
, a
, sizeof(b
));
55 if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2],
56 &a
[3], &a
[4], &a
[5], &a
[6]) != 7) {
64 perc
= 100 * ((b
[4]) - (a
[4])) /
65 ((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]));
67 return bprintf("%d", perc
);
69 #elif defined(__OpenBSD__)
81 if (sysctl(mib
, 2, &freq
, &size
, NULL
, 0) == -1) {
82 fprintf(stderr
, "sysctl 'HW_CPUSPEED': %s\n", strerror(errno
));
86 return bprintf("%d", freq
);