1 // vim:ts=4:sw=4:expandtab
4 #include <sys/sysinfo.h>
10 #include <yajl/yajl_gen.h>
11 #include <yajl/yajl_version.h>
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>
20 #include <sys/dkstat.h>
24 #if defined(__DragonFly__)
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <sys/sysctl.h>
28 #include <sys/resource.h>
31 #if defined(__NetBSD__)
32 #include <sys/param.h>
33 #include <sys/resource.h>
34 #include <sys/sysctl.h>
35 #include <sys/sched.h>
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;
54 * Reads the CPU utilization from /proc/stat and returns the usage as a
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;
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;
66 #if defined(__linux__)
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;
73 prev_cpus = (struct cpu_usage *)calloc(cpu_count, sizeof(struct cpu_usage));
75 curr_cpus = (struct cpu_usage *)calloc(cpu_count, sizeof(struct cpu_usage));
78 memcpy(curr_cpus, prev_cpus, cpu_count * sizeof(struct cpu_usage));
80 curr_cpu_count = get_nprocs();
81 if (!slurp(path, buf, sizeof(buf)))
83 // Parsing all cpu values using strtok
84 if (strtok(buf, "\n") == 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) {
93 if (cpu_idx < 0 || cpu_idx >= cpu_count)
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;
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;
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);
113 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
115 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
117 long cp_time[CPUSTATES];
118 size = sizeof cp_time;
119 if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
122 /* This information is taken from the boot cpu, any other cpus are currently ignored. */
123 long cp_time[CPUSTATES];
125 size_t size = sizeof(cp_time);
128 mib[1] = KERN_CPTIME;
130 if (sysctl(mib, 2, cp_time, &size, NULL, 0))
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);
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;
159 for (walk = selected_format; *walk != '\0'; walk++) {
161 *(outwalk++) = *walk;
163 } else if (BEGINS_WITH(walk + 1, "usage")) {
164 outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
165 walk += strlen("usage");
167 #if defined(__linux__)
168 else if (BEGINS_WITH(walk + 1, "cpu")) {
170 sscanf(walk + 1, "cpu%d", &number);
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);
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);
183 while (step <= number) {
187 walk += strlen("cpu") + padding;
195 struct cpu_usage *temp_cpus = prev_cpus;
196 prev_cpus = curr_cpus;
197 curr_cpus = temp_cpus;
202 OUTPUT_FULL_TEXT(buffer);
205 OUTPUT_FULL_TEXT("cant read cpu usage");
206 (void)fputs("i3status: Cannot read CPU usage\n", stderr);