3 #include <minix/sysinfo.h>
8 /* Retrieve system load average information. */
9 int getloadavg(double *loadavg
, int nelem
)
11 struct loadinfo loadinfo
;
12 int h
, p
, unfilled_ticks
;
14 int minutes
[3] = { 1, 5, 15 };
22 loadsize
= sizeof(loadinfo
);
23 if((l
=getsysinfo_up(PM_PROC_NR
, SIU_LOADINFO
, loadsize
, &loadinfo
)) < 0)
25 if(l
!= sizeof(loadinfo
))
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
++) {
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
++) {
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
);