11 #ifndef CPU_HISTORY_SIZE
12 #define CPU_HISTORY_SIZE 1
14 #if CPU_HISTORY_SIZE < 1
15 #define CPU_HISTORY_SIZE 1
19 unsigned long last_cpu_used = 0;
20 unsigned long last_cpu_total = 0;
21 float cpu_history[CPU_HISTORY_SIZE]; // don't care about init values
23 void status_cpuusage()
25 double loadavg[3] = { -13.37, -13.37, -13.37 } ;
31 unsigned long cpu_user;
32 unsigned long cpu_nice;
33 unsigned long cpu_sys;
34 unsigned long cpu_used;
35 unsigned long cpu_idle;
36 unsigned long cpu_total;
40 fputs("^fg(yellow)", stdout); // Error signaling color
42 stfile = fopen("/proc/stat", "r");
45 for(i = CPU_HISTORY_SIZE - 1; i > 0; i--)
46 cpu_history[i] = cpu_history[i - 1];
48 stlen = getline(&stline, &stlen, stfile);
51 if ( 4 == sscanf(stline, "%*s %ld %ld %ld %ld", &cpu_user, &cpu_nice, &cpu_sys, &cpu_idle) );
53 cpu_used = cpu_user + cpu_nice + cpu_sys;
54 cpu_total = cpu_used + cpu_idle;
56 // Now do the percentage
57 cpu_history[0] = (float)(cpu_used - last_cpu_used) /
58 (float)(cpu_total - last_cpu_total);
60 last_cpu_used = cpu_used;
61 last_cpu_total = cpu_total;
64 if (cpu_history[0] < 0.4)
65 fputs("^fg(#0077FF)", stdout); // CPU idling OK
67 fputs("^fg(#FF00FF)", stdout); // CPU busy
69 //printf(" CPU: %.0f%% ", cpu_history[0] * 100);
76 if (getloadavg(loadavg, 3) > 0);
77 printf(" %s%.0f,%.0f,%.0f,%.0f ",
78 cpu_history[0] < 0.1000 ? " " : "",
80 loadavg[0] * (100 / 1),
81 loadavg[1] * (100 / 1), // (100 / NUM_CPUS)
82 loadavg[2] * (100 / 1));
84 //fputs(" CPU usage ", stdout);