arm: make signal handlers work
[minix.git] / servers / procfs / util.c
blobeeaae9a9514076426e6b75dfc25c5748638000ee
1 /* ProcFS - util.c - by Alen Stojanov and David van Moolenbroek */
3 #include "inc.h"
5 /*===========================================================================*
6 * procfs_getloadavg *
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 };
16 ssize_t l;
18 if(nelem < 1) {
19 errno = ENOSPC;
20 return -1;
23 system_hz = sys_hz();
25 if((l=sys_getloadinfo(&loadinfo)) != OK)
26 return -1;
27 if(nelem > 3)
28 nelem = 3;
30 /* How many ticks are missing from the newest-filled slot? */
31 ticks_per_slot = _LOAD_UNIT_SECS * system_hz;
32 unfilled_ticks =
33 ticks_per_slot - (loadinfo.last_clock % ticks_per_slot);
35 for(p = 0; p < nelem; p++) {
36 int h, slots;
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++) {
49 int slot;
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
59 * around yet.
61 loadavg[p].ticks = slots * ticks_per_slot - unfilled_ticks;
64 return nelem;