source: trunk/minix/lib/posix/getloadavg.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 1.6 KB
Line 
1
2#include <sys/types.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <lib.h>
6
7/* Retrieve system load average information. */
8int getloadavg(double *loadavg, int nelem)
9{
10 struct loadinfo loadinfo;
11 int h, p, unfilled_ticks;
12#define PERIODS 3
13 int minutes[3] = { 1, 5, 15 };
14 if(nelem < 1) {
15 errno = ENOSPC;
16 return -1;
17 }
18
19 if(getsysinfo(PM_PROC_NR, SI_LOADINFO, &loadinfo) < 0)
20 return -1;
21 if(nelem > PERIODS)
22 nelem = PERIODS;
23
24 /* How many ticks are missing from the newest-filled slot? */
25#define TICKSPERSLOT (_LOAD_UNIT_SECS * HZ)
26 unfilled_ticks = TICKSPERSLOT - (loadinfo.last_clock % TICKSPERSLOT);
27
28 for(p = 0; p < nelem; p++) {
29 int h, offset, slots;
30 double l = 0.0;
31 int latest = loadinfo.proc_last_slot;
32 slots = minutes[p] * 60 / _LOAD_UNIT_SECS;
33
34 /* Add up the total number of process ticks for this number
35 * of minutes (minutes[p]). Start with the newest slot, which
36 * is latest, and count back for the number of slots that
37 * correspond to the right number of minutes. Take wraparound
38 * into account by calculating the index modulo _LOAD_HISTORY,
39 * which is the number of slots of history kept.
40 */
41 for(h = 0; h < slots; h++) {
42 int slot;
43 slot = (latest - h + _LOAD_HISTORY) % _LOAD_HISTORY;
44 l += (double) loadinfo.proc_load_history[slot];
45 }
46
47 /* The load average over this number of minutes is the number of
48 * process-ticks divided by the number of ticks, not counting the
49 * number of ticks the last slot hasn't been around yet.
50 */
51 loadavg[p] = l / (slots * TICKSPERSLOT - unfilled_ticks);
52 }
53
54 return nelem;
55}
56
Note: See TracBrowser for help on using the repository browser.