Xinqi Bao's Git

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