1 /* getidle.c - by David van Moolenbroek <dcvmoole@cs.vu.nl> */
8 * idleperc = getidle();
9 * printf("CPU usage: %lg%%\n", 100.0 - idleperc);
11 * This routine goes through PM to get the idle time, rather than making the
12 * sys_getinfo() call to the kernel directly. This means that it can be used
13 * by non-system processes as well, but it will incur some extra overhead in
14 * the system case. The overhead does not end up being measured, because the
15 * system is clearly not idle while the system calls are being made. In any
16 * case, for this reason, only one getidle() run is allowed at a time.
18 * Note that the kernel has to be compiled with CONFIG_IDLE_TSC support.
23 #include <minix/sysinfo.h>
24 #include <minix/u64.h>
25 #include <minix/sysutil.h>
27 static u64_t start
, idle
;
28 static int running
= 0;
30 static double make_double(u64_t d
)
32 /* Convert a 64-bit fixed point value into a double.
33 * This whole thing should be replaced by something better eventually.
38 value
= (double) ex64hi(d
);
39 for (i
= 0; i
< sizeof(unsigned long); i
+= 2)
42 value
+= (double) ex64lo(d
);
55 r
= getsysinfo_up(PM_PROC_NR
, SIU_IDLETSC
, sizeof(idle
), &idle
);
56 if (r
!= sizeof(idle
))
70 r
= getsysinfo_up(PM_PROC_NR
, SIU_IDLETSC
, sizeof(idle2
), &idle2
);
71 if (r
!= sizeof(idle2
))
74 idelta
= sub64(idle2
, idle
);
75 tdelta
= sub64(stop
, start
);
77 if (cmp64(idelta
, tdelta
) >= 0)
80 ifp
= make_double(idelta
);
81 tfp
= make_double(tdelta
);
83 rfp
= ifp
/ tfp
* 100.0;
85 if (rfp
< 0.0) rfp
= 0.0;
86 else if (rfp
> 100.0) rfp
= 100.0;