[PATCH v11 43/43] selftests/sched: Share the helpers of the deadline tests

From: Mathieu Desnoyers

Date: Wed Nov 25 2026 - 20:12:36 EST


deadline_bw_test, deadline_rebuild_test and deadline_switch_test each
have a copy of the same helpers. Move these to deadline_util.h:
- now_ns(), thread_time_ns() and sleep_ns();
- report();
- read_file(), write_file() and enter().

deadline_rebuild_test uses thread_time_ns() in place of its own
cpu_time_ns(). The helpers that differ from one test to the other stay
in each test. The Makefile adds the header to LOCAL_HDRS, so that
lib.mk rebuilds the tests when the header changes.

The Sashiko AI review of this series asked for this. No case changes.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
.../testing/selftests/sched/deadline/Makefile | 2 +
.../sched/deadline/deadline_bw_test.c | 86 +-------------
.../sched/deadline/deadline_rebuild_test.c | 94 +---------------
.../sched/deadline/deadline_switch_test.c | 36 +-----
.../selftests/sched/deadline/deadline_util.h | 106 ++++++++++++++++++
5 files changed, 116 insertions(+), 208 deletions(-)
create mode 100644 tools/testing/selftests/sched/deadline/deadline_util.h

diff --git a/tools/testing/selftests/sched/deadline/Makefile b/tools/testing/selftests/sched/deadline/Makefile
index 15f1e96a21e6..a81aa97c6cc6 100644
--- a/tools/testing/selftests/sched/deadline/Makefile
+++ b/tools/testing/selftests/sched/deadline/Makefile
@@ -5,4 +5,6 @@ LDLIBS += -lpthread

TEST_GEN_PROGS := deadline_switch_test deadline_rebuild_test deadline_bw_test

+LOCAL_HDRS += deadline_util.h
+
include ../../lib.mk
diff --git a/tools/testing/selftests/sched/deadline/deadline_bw_test.c b/tools/testing/selftests/sched/deadline/deadline_bw_test.c
index 78b58888e9d5..56dc73264e0a 100644
--- a/tools/testing/selftests/sched/deadline/deadline_bw_test.c
+++ b/tools/testing/selftests/sched/deadline/deadline_bw_test.c
@@ -97,6 +97,8 @@
#define NSEC_PER_MSEC 1000000ULL
#define NSEC_PER_SEC 1000000000ULL

+#include "deadline_util.h"
+
/* The threads of probe() and hold() do nothing: a small stack will do. */
#define THREAD_STACK (64 * 1024 > PTHREAD_STACK_MIN ? 64 * 1024 : PTHREAD_STACK_MIN)

@@ -127,34 +129,6 @@ static int top_cpu(int n)
return online[ncpus - n];
}

