]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_usage.c
Merge pull request #139 from Gjum/wb-colored-cpu-usage
[i3/i3status] / src / print_cpu_usage.c
1 // vim:ts=4:sw=4:expandtab
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 #if defined(__FreeBSD__) || defined(__OpenBSD__)
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/sysctl.h>
13 #if defined(__OpenBSD__)
14 #include <sys/sched.h>
15 #else
16 #include <sys/dkstat.h>
17 #endif
18 #endif
19
20 #if defined(__DragonFly__)
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
24 #include <sys/resource.h>
25 #endif
26
27 #if defined(__NetBSD__)
28 #include <sys/param.h>
29 #include <sys/resource.h>
30 #include <sys/sysctl.h>
31 #include <sys/sched.h>
32 #endif
33
34 #include "i3status.h"
35
36 static int prev_total = 0;
37 static int prev_idle = 0;
38
39 /*
40  * Reads the CPU utilization from /proc/stat and returns the usage as a
41  * percentage.
42  *
43  */
44 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold, const float degraded_threshold) {
45     const char *walk;
46     char *outwalk = buffer;
47     int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
48     int diff_idle, diff_total, diff_usage;
49     bool colorful_output = false;
50
51 #if defined(LINUX)
52     static char statpath[512];
53     char buf[1024];
54     strcpy(statpath, "/proc/stat");
55     if (!slurp(statpath, buf, sizeof(buf)) ||
56         sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4)
57         goto error;
58
59     curr_total = curr_user + curr_nice + curr_system + curr_idle;
60     diff_idle = curr_idle - prev_idle;
61     diff_total = curr_total - prev_total;
62     diff_usage = (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 : 0);
63     prev_total = curr_total;
64     prev_idle = curr_idle;
65 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
66
67 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
68     size_t size;
69     long cp_time[CPUSTATES];
70     size = sizeof cp_time;
71     if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
72         goto error;
73 #else
74     /* This information is taken from the boot cpu, any other cpus are currently ignored. */
75     long cp_time[CPUSTATES];
76     int mib[2];
77     size_t size = sizeof(cp_time);
78
79     mib[0] = CTL_KERN;
80     mib[1] = KERN_CPTIME;
81
82     if (sysctl(mib, 2, cp_time, &size, NULL, 0))
83         goto error;
84 #endif
85
86     curr_user = cp_time[CP_USER];
87     curr_nice = cp_time[CP_NICE];
88     curr_system = cp_time[CP_SYS];
89     curr_idle = cp_time[CP_IDLE];
90     curr_total = curr_user + curr_nice + curr_system + curr_idle;
91     diff_idle = curr_idle - prev_idle;
92     diff_total = curr_total - prev_total;
93     diff_usage = (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 : 0);
94     prev_total = curr_total;
95     prev_idle = curr_idle;
96 #else
97     goto error;
98 #endif
99     for (walk = format; *walk != '\0'; walk++) {
100         if (*walk != '%') {
101             *(outwalk++) = *walk;
102             continue;
103         }
104
105         if (diff_usage >= max_threshold) {
106             START_COLOR("color_bad");
107             colorful_output = true;
108         } else if (diff_usage >= degraded_threshold) {
109             START_COLOR("color_degraded");
110             colorful_output = true;
111         }
112
113         if (BEGINS_WITH(walk + 1, "usage")) {
114             outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
115             walk += strlen("usage");
116         }
117
118         if (colorful_output)
119             END_COLOR;
120     }
121
122     OUTPUT_FULL_TEXT(buffer);
123     return;
124 error:
125     OUTPUT_FULL_TEXT("cant read cpu usage");
126     (void)fputs("i3status: Cannot read CPU usage\n", stderr);
127 }