From: ujwal300@linux.ibm.com
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] sched/psi: Add psi_trigger_interface01 test
Date: Wed, 2 Dec 2026 12:31:07 +0530 [thread overview]
Message-ID: <20261202070106.35620-2-ujwal300@linux.ibm.com> (raw)
From: Ujwal Kumar <ujwal300@linux.ibm.com>
Add new LTP test case to verify PSI (Pressure Stall Information) trigger interface.
Signed-off-by: Ujwal Kumar <ujwal300@linux.ibm.com>
---
testcases/kernel/sched/psi/Makefile | 6 +
.../sched/psi/psi_trigger_interface01.c | 324 ++++++++++++++++++
2 files changed, 330 insertions(+)
create mode 100644 testcases/kernel/sched/psi/Makefile
create mode 100644 testcases/kernel/sched/psi/psi_trigger_interface01.c
diff --git a/testcases/kernel/sched/psi/Makefile b/testcases/kernel/sched/psi/Makefile
new file mode 100644
index 000000000..5ea7d67db
--- /dev/null
+++ b/testcases/kernel/sched/psi/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/sched/psi/psi_trigger_interface01.c b/testcases/kernel/sched/psi/psi_trigger_interface01.c
new file mode 100644
index 000000000..94d824d30
--- /dev/null
+++ b/testcases/kernel/sched/psi/psi_trigger_interface01.c
@@ -0,0 +1,324 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Linux Test Project
+ */
+
+/*\
+ * Verify the PSI trigger interface of the pressure files.
+ *
+ * A trigger is registered by writing "<some|full> <threshold_us> <window_us>"
+ * to an open pressure file and is monitored with poll(2) and POLLPRI. It
+ * lives as long as the file descriptor it was registered on. The test uses
+ * the pressure files of a cgroup of its own and the ones in /proc/pressure/
+ * and does not generate any load, so only the interface is checked:
+ *
+ * - valid triggers are accepted, including the smallest and the largest
+ * allowed threshold and window.
+ * - malformed triggers, a window of zero or above 10s and a threshold of zero
+ * or above the window are rejected with EINVAL.
+ * - a second trigger written to a file descriptor that has one already is
+ * rejected with EBUSY.
+ * - several file descriptors of the same file can hold different triggers
+ * at the same time.
+ * - the trigger is released when its file descriptor is closed, so a new one
+ * can be registered afterwards.
+ * - poll(2) on a file descriptor without a trigger reports POLLERR, while on
+ * one with a trigger it does not report any event as long as there is no
+ * stall in the idle cgroup.
+ *
+ * The accounting of the stall time is checked by psi_accounting01.
+ *
+ * [Kernel config]
+ * CONFIG_PSI=y
+ *
+ * [References]
+ * - kernel/sched/psi.c
+ * - Documentation/accounting/psi.rst
+ */
+
+#define _GNU_SOURCE
+
+#include <string.h>
+#include <poll.h>
+
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+
+#define PSI_PROBE "/proc/pressure/cpu"
+#define PSI_CPU "/proc/pressure/cpu"
+#define PSI_MEMORY "/proc/pressure/memory"
+#define PSI_IO "/proc/pressure/io"
+
+/* The kernel checks the trigger for a whole window, so wait at least for one */
+#define IDLE_TIMEOUT_MS 1500
+
+#define TRIGGER_OK "some 100000 500000"
+#define TRIGGER_OK_FULL "full 100000 500000"
+
+static struct tst_cg_group *cg_psi;
+static int cg_fd = -1;
+
+static const struct trigger_case {
+ const char *file;
+ const char *trigger;
+ /* 0 if the trigger has to be accepted, otherwise the expected errno */
+ int err;
+ const char *desc;
+} trigger_cases[] = {
+ {"cpu.pressure", TRIGGER_OK, 0, "cgroup cpu some"},
+ {"memory.pressure", TRIGGER_OK, 0, "cgroup memory some"},
+ {"memory.pressure", TRIGGER_OK_FULL, 0, "cgroup memory full"},
+ {"io.pressure", TRIGGER_OK, 0, "cgroup io some"},
+ {"io.pressure", TRIGGER_OK_FULL, 0, "cgroup io full"},
+ /* openat() ignores the directory fd for an absolute path */
+ {PSI_CPU, TRIGGER_OK, 0, "system cpu some"},
+ {PSI_MEMORY, TRIGGER_OK, 0, "system memory some"},
+ {PSI_MEMORY, TRIGGER_OK_FULL, 0, "system memory full"},
+ {PSI_IO, TRIGGER_OK, 0, "system io some"},
+ {PSI_IO, TRIGGER_OK_FULL, 0, "system io full"},
+
+ {"memory.pressure", "some 1 500000", 0, "minimal threshold and window"},
+ {"memory.pressure", "some 500000 500000", 0, "threshold equal to window"},
+ {"memory.pressure", "some 1 10000000", 0, "maximal window"},
+
+ {"memory.pressure", "bogus 100000 500000", EINVAL, "unknown type"},
+ {"memory.pressure", "aaa bbb ccc ddd eee", EINVAL, "garbage"},
+ {"memory.pressure", "some 1 0", EINVAL, "zero window"},
+ {"memory.pressure", "some 100000 10000001", EINVAL, "window above 10s"},
+ {"memory.pressure", "some 500001 500000", EINVAL, "threshold above window"},
+ {"memory.pressure", "some 0 500000", EINVAL, "zero threshold"},
+};
+
+/*
+ * The terminating NUL is written as well, the handlers of the files in
+ * /proc/pressure/ replace the last byte of the data with it.
+ */
+static size_t trigger_len(const char *trigger)
+{
+ return strlen(trigger) + 1;
+}
+
+static ssize_t write_trigger(int fd, const char *trigger)
+{
+ return write(fd, trigger, trigger_len(trigger));
+}
+
+/*
+ * Check the result of writing a trigger, err is 0 if it has to be accepted
+ * and the expected errno otherwise.
+ */
+static void check_write(ssize_t ret, const char *trigger, int err,
+ const char *desc)
+{
+ if (!err && ret == (ssize_t)trigger_len(trigger)) {
+ tst_res(TPASS, "%s: '%s' accepted", desc, trigger);
+ } else if (!err) {
+ tst_res(TFAIL | TERRNO, "%s: '%s' not accepted, ret=%zd",
+ desc, trigger, ret);
+ } else if (ret >= 0) {
+ tst_res(TFAIL, "%s: '%s' accepted, expected %s",
+ desc, trigger, tst_strerrno(err));
+ } else if (errno != err) {
+ tst_res(TFAIL | TERRNO, "%s: '%s' expected %s",
+ desc, trigger, tst_strerrno(err));
+ } else {
+ tst_res(TPASS | TERRNO, "%s: '%s' rejected", desc, trigger);
+ }
+}
+
+static int open_pressure(const char *file)
+{
+ return SAFE_OPENAT(cg_fd, file, O_RDWR | O_NONBLOCK);
+}
+
+static int register_trigger(const char *file, const char *trigger)
+{
+ int fd = open_pressure(file);
+
+ if (write_trigger(fd, trigger) != (ssize_t)trigger_len(trigger))
+ tst_brk(TBROK | TERRNO, "write('%s') to %s failed", trigger, file);
+
+ return fd;
+}
+
+static short poll_events(int fd, const char *file, int timeout_ms, int *ret)
+{
+ struct pollfd pfd = {.fd = fd, .events = POLLPRI};
+
+ *ret = poll(&pfd, 1, timeout_ms);
+ if (*ret < 0)
+ tst_brk(TBROK | TERRNO, "poll(%s) failed", file);
+
+ return pfd.revents;
+}
+
+static void verify_trigger_write(void)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(trigger_cases); i++) {
+ const struct trigger_case *tc = &trigger_cases[i];
+ int fd = open_pressure(tc->file);
+
+ check_write(write_trigger(fd, tc->trigger), tc->trigger,
+ tc->err, tc->desc);
+ SAFE_CLOSE(fd);
+ }
+}
+
+static void verify_second_write(void)
+{
+ int fd = register_trigger("memory.pressure", TRIGGER_OK);
+
+ check_write(write_trigger(fd, TRIGGER_OK_FULL), TRIGGER_OK_FULL, EBUSY,
+ "second trigger on the same fd");
+ SAFE_CLOSE(fd);
+}
+
+static void verify_multiple_fds(void)
+{
+ static const char *const triggers[] = {
+ "some 50000 500000",
+ "some 100000 500000",
+ "full 200000 1000000",
+ };
+ int fds[ARRAY_SIZE(triggers)];
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(triggers); i++) {
+ fds[i] = open_pressure("memory.pressure");
+ check_write(write_trigger(fds[i], triggers[i]), triggers[i], 0,
+ "independent fd of memory.pressure");
+ }
+
+ for (i = 0; i < ARRAY_SIZE(triggers); i++)
+ SAFE_CLOSE(fds[i]);
+}
+
+static void verify_reopen(void)
+{
+ int fd = register_trigger("io.pressure", TRIGGER_OK);
+
+ SAFE_CLOSE(fd);
+
+ fd = open_pressure("io.pressure");
+ check_write(write_trigger(fd, TRIGGER_OK), TRIGGER_OK, 0,
+ "trigger after the previous fd was closed");
+ SAFE_CLOSE(fd);
+}
+
+static void verify_poll_no_trigger(void)
+{
+ int fd = open_pressure("memory.pressure");
+ int ret;
+ short revents = poll_events(fd, "memory.pressure", 0, &ret);
+
+ if (revents & POLLERR)
+ tst_res(TPASS, "poll() on an fd without trigger reports POLLERR");
+ else
+ tst_res(TFAIL, "poll() on an fd without trigger, revents=0x%x",
+ revents);
+
+ SAFE_CLOSE(fd);
+}
+
+static void verify_poll_idle(void)
+{
+ static const char *const files[] = {
+ "cpu.pressure", "memory.pressure", "io.pressure",
+ };
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(files); i++) {
+ int fd = register_trigger(files[i], TRIGGER_OK);
+ int ret;
+ short revents = poll_events(fd, files[i], IDLE_TIMEOUT_MS, &ret);
+
+ if (revents & POLLERR)
+ tst_res(TFAIL, "poll(%s) returned POLLERR", files[i]);
+ else if (ret == 0)
+ tst_res(TPASS, "%s: no event in the idle cgroup", files[i]);
+ else
+ tst_res(TFAIL, "%s: event in the idle cgroup, revents=0x%x",
+ files[i], revents);
+
+ SAFE_CLOSE(fd);
+ }
+}
+
+static void (*const tests[])(void) = {
+ verify_trigger_write,
+ verify_second_write,
+ verify_multiple_fds,
+ verify_reopen,
+ verify_poll_no_trigger,
+ verify_poll_idle,
+};
+
+static void run(unsigned int n)
+{
+ tests[n]();
+}
+
+static void check_pressure_file(const char *file)
+{
+ if (faccessat(cg_fd, file, R_OK, 0))
+ tst_brk(TCONF | TERRNO, "%s is not available in the cgroup", file);
+}
+
+static void setup(void)
+{
+ char buf[64];
+ int fd = open(PSI_PROBE, O_RDONLY);
+
+ /* Some distributions build PSI but keep it disabled unless psi=1 is set */
+ if (fd < 0) {
+ if (errno == ENOENT)
+ tst_brk(TCONF, "PSI is not enabled in GRUB, %s does not exist "
+ "(add psi=1 to the kernel command line)", PSI_PROBE);
+
+ tst_brk(TBROK | TERRNO, "open(%s) failed", PSI_PROBE);
+ }
+
+ /* psi=0 on the kernel command line makes the interface return EOPNOTSUPP */
+ if (read(fd, buf, sizeof(buf)) < 0) {
+ if (errno == EOPNOTSUPP)
+ tst_brk(TCONF, "PSI is not enabled in GRUB, it is disabled by "
+ "psi=0 (use psi=1 on the kernel command line)");
+
+ tst_brk(TBROK | TERRNO, "read(%s) failed", PSI_PROBE);
+ }
+
+ SAFE_CLOSE(fd);
+
+ cg_psi = tst_cg_group_mk(tst_cg, "psi");
+ cg_fd = tst_cg_group_unified_dir_fd(cg_psi);
+
+ if (cg_fd < 0)
+ tst_brk(TBROK, "Cannot get the directory of the cgroup");
+
+ check_pressure_file("cpu.pressure");
+ check_pressure_file("memory.pressure");
+ check_pressure_file("io.pressure");
+}
+
+static void cleanup(void)
+{
+ if (cg_psi)
+ cg_psi = tst_cg_group_rm(cg_psi);
+}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(tests),
+ .test = run,
+ .setup = setup,
+ .cleanup = cleanup,
+ .needs_root = 1,
+ .min_kver = "5.2",
+ .needs_cgroup_ver = TST_CG_V2,
+ .needs_cgroup_ctrls = (const char *const[]) {"memory", NULL},
+ .needs_kconfigs = (const char *const[]) {
+ "CONFIG_PSI=y",
+ NULL
+ },
+};
--
2.50.1 (Apple Git-155)
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2026-12-02 7:01 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 7:01 ujwal300 [this message]
2026-12-02 22:12 ` [LTP] sched/psi: Add psi_trigger_interface01 test linuxtestproject.agent
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202070106.35620-2-ujwal300@linux.ibm.com \
--to=ujwal300@linux.ibm.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.