cpu-load.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. CPU load
  2. --------
  3. Linux exports various bits of information via `/proc/stat' and
  4. `/proc/uptime' that userland tools, such as top(1), use to calculate
  5. the average time system spent in a particular state, for example:
  6. $ iostat
  7. Linux 2.6.18.3-exp (linmac) 02/20/2007
  8. avg-cpu: %user %nice %system %iowait %steal %idle
  9. 10.01 0.00 2.92 5.44 0.00 81.63
  10. ...
  11. Here the system thinks that over the default sampling period the
  12. system spent 10.01% of the time doing work in user space, 2.92% in the
  13. kernel, and was overall 81.63% of the time idle.
  14. In most cases the `/proc/stat' information reflects the reality quite
  15. closely, however due to the nature of how/when the kernel collects
  16. this data sometimes it can not be trusted at all.
  17. So how is this information collected? Whenever timer interrupt is
  18. signalled the kernel looks what kind of task was running at this
  19. moment and increments the counter that corresponds to this tasks
  20. kind/state. The problem with this is that the system could have
  21. switched between various states multiple times between two timer
  22. interrupts yet the counter is incremented only for the last state.
  23. Example
  24. -------
  25. If we imagine the system with one task that periodically burns cycles
  26. in the following manner:
  27. time line between two timer interrupts
  28. |--------------------------------------|
  29. ^ ^
  30. |_ something begins working |
  31. |_ something goes to sleep
  32. (only to be awaken quite soon)
  33. In the above situation the system will be 0% loaded according to the
  34. `/proc/stat' (since the timer interrupt will always happen when the
  35. system is executing the idle handler), but in reality the load is
  36. closer to 99%.
  37. One can imagine many more situations where this behavior of the kernel
  38. will lead to quite erratic information inside `/proc/stat'.
  39. /* gcc -o hog smallhog.c */
  40. #include <time.h>
  41. #include <limits.h>
  42. #include <signal.h>
  43. #include <sys/time.h>
  44. #define HIST 10
  45. static volatile sig_atomic_t stop;
  46. static void sighandler (int signr)
  47. {
  48. (void) signr;
  49. stop = 1;
  50. }
  51. static unsigned long hog (unsigned long niters)
  52. {
  53. stop = 0;
  54. while (!stop && --niters);
  55. return niters;
  56. }
  57. int main (void)
  58. {
  59. int i;
  60. struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 },
  61. .it_value = { .tv_sec = 0, .tv_usec = 1 } };
  62. sigset_t set;
  63. unsigned long v[HIST];
  64. double tmp = 0.0;
  65. unsigned long n;
  66. signal (SIGALRM, &sighandler);
  67. setitimer (ITIMER_REAL, &it, NULL);
  68. hog (ULONG_MAX);
  69. for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX - hog (ULONG_MAX);
  70. for (i = 0; i < HIST; ++i) tmp += v[i];
  71. tmp /= HIST;
  72. n = tmp - (tmp / 3.0);
  73. sigemptyset (&set);
  74. sigaddset (&set, SIGALRM);
  75. for (;;) {
  76. hog (n);
  77. sigwait (&set, &i);
  78. }
  79. return 0;
  80. }
  81. References
  82. ----------
  83. http://lkml.org/lkml/2007/2/12/6
  84. Documentation/filesystems/proc.txt (1.8)
  85. Thanks
  86. ------
  87. Con Kolivas, Pavel Machek