12 #ifndef CPU_HISTORY_SIZE
13 #define CPU_HISTORY_SIZE 1
15 #if CPU_HISTORY_SIZE < 1
16 #define CPU_HISTORY_SIZE 1
20 unsigned long last_cpu_used = 0;
21 unsigned long last_cpu_total = 0;
22 float cpu_history[CPU_HISTORY_SIZE]; // don't care about init values
24 void status_cpuusage(GlobalData *g)
27 char text[32] = { 0 };
29 double loadavg[3] = { -13.37, -13.37, -13.37 } ;
35 unsigned long cpu_user;
36 unsigned long cpu_nice;
37 unsigned long cpu_sys;
38 unsigned long cpu_used;
39 unsigned long cpu_idle;
40 unsigned long cpu_total;
47 // Error signaling color
48 s.color = "#FFFF00"; // yellow
50 stfile = fopen("/proc/stat", "r");
52 for(i = CPU_HISTORY_SIZE - 1; i > 0; i--) {
53 cpu_history[i] = cpu_history[i - 1];
56 stlen = getline(&stline, &stlen, stfile);
59 if ( 4 == sscanf(stline, "%*s %ld %ld %ld %ld",
60 &cpu_user, &cpu_nice, &cpu_sys, &cpu_idle) ) {
61 cpu_used = cpu_user + cpu_nice + cpu_sys;
62 cpu_total = cpu_used + cpu_idle;
64 // Now do the percentage
65 cpu_history[0] = (float)(cpu_used - last_cpu_used) /
66 (float)(cpu_total - last_cpu_total);
68 last_cpu_used = cpu_used;
69 last_cpu_total = cpu_total;
72 if (cpu_history[0] < 0.4) {
85 if (getloadavg(loadavg, 3) > 0) {
86 snprintf(text, sizeof(text),
87 "%s%.0f,%.0f,%.0f,%.0f",
88 cpu_history[0] < 0.1000 ? " " : "",
90 loadavg[0] * (100 / 1),
91 loadavg[1] * (100 / 1), // (100 / NUM_CPUS)
92 loadavg[2] * (100 / 1));
95 line_append_item(g, &s);