selftests/cgroup: add PSI pressure trigger and validation tests
PATCH v2 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: trigger smoke tests (one per fd, IRQ full-only), cgroup.pressure toggle, and a CPU-pressure trigger test using over-subscription. Skips when PSI is disabled or a resource is absent.
---
Changes since v1 (Michal Koutny, sashiko review):
- Trim errno checks to smoke level; loosen toggle range checks.
- Switch trigger test from memory to CPU pressure (deterministic, no SKIP).
- churn_memory() removed -- sysconf/shared-helper point is moot.
- Runner stays out of the cgroup; hogs reaped via cg_killall().
- Add PSI/IRQ skip-guards, zero-init buffers, .gitignore entry.
--- 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 | 280 ++++++++++++++++++++++ 4 files changed, 284 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 +280 -0
280 lines not on this page · raw message
Signature · 1 line
2.43.0
Michal Koutný
maintainerHi,
thanks for continuing with this.
tools/testing/selftests/cgroup/test_psi.c line 41
Quoted 9 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c new file +9
-0+/* Trigger smoke test: second on same fd -> EBUSY; IRQ rejects "some". */ +static int test_proc_triggers (const char *root) +{ +static const char *const resources[] = {"io" ,"memory" ,"cpu" }; +int ret =KSFT_FAIL ; +int fd =-1 ; +int i; + + (void )root;
WTH?
Review
Quoted 2 lines
(quoted diff) +2
-0+ + for (i = 0; i < (int)ARRAY_SIZE(resources); i++) {
This (int) cast is quite uncommon, look around.
Quoted 20 lines
tools/testing/selftests/cgroup/test_psi.c new file +20
-0+/* Induce deterministic CPU pressure (more hogs than CPUs). */ +static int test_cgroup_trigger_fire (const char *root) +{ +char *cg =NULL , *cpupress =NULL ; +int fd =-1 , ret =KSFT_FAIL ; +struct pollfd pfd; +long ncpus, i; +pid_t pid; + + cg =cg_name (root,"psi_trigger_test" ); +if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup; + + cpupress =cg_control (cg,"cpu.pressure" ); +if (!cpupress) + goto cleanup; + fd =open (cpupress,O_RDWR ); +if (fd <0 ) {
tools/testing/selftests/cgroup/test_psi.c line 165
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ksft_print_msg ("open cpu.pressure: %s\n" ,strerror (errno)); + goto cleanup; + } + +/* 1us threshold in a 1s window: any cpu stall fires it. */ + errno =0 ; +if (write_trigger (fd,"some 1 1000000" ) <=0 ) { +ksft_print_msg ("arming trigger failed: %s\n" ,strerror (errno)); + goto cleanup; + }
When I see this, could you increase the window size to 2000000 (so that it has potential to run for unprivileged users)?
Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c new file +15
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus <=0 ) + ncpus =1 ; + + pid =fork (); +if (pid <0 ) { +ksft_print_msg ("fork: %s\n" ,strerror (errno)); + goto cleanup; + } +if (pid ==0 ) { +/* Enter the cgroup, then over-subscribe it with CPU hogs. */ +if (cg_enter_current (cg)) +_exit (KSFT_FAIL ); +for (i =0 ; i < ncpus; i++) {
tools/testing/selftests/cgroup/test_psi.c line 190
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+if (fork () ==0 ) { +for (;;) + asmvolatile ("" :::"memory" ); +_exit (0 ); + } + } +for (;;) + asmvolatile ("" :::"memory" );/* child is also a hog */ +_exit (0 ); + }
This part could be replaced with generalized hog_cpus_timed() from test_cpu.c (after move to cgroup_util.c).
tools/testing/selftests/cgroup/test_psi.c line 245
Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7
-0+int main (int argc,char **argv) +{ +char root[PATH_MAX ]; +int mempress_fd; +int i; + + (void )argc;
What's up with this?
Quoted 8 lines
tools/testing/selftests/cgroup/test_psi.c new file +8
-0+ +ksft_print_header (); +ksft_set_plan (ARRAY_SIZE (tests)); + +if (cg_find_unified_root (root,sizeof (root),NULL )) +ksft_exit_skip ("cgroup v2 isn't mounted\n" ); + +/* PSI must be enabled (CONFIG_PSI=y, not default-disabled). */
tools/testing/selftests/cgroup/test_psi.c line 263
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ mempress_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (mempress_fd <0 ) +ksft_exit_skip ("PSI unavailable (CONFIG_PSI=n or psi=0)\n" ); +close (mempress_fd); + +if (cg_read_strstr (root,"cgroup.controllers" ,"memory" )) +ksft_exit_skip ("memory controller isn't available\n" ); +if (cg_read_strstr (root,"cgroup.subtree_control" ,"memory" )) +if (cg_write (root,"cgroup.subtree_control" ,"+memory" )) +ksft_exit_skip ("failed to enable memory controller\n" );
The memory controller is unnecessary now, right?
Regards,
Michal
Quoted 3 lines · Michal Koutný
Hi,
thanks for continuing with this.
Thanks for adding these tests and sorry I was not able to review them yet. I'll try to review them this weekend and will provide my feedback.
Thanks,
Suren.
Quoted 116 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c line 41
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c new file +9
-0+/* Trigger smoke test: second on same fd -> EBUSY; IRQ rejects "some". */ +static int test_proc_triggers (const char *root) +{ +static const char *const resources[] = {"io" ,"memory" ,"cpu" }; +int ret =KSFT_FAIL ; +int fd =-1 ; +int i; + + (void )root;WTH?
Review
Quoted 2 lines
(quoted diff) +2
-0+ + for (i = 0; i < (int)ARRAY_SIZE(resources); i++) {This (int) cast is quite uncommon, look around.
Quoted 20 lines
tools/testing/selftests/cgroup/test_psi.c new file +20
-0+/* Induce deterministic CPU pressure (more hogs than CPUs). */ +static int test_cgroup_trigger_fire (const char *root) +{ +char *cg =NULL , *cpupress =NULL ; +int fd =-1 , ret =KSFT_FAIL ; +struct pollfd pfd; +long ncpus, i; +pid_t pid; + + cg =cg_name (root,"psi_trigger_test" ); +if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup; + + cpupress =cg_control (cg,"cpu.pressure" ); +if (!cpupress) + goto cleanup; + fd =open (cpupress,O_RDWR ); +if (fd <0 ) {tools/testing/selftests/cgroup/test_psi.c line 165
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ksft_print_msg ("open cpu.pressure: %s\n" ,strerror (errno)); + goto cleanup; + } + +/* 1us threshold in a 1s window: any cpu stall fires it. */ + errno =0 ; +if (write_trigger (fd,"some 1 1000000" ) <=0 ) { +ksft_print_msg ("arming trigger failed: %s\n" ,strerror (errno)); + goto cleanup; + }When I see this, could you increase the window size to 2000000 (so that it has potential to run for unprivileged users)?
Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c new file +15
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus <=0 ) + ncpus =1 ; + + pid =fork (); +if (pid <0 ) { +ksft_print_msg ("fork: %s\n" ,strerror (errno)); + goto cleanup; + } +if (pid ==0 ) { +/* Enter the cgroup, then over-subscribe it with CPU hogs. */ +if (cg_enter_current (cg)) +_exit (KSFT_FAIL ); +for (i =0 ; i < ncpus; i++) {tools/testing/selftests/cgroup/test_psi.c line 190
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+if (fork () ==0 ) { +for (;;) + asmvolatile ("" :::"memory" ); +_exit (0 ); + } + } +for (;;) + asmvolatile ("" :::"memory" );/* child is also a hog */ +_exit (0 ); + }This part could be replaced with generalized
hog_cpus_timed()fromtest_cpu.c(after move tocgroup_util.c).tools/testing/selftests/cgroup/test_psi.c line 245
Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7
-0+int main (int argc,char **argv) +{ +char root[PATH_MAX ]; +int mempress_fd; +int i; + + (void )argc;What's up with this?
Quoted 8 lines
tools/testing/selftests/cgroup/test_psi.c new file +8
-0+ +ksft_print_header (); +ksft_set_plan (ARRAY_SIZE (tests)); + +if (cg_find_unified_root (root,sizeof (root),NULL )) +ksft_exit_skip ("cgroup v2 isn't mounted\n" ); + +/* PSI must be enabled (CONFIG_PSI=y, not default-disabled). */ tools/testing/selftests/cgroup/test_psi.c line 263
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ mempress_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (mempress_fd <0 ) +ksft_exit_skip ("PSI unavailable (CONFIG_PSI=n or psi=0)\n" ); +close (mempress_fd); + +if (cg_read_strstr (root,"cgroup.controllers" ,"memory" )) +ksft_exit_skip ("memory controller isn't available\n" ); +if (cg_read_strstr (root,"cgroup.subtree_control" ,"memory" )) +if (cg_write (root,"cgroup.subtree_control" ,"+memory" )) +ksft_exit_skip ("failed to enable memory controller\n" );The memory controller is unnecessary now, right?
Regards,
Michal
Hi Michal,
Quoted 3 lines · Michal Koutný
Hi,
thanks for continuing with this.
Thanks -- though honestly I owe you the thanks. Your v1 review is what got me to drop the memory-pressure approach and churn_memory(), and to keep the runner out of the cgroup. v2 basically exists because you took the time on v1. Still only my second selftests patch, so still feeling my way around the conventions here (clearly -- see below).
Quoted 13 lines · Tao Cui
tools/testing/selftests/cgroup/test_psi.c line 41
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c new file +9
-0+/* Trigger smoke test: second on same fd -> EBUSY; IRQ rejects "some". */ +static int test_proc_triggers (const char *root) +{ +static const char *const resources[] = {"io" ,"memory" ,"cpu" }; +int ret =KSFT_FAIL ; +int fd =-1 ; +int i; + + (void )root;WTH?
Yeah, bogus. Dropping the param entirely; details in my reply to Suren.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + for (i = 0; i < (int)ARRAY_SIZE(resources); i++) {This (int) cast is quite uncommon, look around.
Dropping it, size_t loop var.
Quoted 25 lines
Quoted 25 lines
tools/testing/selftests/cgroup/test_psi.c new file +25
-0+/* Induce deterministic CPU pressure (more hogs than CPUs). */ +static int test_cgroup_trigger_fire (const char *root) +{ +char *cg =NULL , *cpupress =NULL ; +int fd =-1 , ret =KSFT_FAIL ; +struct pollfd pfd; +long ncpus, i; +pid_t pid; + + cg =cg_name (root,"psi_trigger_test" ); +if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup; + + cpupress =cg_control (cg,"cpu.pressure" ); +if (!cpupress) + goto cleanup; + fd =open (cpupress,O_RDWR ); +if (fd <0 ) { +ksft_print_msg ("open cpu.pressure: %s\n" ,strerror (errno)); + goto cleanup; + } + +/* 1us threshold in a 1s window: any cpu stall fires it. */
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 165
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ errno =0 ; +if (write_trigger (fd,"some 1 1000000" ) <=0 ) { +ksft_print_msg ("arming trigger failed: %s\n" ,strerror (errno)); + goto cleanup; + }When I see this, could you increase the window size to 2000000 (so that it has potential to run for unprivileged users)?
Yes, "some 1 2000000".
Quoted 20 lines
Quoted 20 lines
tools/testing/selftests/cgroup/test_psi.c new file +20
-0+ + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus <=0 ) + ncpus =1 ; + + pid =fork (); +if (pid <0 ) { +ksft_print_msg ("fork: %s\n" ,strerror (errno)); + goto cleanup; + } +if (pid ==0 ) { +/* Enter the cgroup, then over-subscribe it with CPU hogs. */ +if (cg_enter_current (cg)) +_exit (KSFT_FAIL ); +for (i =0 ; i < ncpus; i++) { +if (fork () ==0 ) { +for (;;) + asmvolatile ("" :::"memory" ); +_exit (0 ); + }
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 190
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ } +for (;;) + asmvolatile ("" :::"memory" );/* child is also a hog */ +_exit (0 ); + }This part could be replaced with generalized
hog_cpus_timed()fromtest_cpu.c(after move tocgroup_util.c).
Agreed it shouldn't be open-coded. One snag though: hog_cpus_timed() runs for a fixed duration and returns, but here I want hogs that keep running until the trigger fires and then get killed. So I'll move a shared helper into cgroup_util.[ch] and call it from both spots, rather than using hog_cpus_timed() as-is.
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c line 245
Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7
-0+int main (int argc,char **argv) +{ +char root[PATH_MAX ]; +int mempress_fd; +int i; + + (void )argc;What's up with this?
Leftover, removed.
Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/cgroup/test_psi.c new file +12
-0+ +ksft_print_header (); +ksft_set_plan (ARRAY_SIZE (tests)); + +if (cg_find_unified_root (root,sizeof (root),NULL )) +ksft_exit_skip ("cgroup v2 isn't mounted\n" ); + +/* PSI must be enabled (CONFIG_PSI=y, not default-disabled). */ + mempress_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (mempress_fd <0 ) +ksft_exit_skip ("PSI unavailable (CONFIG_PSI=n or psi=0)\n" ); +close (mempress_fd);
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 263
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +6
-0+ +if (cg_read_strstr (root,"cgroup.controllers" ,"memory" )) +ksft_exit_skip ("memory controller isn't available\n" ); +if (cg_read_strstr (root,"cgroup.subtree_control" ,"memory" )) +if (cg_write (root,"cgroup.subtree_control" ,"+memory" )) +ksft_exit_skip ("failed to enable memory controller\n" );The memory controller is unnecessary now, right?
Right. v1 leftover, CPU pressure now, so that setup is dead code. Dropping it.
I'll fold all of this into v3.
Thanks,
Tao
Quoted 3 lines
Regards,
Michal
Quoted 91 lines · Tao Cui
From: Tao Cui <[email protected]>
The cgroup selftests have no PSI coverage. Add
test_psi.c: trigger smoke tests (one per fd, IRQ full-only), cgroup.pressure toggle, and a CPU-pressure trigger test using over-subscription. Skips when PSI is disabled or a resource is absent.Signed-off-by Tao Cui <[email protected]>---
Changes since v1 (Michal Koutny, sashiko review):
- Trim errno checks to smoke level; loosen toggle range checks.
- Switch trigger test from memory to CPU pressure (deterministic, no SKIP).
-churn_memory()removed -- sysconf/shared-helper point is moot.
- Runner stays out of the cgroup; hogs reaped viacg_killall().
- Add PSI/IRQ skip-guards, zero-init buffers, .gitignore entry.--- 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 | 280 ++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_psi.ctools/testing/selftests/cgroup/.gitignore +1
-0@@ -8,5 +8,6 @@ test_kill test_kmem test_memcontrol test_pids +test_psi test_zswap wait_inotifytools/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=ytools/testing/selftests/cgroup/test_psi.c new file +23
-0@@ -0,0 +1,280 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <poll.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/wait.h> +#include <unistd.h> +#include <linux/limits.h> + +#include "kselftest.h" +#include "cgroup_util.h" + +/* How long to wait for the CPU-pressure trigger before giving up. */ +#define PSI_POLL_TIMEOUT_MS 5000 + +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource);
tools/testing/selftests/cgroup/test_psi.c line 33
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+return open (path,O_RDWR ); +} + +/* Write a trigger descriptor, including the trailing NUL the parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger) +{ +return write (fd, trigger,strlen (trigger) +1 ); +} + +/* Trigger smoke test: second on same fd -> EBUSY; IRQ rejects "some". */
Please write more descriptive comments for your tests. I have to guess what you mean by this.
tools/testing/selftests/cgroup/test_psi.c line 34
Quoted 1 line
tools/testing/selftests/cgroup/test_psi.c new file +1
-0+static int test_proc_triggers (const char *root)
Why do you pass root here if you are not using it? AI glitch?
tools/testing/selftests/cgroup/test_psi.c line 47
Quoted 13 lines
tools/testing/selftests/cgroup/test_psi.c new file +13
-0+{ +static const char *const resources[] = {"io" ,"memory" ,"cpu" }; +int ret =KSFT_FAIL ; +int fd =-1 ; +int i; + + (void )root; + +for (i =0 ; i < (int )ARRAY_SIZE (resources); i++) { + fd =pressure_open (resources[i]); +if (fd <0 ) { +ksft_print_msg ("open /proc/pressure/%s: %s\n" , + resources[i],strerror (errno));
You could move these ksft_print_msg() into pressure_open() and make that function a bit more useful while making the code here simpler.
tools/testing/selftests/cgroup/test_psi.c line 51
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+ goto cleanup; + } + + errno =0 ;
Why are you resetting the errno? If the next syscall fails, it will reset the errno and if it succeeds, you won't be using errno anyway. I'm confused.
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+if (write_trigger (fd,"some 150000 2000000" ) <=0 ) { +ksft_print_msg ("%s: 'some' trigger rejected: %s\n" , + resources[i],strerror (errno)); + goto cleanup; + }
tools/testing/selftests/cgroup/test_psi.c line 66
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+/* A second trigger on the same fd must fail with EBUSY. */ + errno =0 ; +if (write_trigger (fd,"full 150000 2000000" ) !=-1 || errno !=EBUSY ) { +ksft_print_msg ("%s: second trigger expected EBUSY, got %s\n" , + resources[i],strerror (errno)); + goto cleanup; + } + +close (fd); + fd =-1 ;
When you jump to cleanup label you always need to close the fd. You could simplify the flow if you remove all these "fd = -1;" and change the end of the function to be:
return KSFT_PASS
cleanup:
close(fd);
return ret;
}tools/testing/selftests/cgroup/test_psi.c line 69
Quoted 3 lines
tools/testing/selftests/cgroup/test_psi.c new file +3
-0+ } + +/* IRQ is full-only, and only exists with CONFIG_IRQ_TIME_ACCOUNTING. */
It would be better if you split this test into separate tests for "io", "memory", "cpu" and "irq" and if "irq" is not available you can return KSFT_SKIP for that test only. This way when a test fails the user will know exactly what failed.
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ fd =pressure_open ("irq" ); +if (fd >=0 ) { + errno =0 ; +if (write_trigger (fd,"some 150000 1000000" ) !=-1 ) { +ksft_print_msg ("irq 'some': expected failure, got %s\n" , +strerror (errno)); + goto cleanup; + } +close (fd); + fd =-1 ;
tools/testing/selftests/cgroup/test_psi.c line 89
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ } + + ret =KSFT_PASS ; +cleanup: +if (fd >=0 ) +close (fd); +return ret; +} + +/* cgroup.pressure 0/1 hides/shows the *.pressure files and round-trips. */
The above comment needs to be expanded to explain what you are testing. It does not read as a proper English sentence.
tools/testing/selftests/cgroup/test_psi.c line 100
Quoted 11 lines
tools/testing/selftests/cgroup/test_psi.c new file +11
-0+static int test_cgroup_pressure_toggle (const char *root) +{ +char buf[BUF_SIZE ] = {0 }; +char *cg =NULL ; +int ret =KSFT_FAIL ; + + cg =cg_name (root,"psi_toggle_test" ); +if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup;
Jumping to "cleanup" above results in a call to cg_destroy() while cg_create() failed. It will try to rmdir() a directory which we never created.
tools/testing/selftests/cgroup/test_psi.c line 103
Quoted 3 lines
tools/testing/selftests/cgroup/test_psi.c new file +3
-0+ +if (cg_write (cg,"cgroup.pressure" ,"0" )) { +ksft_print_msg ("failed to disable cgroup.pressure\n" );
Reporting strerror() in these failure logs would be useful.
tools/testing/selftests/cgroup/test_psi.c line 106
Quoted 3 lines
tools/testing/selftests/cgroup/test_psi.c new file +3
-0+ goto cleanup; + } +if (cg_read (cg,"cgroup.pressure" , buf,sizeof (buf)) <0 ||atoi (buf) !=0 ) {
atoi() will return 0 even on error, so if you want to really check the value is 0 use a more robust method like strtol() or simply do a string comparison.
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c new file +16
-0+ksft_print_msg ("cgroup.pressure=0 readback: '%s'\n" , buf); + goto cleanup; + } +if (cg_read (cg,"memory.pressure" , buf,sizeof (buf)) >=0 ) { +ksft_print_msg ("memory.pressure visible while disabled\n" ); + goto cleanup; + } + +if (cg_write (cg,"cgroup.pressure" ,"1" )) { +ksft_print_msg ("failed to re-enable cgroup.pressure\n" ); + goto cleanup; + } +if (cg_read (cg,"cgroup.pressure" , buf,sizeof (buf)) <0 ||atoi (buf) !=1 ) { +ksft_print_msg ("cgroup.pressure=1 readback: '%s'\n" , buf); + goto cleanup; + }
tools/testing/selftests/cgroup/test_psi.c line 132
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+if (cg_read (cg,"memory.pressure" , buf,sizeof (buf)) <0 ) { +ksft_print_msg ("memory.pressure unreadable while enabled\n" ); + goto cleanup; + } + + ret =KSFT_PASS ; +cleanup: +if (cg) +cg_destroy (cg); +free (cg);
free(NULL) works but would be cleaner to do this instead:
if (cg) {
cg_destroy(cg);
free(cg);
}Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7
-0+return ret; +} + +/* Induce deterministic CPU pressure (more hogs than CPUs). */ +static int test_cgroup_trigger_fire (const char *root) +{ +char *cg =NULL , *cpupress =NULL ;
tools/testing/selftests/cgroup/test_psi.c line 149
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+int fd =-1 , ret =KSFT_FAIL ; +struct pollfd pfd; +long ncpus, i; +pid_t pid; + + cg =cg_name (root,"psi_trigger_test" ); +if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup;
Same issue with this jump. You will be calling cg_killall(cg) and cg_destroy(cg) even though cg_create() did not succeed.
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c new file +9
-0+ + cpupress =cg_control (cg,"cpu.pressure" ); +if (!cpupress) + goto cleanup; + fd =open (cpupress,O_RDWR ); +if (fd <0 ) { +ksft_print_msg ("open cpu.pressure: %s\n" ,strerror (errno)); + goto cleanup; + }
tools/testing/selftests/cgroup/test_psi.c line 168
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +/* 1us threshold in a 1s window: any cpu stall fires it. */ + errno =0 ; +if (write_trigger (fd,"some 1 1000000" ) <=0 ) { +ksft_print_msg ("arming trigger failed: %s\n" ,strerror (errno)); + goto cleanup; + } + + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus <=0 )
Why is this not treated as a test failure?
Quoted 58 lines
tools/testing/selftests/cgroup/test_psi.c new file +58
-0+ ncpus =1 ; + + pid =fork (); +if (pid <0 ) { +ksft_print_msg ("fork: %s\n" ,strerror (errno)); + goto cleanup; + } +if (pid ==0 ) { +/* Enter the cgroup, then over-subscribe it with CPU hogs. */ +if (cg_enter_current (cg)) +_exit (KSFT_FAIL ); +for (i =0 ; i < ncpus; i++) { +if (fork () ==0 ) { +for (;;) + asmvolatile ("" :::"memory" ); +_exit (0 ); + } + } +for (;;) + asmvolatile ("" :::"memory" );/* child is also a hog */ +_exit (0 ); + } + + pfd.fd = fd; + pfd.events =POLLPRI ; +switch (poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )) { +case -1 : +ksft_print_msg ("poll: %s\n" ,strerror (errno)); +break ; +case 0 : +ksft_print_msg ("no trigger event; could not induce cpu pressure\n" ); + ret =KSFT_SKIP ; +break ; +default : +if (pfd.revents &POLLPRI ) + ret =KSFT_PASS ; +else +ksft_print_msg ("poll returned 0x%x\n" , pfd.revents); +break ; + } + +/* Stop the hogs (bounded, so waitpid can't hang) and reap the child. */ +cg_killall (cg); +waitpid (pid,NULL ,0 ); + +cleanup: +if (fd >=0 ) +close (fd); +if (cg) { +cg_killall (cg); +cg_destroy (cg); + } +free (cpupress); +free (cg); +return ret; +} + +#define TEST (x) { #x, x }
tools/testing/selftests/cgroup/test_psi.c line 236
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+ +struct psi_test { +const char *name; +int (*fn)(const char *root); +}; + +static struct psi_test tests[] = { +TEST (test_proc_triggers), +TEST (test_cgroup_pressure_toggle), +TEST (test_cgroup_trigger_fire),
Instead of setting up these tests manually you could use TEST_HARNESS_MAIN, TEST_F and other helpers from kselftest_harness.h.
Quoted 47 lines
tools/testing/selftests/cgroup/test_psi.c new file +44
-1+}; + +int main (int argc,char **argv) +{ +char root[PATH_MAX ]; +int mempress_fd; +int i; + + (void )argc; + +ksft_print_header (); +ksft_set_plan (ARRAY_SIZE (tests)); + +if (cg_find_unified_root (root,sizeof (root),NULL )) +ksft_exit_skip ("cgroup v2 isn't mounted\n" ); + +/* PSI must be enabled (CONFIG_PSI=y, not default-disabled). */ + mempress_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (mempress_fd <0 ) +ksft_exit_skip ("PSI unavailable (CONFIG_PSI=n or psi=0)\n" ); +close (mempress_fd); + +if (cg_read_strstr (root,"cgroup.controllers" ,"memory" )) +ksft_exit_skip ("memory controller isn't available\n" ); +if (cg_read_strstr (root,"cgroup.subtree_control" ,"memory" )) +if (cg_write (root,"cgroup.subtree_control" ,"+memory" )) +ksft_exit_skip ("failed to enable memory controller\n" ); + +for (i =0 ; i < (int )ARRAY_SIZE (tests); i++) { +switch (tests[i].fn (root)) { +case KSFT_PASS : +ksft_test_result_pass ("%s\n" , tests[i].name); +break ; +case KSFT_SKIP : +ksft_test_result_skip ("%s\n" , tests[i].name); +break ; +default : +ksft_test_result_fail ("%s\n" , tests[i].name); +break ; + } + } + +ksft_finished (); +} --2.43.0
Hi Suren,
Thanks for the review.
Yeah, you called the "AI glitch" correctly. This is only my second selftests patch and I ran it through an AI assistant for "polishing" before sending -- that's where the (void)root, the errno = 0 lines, and the (int)ARRAY_SIZE cast came from. None of it belongs here and I should've caught it before sending.
Quoted 97 lines · Suren Baghdasaryan
Quoted 96 lines · Tao Cui
From: Tao Cui <[email protected]>
The cgroup selftests have no PSI coverage. Add
test_psi.c: trigger smoke tests (one per fd, IRQ full-only), cgroup.pressure toggle, and a CPU-pressure trigger test using over-subscription. Skips when PSI is disabled or a resource is absent.Signed-off-by Tao Cui <[email protected]>---
Changes since v1 (Michal Koutny, sashiko review):
- Trim errno checks to smoke level; loosen toggle range checks.
- Switch trigger test from memory to CPU pressure (deterministic, no SKIP).
-churn_memory()removed -- sysconf/shared-helper point is moot.
- Runner stays out of the cgroup; hogs reaped viacg_killall().
- Add PSI/IRQ skip-guards, zero-init buffers, .gitignore entry.--- 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 | 280 ++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_psi.ctools/testing/selftests/cgroup/.gitignore +1
-0@@ -8,5 +8,6 @@ test_kill test_kmem test_memcontrol test_pids +test_psi test_zswap wait_inotifytools/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=ytools/testing/selftests/cgroup/test_psi.c new file +28
-0@@ -0,0 +1,280 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <poll.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/wait.h> +#include <unistd.h> +#include <linux/limits.h> + +#include "kselftest.h" +#include "cgroup_util.h" + +/* How long to wait for the CPU-pressure trigger before giving up. */ +#define PSI_POLL_TIMEOUT_MS 5000 + +static int pressure_open (const char *resource) +{ +char path[PATH_MAX ]; + +snprintf (path,sizeof (path),"/proc/pressure/%s" , resource); +return open (path,O_RDWR ); +} + +/* Write a trigger descriptor, including the trailing NUL the parser expects. */ +static ssize_t write_trigger (int fd,const char *trigger)
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 33
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+{ +return write (fd, trigger,strlen (trigger) +1 ); +} + +/* Trigger smoke test: second on same fd -> EBUSY; IRQ rejects "some". */ Please write more descriptive comments for your tests. I have to guess what you mean by this.
Yeah, those are too cryptic. I'll spell out what each case is actually exercising instead of just the expected result.
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c line 34
Quoted 1 line
tools/testing/selftests/cgroup/test_psi.c new file +1
-0+static int test_proc_triggers (const char *root)Why do you pass root here if you are not using it? AI glitch?
Dropping the param entirely. The proc-trigger cases don't need a cgroup, so they shouldn't take one (this also ties into the split below).
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/cgroup/test_psi.c new file +8
-0+{ +static const char *const resources[] = {"io" ,"memory" ,"cpu" }; +int ret =KSFT_FAIL ; +int fd =-1 ; +int i; + + (void )root; +
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 47
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+for (i =0 ; i < (int )ARRAY_SIZE (resources); i++) { + fd =pressure_open (resources[i]); +if (fd <0 ) { +ksft_print_msg ("open /proc/pressure/%s: %s\n" , + resources[i],strerror (errno));You could move these
ksft_print_msg()intopressure_open()and make that function a bit more useful while making the code here simpler.
Will do, folding the error printing in there.
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 51
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+ goto cleanup; + } + + errno =0 ;Why are you resetting the errno? If the next syscall fails, it will reset the errno and if it succeeds, you won't be using errno anyway. I'm confused.
No good reason -- removing. I'll only read errno right after a failed call.
Quoted 20 lines
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+if (write_trigger (fd,"some 150000 2000000" ) <=0 ) { +ksft_print_msg ("%s: 'some' trigger rejected: %s\n" , + resources[i],strerror (errno)); + goto cleanup; + }tools/testing/selftests/cgroup/test_psi.c line 66
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+/* A second trigger on the same fd must fail with EBUSY. */ + errno =0 ; +if (write_trigger (fd,"full 150000 2000000" ) !=-1 || errno !=EBUSY ) { +ksft_print_msg ("%s: second trigger expected EBUSY, got %s\n" , + resources[i],strerror (errno)); + goto cleanup; + } + +close (fd); + fd =-1 ;When you jump to cleanup label you always need to close the fd. You could simplify the flow if you remove all these "fd = -1;" and change the end of the function to be:
Quoted 6 lines
return
KSFT_PASS
cleanup:close(fd); return ret; }
Right, that's just noise. Single close(fd) in cleanup.
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 69
Quoted 3 lines
tools/testing/selftests/cgroup/test_psi.c new file +3
-0+ } + +/* IRQ is full-only, and only exists with CONFIG_IRQ_TIME_ACCOUNTING. */ It would be better if you split this test into separate tests for "io", "memory", "cpu" and "irq" and if "irq" is not available you can return
KSFT_SKIPfor that test only. This way when a test fails the user will know exactly what failed.
Agreed, splitting them. irq will KSFT_SKIP when /proc/pressure/irq isn't there.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/cgroup/test_psi.c new file +15
-0+ fd =pressure_open ("irq" ); +if (fd >=0 ) { + errno =0 ; +if (write_trigger (fd,"some 150000 1000000" ) !=-1 ) { +ksft_print_msg ("irq 'some': expected failure, got %s\n" , +strerror (errno)); + goto cleanup; + } +close (fd); + fd =-1 ; + } + + ret =KSFT_PASS ; +cleanup: +if (fd >=0 )
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 89
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+close (fd); +return ret; +} + +/* cgroup.pressure 0/1 hides/shows the *.pressure files and round-trips. */ The above comment needs to be expanded to explain what you are testing. It does not read as a proper English sentence.
Rewriting it as an actual sentence.
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/cgroup/test_psi.c new file +7
-0+static int test_cgroup_pressure_toggle (const char *root) +{ +char buf[BUF_SIZE ] = {0 }; +char *cg =NULL ; +int ret =KSFT_FAIL ; + + cg =cg_name (root,"psi_toggle_test" );
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 100
Quoted 4 lines
tools/testing/selftests/cgroup/test_psi.c new file +4
-0+if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup;Jumping to "cleanup" above results in a call to
cg_destroy()whilecg_create()failed. It will try tormdir()a directory which we never created.
that's a real bug. Adding a "created" flag so we don't tear down a cgroup that was never set up.
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c line 103
Quoted 3 lines
tools/testing/selftests/cgroup/test_psi.c new file +3
-0+ +if (cg_write (cg,"cgroup.pressure" ,"0" )) { +ksft_print_msg ("failed to disable cgroup.pressure\n" );Reporting
strerror()in these failure logs would be useful.
Adding it to the cg_write/cg_read paths.
Quoted 8 lines
tools/testing/selftests/cgroup/test_psi.c line 106
Quoted 3 lines
tools/testing/selftests/cgroup/test_psi.c new file +3
-0+ goto cleanup; + } +if (cg_read (cg,"cgroup.pressure" , buf,sizeof (buf)) <0 ||atoi (buf) !=0 ) {
atoi()will return 0 even on error, so if you want to really check the value is 0 use a more robust method likestrtol()or simply do a string comparison.
Right, atoi is wrong here. String compare instead.
Quoted 29 lines
Quoted 16 lines
tools/testing/selftests/cgroup/test_psi.c new file +16
-0+ksft_print_msg ("cgroup.pressure=0 readback: '%s'\n" , buf); + goto cleanup; + } +if (cg_read (cg,"memory.pressure" , buf,sizeof (buf)) >=0 ) { +ksft_print_msg ("memory.pressure visible while disabled\n" ); + goto cleanup; + } + +if (cg_write (cg,"cgroup.pressure" ,"1" )) { +ksft_print_msg ("failed to re-enable cgroup.pressure\n" ); + goto cleanup; + } +if (cg_read (cg,"cgroup.pressure" , buf,sizeof (buf)) <0 ||atoi (buf) !=1 ) { +ksft_print_msg ("cgroup.pressure=1 readback: '%s'\n" , buf); + goto cleanup; + }tools/testing/selftests/cgroup/test_psi.c line 132
Quoted 10 lines
tools/testing/selftests/cgroup/test_psi.c new file +10
-0+if (cg_read (cg,"memory.pressure" , buf,sizeof (buf)) <0 ) { +ksft_print_msg ("memory.pressure unreadable while enabled\n" ); + goto cleanup; + } + + ret =KSFT_PASS ; +cleanup: +if (cg) +cg_destroy (cg); +free (cg);free(NULL) works but would be cleaner to do this instead:
Quoted 5 lines
if (cg) {
cg_destroy(cg);
free(cg);
}ok, collapsing those.
Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/cgroup/test_psi.c new file +12
-0+return ret; +} + +/* Induce deterministic CPU pressure (more hogs than CPUs). */ +static int test_cgroup_trigger_fire (const char *root) +{ +char *cg =NULL , *cpupress =NULL ; +int fd =-1 , ret =KSFT_FAIL ; +struct pollfd pfd; +long ncpus, i; +pid_t pid; +
Quoted 8 lines
tools/testing/selftests/cgroup/test_psi.c line 149
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ cg =cg_name (root,"psi_trigger_test" ); +if (!cg) + goto cleanup; +if (cg_create (cg)) + goto cleanup;Same issue with this jump. You will be calling
cg_killall(cg)andcg_destroy(cg)even thoughcg_create()did not succeed.
Same created-flag fix.
Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/cgroup/test_psi.c new file +13
-0+ + cpupress =cg_control (cg,"cpu.pressure" ); +if (!cpupress) + goto cleanup; + fd =open (cpupress,O_RDWR ); +if (fd <0 ) { +ksft_print_msg ("open cpu.pressure: %s\n" ,strerror (errno)); + goto cleanup; + } + +/* 1us threshold in a 1s window: any cpu stall fires it. */ + errno =0 ; +if (write_trigger (fd,"some 1 1000000" ) <=0 ) {
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 168
Quoted 6 lines
tools/testing/selftests/cgroup/test_psi.c new file +6
-0+ksft_print_msg ("arming trigger failed: %s\n" ,strerror (errno)); + goto cleanup; + } + + ncpus =sysconf (_SC_NPROCESSORS_ONLN); +if (ncpus <=0 )Why is this not treated as a test failure?
Yeah, making sysconf() <= 0 a KSFT_FAIL.
Quoted 63 lines
Quoted 63 lines
tools/testing/selftests/cgroup/test_psi.c new file +63
-0+ ncpus =1 ; + + pid =fork (); +if (pid <0 ) { +ksft_print_msg ("fork: %s\n" ,strerror (errno)); + goto cleanup; + } +if (pid ==0 ) { +/* Enter the cgroup, then over-subscribe it with CPU hogs. */ +if (cg_enter_current (cg)) +_exit (KSFT_FAIL ); +for (i =0 ; i < ncpus; i++) { +if (fork () ==0 ) { +for (;;) + asmvolatile ("" :::"memory" ); +_exit (0 ); + } + } +for (;;) + asmvolatile ("" :::"memory" );/* child is also a hog */ +_exit (0 ); + } + + pfd.fd = fd; + pfd.events =POLLPRI ; +switch (poll (&pfd,1 ,PSI_POLL_TIMEOUT_MS )) { +case -1 : +ksft_print_msg ("poll: %s\n" ,strerror (errno)); +break ; +case 0 : +ksft_print_msg ("no trigger event; could not induce cpu pressure\n" ); + ret =KSFT_SKIP ; +break ; +default : +if (pfd.revents &POLLPRI ) + ret =KSFT_PASS ; +else +ksft_print_msg ("poll returned 0x%x\n" , pfd.revents); +break ; + } + +/* Stop the hogs (bounded, so waitpid can't hang) and reap the child. */ +cg_killall (cg); +waitpid (pid,NULL ,0 ); + +cleanup: +if (fd >=0 ) +close (fd); +if (cg) { +cg_killall (cg); +cg_destroy (cg); + } +free (cpupress); +free (cg); +return ret; +} + +#define TEST (x) { #x, x } + +struct psi_test { +const char *name; +int (*fn)(const char *root); +};
Quoted 9 lines
tools/testing/selftests/cgroup/test_psi.c line 236
Quoted 5 lines
tools/testing/selftests/cgroup/test_psi.c new file +5
-0+ +static struct psi_test tests[] = { +TEST (test_proc_triggers), +TEST (test_cgroup_pressure_toggle), +TEST (test_cgroup_trigger_fire),Instead of setting up these tests manually you could use
TEST_HARNESS_MAIN,TEST_Fand other helpers fromkselftest_harness.h.
I just went with the manual style to match the rest of the directory -- test_memcontrol.c, test_core.c, test_cpu.c, etc. all use ksft_set_plan() and none use kselftest_harness.h. Happy to switch to the harness if you'd prefer.
I'll fold all of this into v3.
Thanks,
Tao
Quoted 47 lines
Quoted 47 lines
tools/testing/selftests/cgroup/test_psi.c new file +44
-1+}; + +int main (int argc,char **argv) +{ +char root[PATH_MAX ]; +int mempress_fd; +int i; + + (void )argc; + +ksft_print_header (); +ksft_set_plan (ARRAY_SIZE (tests)); + +if (cg_find_unified_root (root,sizeof (root),NULL )) +ksft_exit_skip ("cgroup v2 isn't mounted\n" ); + +/* PSI must be enabled (CONFIG_PSI=y, not default-disabled). */ + mempress_fd =open ("/proc/pressure/memory" ,O_RDONLY ); +if (mempress_fd <0 ) +ksft_exit_skip ("PSI unavailable (CONFIG_PSI=n or psi=0)\n" ); +close (mempress_fd); + +if (cg_read_strstr (root,"cgroup.controllers" ,"memory" )) +ksft_exit_skip ("memory controller isn't available\n" ); +if (cg_read_strstr (root,"cgroup.subtree_control" ,"memory" )) +if (cg_write (root,"cgroup.subtree_control" ,"+memory" )) +ksft_exit_skip ("failed to enable memory controller\n" ); + +for (i =0 ; i < (int )ARRAY_SIZE (tests); i++) { +switch (tests[i].fn (root)) { +case KSFT_PASS : +ksft_test_result_pass ("%s\n" , tests[i].name); +break ; +case KSFT_SKIP : +ksft_test_result_skip ("%s\n" , tests[i].name); +break ; +default : +ksft_test_result_fail ("%s\n" , tests[i].name); +break ; + } + } + +ksft_finished (); +} --2.43.0