-/*
- * clock_gettime() cannot fail in now_ns() and thread_time_ns(). Both
- * clocks are always supported, and the pointer is valid.
- */
-static uint64_t now_ns(void)
-{
- struct timespec ts;
-
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
-static uint64_t thread_time_ns(void)
-{
- struct timespec ts;
-
- clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
- return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
-static void sleep_ns(uint64_t ns)
-{
- struct timespec ts = { .tv_sec = ns / NSEC_PER_SEC, .tv_nsec = ns % NSEC_PER_SEC };
-
- while (nanosleep(&ts, &ts) && errno == EINTR)
- ;
-}
-
static void spin_ns(uint64_t ns)
{
uint64_t end = thread_time_ns() + ns;
@@ -308,52 +282,6 @@ static int set_other(pid_t tid)
return syscall(SYS_sched_setattr, tid, &attr, 0) ? -errno : 0;
}

-static int write_file(const char *dir, const char *name, const char *val)
-{
- char path[PATH_MAX];
- int fd, err = 0;
- ssize_t n;
-
- if (snprintf(path, sizeof(path), "%s/%s", dir, name) >= (int)sizeof(path))
- return -ENAMETOOLONG;
- fd = open(path, O_WRONLY);
- if (fd < 0)
- return -errno;
- n = write(fd, val, strlen(val));
- if (n < 0)
- err = -errno;
- else if (n != (ssize_t)strlen(val))
- err = -EIO;
- if (close(fd) && !err)
- err = -errno;
- return err;
-}
-
-static int read_file(const char *path, char *buf, size_t len)
-{
- int fd = open(path, O_RDONLY);
- ssize_t n;
-
- if (fd < 0)
- return -1;
- do {
- n = read(fd, buf, len - 1);
- } while (n < 0 && errno == EINTR);
- close(fd);
- if (n < 0)
- return -1;
- buf[n] = 0;
- return 0;
-}
-
-static int enter(const char *cg, pid_t pid)
-{
- char buf[16];
-
- snprintf(buf, sizeof(buf), "%d", pid);
- return write_file(cg, "cgroup.procs", buf);
-}
-
/* Return whether @list, words separated by blanks, holds the word @w. */
static int has_word(const char *list, const char *w)
{
@@ -14797,16 +14725,6 @@ static int find_cgroup2(void)
return found ? 0 : -1;
}

-static void report(const char *name, int ret)
-{
- if (ret == KSFT_PASS)
- ksft_test_result_pass("%s\n", name);
- else if (ret == KSFT_SKIP)
- ksft_test_result_skip("%s\n", name);
- else
- ksft_test_result_fail("%s\n", name);
-}
-
/* sched_setaffinity() against sched_setattr(), with no P */

/*
diff --git a/tools/testing/selftests/sched/deadline/deadline_rebuild_test.c b/tools/testing/selftests/sched/deadline/deadline_rebuild_test.c
index 735c3c65e156..e0ec2a559632 100644
--- a/tools/testing/selftests/sched/deadline/deadline_rebuild_test.c
+++ b/tools/testing/selftests/sched/deadline/deadline_rebuild_test.c
@@ -52,6 +52,8 @@
#define NSEC_PER_MSEC 1000000ULL
#define NSEC_PER_SEC 1000000000ULL

+#include "deadline_util.h"
+
struct dl_attr {
uint32_t size;
uint32_t sched_policy;
@@ -87,26 +89,6 @@ static int set_other(pid_t pid)
return sched_setscheduler(pid, SCHED_OTHER, &sp) ? -errno : 0;
}

-/*
- * Return the CLOCK_MONOTONIC time in ns. clock_gettime() cannot fail: the
- * clock is always supported, and the pointer is valid.
- */
-static uint64_t now_ns(void)
-{
- struct timespec ts;
-
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
-static void sleep_ns(uint64_t ns)
-{
- struct timespec ts = { .tv_sec = ns / NSEC_PER_SEC, .tv_nsec = ns % NSEC_PER_SEC };
-
- while (nanosleep(&ts, &ts) && errno == EINTR)
- ;
-}
-
static void reap(pid_t pid)
{
int status;
@@ -125,52 +107,6 @@ static void reap(pid_t pid)
}
}

-static int write_file(const char *dir, const char *name, const char *val)
-{
- char path[PATH_MAX];
- int fd, err = 0;
- ssize_t n;
-
- if (snprintf(path, sizeof(path), "%s/%s", dir, name) >= (int)sizeof(path))
- return -ENAMETOOLONG;
- fd = open(path, O_WRONLY);
- if (fd < 0)
- return -errno;
- n = write(fd, val, strlen(val));
- if (n < 0)
- err = -errno;
- else if (n != (ssize_t)strlen(val))
- err = -EIO;
- if (close(fd) && !err)
- err = -errno;
- return err;
-}
-
-static int read_file(const char *path, char *buf, size_t len)
-{
- int fd = open(path, O_RDONLY);
- ssize_t n;
-
- if (fd < 0)
- return -1;
- do {
- n = read(fd, buf, len - 1);
- } while (n < 0 && errno == EINTR);
- close(fd);
- if (n < 0)
- return -1;
- buf[n] = 0;
- return 0;
-}
-
-static int enter(const char *cg, pid_t pid)
-{
- char buf[16];
-
- snprintf(buf, sizeof(buf), "%d", pid);
- return write_file(cg, "cgroup.procs", buf);
-}
-
/* Make @cg a partition root of CPU @cpu alone. Return 0, or -1 on error. */
static int make_partition(const char *cg, int cpu)
{
@@ -207,18 +143,6 @@ static void remove_partition(const char *cg)
}
}

-/*
- * Return the CPU time of the calling thread in ns. clock_gettime() cannot
- * fail: the clock is always supported, and the pointer is valid.
- */
-static uint64_t cpu_time_ns(void)
-{
- struct timespec ts;
-
- clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
- return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
enum depart { DIES, SWITCHED_OUT };

/* Kill the calling child if the test exits, so that no deadline task stays. */
@@ -250,8 +174,8 @@ static pid_t departing(const char *cg, enum depart how, int fd[2])
_exit(2);
if (set_deadline(0, 400 * NSEC_PER_MSEC))
_exit(3);
- t0 = cpu_time_ns();
- while (cpu_time_ns() - t0 < 380 * NSEC_PER_MSEC)
+ t0 = thread_time_ns();
+ while (thread_time_ns() - t0 < 380 * NSEC_PER_MSEC)
;
if (how == DIES)
_exit(0);
@@ -531,16 +455,6 @@ static int find_cgroup2(void)
return found ? 0 : -1;
}

-static void report(const char *name, int ret)
-{
- if (ret == KSFT_PASS)
- ksft_test_result_pass("%s\n", name);
- else if (ret == KSFT_SKIP)
- ksft_test_result_skip("%s\n", name);
- else
- ksft_test_result_fail("%s\n", name);
-}
-
int main(void)
{
char buf[256], path[PATH_MAX + 32];
diff --git a/tools/testing/selftests/sched/deadline/deadline_switch_test.c b/tools/testing/selftests/sched/deadline/deadline_switch_test.c
index 40c7c6bafc2b..a544bee5af5a 100644
--- a/tools/testing/selftests/sched/deadline/deadline_switch_test.c
+++ b/tools/testing/selftests/sched/deadline/deadline_switch_test.c
@@ -51,6 +51,8 @@
#define NSEC_PER_MSEC 1000000ULL
#define NSEC_PER_SEC 1000000000ULL

+#include "deadline_util.h"
+
struct dl_attr {
uint32_t size;
uint32_t sched_policy;
@@ -70,22 +72,6 @@ struct shared {
uint64_t sleeps; /* sleeps begun */
};

-static uint64_t now_ns(void)
-{
- struct timespec ts;
-
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
-static void sleep_ns(uint64_t ns)
-{
- struct timespec ts = { .tv_sec = ns / NSEC_PER_SEC, .tv_nsec = ns % NSEC_PER_SEC };
-
- while (nanosleep(&ts, &ts) && errno == EINTR)
- ;
-}
-
static void sleep_until(uint64_t t)
{
struct timespec ts = { .tv_sec = t / NSEC_PER_SEC, .tv_nsec = t % NSEC_PER_SEC };
@@ -518,14 +504,6 @@ static sem_t owner_locked, owner_go;
static pid_t owner_tid;
static int owner_ms; /* the owner's CPU time in its first period */

-static uint64_t thread_time_ns(void)
-{
- struct timespec ts;
-
- clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
- return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
-}
-
/*
* Wait on @s for at most @ms, retrying on signals. Return 0, or -1 on a
* timeout or an error.
@@ -1081,16 +1059,6 @@ static int test_sysrq_running_owner(int normalized)
return ret;
}

-static void report(const char *name, int ret)
-{
- if (ret == KSFT_PASS)
- ksft_test_result_pass("%s\n", name);
- else if (ret == KSFT_SKIP)
- ksft_test_result_skip("%s\n", name);
- else
- ksft_test_result_fail("%s\n", name);
-}
-
int main(void)
{
int ret;
diff --git a/tools/testing/selftests/sched/deadline/deadline_util.h b/tools/testing/selftests/sched/deadline/deadline_util.h
new file mode 100644
index 000000000000..489233a32cbd
--- /dev/null
+++ b/tools/testing/selftests/sched/deadline/deadline_util.h
@@ -0,0 +1,106 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Helpers that the deadline selftests share. */
+#ifndef DEADLINE_UTIL_H
+#define DEADLINE_UTIL_H
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "../../kselftest.h"
+
+#ifndef NSEC_PER_SEC
+#define NSEC_PER_SEC 1000000000ULL
+#endif
+
+/*
+ * clock_gettime() cannot fail in now_ns() and thread_time_ns(). Both
+ * clocks are always supported, and the pointer is valid.
+ */
+static inline uint64_t now_ns(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
+}
+
+static inline uint64_t thread_time_ns(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
+ return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
+}
+
+static inline void sleep_ns(uint64_t ns)
+{
+ struct timespec ts = { .tv_sec = ns / NSEC_PER_SEC, .tv_nsec = ns % NSEC_PER_SEC };
+
+ while (nanosleep(&ts, &ts) && errno == EINTR)
+ ;
+}
+
+static inline void report(const char *name, int ret)
+{
+ if (ret == KSFT_PASS)
+ ksft_test_result_pass("%s\n", name);
+ else if (ret == KSFT_SKIP)
+ ksft_test_result_skip("%s\n", name);
+ else
+ ksft_test_result_fail("%s\n", name);
+}
+
+static inline int read_file(const char *path, char *buf, size_t len)
+{
+ int fd = open(path, O_RDONLY);
+ ssize_t n;
+
+ if (fd < 0)
+ return -1;
+ do {
+ n = read(fd, buf, len - 1);
+ } while (n < 0 && errno == EINTR);
+ close(fd);
+ if (n < 0)
+ return -1;
+ buf[n] = 0;
+ return 0;
+}
+
+static inline int write_file(const char *dir, const char *name, const char *val)
+{
+ char path[PATH_MAX];
+ int fd, err = 0;
+ ssize_t n;
+
+ if (snprintf(path, sizeof(path), "%s/%s", dir, name) >= (int)sizeof(path))
+ return -ENAMETOOLONG;
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ return -errno;
+ n = write(fd, val, strlen(val));
+ if (n < 0)
+ err = -errno;
+ else if (n != (ssize_t)strlen(val))
+ err = -EIO;
+ if (close(fd) && !err)
+ err = -errno;
+ return err;
+}
+
+static inline int enter(const char *cg, pid_t pid)
+{
+ char buf[16];
+
+ snprintf(buf, sizeof(buf), "%d", pid);
+ return write_file(cg, "cgroup.procs", buf);
+}
+
+#endif /* DEADLINE_UTIL_H */
--
2.47.3