1 /* getidle.c - by David van Moolenbroek <dcvmoole@cs.vu.nl> */
8 * idleperc = getidle();
10 * printf("CPU usage: %lg%%\n", 100.0 - idleperc);
13 * - This functionality can only be used by system processes.
14 * - Only one getidle() run is allowed per process at a time.
21 #include <minix/u64.h>
22 #include <minix/sysutil.h>
23 #include <minix/syslib.h>
24 #include <minix/minlib.h>
26 static u64_t start
, idle
;
27 static int running
= 0;
29 static double make_double(u64_t d
)
31 /* Convert a 64-bit fixed point value into a double.
32 * This whole thing should be replaced by something better eventually.
37 value
= (double) ex64hi(d
);
38 for (i
= 0; i
< sizeof(unsigned long); i
+= 2)
41 value
+= (double) ex64lo(d
);
54 if ((r
= sys_getidletsc(&idle
)) != OK
)
68 if ((r
= sys_getidletsc(&idle2
)) != OK
)
71 idelta
= sub64(idle2
, idle
);
72 tdelta
= sub64(stop
, start
);
74 if (cmp64(idelta
, tdelta
) >= 0)
77 ifp
= make_double(idelta
);
78 tfp
= make_double(tdelta
);
80 rfp
= ifp
/ tfp
* 100.0;
82 if (rfp
< 0.0) rfp
= 0.0;
83 else if (rfp
> 100.0) rfp
= 100.0;