1 // vim:sw=8:sts=8:ts=8:expandtab
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
9 #if defined(__FreeBSD__) || defined(__OpenBSD__)
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/sysctl.h>
13 #include <sys/dkstat.h>
18 static int prev_total = 0;
19 static int prev_idle = 0;
22 * Reads the CPU utilization from /proc/stat and returns the usage as a
26 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
28 char *outwalk = buffer;
30 int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
31 int diff_idle, diff_total, diff_usage;
34 static char statpath[512];
35 strcpy(statpath, "/proc/stat");
36 if (!slurp(statpath, buf, sizeof(buf)) ||
37 sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4)
40 curr_total = curr_user + curr_nice + curr_system + curr_idle;
41 diff_idle = curr_idle - prev_idle;
42 diff_total = curr_total - prev_total;
43 diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
44 prev_total = curr_total;
45 prev_idle = curr_idle;
46 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
48 #if defined(__FreeBSD__)
50 long cp_time[CPUSTATES];
51 size = sizeof cp_time;
52 if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
55 /* This information is taken from the boot cpu, any other cpus are currently ignored. */
56 long cp_time[CPUSTATES];
58 size_t size = sizeof(cp_time);
63 if (sysctl(mib, 2, cp_time, &size, NULL, 0))
67 curr_user = cp_time[CP_USER];
68 curr_nice = cp_time[CP_NICE];
69 curr_system = cp_time[CP_SYS];
70 curr_idle = cp_time[CP_IDLE];
71 curr_total = curr_user + curr_nice + curr_system + curr_idle;
72 diff_idle = curr_idle - prev_idle;
73 diff_total = curr_total - prev_total;
74 diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
75 prev_total = curr_total;
76 prev_idle = curr_idle;
80 for (walk = format; *walk != '\0'; walk++) {
86 if (strncmp(walk+1, "usage", strlen("usage")) == 0) {
87 outwalk += sprintf(outwalk, "%02d%%", diff_usage);
88 walk += strlen("usage");
92 OUTPUT_FULL_TEXT(buffer);
95 OUTPUT_FULL_TEXT("cant read cpu usage");
96 (void)fputs("i3status: Cannot read CPU usage\n", stderr);