]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_usage.c
3c3c0d54b8b372abc80dbb15d51ad750bf86c890
[i3/i3status] / src / print_cpu_usage.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include <sys/sysinfo.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <yajl/yajl_gen.h>
9 #include <yajl/yajl_version.h>
10
11 #if defined(__FreeBSD__) || defined(__OpenBSD__)
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
15 #if defined(__OpenBSD__)
16 #include <sys/sched.h>
17 #else
18 #include <sys/dkstat.h>
19 #endif
20 #endif
21
22 #if defined(__DragonFly__)
23 #include <sys/param.h>
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
26 #include <sys/resource.h>
27 #endif
28
29 #if defined(__NetBSD__)
30 #include <sys/param.h>
31 #include <sys/resource.h>
32 #include <sys/sysctl.h>
33 #include <sys/sched.h>
34 #endif
35
36 #include "i3status.h"
37
38 struct cpu_usage {
39     int user;
40     int nice;
41     int system;
42     int idle;
43     int total;
44 };
45
46 static int cpu_count = 0;
47 static struct cpu_usage prev_all = {0, 0, 0, 0, 0};
48 static struct cpu_usage *prev_cpus = NULL;
49 static struct cpu_usage *curr_cpus = NULL;
50
51 /*
52  * Reads the CPU utilization from /proc/stat and returns the usage as a
53  * percentage.
54  *
55  */
56 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const char *format_above_degraded_threshold, const char *path, const float max_threshold, const float degraded_threshold) {
57     const char *selected_format = format;
58     const char *walk;
59     char *outwalk = buffer;
60     struct cpu_usage curr_all = {0, 0, 0, 0, 0};
61     int diff_idle, diff_total, diff_usage;
62     bool colorful_output = false;
63
64 #if defined(__linux__)
65
66     // Detecting if CPU count has changed
67     int curr_cpu_count = get_nprocs_conf();
68     if (curr_cpu_count != cpu_count) {
69         cpu_count = curr_cpu_count;
70         free(prev_cpus);
71         prev_cpus = (struct cpu_usage *)calloc(cpu_count, sizeof(struct cpu_usage));
72         free(curr_cpus);
73         curr_cpus = (struct cpu_usage *)calloc(cpu_count, sizeof(struct cpu_usage));
74     }
75
76     memcpy(curr_cpus, prev_cpus, cpu_count * sizeof(struct cpu_usage));
77     char buf[4096];
78     curr_cpu_count = get_nprocs();
79     if (!slurp(path, buf, sizeof(buf)))
80         goto error;
81     // Parsing all cpu values using strtok
82     if (strtok(buf, "\n") == NULL)
83         goto error;
84     char *buf_itr = NULL;
85     for (int idx = 0; idx < curr_cpu_count; ++idx) {
86         buf_itr = strtok(NULL, "\n");
87         int cpu_idx, user, nice, system, idle;
88         if (!buf_itr || sscanf(buf_itr, "cpu%d %d %d %d %d", &cpu_idx, &user, &nice, &system, &idle) != 5) {
89             goto error;
90         }
91         if (cpu_idx < 0 || cpu_idx >= cpu_count)
92             goto error;
93         curr_cpus[cpu_idx].user = user;
94         curr_cpus[cpu_idx].nice = nice;
95         curr_cpus[cpu_idx].system = system;
96         curr_cpus[cpu_idx].idle = idle;
97         curr_cpus[cpu_idx].total = user + nice + system + idle;
98     }
99     for (int cpu_idx = 0; cpu_idx < cpu_count; cpu_idx++) {
100         curr_all.user += curr_cpus[cpu_idx].user;
101         curr_all.nice += curr_cpus[cpu_idx].nice;
102         curr_all.system += curr_cpus[cpu_idx].system;
103         curr_all.idle += curr_cpus[cpu_idx].idle;
104         curr_all.total += curr_cpus[cpu_idx].total;
105     }
106
107     diff_idle = curr_all.idle - prev_all.idle;
108     diff_total = curr_all.total - prev_all.total;
109     diff_usage = (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 : 0);
110     prev_all = curr_all;
111 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
112
113 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
114     size_t size;
115     long cp_time[CPUSTATES];
116     size = sizeof cp_time;
117     if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
118         goto error;
119 #else
120     /* This information is taken from the boot cpu, any other cpus are currently ignored. */
121     long cp_time[CPUSTATES];
122     int mib[2];
123     size_t size = sizeof(cp_time);
124
125     mib[0] = CTL_KERN;
126     mib[1] = KERN_CPTIME;
127
128     if (sysctl(mib, 2, cp_time, &size, NULL, 0))
129         goto error;
130 #endif
131
132     curr_all.user = cp_time[CP_USER];
133     curr_all.nice = cp_time[CP_NICE];
134     curr_all.system = cp_time[CP_SYS];
135     curr_all.idle = cp_time[CP_IDLE];
136     curr_all.total = curr_all.user + curr_all.nice + curr_all.system + curr_all.idle;
137     diff_idle = curr_all.idle - prev_all.idle;
138     diff_total = curr_all.total - prev_all.total;
139     diff_usage = (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 : 0);
140     prev_all = curr_all;
141 #else
142     goto error;
143 #endif
144
145     if (diff_usage >= max_threshold) {
146         START_COLOR("color_bad");
147         colorful_output = true;
148         if (format_above_threshold != NULL)
149             selected_format = format_above_threshold;
150     } else if (diff_usage >= degraded_threshold) {
151         START_COLOR("color_degraded");
152         colorful_output = true;
153         if (format_above_degraded_threshold != NULL)
154             selected_format = format_above_degraded_threshold;
155     }
156
157     for (walk = selected_format; *walk != '\0'; walk++) {
158         if (*walk != '%') {
159             *(outwalk++) = *walk;
160
161         } else if (BEGINS_WITH(walk + 1, "usage")) {
162             outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
163             walk += strlen("usage");
164         }
165 #if defined(__linux__)
166         else if (BEGINS_WITH(walk + 1, "cpu")) {
167             int number = -1;
168             sscanf(walk + 1, "cpu%d", &number);
169             if (number == -1) {
170                 fprintf(stderr, "i3status: provided CPU number cannot be parsed\n");
171             } else if (number >= cpu_count) {
172                 fprintf(stderr, "i3status: provided CPU number '%d' above detected number of CPU %d\n", number, cpu_count);
173             } else {
174                 int cpu_diff_idle = curr_cpus[number].idle - prev_cpus[number].idle;
175                 int cpu_diff_total = curr_cpus[number].total - prev_cpus[number].total;
176                 int cpu_diff_usage = (cpu_diff_total ? (1000 * (cpu_diff_total - cpu_diff_idle) / cpu_diff_total + 5) / 10 : 0);
177                 outwalk += sprintf(outwalk, "%02d%s", cpu_diff_usage, pct_mark);
178             }
179             int padding = 1;
180             int step = 10;
181             while (step <= number) {
182                 step *= 10;
183                 padding++;
184             }
185             walk += strlen("cpu") + padding;
186         }
187 #endif
188         else {
189             *(outwalk++) = '%';
190         }
191     }
192
193     struct cpu_usage *temp_cpus = prev_cpus;
194     prev_cpus = curr_cpus;
195     curr_cpus = temp_cpus;
196
197     if (colorful_output)
198         END_COLOR;
199
200     OUTPUT_FULL_TEXT(buffer);
201     return;
202 error:
203     OUTPUT_FULL_TEXT("cant read cpu usage");
204     (void)fputs("i3status: Cannot read CPU usage\n", stderr);
205 }