Xinqi Bao's Git

Return actual percentage for wifi_perc()
[slstatus.git] / components / cpu.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "../util.h"
6
7 const char *
8 cpu_freq(void)
9 {
10 int freq;
11
12 return (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
13 "%i", &freq) == 1) ?
14 bprintf("%d", (freq + 500) / 1000) : NULL;
15 }
16
17 const char *
18 cpu_perc(void)
19 {
20 int perc;
21 static long double a[7];
22 static int valid;
23 long double b[7];
24
25 memcpy(b, a, sizeof(b));
26 if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
27 &a[3], &a[4], &a[5], &a[6]) != 7) {
28 return NULL;
29 }
30 if (!valid) {
31 valid = 1;
32 return NULL;
33 }
34
35 perc = 100 * ((b[0]+b[1]+b[2]+b[5]+b[6]) - (a[0]+a[1]+a[2]+a[5]+a[6])) /
36 ((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]));
37
38 return bprintf("%d", perc);
39 }
40
41 const char *
42 cpu_iowait(void)
43 {
44 int perc;
45 static int valid;
46 static long double a[7];
47 long double b[7];
48
49 memcpy(b, a, sizeof(b));
50 if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2],
51 &a[3], &a[4], &a[5], &a[6]) != 7) {
52 return NULL;
53 }
54 if (!valid) {
55 valid = 1;
56 return NULL;
57 }
58
59 perc = 100 * ((b[4]) - (a[4])) /
60 ((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]));
61
62 return bprintf("%d", perc);
63 }