psi.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. .. _psi:
  2. ================================
  3. PSI - Pressure Stall Information
  4. ================================
  5. :Date: April, 2018
  6. :Author: Johannes Weiner <hannes@cmpxchg.org>
  7. When CPU, memory or IO devices are contended, workloads experience
  8. latency spikes, throughput losses, and run the risk of OOM kills.
  9. Without an accurate measure of such contention, users are forced to
  10. either play it safe and under-utilize their hardware resources, or
  11. roll the dice and frequently suffer the disruptions resulting from
  12. excessive overcommit.
  13. The psi feature identifies and quantifies the disruptions caused by
  14. such resource crunches and the time impact it has on complex workloads
  15. or even entire systems.
  16. Having an accurate measure of productivity losses caused by resource
  17. scarcity aids users in sizing workloads to hardware--or provisioning
  18. hardware according to workload demand.
  19. As psi aggregates this information in realtime, systems can be managed
  20. dynamically using techniques such as load shedding, migrating jobs to
  21. other systems or data centers, or strategically pausing or killing low
  22. priority or restartable batch jobs.
  23. This allows maximizing hardware utilization without sacrificing
  24. workload health or risking major disruptions such as OOM kills.
  25. Pressure interface
  26. ==================
  27. Pressure information for each resource is exported through the
  28. respective file in /proc/pressure/ -- cpu, memory, and io.
  29. The format for CPU is as such::
  30. some avg10=0.00 avg60=0.00 avg300=0.00 total=0
  31. and for memory and IO::
  32. some avg10=0.00 avg60=0.00 avg300=0.00 total=0
  33. full avg10=0.00 avg60=0.00 avg300=0.00 total=0
  34. The "some" line indicates the share of time in which at least some
  35. tasks are stalled on a given resource.
  36. The "full" line indicates the share of time in which all non-idle
  37. tasks are stalled on a given resource simultaneously. In this state
  38. actual CPU cycles are going to waste, and a workload that spends
  39. extended time in this state is considered to be thrashing. This has
  40. severe impact on performance, and it's useful to distinguish this
  41. situation from a state where some tasks are stalled but the CPU is
  42. still doing productive work. As such, time spent in this subset of the
  43. stall state is tracked separately and exported in the "full" averages.
  44. The ratios (in %) are tracked as recent trends over ten, sixty, and
  45. three hundred second windows, which gives insight into short term events
  46. as well as medium and long term trends. The total absolute stall time
  47. (in us) is tracked and exported as well, to allow detection of latency
  48. spikes which wouldn't necessarily make a dent in the time averages,
  49. or to average trends over custom time frames.
  50. Monitoring for pressure thresholds
  51. ==================================
  52. Users can register triggers and use poll() to be woken up when resource
  53. pressure exceeds certain thresholds.
  54. A trigger describes the maximum cumulative stall time over a specific
  55. time window, e.g. 100ms of total stall time within any 500ms window to
  56. generate a wakeup event.
  57. To register a trigger user has to open psi interface file under
  58. /proc/pressure/ representing the resource to be monitored and write the
  59. desired threshold and time window. The open file descriptor should be
  60. used to wait for trigger events using select(), poll() or epoll().
  61. The following format is used::
  62. <some|full> <stall amount in us> <time window in us>
  63. For example writing "some 150000 1000000" into /proc/pressure/memory
  64. would add 150ms threshold for partial memory stall measured within
  65. 1sec time window. Writing "full 50000 1000000" into /proc/pressure/io
  66. would add 50ms threshold for full io stall measured within 1sec time window.
  67. Triggers can be set on more than one psi metric and more than one trigger
  68. for the same psi metric can be specified. However for each trigger a separate
  69. file descriptor is required to be able to poll it separately from others,
  70. therefore for each trigger a separate open() syscall should be made even
  71. when opening the same psi interface file. Write operations to a file descriptor
  72. with an already existing psi trigger will fail with EBUSY.
  73. Monitors activate only when system enters stall state for the monitored
  74. psi metric and deactivates upon exit from the stall state. While system is
  75. in the stall state psi signal growth is monitored at a rate of 10 times per
  76. tracking window.
  77. The kernel accepts window sizes ranging from 500ms to 10s, therefore min
  78. monitoring update interval is 50ms and max is 1s. Min limit is set to
  79. prevent overly frequent polling. Max limit is chosen as a high enough number
  80. after which monitors are most likely not needed and psi averages can be used
  81. instead.
  82. When activated, psi monitor stays active for at least the duration of one
  83. tracking window to avoid repeated activations/deactivations when system is
  84. bouncing in and out of the stall state.
  85. Notifications to the userspace are rate-limited to one per tracking window.
  86. The trigger will de-register when the file descriptor used to define the
  87. trigger is closed.
  88. Userspace monitor usage example
  89. ===============================
  90. ::
  91. #include <errno.h>
  92. #include <fcntl.h>
  93. #include <stdio.h>
  94. #include <poll.h>
  95. #include <string.h>
  96. #include <unistd.h>
  97. /*
  98. * Monitor memory partial stall with 1s tracking window size
  99. * and 150ms threshold.
  100. */
  101. int main() {
  102. const char trig[] = "some 150000 1000000";
  103. struct pollfd fds;
  104. int n;
  105. fds.fd = open("/proc/pressure/memory", O_RDWR | O_NONBLOCK);
  106. if (fds.fd < 0) {
  107. printf("/proc/pressure/memory open error: %s\n",
  108. strerror(errno));
  109. return 1;
  110. }
  111. fds.events = POLLPRI;
  112. if (write(fds.fd, trig, strlen(trig) + 1) < 0) {
  113. printf("/proc/pressure/memory write error: %s\n",
  114. strerror(errno));
  115. return 1;
  116. }
  117. printf("waiting for events...\n");
  118. while (1) {
  119. n = poll(&fds, 1, -1);
  120. if (n < 0) {
  121. printf("poll error: %s\n", strerror(errno));
  122. return 1;
  123. }
  124. if (fds.revents & POLLERR) {
  125. printf("got POLLERR, event source is gone\n");
  126. return 0;
  127. }
  128. if (fds.revents & POLLPRI) {
  129. printf("event triggered!\n");
  130. } else {
  131. printf("unknown event received: 0x%x\n", fds.revents);
  132. return 1;
  133. }
  134. }
  135. return 0;
  136. }
  137. Cgroup2 interface
  138. =================
  139. In a system with a CONFIG_CGROUP=y kernel and the cgroup2 filesystem
  140. mounted, pressure stall information is also tracked for tasks grouped
  141. into cgroups. Each subdirectory in the cgroupfs mountpoint contains
  142. cpu.pressure, memory.pressure, and io.pressure files; the format is
  143. the same as the /proc/pressure/ files.
  144. Per-cgroup psi monitors can be specified and used the same way as
  145. system-wide ones.