selftests/cgroup: add PSI pressure trigger and validation tests
PATCH v5 1/1 selftests/cgroup: add PSI pressure trigger and validation tests
From: Tao Cui <[email protected]>
The cgroup selftests have no PSI coverage. Add test_psi.c: per-resource trigger smoke tests (one trigger per fd, IRQ full-only), a cgroup.pressure hide/show toggle test, and a CPU-pressure trigger test using over-subscription. Skips when PSI is disabled or a resource is absent.
---
Changes since v4 (Suren Baghdasaryan review):
- Report pressure_open() errors with ksft_perror() instead of a raw fprintf.
- Create the test cgroup in FIXTURE_SETUP() so that setup and teardown are symmetric, and drop the per-test creation and the NULL guard in the teardown.
- Factor the identical bodies of proc_trigger_io/memory/cpu into test_psi_write(); the helper takes _metadata so that its ASSERTs attribute to the calling test, following the seccomp selftests precedent.
- Drop the now-unneeded NULL init of self->cg and the stale stdbool.h include left over from the restructuring.
Changes since v3 (Suren Baghdasaryan review):
- Convert to the kselftest harness: each case is a TEST_F(psi, ...) with the cgroup root/PSI availability checks in FIXTURE_SETUP() and the teardown (kill hogs, destroy cgroup) in FIXTURE_TEARDOWN(), which also removes the "ret"/"created" bookkeeping.
- A trigger that does not fire within the poll timeout is now a FAIL instead of a SKIP: ncpus+1 hogs with a 1usec threshold must stall, so a timeout indicates a real problem.
- Treat a poll() timeout and a poll() error uniformly via ASSERT.
- Check sysconf(_SC_NPROCESSORS_ONLN) only for -1 and report strerror(errno); declare variables one per line; for(;;) {}.
- Make hog_cpu() die with the runner via PR_SET_PDEATHSIG so an interrupted run does not leave orphaned hogs pinning every CPU.
Changes since v2 (Suren Baghdasaryan, Michal Koutny review):
- Restructure the trigger test into per-resource cases (io, memory, cpu, irq) so a failure points at the specific resource; irq is skipped when /proc/pressure/irq is absent.
- Spawn the CPU hogs with cg_run_nowait() instead of open-coding fork(), and arm the trigger with a 2s window so unprivileged users can set it.
- Address the remaining review comments on cleanup and robustness:
guard teardown with a "created" flag, use cg_read_strcmp() instead of atoi(), report strerror() on errors, and fix the unused-parameter and sign-compare nits.
Changes since v1 (Michal Koutny, sashiko review):
- Keep trigger tests smoke-level; switch the firing test from memory to CPU pressure; drop churn_memory().
- Keep the runner out of the cgroup; add PSI/IRQ skip-guards and a .gitignore entry.
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
--- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/config | 1 + tools/testing/selftests/cgroup/test_psi.c | 191 ++++++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_psi.c
tools/testing/selftests/cgroup/.gitignore +1 -0
@@ -8,5 +8,6 @@ test_kill test_kmem test_memcontrol test_pids +test_psi test_zswap wait_inotify
tools/testing/selftests/cgroup/Makefile +2 -0
@@ -16,6 +16,7 @@ TEST_GEN_PROGS += test_kill TEST_GEN_PROGS += test_kmem TEST_GEN_PROGS += test_memcontrol TEST_GEN_PROGS += test_pids +TEST_GEN_PROGS += test_psi TEST_GEN_PROGS += test_zswap LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
@@ -32,4 +33,5 @@ $(OUTPUT)/test_kill: $(LIBCGROUP_O) $(OUTPUT)/test_kmem: $(LIBCGROUP_O) $(OUTPUT)/test_memcontrol: $(LIBCGROUP_O) $(OUTPUT)/test_pids: $(LIBCGROUP_O) +$(OUTPUT)/test_psi: $(LIBCGROUP_O) $(OUTPUT)/test_zswap: $(LIBCGROUP_O)
tools/testing/selftests/cgroup/config +1 -0
@@ -4,3 +4,4 @@ CONFIG_CGROUP_FREEZER=y CONFIG_CGROUP_SCHED=y CONFIG_MEMCG=y CONFIG_PAGE_COUNTER=y +CONFIG_PSI=y
tools/testing/selftests/cgroup/test_psi.c new file +191 -0
191 lines not on this page · raw message
Signature · 1 line
2.43.0
Michal Koutný
maintainerHello Tao.
tools/testing/selftests/cgroup/test_psi.c line 22
Quoted 5 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+/* PSI triggers are written with a trailing NUL the kernel parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger) +{ +return write (fd, trigger,strlen (trigger) +1 ); +}
Hyrum's law. It all works for me: NUL, \n or just write(2) the exact length of the string.
For conventionality, I'd prefer the simple literals and plain strlen() +
0. (I reckon cg_write() cannot be used because of FD access.)
tools/testing/selftests/cgroup/test_psi.c line 32
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; +int fd; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource); + fd =open (path,O_RDWR ); +if (fd <0 ) +ksft_perror (path);
This outputs:
| # /proc/pressure/irq: No such file or directory (2)
| # SKIP /proc/pressure/irq unavailable
I.e. similar message is printed twice.
Since strace is a companion of cgroup selftests, I'd keep this helper silent.
Quoted 17 lines
tools/testing/selftests/cgroup/test_psi.c new file +17
-0+return fd; +} + +FIXTURE (psi) +{ +char root[PATH_MAX ]; +char *cg; +}; + +FIXTURE_SETUP (psi) +{ +int psi_fd; + +if (cg_find_unified_root (self->root,sizeof (self->root),NULL )) +SKIP (return,"cgroup v2 isn't mounted" ); + +/* PSI must be enabled (CONFIG_PSI=y, not disabled on the cmdline). */
tools/testing/selftests/cgroup/test_psi.c line 59
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ psi_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (psi_fd <0 ) +SKIP (return,"PSI unavailable (CONFIG_PSI=n or psi=0)" ); +close (psi_fd); + + self->cg =cg_name (self->root,"psi_trigger_test" ); +if (!self->cg) +SKIP (return,"failed to allocate cgroup name" ); +if (cg_create (self->cg)) +SKIP (return,"failed to create cgroup: %s" ,strerror (errno));
Why are these two SKIPs (not failures)?
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+TEST_F(psi, cgroup_trigger_fire) +{ +char *cpupress; +struct pollfd pfd = { .events =POLLPRI }; +long ncpus; +int fd; +int i; + + cpupress =cg_control (self->cg,"cpu.pressure" ); +ASSERT_NE (NULL , cpupress);
tools/testing/selftests/cgroup/test_psi.c line 175
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ fd =open (cpupress,O_RDWR ); +free (cpupress); +ASSERT_GE (fd,0 ); + pfd.fd = fd; + +/* +* 1usec threshold over a 2s window: any CPU stall fires it. The 2s +* window is the smallest unprivileged users are allowed to arm. +*/ +ASSERT_GT (write_trigger (fd,"some 1 2000000" ),0 );
The selftest rarely can be run as unprivileged user (even test cgroup creation needs privileges), so this comment is irrelevant. (But it's fine to test with that value.)
On the more abstract level -- I was playing with this and thinking about a value that'd test both sides, i.e. false triggers as well as false non-triggers. I'd find that to be the half of the window and the number of tasks should be then (3*ncpus + 1) / 2.
Or perhaps test two thresholds, one tiny like you did and one maximum (whole window) with same amount tasks but expect trigger, no trigger respectively.
tools/testing/selftests/cgroup/test_psi.c line 180
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus ==-1 ) +TH_LOG ("sysconf(_SC_NPROCESSORS_ONLN): %s" ,strerror (errno)); +ASSERT_NE (-1 , ncpus);
Same as messages from pressure_open() above. Simply assert.
tools/testing/selftests/cgroup/test_psi.c line 191
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +11
-0+ +/* ncpus+1 hogs guarantee CPU contention inside the cgroup. */ +for (i =0 ; i < ncpus +1 ; i++) +ASSERT_GE (cg_run_nowait (self->cg, hog_cpu,NULL ),0 ); + +ASSERT_EQ (1 ,poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )); +ASSERT_NE (0 , pfd.revents &POLLPRI ); +close (fd); +} + +TEST_HARNESS_MAIN
All in all, this looks so much better than the initial version, well done. Just a few polishing touches.
Michal
Hello Michal, Suren,
Quoted 14 lines · Michal Koutný
Hello Tao.
tools/testing/selftests/cgroup/test_psi.c line 22
Quoted 5 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+/* PSI triggers are written with a trailing NUL the kernel parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger) +{ +return write (fd, trigger,strlen (trigger) +1 ); +}Hyrum's law. It all works for me: NUL, \n or just write(2) the exact length of the string.
For conventionality, I'd prefer the simple literals and plainstrlen()+
0. (I reckoncg_write()cannot be used because of FD access.)
Your Hyrum's law point made me look at the parser, and I'm glad it did, because the behavior is more subtle than "all of them work". psi_write() does
buf[buf_size - 1] = '\0';
i.e. it overwrites the last byte of whatever was written. With a plain strlen()-sized write that eats the last digit: "some 150000 2000000" silently arms a 200000us window when privileged, and fails with EINVAL for unprivileged users (200000 is not a multiple of the 2s minimum). I reproduced both on 7.0.0-28 here. I suspect your runs succeeded because a truncated window still makes a valid trigger for root, so nothing looked off.
I went with your \n variant instead: the newline gets clobbered, the payload stays intact, and it is the conventional procfile form. So the reliance on the undocumented NUL is gone, even though not quite via strlen()+0.
Two follow-ups this suggests, if there is interest (I'm not pushing either within this series):
- psi.rst says nothing about the terminator while sysfs documents its (append, not clobber) behavior explicitly; a sentence in psi.rst would at least make the convention discoverable.
- kernfs and sysfs both append the NUL after the written data, so the user bytes survive. psi_write() could do the same with buf_size = min(nbytes, sizeof(buf) - 1) and buf[buf_size] = '\0'. Terminator-terminated writes keep their exact meaning, and a bare strlen() write would parse in full instead of losing its last digit. That would be the more principled fix, but it is a behavior change for unterminated writes, so it needs a call from the PSI maintainers.
Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c line 32
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; +int fd; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource); + fd =open (path,O_RDWR ); +if (fd <0 ) +ksft_perror (path);This outputs:
| #/proc/pressure/irq: No such file or directory (2)
| # SKIP/proc/pressure/irqunavailable
Quoted 4 lines
I.e. similar message is printed twice.
Since strace is a companion of cgroup selftests, I'd keep this helper silent.
Agreed, the helper is silent now. The duplicate was my own doing: I added the print in v5 on Suren's v4 request (it replaced a raw fprintf) without noticing the SKIP message right below it already carries the reason, so removing it satisfies both comments.
Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/cgroup/test_psi.c new file +21
-0+return fd; +} + +FIXTURE (psi) +{ +char root[PATH_MAX ]; +char *cg; +}; + +FIXTURE_SETUP (psi) +{ +int psi_fd; + +if (cg_find_unified_root (self->root,sizeof (self->root),NULL )) +SKIP (return,"cgroup v2 isn't mounted" ); + +/* PSI must be enabled (CONFIG_PSI=y, not disabled on the cmdline). */ + psi_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (psi_fd <0 ) +SKIP (return,"PSI unavailable (CONFIG_PSI=n or psi=0)" ); +close (psi_fd);
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 59
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +6
-0+ + self->cg =cg_name (self->root,"psi_trigger_test" ); +if (!self->cg) +SKIP (return,"failed to allocate cgroup name" ); +if (cg_create (self->cg)) +SKIP (return,"failed to create cgroup: %s" ,strerror (errno));Why are these two SKIPs (not failures)?
You're right, they are not environment problems. They are ASSERTs in FIXTURE_SETUP() now, so a run without privileges fails loudly instead of vanishing into skips.
Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c new file +16
-0+TEST_F(psi, cgroup_trigger_fire) +{ +char *cpupress; +struct pollfd pfd = { .events =POLLPRI }; +long ncpus; +int fd; +int i; + + cpupress =cg_control (self->cg,"cpu.pressure" ); +ASSERT_NE (NULL , cpupress); + fd =open (cpupress,O_RDWR ); +free (cpupress); +ASSERT_GE (fd,0 ); + pfd.fd = fd; + +/*
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 175
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+* 1usec threshold over a 2s window: any CPU stall fires it. The 2s +* window is the smallest unprivileged users are allowed to arm. +*/ +ASSERT_GT (write_trigger (fd,"some 1 2000000" ),0 );The selftest rarely can be run as unprivileged user (even test cgroup creation needs privileges), so this comment is irrelevant. (But it's fine to test with that value.)
Dropped.
Quoted 8 lines
On the more abstract level -- I was playing with this and thinking about a value that'd test both sides, i.e. false triggers as well as false non-triggers. I'd find that to be the half of the window and the number of tasks should be then (3*ncpus + 1) / 2.
Or perhaps test two thresholds, one tiny like you did and one maximum (whole window) with same amount tasks but expect trigger, no trigger respectively.
I tried both of your variants before picking one. The half-window threshold with (3*ncpus+1)/2 hogs fired reliably, and the two-threshold variant also held up on the negative side: with the threshold equal to the whole window, ncpus+1 hogs never fired it (measured cpu.pressure "some" stays around 2-17% under that oversubscription, so the margin is comfortable rather than borderline). I kept the existing tiny- threshold test and added cgroup_trigger_no_fire for the whole-window side, which matches your second variant and keeps the hog setup shared.
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 180
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus ==-1 ) +TH_LOG ("sysconf(_SC_NPROCESSORS_ONLN): %s" ,strerror (errno)); +ASSERT_NE (-1 , ncpus);Same as messages from
pressure_open()above. Simply assert.
Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c line 191
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +11
-0+ +/* ncpus+1 hogs guarantee CPU contention inside the cgroup. */ +for (i =0 ; i < ncpus +1 ; i++) +ASSERT_GE (cg_run_nowait (self->cg, hog_cpu,NULL ),0 ); + +ASSERT_EQ (1 ,poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )); +ASSERT_NE (0 , pfd.revents &POLLPRI ); +close (fd); +} + +TEST_HARNESS_MAIN All in all, this looks so much better than the initial version, well done. Just a few polishing touches.
All 7 tests pass on a 2-CPU and a 16-CPU machine.
Thank you both, this series learned a lot from you: Suren walked me through the harness conversion and the reporting details, and you kept the semantics honest, which is what surfaced the parser behavior. The test is in a much better shape than what I started with, and the parser finding above came out of your push on both directions of the trigger.
Thanks
Tao
Quoted 1 line
Michal
Quoted 41 lines · Tao Cui
Hello Michal, Suren,
Quoted 14 lines · Michal Koutný
Hello Tao.
tools/testing/selftests/cgroup/test_psi.c line 22
Quoted 5 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+/* PSI triggers are written with a trailing NUL the kernel parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger) +{ +return write (fd, trigger,strlen (trigger) +1 ); +}Hyrum's law. It all works for me: NUL, \n or just write(2) the exact length of the string.
For conventionality, I'd prefer the simple literals and plainstrlen()+
0. (I reckoncg_write()cannot be used because of FD access.)Your Hyrum's law point made me look at the parser, and I'm glad it did, because the behavior is more subtle than "all of them work".
psi_write()doesbuf[buf_size - 1] = '\0';i.e. it overwrites the last byte of whatever was written. With a plain strlen()-sized write that eats the last digit: "some 150000 2000000" silently arms a 200000us window when privileged, and fails with EINVAL for unprivileged users (200000 is not a multiple of the 2s minimum). I reproduced both on 7.0.0-28 here. I suspect your runs succeeded because a truncated window still makes a valid trigger for root, so nothing looked off.
I went with your \n variant instead: the newline gets clobbered, the payload stays intact, and it is the conventional procfile form. So the reliance on the undocumented NUL is gone, even though not quite via strlen()+0.
Two follow-ups this suggests, if there is interest (I'm not pushing either within this series):
Quoted 10 lines
-
psi.rstsays nothing about the terminator while sysfs documents its (append, not clobber) behavior explicitly; a sentence inpsi.rstwould at least make the convention discoverable.
- kernfs and sysfs both append the NUL after the written data, so the user bytes survive.psi_write()could do the same withbuf_size=min(nbytes, sizeof(buf) - 1)and buf[buf_size] = '\0'. Terminator-terminated writes keep their exact meaning, and a barestrlen()write would parse in full instead of losing its last digit. That would be the more principled fix, but it is a behavior change for unterminated writes, so it needs a call from the PSI maintainers.
Yeah, I was expecting a NUL-terminated string with the NUL included when we write, like shown in the psi.rst code example:
write(fds.fd, trig, strlen(trig) + 1)
but I agree that this might be unconventional.
I guess we could check if buf[buf_size - 1] is already '\0' then do nothing (keeps current users happy) and otherwise we append the NUL at the end. That way both write(fds.fd, trig, strlen(trig) + 1) and write(fds.fd, trig, strlen(trig) + 0) would work. Would you agree or did I miss anything?
Quoted 146 lines
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c line 32
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; +int fd; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource); + fd =open (path,O_RDWR ); +if (fd <0 ) +ksft_perror (path);This outputs:
| #/proc/pressure/irq: No such file or directory (2)
| # SKIP/proc/pressure/irqunavailableQuoted 4 lines
I.e. similar message is printed twice.
Since strace is a companion of cgroup selftests, I'd keep this helper silent.Agreed, the helper is silent now. The duplicate was my own doing: I added the print in v5 on Suren's v4 request (it replaced a raw fprintf) without noticing the SKIP message right below it already carries the reason, so removing it satisfies both comments.
Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/cgroup/test_psi.c new file +21
-0+return fd; +} + +FIXTURE (psi) +{ +char root[PATH_MAX ]; +char *cg; +}; + +FIXTURE_SETUP (psi) +{ +int psi_fd; + +if (cg_find_unified_root (self->root,sizeof (self->root),NULL )) +SKIP (return,"cgroup v2 isn't mounted" ); + +/* PSI must be enabled (CONFIG_PSI=y, not disabled on the cmdline). */ + psi_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (psi_fd <0 ) +SKIP (return,"PSI unavailable (CONFIG_PSI=n or psi=0)" ); +close (psi_fd);Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 59
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +6
-0+ + self->cg =cg_name (self->root,"psi_trigger_test" ); +if (!self->cg) +SKIP (return,"failed to allocate cgroup name" ); +if (cg_create (self->cg)) +SKIP (return,"failed to create cgroup: %s" ,strerror (errno));Why are these two SKIPs (not failures)?
You're right, they are not environment problems. They are ASSERTs in
FIXTURE_SETUP()now, so a run without privileges fails loudly instead of vanishing into skips.Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c new file +16
-0+TEST_F(psi, cgroup_trigger_fire) +{ +char *cpupress; +struct pollfd pfd = { .events =POLLPRI }; +long ncpus; +int fd; +int i; + + cpupress =cg_control (self->cg,"cpu.pressure" ); +ASSERT_NE (NULL , cpupress); + fd =open (cpupress,O_RDWR ); +free (cpupress); +ASSERT_GE (fd,0 ); + pfd.fd = fd; + +/* Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 175
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+* 1usec threshold over a 2s window: any CPU stall fires it. The 2s +* window is the smallest unprivileged users are allowed to arm. +*/ +ASSERT_GT (write_trigger (fd,"some 1 2000000" ),0 );The selftest rarely can be run as unprivileged user (even test cgroup creation needs privileges), so this comment is irrelevant. (But it's fine to test with that value.)
Dropped.
Quoted 8 lines
On the more abstract level -- I was playing with this and thinking about a value that'd test both sides, i.e. false triggers as well as false non-triggers. I'd find that to be the half of the window and the number of tasks should be then (3*ncpus + 1) / 2.
Or perhaps test two thresholds, one tiny like you did and one maximum (whole window) with same amount tasks but expect trigger, no trigger respectively.I tried both of your variants before picking one. The half-window threshold with (3*ncpus+1)/2 hogs fired reliably, and the two-threshold variant also held up on the negative side: with the threshold equal to the whole window, ncpus+1 hogs never fired it (measured cpu.pressure "some" stays around 2-17% under that oversubscription, so the margin is comfortable rather than borderline). I kept the existing tiny- threshold test and added
cgroup_trigger_no_firefor the whole-window side, which matches your second variant and keeps the hog setup shared.Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 180
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus ==-1 ) +TH_LOG ("sysconf(_SC_NPROCESSORS_ONLN): %s" ,strerror (errno)); +ASSERT_NE (-1 , ncpus);Same as messages from
pressure_open()above. Simply assert.Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c line 191
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +11
-0+ +/* ncpus+1 hogs guarantee CPU contention inside the cgroup. */ +for (i =0 ; i < ncpus +1 ; i++) +ASSERT_GE (cg_run_nowait (self->cg, hog_cpu,NULL ),0 ); + +ASSERT_EQ (1 ,poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )); +ASSERT_NE (0 , pfd.revents &POLLPRI ); +close (fd); +} + +TEST_HARNESS_MAIN All in all, this looks so much better than the initial version, well done. Just a few polishing touches.
All 7 tests pass on a 2-CPU and a 16-CPU machine.
Thank you both, this series learned a lot from you: Suren walked me through the harness conversion and the reporting details, and you kept the semantics honest, which is what surfaced the parser behavior. The test is in a much better shape than what I started with, and the parser finding above came out of your push on both directions of the trigger.
Thanks
TaoQuoted 1 line
Michal
Quoted 57 lines · Suren Baghdasaryan
Quoted 41 lines · Tao Cui
Hello Michal, Suren,
Quoted 14 lines · Michal Koutný
Hello Tao.
tools/testing/selftests/cgroup/test_psi.c line 22
Quoted 5 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+/* PSI triggers are written with a trailing NUL the kernel parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger) +{ +return write (fd, trigger,strlen (trigger) +1 ); +}Hyrum's law. It all works for me: NUL, \n or just write(2) the exact length of the string.
For conventionality, I'd prefer the simple literals and plainstrlen()+
0. (I reckoncg_write()cannot be used because of FD access.)Your Hyrum's law point made me look at the parser, and I'm glad it did, because the behavior is more subtle than "all of them work".
psi_write()doesbuf[buf_size - 1] = '\0';i.e. it overwrites the last byte of whatever was written. With a plain strlen()-sized write that eats the last digit: "some 150000 2000000" silently arms a 200000us window when privileged, and fails with EINVAL for unprivileged users (200000 is not a multiple of the 2s minimum). I reproduced both on 7.0.0-28 here. I suspect your runs succeeded because a truncated window still makes a valid trigger for root, so nothing looked off.
I went with your \n variant instead: the newline gets clobbered, the payload stays intact, and it is the conventional procfile form. So the reliance on the undocumented NUL is gone, even though not quite via strlen()+0.
Two follow-ups this suggests, if there is interest (I'm not pushing either within this series):
Quoted 10 lines
-
psi.rstsays nothing about the terminator while sysfs documents its (append, not clobber) behavior explicitly; a sentence inpsi.rstwould at least make the convention discoverable.
- kernfs and sysfs both append the NUL after the written data, so the user bytes survive.psi_write()could do the same withbuf_size=min(nbytes, sizeof(buf) - 1)and buf[buf_size] = '\0'. Terminator-terminated writes keep their exact meaning, and a barestrlen()write would parse in full instead of losing its last digit. That would be the more principled fix, but it is a behavior change for unterminated writes, so it needs a call from the PSI maintainers.Yeah, I was expecting a NUL-terminated string with the NUL included when we write, like shown in the
psi.rstcode example:
Quoted 8 lines
write(fds.fd, trig, strlen(trig) + 1)but I agree that this might be unconventional.
I guess we could check if buf[buf_size - 1] is already '\0' then do nothing (keeps current users happy) and otherwise we append the NUL at the end. That way bothwrite(fds.fd, trig, strlen(trig) + 1)andwrite(fds.fd, trig, strlen(trig) + 0)would work. Would you agree or did I miss anything?
Hmm. I'm looking into v6 version of your patch and you are adding '\n' at the end. So, my plan would not work for this case and I really don't want to handle these terminators as special cases...
Quoted 147 lines
Quoted 147 lines
Quoted 17 lines
tools/testing/selftests/cgroup/test_psi.c line 32
Quoted 12 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; +int fd; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource); + fd =open (path,O_RDWR ); +if (fd <0 ) +ksft_perror (path);This outputs:
| #/proc/pressure/irq: No such file or directory (2)
| # SKIP/proc/pressure/irqunavailableQuoted 4 lines
I.e. similar message is printed twice.
Since strace is a companion of cgroup selftests, I'd keep this helper silent.Agreed, the helper is silent now. The duplicate was my own doing: I added the print in v5 on Suren's v4 request (it replaced a raw fprintf) without noticing the SKIP message right below it already carries the reason, so removing it satisfies both comments.
Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/cgroup/test_psi.c new file +21
-0+return fd; +} + +FIXTURE (psi) +{ +char root[PATH_MAX ]; +char *cg; +}; + +FIXTURE_SETUP (psi) +{ +int psi_fd; + +if (cg_find_unified_root (self->root,sizeof (self->root),NULL )) +SKIP (return,"cgroup v2 isn't mounted" ); + +/* PSI must be enabled (CONFIG_PSI=y, not disabled on the cmdline). */ + psi_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (psi_fd <0 ) +SKIP (return,"PSI unavailable (CONFIG_PSI=n or psi=0)" ); +close (psi_fd);Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 59
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +6
-0+ + self->cg =cg_name (self->root,"psi_trigger_test" ); +if (!self->cg) +SKIP (return,"failed to allocate cgroup name" ); +if (cg_create (self->cg)) +SKIP (return,"failed to create cgroup: %s" ,strerror (errno));Why are these two SKIPs (not failures)?
You're right, they are not environment problems. They are ASSERTs in
FIXTURE_SETUP()now, so a run without privileges fails loudly instead of vanishing into skips.Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c new file +16
-0+TEST_F(psi, cgroup_trigger_fire) +{ +char *cpupress; +struct pollfd pfd = { .events =POLLPRI }; +long ncpus; +int fd; +int i; + + cpupress =cg_control (self->cg,"cpu.pressure" ); +ASSERT_NE (NULL , cpupress); + fd =open (cpupress,O_RDWR ); +free (cpupress); +ASSERT_GE (fd,0 ); + pfd.fd = fd; + +/* Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 175
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+* 1usec threshold over a 2s window: any CPU stall fires it. The 2s +* window is the smallest unprivileged users are allowed to arm. +*/ +ASSERT_GT (write_trigger (fd,"some 1 2000000" ),0 );The selftest rarely can be run as unprivileged user (even test cgroup creation needs privileges), so this comment is irrelevant. (But it's fine to test with that value.)
Dropped.
Quoted 8 lines
On the more abstract level -- I was playing with this and thinking about a value that'd test both sides, i.e. false triggers as well as false non-triggers. I'd find that to be the half of the window and the number of tasks should be then (3*ncpus + 1) / 2.
Or perhaps test two thresholds, one tiny like you did and one maximum (whole window) with same amount tasks but expect trigger, no trigger respectively.I tried both of your variants before picking one. The half-window threshold with (3*ncpus+1)/2 hogs fired reliably, and the two-threshold variant also held up on the negative side: with the threshold equal to the whole window, ncpus+1 hogs never fired it (measured cpu.pressure "some" stays around 2-17% under that oversubscription, so the margin is comfortable rather than borderline). I kept the existing tiny- threshold test and added
cgroup_trigger_no_firefor the whole-window side, which matches your second variant and keeps the hog setup shared.Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 180
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus ==-1 ) +TH_LOG ("sysconf(_SC_NPROCESSORS_ONLN): %s" ,strerror (errno)); +ASSERT_NE (-1 , ncpus);Same as messages from
pressure_open()above. Simply assert.Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c line 191
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +11
-0+ +/* ncpus+1 hogs guarantee CPU contention inside the cgroup. */ +for (i =0 ; i < ncpus +1 ; i++) +ASSERT_GE (cg_run_nowait (self->cg, hog_cpu,NULL ),0 ); + +ASSERT_EQ (1 ,poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )); +ASSERT_NE (0 , pfd.revents &POLLPRI ); +close (fd); +} + +TEST_HARNESS_MAIN All in all, this looks so much better than the initial version, well done. Just a few polishing touches.
All 7 tests pass on a 2-CPU and a 16-CPU machine.
Thank you both, this series learned a lot from you: Suren walked me through the harness conversion and the reporting details, and you kept the semantics honest, which is what surfaced the parser behavior. The test is in a much better shape than what I started with, and the parser finding above came out of your push on both directions of the trigger.
Thanks
TaoQuoted 1 line
Michal
Hi Suren,
Quoted 67 lines · Suren Baghdasaryan
Quoted 57 lines · Suren Baghdasaryan
Quoted 41 lines · Tao Cui
Hello Michal, Suren,
Quoted 14 lines · Michal Koutný
Hello Tao.
tools/testing/selftests/cgroup/test_psi.c line 22
Quoted 5 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+/* PSI triggers are written with a trailing NUL the kernel parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger) +{ +return write (fd, trigger,strlen (trigger) +1 ); +}Hyrum's law. It all works for me: NUL, \n or just write(2) the exact length of the string.
For conventionality, I'd prefer the simple literals and plainstrlen()+
0. (I reckoncg_write()cannot be used because of FD access.)Your Hyrum's law point made me look at the parser, and I'm glad it did, because the behavior is more subtle than "all of them work".
psi_write()doesbuf[buf_size - 1] = '\0';i.e. it overwrites the last byte of whatever was written. With a plain strlen()-sized write that eats the last digit: "some 150000 2000000" silently arms a 200000us window when privileged, and fails with EINVAL for unprivileged users (200000 is not a multiple of the 2s minimum). I reproduced both on 7.0.0-28 here. I suspect your runs succeeded because a truncated window still makes a valid trigger for root, so nothing looked off.
I went with your \n variant instead: the newline gets clobbered, the payload stays intact, and it is the conventional procfile form. So the reliance on the undocumented NUL is gone, even though not quite via strlen()+0.
Two follow-ups this suggests, if there is interest (I'm not pushing either within this series):
Quoted 10 lines
-
psi.rstsays nothing about the terminator while sysfs documents its (append, not clobber) behavior explicitly; a sentence inpsi.rstwould at least make the convention discoverable.
- kernfs and sysfs both append the NUL after the written data, so the user bytes survive.psi_write()could do the same withbuf_size=min(nbytes, sizeof(buf) - 1)and buf[buf_size] = '\0'. Terminator-terminated writes keep their exact meaning, and a barestrlen()write would parse in full instead of losing its last digit. That would be the more principled fix, but it is a behavior change for unterminated writes, so it needs a call from the PSI maintainers.Yeah, I was expecting a NUL-terminated string with the NUL included when we write, like shown in the
psi.rstcode example:Quoted 8 lines
write(fds.fd, trig, strlen(trig) + 1)but I agree that this might be unconventional.
I guess we could check if buf[buf_size - 1] is already '\0' then do nothing (keeps current users happy) and otherwise we append the NUL at the end. That way bothwrite(fds.fd, trig, strlen(trig) + 1)andwrite(fds.fd, trig, strlen(trig) + 0)would work. Would you agree or did I miss anything?
Quoted 4 lines
Hmm. I'm looking into v6 version of your patch and you are adding '\n' at the end. So, my plan would not work for this case and I really don't want to handle these terminators as special cases...
Understood -- the '\n' is not something I'm attached to.
The issue I was trying to point out is really the truncation of unterminated writes, not that '\n' itself should be treated specially. If supporting only the documented NUL-terminated form keeps the parser simpler, I'm fine with that as well.
Thanks again for all the reviews. They made this test much better than what I started with.
Thanks,
Tao
Quoted 147 lines
Quoted 147 lines
Quoted 147 lines
Quoted 17 lines
tools/testing/selftests/cgroup/test_psi.c line 32
Quoted 12 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; +int fd; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource); + fd =open (path,O_RDWR ); +if (fd <0 ) +ksft_perror (path);This outputs:
| #/proc/pressure/irq: No such file or directory (2)
| # SKIP/proc/pressure/irqunavailableQuoted 4 lines
I.e. similar message is printed twice.
Since strace is a companion of cgroup selftests, I'd keep this helper silent.Agreed, the helper is silent now. The duplicate was my own doing: I added the print in v5 on Suren's v4 request (it replaced a raw fprintf) without noticing the SKIP message right below it already carries the reason, so removing it satisfies both comments.
Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/cgroup/test_psi.c new file +21
-0+return fd; +} + +FIXTURE (psi) +{ +char root[PATH_MAX ]; +char *cg; +}; + +FIXTURE_SETUP (psi) +{ +int psi_fd; + +if (cg_find_unified_root (self->root,sizeof (self->root),NULL )) +SKIP (return,"cgroup v2 isn't mounted" ); + +/* PSI must be enabled (CONFIG_PSI=y, not disabled on the cmdline). */ + psi_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (psi_fd <0 ) +SKIP (return,"PSI unavailable (CONFIG_PSI=n or psi=0)" ); +close (psi_fd);Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 59
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +6
-0+ + self->cg =cg_name (self->root,"psi_trigger_test" ); +if (!self->cg) +SKIP (return,"failed to allocate cgroup name" ); +if (cg_create (self->cg)) +SKIP (return,"failed to create cgroup: %s" ,strerror (errno));Why are these two SKIPs (not failures)?
You're right, they are not environment problems. They are ASSERTs in
FIXTURE_SETUP()now, so a run without privileges fails loudly instead of vanishing into skips.Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c new file +16
-0+TEST_F(psi, cgroup_trigger_fire) +{ +char *cpupress; +struct pollfd pfd = { .events =POLLPRI }; +long ncpus; +int fd; +int i; + + cpupress =cg_control (self->cg,"cpu.pressure" ); +ASSERT_NE (NULL , cpupress); + fd =open (cpupress,O_RDWR ); +free (cpupress); +ASSERT_GE (fd,0 ); + pfd.fd = fd; + +/* Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 175
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+* 1usec threshold over a 2s window: any CPU stall fires it. The 2s +* window is the smallest unprivileged users are allowed to arm. +*/ +ASSERT_GT (write_trigger (fd,"some 1 2000000" ),0 );The selftest rarely can be run as unprivileged user (even test cgroup creation needs privileges), so this comment is irrelevant. (But it's fine to test with that value.)
Dropped.
Quoted 8 lines
On the more abstract level -- I was playing with this and thinking about a value that'd test both sides, i.e. false triggers as well as false non-triggers. I'd find that to be the half of the window and the number of tasks should be then (3*ncpus + 1) / 2.
Or perhaps test two thresholds, one tiny like you did and one maximum (whole window) with same amount tasks but expect trigger, no trigger respectively.I tried both of your variants before picking one. The half-window threshold with (3*ncpus+1)/2 hogs fired reliably, and the two-threshold variant also held up on the negative side: with the threshold equal to the whole window, ncpus+1 hogs never fired it (measured cpu.pressure "some" stays around 2-17% under that oversubscription, so the margin is comfortable rather than borderline). I kept the existing tiny- threshold test and added
cgroup_trigger_no_firefor the whole-window side, which matches your second variant and keeps the hog setup shared.Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 180
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus ==-1 ) +TH_LOG ("sysconf(_SC_NPROCESSORS_ONLN): %s" ,strerror (errno)); +ASSERT_NE (-1 , ncpus);Same as messages from
pressure_open()above. Simply assert.Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c line 191
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +11
-0+ +/* ncpus+1 hogs guarantee CPU contention inside the cgroup. */ +for (i =0 ; i < ncpus +1 ; i++) +ASSERT_GE (cg_run_nowait (self->cg, hog_cpu,NULL ),0 ); + +ASSERT_EQ (1 ,poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )); +ASSERT_NE (0 , pfd.revents &POLLPRI ); +close (fd); +} + +TEST_HARNESS_MAIN All in all, this looks so much better than the initial version, well done. Just a few polishing touches.
All 7 tests pass on a 2-CPU and a 16-CPU machine.
Thank you both, this series learned a lot from you: Suren walked me through the harness conversion and the reporting details, and you kept the semantics honest, which is what surfaced the parser behavior. The test is in a much better shape than what I started with, and the parser finding above came out of your push on both directions of the trigger.
Thanks
TaoQuoted 1 line
Michal