10 #include <sys/sysinfo.h>
12 static double now (void)
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
)
24 size_t n
= nprocs
* sizeof (tv
);
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
)
49 nprocs
= get_nprocs ();
50 if (nprocs
<= 0) errx (1, "get_nprocs returned %d", nprocs
);
52 idle
= malloc (2 * nprocs
* sizeof (idle
[0]));
53 if (!idle
) errx (1, "malloc %zu failed", 2 * nprocs
* sizeof (idle
[0]));
55 fd
= open ("/dev/itc", O_RDONLY
);
56 if (fd
< 0) err (1, "open /dev/itc");
58 idlenow (fd
, nprocs
, idle
);
62 setbuf (stdout
, NULL
);
66 double s
, e
, d
, *t
, a
= 0.0, ai
= 0.0;
68 idlenow (fd
, nprocs
, prev
);
71 idlenow (fd
, nprocs
, curr
);
75 for (i
= 0; i
< nprocs
; ++i
) {
76 double di
= curr
[i
] - prev
[i
];
78 /* printf ("\rcpu%d - %.2f", i, 100.0 * (1.0 - di / d)); */
79 /* printf ("cpu%d - %6.2f\n", i, 100.0 * (1.0 - di / d)); */
82 printf ("%7.2f", 100.0 * (1.0 - di
/ d
));
83 if (i
< nprocs
) fputc (' ', stdout
);
86 printf ("%6.2f\n", 100.0 * (1.0 - ai
/ a
));