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
)
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
);
70 idlenow (fd
, nprocs
, prev
);
74 idlenow (fd
, nprocs
, curr
);
76 for (i
= 0; i
< nprocs
; ++i
) {
77 double di
= curr
[i
] - prev
[i
];
79 printf ("cpu%d load - %.2f%%\n", i
, 100.0 * (1.0 - di
/ d
));