psi: introduce psi monitor
Subsystems (5)
Suren Baghdasaryan
maintainerPATCH v4 1/1 psi: introduce psi monitor
Psi monitor aims to provide a low-latency short-term pressure detection mechanism configurable by users. It allows users to monitor psi metrics growth and trigger events whenever a metric raises above user-defined threshold within user-defined time window.
Time window and threshold are both expressed in usecs. Multiple psi resources with different thresholds and window sizes can be monitored concurrently.
Psi monitors activate when system enters stall state for the monitored psi metric and deactivate upon exit from the stall state. While system is in the stall state psi signal growth is monitored at a rate of 10 times per tracking window. Min window size is 500ms, therefore the min monitoring interval is 50ms. Max window size is 10s with monitoring interval of 1s.
When activated psi monitor stays active for at least the duration of one tracking window to avoid repeated activations/deactivations when psi signal is bouncing.
Notifications to the users are rate-limited to one per tracking window.
---
This is respin of:
https://lwn.net/ml/linux-kernel/20190124211518.244221-1-surenb%40google.com/
First 4 patches in the series are in linux-next:
1. fs: kernfs: add poll file operation
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=6a78cef7ad8a1734477a1352dd04a97f1dc58a70
2. kernel: cgroup: add poll file operation
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=c88177361203be291a49956b6c9d5ec164ea24b2
3. psi: introduce state_mask to represent stalled psi states
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=9d8a0c4a7f1c197de9c12bd53ef45fb6d273374e
4. psi: rename psi fields in preparation for psi trigger addition
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=0ef9bb049a4db519152a8664088f7ce34bbee5ac
This patch can be cleanly applied either over linux-next tree (tag: next-20190201) or over linux-stable v5.0-rc4 after applying abovementioned 4 patches.
Changes in v4:
- Resolved conflict with "psi: fix aggregation idle shut-off" patch, as per Andrew Morton
- Replaced smp_mb__after_atomic() with smp_mb() for proper ordering, as per Peter
- Moved now=sched_clock() in psi_update_work() after mutex acquisition, as per Peter
- Expanded comments to explain why smp_mb() is needed in psi_update_work, as per Peter
- Fixed g->polling operation order in the diagram above psi_update_work(), as per Johannes
- Merged psi_trigger_parse() into psi_trigger_create(), as per Johannes
- Replaced list_del_init with list_del in psi_trigger_destroy(), as per Minchan
- Replaced return value in get_recent_times and collect_percpu_times to return-by-parameter, as per Minchan
- Renamed window_init into window_reset and reused it, as per Minchan
- Replaced kzalloc with kmalloc, as per Minchan
- Added explanation in psi.txt for min/max window size choices, as per Minchan
- Misc variable name cleanups, as per Minchan and Johannes
Documentation/accounting/psi.txt | 107 ++++++ include/linux/psi.h | 8 + include/linux/psi_types.h | 59 ++++ kernel/cgroup/cgroup.c | 95 +++++- kernel/sched/psi.c | 559 +++++++++++++++++++++++++++++-- 5 files changed, 794 insertions(+), 34 deletions(-)
Documentation/accounting/psi.txt +107 -0
@@ -63,6 +63,110 @@ tracked and exported as well, to allow detection of latency spikes which wouldn't necessarily make a dent in the time averages, or to average trends over custom time frames. +Monitoring for pressure thresholds +================================== + +Users can register triggers and use poll() to be woken up when resource +pressure exceeds certain thresholds. + +A trigger describes the maximum cumulative stall time over a specific +time window, e.g. 100ms of total stall time within any 500ms window to +generate a wakeup event. + +To register a trigger user has to open psi interface file under +/proc/pressure/ representing the resource to be monitored and write the +desired threshold and time window. The open file descriptor should be +used to wait for trigger events using select(), poll() or epoll(). +The following format is used: + +<some|full> <stall amount in us> <time window in us> + +For example writing "some 150000 1000000" into /proc/pressure/memory +would add 150ms threshold for partial memory stall measured within +1sec time window. Writing "full 50000 1000000" into /proc/pressure/io +would add 50ms threshold for full io stall measured within 1sec time window. + +Triggers can be set on more than one psi metric and more than one trigger +for the same psi metric can be specified. However for each trigger a separate +file descriptor is required to be able to poll it separately from others, +therefore for each trigger a separate open() syscall should be made even +when opening the same psi interface file. + +Monitors activate only when system enters stall state for the monitored +psi metric and deactivates upon exit from the stall state. While system is +in the stall state psi signal growth is monitored at a rate of 10 times per +tracking window. + +The kernel accepts window sizes ranging from 500ms to 10s, therefore min +monitoring update interval is 50ms and max is 1s. Min limit is set to +prevent overly frequent polling. Max limit is chosen as a high enough number +after which monitors are most likely not needed and psi averages can be used +instead. + +When activated, psi monitor stays active for at least the duration of one +tracking window to avoid repeated activations/deactivations when system is +bouncing in and out of the stall state. + +Notifications to the userspace are rate-limited to one per tracking window. + +The trigger will de-register when the file descriptor used to define the +trigger is closed. + +Userspace monitor usage example +=============================== + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <poll.h> +#include <string.h> +#include <unistd.h> + +/* + * Monitor memory partial stall with 1s tracking window size + * and 150ms threshold. + */ +int main() { + const char trig[] = "some 150000 1000000"; + struct pollfd fds; + int n; + + fds.fd = open("/proc/pressure/memory", O_RDWR | O_NONBLOCK); + if (fds.fd < 0) { + printf("/proc/pressure/memory open error: %s\n", + strerror(errno)); + return 1; + } + fds.events = POLLPRI; + + if (write(fds.fd, trig, strlen(trig) + 1) < 0) { + printf("/proc/pressure/memory write error: %s\n", + strerror(errno)); + return 1; + } + + printf("waiting for events...\n"); + while (1) { + n = poll(&fds, 1, -1); + if (n < 0) { + printf("poll error: %s\n", strerror(errno)); + return 1; + } + if (fds.revents & POLLERR) { + printf("got POLLERR, event source is gone\n"); + return 0; + } + if (fds.revents & POLLPRI) { + printf("event triggered!\n"); + } else { + printf("unknown event received: 0x%x\n", fds.revents); + return 1; + } + } + + return 0; +} + Cgroup2 interface =================
@@ -71,3 +175,6 @@ mounted, pressure stall information is also tracked for tasks grouped into cgroups. Each subdirectory in the cgroupfs mountpoint contains cpu.pressure, memory.pressure, and io.pressure files; the format is the same as the /proc/pressure/ files. + +Per-cgroup psi monitors can be specified and used the same way as +system-wide ones.
include/linux/psi.h +8 -0
@@ -4,6 +4,7 @@#include <linux/jump_label.h> #include <linux/psi_types.h> #include <linux/sched.h> +#include <linux/poll.h> struct seq_file ;struct css_set ;
@@ -26,6 +27,13 @@ int psi_show(struct seq_file *s, struct psi_group *group, enum psi_res res);int psi_cgroup_alloc (struct cgroup *cgrp);void psi_cgroup_free (struct cgroup *cgrp);void cgroup_move_task (struct task_struct *p,struct css_set *to); + +struct psi_trigger *psi_trigger_create (struct psi_group *group, +char *buf,size_t nbytes,enum psi_res res); +void psi_trigger_destroy (struct psi_trigger *t); + +__poll_t psi_trigger_poll (struct psi_trigger *t,struct file *file, +poll_table *wait);#endif #else /* CONFIG_PSI */
include/linux/psi_types.h +59 -0
83 lines not on this page · raw message
kernel/cgroup/cgroup.c +93 -2
118 lines not on this page · raw message
kernel/sched/psi.c +527 -32
723 lines not on this page · raw message
Signature · 1 line
2.20.1.611.gfbb209baf1-goog