1 /* ProcFS - util.c - by Alen Stojanov and David van Moolenbroek */
5 /*===========================================================================*
7 *===========================================================================*/
8 int procfs_getloadavg(struct load
*loadavg
, int nelem
)
10 /* Retrieve system load average information.
12 struct loadinfo loadinfo
;
13 u32_t system_hz
, ticks_per_slot
;
14 int p
, unfilled_ticks
;
15 int minutes
[3] = { 1, 5, 15 };
25 if((l
=sys_getloadinfo(&loadinfo
)) != OK
)
30 /* How many ticks are missing from the newest-filled slot? */
31 ticks_per_slot
= _LOAD_UNIT_SECS
* system_hz
;
33 ticks_per_slot
- (loadinfo
.last_clock
% ticks_per_slot
);
35 for(p
= 0; p
< nelem
; p
++) {
37 int latest
= loadinfo
.proc_last_slot
;
38 slots
= minutes
[p
] * 60 / _LOAD_UNIT_SECS
;
39 loadavg
[p
].proc_load
= 0;
41 /* Add up the total number of process ticks for this number
42 * of minutes (minutes[p]). Start with the newest slot, which
43 * is latest, and count back for the number of slots that
44 * correspond to the right number of minutes. Take wraparound
45 * into account by calculating the index modulo _LOAD_HISTORY,
46 * which is the number of slots of history kept.
48 for(h
= 0; h
< slots
; h
++) {
50 slot
= (latest
- h
+ _LOAD_HISTORY
) % _LOAD_HISTORY
;
51 loadavg
[p
].proc_load
+=
52 loadinfo
.proc_load_history
[slot
];
53 l
+= (double) loadinfo
.proc_load_history
[slot
];
56 /* The load average over this number of minutes is the number
57 * of process-ticks divided by the number of ticks, not
58 * counting the number of ticks the last slot hasn't been
61 loadavg
[p
].ticks
= slots
* ticks_per_slot
- unfilled_ticks
;