VM: full munmap
[minix.git] / lib / libsys / getidle.c
blob989eac53a11c5ffad07cda29a01bfe1cf9ca700e
1 /* getidle.c - by David van Moolenbroek <dcvmoole@cs.vu.nl> */
3 /* Usage:
5 * double idleperc;
6 * getidle();
7 * ...
8 * idleperc = getidle();
9 * if (idleperc >= 0.0)
10 * printf("CPU usage: %lg%%\n", 100.0 - idleperc);
12 * Notes:
13 * - This functionality can only be used by system processes.
14 * - Only one getidle() run is allowed per process at a time.
18 #define _MINIX 1
19 #define _SYSTEM 1
20 #include <lib.h>
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.
34 double value;
35 size_t i;
37 value = (double) ex64hi(d);
38 for (i = 0; i < sizeof(unsigned long); i += 2)
39 value *= 65536.0;
41 value += (double) ex64lo(d);
43 return value;
46 double getidle(void)
48 u64_t stop, idle2;
49 u64_t idelta, tdelta;
50 double ifp, tfp, rfp;
51 int r;
53 if (!running) {
54 if ((r = sys_getidletsc(&idle)) != OK)
55 return -1.0;
57 running = 1;
59 read_tsc_64(&start);
61 return 0.0;
63 else {
64 read_tsc_64(&stop);
66 running = 0;
68 if ((r = sys_getidletsc(&idle2)) != OK)
69 return -1.0;
71 idelta = sub64(idle2, idle);
72 tdelta = sub64(stop, start);
74 if (cmp64(idelta, tdelta) >= 0)
75 return 100.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;
85 return rfp;