4
Linux exports various bits of information via `/proc/stat' and
5
`/proc/uptime' that userland tools, such as top(1), use to calculate
6
the average time system spent in a particular state, for example:
9
Linux 2.6.18.3-exp (linmac) 02/20/2007
11
avg-cpu: %user %nice %system %iowait %steal %idle
12
10.01 0.00 2.92 5.44 0.00 81.63
16
Here the system thinks that over the default sampling period the
17
system spent 10.01% of the time doing work in user space, 2.92% in the
18
kernel, and was overall 81.63% of the time idle.
20
In most cases the `/proc/stat' information reflects the reality quite
21
closely, however due to the nature of how/when the kernel collects
22
this data sometimes it can not be trusted at all.
24
So how is this information collected? Whenever timer interrupt is
25
signalled the kernel looks what kind of task was running at this
26
moment and increments the counter that corresponds to this tasks
27
kind/state. The problem with this is that the system could have
28
switched between various states multiple times between two timer
29
interrupts yet the counter is incremented only for the last state.
35
If we imagine the system with one task that periodically burns cycles
36
in the following manner:
38
time line between two timer interrupts
39
|--------------------------------------|
41
|_ something begins working |
42
|_ something goes to sleep
43
(only to be awaken quite soon)
45
In the above situation the system will be 0% loaded according to the
46
`/proc/stat' (since the timer interrupt will always happen when the
47
system is executing the idle handler), but in reality the load is
50
One can imagine many more situations where this behavior of the kernel
51
will lead to quite erratic information inside `/proc/stat'.
54
/* gcc -o hog smallhog.c */
61
static volatile sig_atomic_t stop;
63
static void sighandler (int signr)
68
static unsigned long hog (unsigned long niters)
71
while (!stop && --niters);
77
struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 },
78
.it_value = { .tv_sec = 0, .tv_usec = 1 } };
80
unsigned long v[HIST];
83
signal (SIGALRM, &sighandler);
84
setitimer (ITIMER_REAL, &it, NULL);
87
for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX - hog (ULONG_MAX);
88
for (i = 0; i < HIST; ++i) tmp += v[i];
90
n = tmp - (tmp / 3.0);
93
sigaddset (&set, SIGALRM);
106
http://lkml.org/lkml/2007/2/12/6
107
Documentation/filesystems/proc.txt (1.8)
113
Con Kolivas, Pavel Machek