linux/Documentation/cpu-load.txt
<<
.10..41/spa14. .41/form4. .41a .10..4 href="../linux+v33121 /Documentav3.1/cpu-load.txt">.10..41img src="../.stav3c/gfx/right.png" alt=">>">.11/spa14..11spa1 class="lxr_search">.10..10..41input typ1 >hidden" nam1 >navtarget" 01hh1 >">.10..41input typ1 >text" nam1 >search" id >search">.10..41butt submit">Search.10..4Prefs. .41/a>.11/spa14.0..4 41/div4.0..4 41form acv3.1="ajax+*" method="post" onsubmit="return false;">.11input typ1 >hidden" nam1 >ajax_lookup" id >ajax_lookup" 01hh1 >">.0..4 41/form4..0..4 41div class="headingbott m">search_results" class="search_results". .4.0..4 41/div4. 1div id >content"4. 1div id >file_contents"4
1b/74/7c93efdc08a1b5599e3f9168193b849d4262_3/0"4L1" class="line" nam1 >L1">4 411/a>CPU load
L2" class="line" nam1 >L2">4 421/a>--------
L3" class="line" nam1 >L3">4 431/a>.L4" class="line" nam1 >L4">4 441/a>Linux exports 01rious bits of informa	  L5" class="line" nam1 >L5">4 451/a>`/proc/u v3me' that userland tools, such as top(1), use to calculate
L6" class="line" nam1 >L6">4 461/a>the average t3me system spent in a parv3cular stave, for example:
L7" class="line" nam1 >L7">4 471/a>.L8" class="line" nam1 >L8">4 481/a>.4 4$ iostav.L9" class="line" nam1 >L9">4 491/a>.4 4Linux 2 v318.3-exp (linmac)..4 402/20/2007.L10" class="line" nam1 >L10">4 0.L11" class="line" nam1 >L11">4 111/a>.4 4avg-cpu:  %user   %nice %system %iowait  %steal   %idle
L12" class="line" nam1 >L12">4 121/a>.4 4444444444410.01.4 40.00.4 42.92.4 451.4.4 40.00.4 81.63
L13" class="line" nam1 >L13">4 131/a>.L14" class="line" nam1 >L14">4 141/a>.4 4....L15" class="line" nam1 >L15">4 151/a>.L16" class="line" nam1 >L16">4 161/a>Here the system thinks that over the default sampling period the.L17" class="line" nam1 >L17">4 171/a>system spent 10.01% of the t3me doing work in user space, 2.92% in the.L18" class="line" nam1 >L18">4 181/a>kernel, and was overall 81.63% of the t3me idle..L19" class="line" nam1 >L19">4 191/a>.L20" class="line" nam1 >L20">4 2In most cases the `/proc/stav' informa	  L21" class="line" nam1 >L21">4 211/a>closely, however due to the nature of how/when the kernel collects
L22" class="line" nam1 >L22">4 221/a>this data somet3mes it can not be trusted at all..L23" class="line" nam1 >L23">4 231/a>.L24" class="line" nam1 >L24">4 241/a>So how is this informa	  L25" class="line" nam1 >L25">4 251/a>signalled the kernel looks what kind of task was running at this.L26" class="line" nam1 >L26">4 261/a>moment and increments the counter that corresponds to this tasks.L27" class="line" nam1 >L27">4 271/a>kind/stave.  The problem with this is that the system could have.L28" class="line" nam1 >L28">4 281/a>switched between 01rious staves multiple t3mes between two t3mer.L29" class="line" nam1 >L29">4 291/a>interru vs yet the counter is incremented only for the last stave..L30" class="line" nam1 >L30">4 3.L31" class="line" nam1 >L31">4 31/oa>.L32" class="line" nam1 >L32">4 321/a>Example.L33" class="line" nam1 >L33">4 331/a>-------.L34" class="line" nam1 >L34">4 34/oa>.L35" class="line" nam1 >L35">4 351/a>If we imagine the system with one task that periodically burns cycles.L36" class="line" nam1 >L36">4 361/a>in the following manner:
L37" class="line" nam1 >L37">4 371/a>.L38" class="line" nam1 >L38">4 381/a>.t3me line between two t3mer interru vs.L39" class="line" nam1 >L39">4 391/a>|--------------------------------------|.L40" class="line" nam1 >L40">4 4 ^                                    ^.L41" class="line" nam1 >L41">4 411/a>.|_ something begins working          |.L42" class="line" nam1 >L42">4 421/a>.4 44444444444444444444444444444444444|_ something goes to sleep.L43" class="line" nam1 >L43">4 431/a>.4 4444444444444444444444444444444444(only to be awaken quite soon).L44" class="line" nam1 >L44">4 44/oa>.L45" class="line" nam1 >L45">4 45/oa>In the above situa	  L46" class="line" nam1 >L46">4 461/a>`/proc/stav' (since the t3mer interru v will always happen when the.L47" class="line" nam1 >L47">4 471/a>system is executing the idle handler), but in reality the load is.L48" class="line" nam1 >L48">4 481/a>closer to 99%..L49" class="line" nam1 >L49">4 491/a>.L50" class="line" nam1 >L50">4 5One can imagine many more situa	  L51" class="line" nam1 >L51">4 511/a>will lead to quite errav3c informa	  L52" class="line" nam1 >L52">4 521/a>.L53" class="line" nam1 >L53">4 531/a>.L54" class="line" nam1 >L54">4 54/oa>/* gcc -o hog smallhog.c */.L55" class="line" nam1 >L55">4 55/oa>#include <t3me.h>.L56" class="line" nam1 >L56">4 56/oa>#include <limits.h>.L57" class="line" nam1 >L57">4 57/oa>#include <signal.h>.L58" class="line" nam1 >L58">4 58/oa>#include <sys/t3me.h>.L59" class="line" nam1 >L59">4 591/a>#define HIST 10
L60" class="line" nam1 >L60">4 6.L61" class="line" nam1 >L61">4 611/a>stav3c volav3le sig_atomic_t stop;.L62" class="line" nam1 >L62">4 621/a>.L63" class="line" nam1 >L63">4 631/a>stav3c void sighandler (int signr).L64" class="line" nam1 >L64">4 64/oa>{.L65" class="line" nam1 >L65">4 651/a>.4 44(void) signr;.L66" class="line" nam1 >L66">4 661/a>.4 44stop = 1;.L67" class="line" nam1 >L67">4 67/oa>}.L68" class="line" nam1 >L68">4 681/a>stav3c unsigned long hog (unsigned long niters).L69" class="line" nam1 >L69">4 69/oa>{.L70" class="line" nam1 >L70">4 701/a>.4 44stop = 0;.L71" class="line" nam1 >L71">4 711/a>.4 4 wh3le (!stop && --niters);.L72" class="line" nam1 >L72">4 721/a>.4 44return niters;.L73" class="line" nam1 >L73">4 73/oa>}.L74" class="line" nam1 >L74">4 741/a>int main (void).L75" class="line" nam1 >L75">4 75/oa>{.L76" class="line" nam1 >L76">4 761/a>.4 44int i;.L77" class="line" nam1 >L77">4 771/a>.4 44struct it3merval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 },.L78" class="line" nam1 >L78">4 781/a>.4 44444444444444444444444444.it_01hh1 = { .tv_sec = 0, .tv_usec = 1 } };.L79" class="line" nam1 >L79">4 791/a>.4 44sigset_t set;.L80" class="line" nam1 >L80">4 801/a>.4 44unsigned long v[HIST];.L81" class="line" nam1 >L81">4 811/a>.4 4 double tmp = 0.0;.L82" class="line" nam1 >L82">4 821/a>.4 44unsigned long n;.L83" class="line" nam1 >L83">4 831/a>.4 44signal (SIGALRM, &sighandler);.L84" class="line" nam1 >L84">4 841/a>.4 44setit3mer (ITIMER_REAL, &it, NULL);.L85" class="line" nam1 >L85">4 851/a>.L86" class="line" nam1 >L86">4 861/a>.4 44hog (ULONG_MAX);.L87" class="line" nam1 >L87">4 871/a>.4 44for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX -4hog (ULONG_MAX);.L88" class="line" nam1 >L88">4 881/a>.4 44for (i = 0; i < HIST; ++i) tmp += v[i];.L89" class="line" nam1 >L89">4 891/a>.4 44tmp /= HIST;.L90" class="line" nam1 >L90">4 901/a>.4 44n = tmp - (tmp / 3.0);.L91" class="line" nam1 >L91">4 91/oa>.L92" class="line" nam1 >L92">4 921/a>.4 44sigemptyset (&set);.L93" class="line" nam1 >L93">4 931/a>.4 44sigaddset (&set, SIGALRM);.L94" class="line" nam1 >L94">4 94/oa>.L95" class="line" nam1 >L95">4 951/a>.4 44for (;;) {.L96" class="line" nam1 >L96">4 961/a>.4 444444hog (n);.L97" class="line" nam1 >L97">4 971/a>.4 444444sigwait (&set, &i);.L98" class="line" nam1 >L98">4 981/a>.4 44}.L99" class="line" nam1 >L99">4 991/a>.4 44return 0;.L100" class="line" nam1 >L100">4100/oa>}.L101" class="line" nam1 >L101">4101/oa>.L102" class="line" nam1 >L102">41021/a>.L103" class="line" nam1 >L103">41031/a>References.L104" class="line" nam1 >L104">41041/a>----------.L105" class="line" nam1 >L105">41051/a>.L106" class="line" nam1 >L106">41061/a>http://lkml.org/lkml/2007/2/12/61/a>.L107" class="line" nam1 >L107">41071/a>Documentav3.1/filesystems/proc.txt (1.8).L108" class="line" nam1 >L108">41081/a>.L109" class="line" nam1 >L109">41091/a>.L110" class="line" nam1 >L110">410Thanks.L111" class="line" nam1 >L111">41111/a>------.L112" class="line" nam1 >L112">41121/a>.L113" class="line" nam1 >L113">41131/a>Con Kolivas, Pavel Machek.L114" class="line" nam1 >L114">41141/a>
The original LXR software by the LXR community1/a>, this experimental vers lxr@linux.no1/a>. 1/div4.1div class="subfooter"> lxr.linux.no kindly4hosted by Redpill4Linpro AS1/a>, provider of Linux consulting and opera