1433d6423SLionel Sambuc /* ProcFS - util.c - by Alen Stojanov and David van Moolenbroek */ 2433d6423SLionel Sambuc 3433d6423SLionel Sambuc #include "inc.h" 4433d6423SLionel Sambuc 5433d6423SLionel Sambuc /*===========================================================================* 6433d6423SLionel Sambuc * procfs_getloadavg * 7433d6423SLionel Sambuc *===========================================================================*/ 8433d6423SLionel Sambuc int procfs_getloadavg(struct load *loadavg, int nelem) 9433d6423SLionel Sambuc { 10433d6423SLionel Sambuc /* Retrieve system load average information. 11433d6423SLionel Sambuc */ 12433d6423SLionel Sambuc struct loadinfo loadinfo; 13433d6423SLionel Sambuc u32_t system_hz, ticks_per_slot; 14433d6423SLionel Sambuc int p, unfilled_ticks; 15433d6423SLionel Sambuc int minutes[3] = { 1, 5, 15 }; 16433d6423SLionel Sambuc ssize_t l; 17433d6423SLionel Sambuc 18433d6423SLionel Sambuc if(nelem < 1) { 19433d6423SLionel Sambuc errno = ENOSPC; 20433d6423SLionel Sambuc return -1; 21433d6423SLionel Sambuc } 22433d6423SLionel Sambuc 23433d6423SLionel Sambuc system_hz = sys_hz(); 24433d6423SLionel Sambuc 25433d6423SLionel Sambuc if((l=sys_getloadinfo(&loadinfo)) != OK) 26433d6423SLionel Sambuc return -1; 27433d6423SLionel Sambuc if(nelem > 3) 28433d6423SLionel Sambuc nelem = 3; 29433d6423SLionel Sambuc 30433d6423SLionel Sambuc /* How many ticks are missing from the newest-filled slot? */ 31433d6423SLionel Sambuc ticks_per_slot = _LOAD_UNIT_SECS * system_hz; 32433d6423SLionel Sambuc unfilled_ticks = 33433d6423SLionel Sambuc ticks_per_slot - (loadinfo.last_clock % ticks_per_slot); 34433d6423SLionel Sambuc 35433d6423SLionel Sambuc for(p = 0; p < nelem; p++) { 36433d6423SLionel Sambuc int h, slots; 37433d6423SLionel Sambuc int latest = loadinfo.proc_last_slot; 38433d6423SLionel Sambuc slots = minutes[p] * 60 / _LOAD_UNIT_SECS; 39433d6423SLionel Sambuc loadavg[p].proc_load = 0; 40433d6423SLionel Sambuc 41433d6423SLionel Sambuc /* Add up the total number of process ticks for this number 42433d6423SLionel Sambuc * of minutes (minutes[p]). Start with the newest slot, which 43433d6423SLionel Sambuc * is latest, and count back for the number of slots that 44433d6423SLionel Sambuc * correspond to the right number of minutes. Take wraparound 45433d6423SLionel Sambuc * into account by calculating the index modulo _LOAD_HISTORY, 46433d6423SLionel Sambuc * which is the number of slots of history kept. 47433d6423SLionel Sambuc */ 48433d6423SLionel Sambuc for(h = 0; h < slots; h++) { 49433d6423SLionel Sambuc int slot; 50433d6423SLionel Sambuc slot = (latest - h + _LOAD_HISTORY) % _LOAD_HISTORY; 51433d6423SLionel Sambuc loadavg[p].proc_load += 52433d6423SLionel Sambuc loadinfo.proc_load_history[slot]; 53*f92baba7SEmmanuel Blot l += (ssize_t) loadinfo.proc_load_history[slot]; 54433d6423SLionel Sambuc } 55433d6423SLionel Sambuc 56433d6423SLionel Sambuc /* The load average over this number of minutes is the number 57433d6423SLionel Sambuc * of process-ticks divided by the number of ticks, not 58433d6423SLionel Sambuc * counting the number of ticks the last slot hasn't been 59433d6423SLionel Sambuc * around yet. 60433d6423SLionel Sambuc */ 61433d6423SLionel Sambuc loadavg[p].ticks = slots * ticks_per_slot - unfilled_ticks; 62433d6423SLionel Sambuc } 63433d6423SLionel Sambuc 64433d6423SLionel Sambuc return nelem; 65433d6423SLionel Sambuc } 66