Module for 3.x kernels
[apc.git] / idlestat.c
blob90d938b3c594f367e4cd24d3a7548e2b20b20ad9
1 #define _GNU_SOURCE
2 #include <err.h>
3 #include <time.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <alloca.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <sys/time.h>
10 #include <sys/sysinfo.h>
12 static double now (void)
14 struct timeval tv;
16 if (gettimeofday (&tv, NULL))
17 err (1, "gettimeofday");
18 return tv.tv_sec + tv.tv_usec * 1e-6;
21 static void idlenow (int fd, int nprocs, double *p)
23 struct timeval tv;
24 size_t n = nprocs * sizeof (tv);
25 ssize_t m;
26 struct timeval *buf;
27 int i;
29 buf = alloca (n);
30 if (!buf) errx (1, "alloca failed");
32 m = read (fd, buf, n);
33 if (n - m) err (1, "read [n=%zu, m=%zi]", n, m);
35 for (i = 0; i < nprocs; ++i)
36 p[i] = buf[i].tv_sec + buf[i].tv_usec * 1e-6;
39 int main (int argc, char **argv)
41 int fd;
42 int nprocs;
43 double *idle;
44 int flip = 0;
45 double *curr, *prev;
47 (void) argc;
48 (void) argv;
50 nprocs = get_nprocs ();
51 if (nprocs <= 0) errx (1, "get_nprocs returned %d", nprocs);
53 idle = malloc (2 * nprocs * sizeof (idle[0]));
54 if (!idle) errx (1, "malloc %zu failed", 2 * nprocs * sizeof (idle[0]));
56 fd = open ("/dev/itc", O_RDONLY);
57 if (fd < 0) err (1, "open /dev/itc");
59 idlenow (fd, nprocs, idle);
61 flip = 0;
62 curr = &idle[nprocs];
63 prev = idle;
65 for (;;) {
66 int i;
67 double s, e, d, *t;
69 s = now ();
70 idlenow (fd, nprocs, prev);
71 sleep (1);
72 e = now ();
73 d = e - s;
74 idlenow (fd, nprocs, curr);
76 for (i = 0; i < nprocs; ++i) {
77 double di = curr[i] - prev[i];
79 printf ("cpu%d - %.2f%%\n", i, 100.0 * (1.0 - di / d));
82 t = curr;
83 curr = prev;
84 prev = t;