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
)
53 nprocs
= get_nprocs ();
54 if (nprocs
<= 0) errx (1, "get_nprocs returned %d", nprocs
);
56 idle
= malloc (2 * nprocs
* sizeof (idle
[0]));
57 if (!idle
) errx (1, "malloc %zu failed", 2 * nprocs
* sizeof (idle
[0]));
59 fd
= open ("/dev/itc", O_RDONLY
);
60 if (fd
< 0) err (1, "open /dev/itc");
62 idlenow (fd
, nprocs
, idle
);
66 setbuf (stdout
, NULL
);
70 double s
, e
, d
, *t
, a
= 0.0, ai
= 0.0;
72 idlenow (fd
, nprocs
, prev
);
75 idlenow (fd
, nprocs
, curr
);
79 for (i
= 0; i
< nprocs
; ++i
) {
80 double di
= curr
[i
] - prev
[i
];
82 /* printf ("\rcpu%d - %.2f", i, 100.0 * (1.0 - di / d)); */
83 /* printf ("cpu%d - %6.2f\n", i, 100.0 * (1.0 - di / d)); */
86 printf ("%7.2f", 100.0 * (1.0 - di
/ d
));
87 if (i
< nprocs
) fputc (' ', stdout
);
90 printf ("%6.2f\n", 100.0 * (1.0 - ai
/ a
));