cpu-load.rst 3.0 KB

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