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