1 /* ProcFS - util.c - utility functions */
6 * Retrieve system load average information.
9 procfs_getloadavg(struct load
* loadavg
, int nelem
)
11 struct loadinfo loadinfo
;
12 u32_t system_hz
, ticks_per_slot
;
13 int p
, unfilled_ticks
;
14 int h
, slots
, latest
, slot
;
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
++) {
36 latest
= loadinfo
.proc_last_slot
;
37 slots
= minutes
[p
] * 60 / _LOAD_UNIT_SECS
;
38 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
++) {
49 slot
= (latest
- h
+ _LOAD_HISTORY
) % _LOAD_HISTORY
;
50 loadavg
[p
].proc_load
+=
51 loadinfo
.proc_load_history
[slot
];
52 l
+= (ssize_t
) 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
;