Ye Olde Brick, hacked one very late night in 2010.
[sysstatus.git] / statuses / cpuusage.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include "cpuusage.h"
6 #include "../config.h"
7
8 #ifndef NUM_CPUS
9         #define NUM_CPUS 1
10 #endif
11 #ifndef CPU_HISTORY_SIZE
12         #define CPU_HISTORY_SIZE 1
13 #endif
14 #if CPU_HISTORY_SIZE < 1
15         #define CPU_HISTORY_SIZE 1
16 #endif
17
18
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
22
23 void status_cpuusage()
24 {
25         double loadavg[3] = { -13.37, -13.37, -13.37 } ;
26         
27         char *stline = NULL;
28         size_t stlen;
29         FILE *stfile;
30         
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;
37         int i;
38         
39         
40         fputs("^fg(yellow)", stdout);   // Error signaling color
41         
42         stfile = fopen("/proc/stat", "r");
43         if (stfile != NULL)
44         {
45                 for(i = CPU_HISTORY_SIZE - 1; i > 0; i--)
46                         cpu_history[i] = cpu_history[i - 1];
47                 
48                 stlen = getline(&stline, &stlen, stfile);
49                 fclose(stfile);
50                 
51                 if ( 4 == sscanf(stline, "%*s %ld %ld %ld %ld", &cpu_user, &cpu_nice, &cpu_sys, &cpu_idle) );
52                 {
53                         cpu_used = cpu_user + cpu_nice + cpu_sys;
54                         cpu_total = cpu_used + cpu_idle;
55                         
56                         // Now do the percentage
57                         cpu_history[0] = (float)(cpu_used - last_cpu_used) /
58                                                 (float)(cpu_total - last_cpu_total);
59                         
60                         last_cpu_used = cpu_used;
61                         last_cpu_total = cpu_total;
62                         
63                         
64                         if (cpu_history[0] < 0.4)
65                                 fputs("^fg(#0077FF)", stdout);  // CPU idling OK
66                         else
67                                 fputs("^fg(#FF00FF)", stdout);  // CPU busy
68                         
69                         //printf(" CPU: %.0f%% ", cpu_history[0] * 100);
70                 }
71                 
72                 free(stline);
73         }
74         
75         
76         if (getloadavg(loadavg, 3) > 0);
77                 printf(" %s%.0f,%.0f,%.0f,%.0f ",
78                         cpu_history[0] < 0.1000 ? " " : "",
79                         cpu_history[0] * 100,
80                         loadavg[0] * (100 / 1),
81                         loadavg[1] * (100 / 1), // (100 / NUM_CPUS)
82                         loadavg[2] * (100 / 1));
83         
84         //fputs(" CPU usage ", stdout);
85 }