summaryrefslogtreecommitdiff
path: root/minix/lib/libsys/arch/i386/getidle.c
blob: 566305211cbde5633b024ce194f23c588f75435d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* getidle.c - by David van Moolenbroek <dcvmoole@cs.vu.nl> */

/* Usage:
 *
 *   double idleperc;
 *   getidle();
 *   ...
 *   idleperc = getidle();
 *   if (idleperc >= 0.0)
 *     printf("CPU usage: %lg%%\n", 100.0 - idleperc);
 *
 * Notes:
 * - This functionality can only be used by system processes.
 * - Only one getidle() run is allowed per process at a time.
 *
 */

#define _SYSTEM		1
#include <lib.h>
#include <minix/u64.h>
#include <minix/sysutil.h>
#include <minix/syslib.h>
#include <minix/minlib.h>

static u64_t start, idle;
static int running = 0;

static double make_double(u64_t d)
{
/* Convert a 64-bit fixed point value into a double.
 * This whole thing should be replaced by something better eventually.
 */
  double value;
  size_t i;

  value = (double) ex64hi(d);
  for (i = 0; i < sizeof(unsigned long); i += 2)
	value *= 65536.0;

  value += (double) ex64lo(d);

  return value;
}

double getidle(void)
{
  u64_t stop, idle2;
  u64_t idelta, tdelta;
  double ifp, tfp, rfp;
  int r;

  if (!running) {
	if ((r = sys_getidletsc(&idle)) != OK)
		return -1.0;

	running = 1;

	read_tsc_64(&start);

	return 0.0;
  }
  else {
	read_tsc_64(&stop);

	running = 0;

	if ((r = sys_getidletsc(&idle2)) != OK)
		return -1.0;

	idelta = idle2 - idle;
	tdelta = stop - start;

	if (idelta >= tdelta)
		return 100.0;

	ifp = make_double(idelta);
	tfp = make_double(tdelta);

	rfp = ifp / tfp * 100.0;

	if (rfp < 0.0) rfp = 0.0;
	else if (rfp > 100.0) rfp = 100.0;

	return rfp;
  }
}