]> git.sur5r.net Git - i3/i3status/blob - src/print_cpu_usage.c
ed17db343d2d5ff857d966a849d048d2b085bb2a
[i3/i3status] / src / print_cpu_usage.c
1 // vim:sw=8:sts=8:ts=8:expandtab
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
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>
14 #endif
15
16 #include "i3status.h"
17
18 static int prev_total = 0;
19 static int prev_idle  = 0;
20
21 /*
22  * Reads the CPU utilization from /proc/stat and returns the usage as a
23  * percentage.
24  *
25  */
26 void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
27         const char *walk;
28         char *outwalk = buffer;
29         char buf[1024];
30         int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
31         int diff_idle, diff_total, diff_usage;
32
33 #if defined(LINUX)
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)
38                 goto error;
39
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__)
47
48 #if defined(__FreeBSD__)
49         size_t size;
50         long cp_time[CPUSTATES];
51         size = sizeof cp_time;
52         if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0)
53                 goto error;
54 #else
55         /* This information is taken from the boot cpu, any other cpus are currently ignored. */
56         long cp_time[CPUSTATES];
57         int mib[2];
58         size_t size = sizeof(cp_time);
59
60         mib[0] = CTL_KERN;
61         mib[1] = KERN_CPTIME;
62
63         if (sysctl(mib, 2, cp_time, &size, NULL, 0))
64                 goto error;
65 #endif
66
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;
77 #else
78         goto error;
79 #endif
80         for (walk = format; *walk != '\0'; walk++) {
81                 if (*walk != '%') {
82                         *(outwalk++) = *walk;
83                         continue;
84                 }
85
86                 if (strncmp(walk+1, "usage", strlen("usage")) == 0) {
87                         outwalk += sprintf(outwalk, "%02d%%", diff_usage);
88                         walk += strlen("usage");
89                 }
90         }
91
92         OUTPUT_FULL_TEXT(buffer);
93         return;
94 error:
95         OUTPUT_FULL_TEXT("cant read cpu usage");
96         (void)fputs("i3status: Cannot read CPU usage\n", stderr);
97 }