Xinqi Bao's Git

Implement scaling for cpu_freq
[slstatus.git] / components / 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 #if defined(__linux__)
9 #include <inttypes.h>
10 #include <stdint.h>
11
12 const char *
13 cpu_freq(void)
14 {
15 uint64_t freq;
16
17 /* in kHz */
18 if (pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
19 "%"SCNu64, &freq) != 1) {
20 return NULL;
21 }
22
23 return fmt_human_10(freq * 1000, "Hz");
24 }
25
26 const char *
27 cpu_perc(void)
28 {
29 static int valid;
30 static long double a[7];
31 long double b[7];
32
33 memcpy(b, a, sizeof(b));
34 /* cpu user nice system idle iowait irq softirq */
35 if (pscanf("/proc/stat", "%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
36 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6]) != 7) {
37 return NULL;
38 }
39 if (!valid) {
40 valid = 1;
41 return NULL;
42 }
43
44 return bprintf("%d%%", (int)(100 *
45 ((b[0] + b[1] + b[2] + b[5] + b[6]) -
46 (a[0] + a[1] + a[2] + a[5] + a[6])) /
47 ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + b[6]) -
48 (a[0] + a[1] + a[2] + a[3] + a[4] + a[5] + a[6]))));
49 }
50 #elif defined(__OpenBSD__)
51 #include <sys/param.h>
52 #include <sys/sched.h>
53 #include <sys/sysctl.h>
54
55 const char *
56 cpu_freq(void)
57 {
58 int freq, mib[2];
59 size_t size;
60
61 mib[0] = CTL_HW;
62 mib[1] = HW_CPUSPEED;
63
64 size = sizeof(freq);
65
66 /* in MHz */
67 if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
68 warn("sysctl 'HW_CPUSPEED':");
69 return NULL;
70 }
71
72 return fmt_human_10((size_t)freq * 1000 * 1000, "Hz");
73 }
74
75 const char *
76 cpu_perc(void)
77 {
78 int mib[2];
79 static int valid;
80 static long int a[CPUSTATES];
81 long int b[CPUSTATES];
82 size_t size;
83
84 mib[0] = CTL_KERN;
85 mib[1] = KERN_CPTIME;
86
87 size = sizeof(a);
88
89 memcpy(b, a, sizeof(b));
90 if (sysctl(mib, 2, &a, &size, NULL, 0) < 0) {
91 warn("sysctl 'KERN_CPTIME':");
92 return NULL;
93 }
94 if (!valid) {
95 valid = 1;
96 return NULL;
97 }
98
99 return bprintf("%d%%", 100 *
100 ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR]) -
101 (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR])) /
102 ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + a[CP_INTR] +
103 a[CP_IDLE]) -
104 (b[CP_USER] + b[CP_NICE] + b[CP_SYS] + b[CP_INTR] +
105 b[CP_IDLE])));
106 }
107 #endif