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