. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / posix / getloadavg.c
blob2d6976d198c397ade3425019b9ed2c93eaebca99
2 #include <sys/types.h>
3 #include <minix/sysinfo.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <lib.h>
8 /* Retrieve system load average information. */
9 int getloadavg(double *loadavg, int nelem)
11 struct loadinfo loadinfo;
12 int h, p, unfilled_ticks;
13 #define PERIODS 3
14 int minutes[3] = { 1, 5, 15 };
15 size_t loadsize;
16 ssize_t l;
17 if(nelem < 1) {
18 errno = ENOSPC;
19 return -1;
22 loadsize = sizeof(loadinfo);
23 if((l=getsysinfo_up(PM_PROC_NR, SIU_LOADINFO, loadsize, &loadinfo)) < 0)
24 return -1;
25 if(l != sizeof(loadinfo))
26 return -1;
27 if(nelem > PERIODS)
28 nelem = PERIODS;
30 /* How many ticks are missing from the newest-filled slot? */
31 #define TICKSPERSLOT (_LOAD_UNIT_SECS * HZ)
32 unfilled_ticks = TICKSPERSLOT - (loadinfo.last_clock % TICKSPERSLOT);
34 for(p = 0; p < nelem; p++) {
35 int h, offset, slots;
36 double l = 0.0;
37 int latest = loadinfo.proc_last_slot;
38 slots = minutes[p] * 60 / _LOAD_UNIT_SECS;
40 /* Add up the total number of process ticks for this number
41 * of minutes (minutes[p]). Start with the newest slot, which
42 * is latest, and count back for the number of slots that
43 * correspond to the right number of minutes. Take wraparound
44 * into account by calculating the index modulo _LOAD_HISTORY,
45 * which is the number of slots of history kept.
47 for(h = 0; h < slots; h++) {
48 int slot;
49 slot = (latest - h + _LOAD_HISTORY) % _LOAD_HISTORY;
50 l += (double) loadinfo.proc_load_history[slot];
53 /* The load average over this number of minutes is the number of
54 * process-ticks divided by the number of ticks, not counting the
55 * number of ticks the last slot hasn't been around yet.
57 loadavg[p] = l / (slots * TICKSPERSLOT - unfilled_ticks);
60 return nelem;