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