1 // vim:ts=4:sw=4: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 #if defined(__OpenBSD__)
14 #include <sys/sched.h>
16 #include <sys/dkstat.h>
20 #if defined(__DragonFly__)
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
24 #include <sys/resource.h>
27 #if defined(__NetBSD__)
28 #include <sys/param.h>
29 #include <sys/resource.h>
30 #include <sys/sysctl.h>
31 #include <sys/sched.h>
36 static int prev_total = 0;
37 static int prev_idle = 0;
40 * Reads the CPU utilization from /proc/stat and returns the usage as a
44 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold, const float degraded_threshold) {
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;
52 static char statpath[512];
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)
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__)
67 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
69 long cp_time[CPUSTATES];
70 size = sizeof cp_time;
71 if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
74 /* This information is taken from the boot cpu, any other cpus are currently ignored. */
75 long cp_time[CPUSTATES];
77 size_t size = sizeof(cp_time);
82 if (sysctl(mib, 2, cp_time, &size, NULL, 0))
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;
99 for (walk = format; *walk != '\0'; walk++) {
101 *(outwalk++) = *walk;
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;
113 if (BEGINS_WITH(walk + 1, "usage")) {
114 outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
115 walk += strlen("usage");
122 OUTPUT_FULL_TEXT(buffer);
125 OUTPUT_FULL_TEXT("cant read cpu usage");
126 (void)fputs("i3status: Cannot read CPU usage\n", stderr);