From: "Richard Weinberger (sigma star gmbh)" <rw@kernel.org>
To: xenomai@lists.linux.dev
Cc: upstream+xenomai@sigma-star.at, Richard Weinberger <richard@nod.at>
Subject: [PATCH 5/5] testsuite/smokey: add sched-throttle test
Date: Tue, 1 Dec 2026 15:12:07 +0100 [thread overview]
Message-ID: <20261201141207.2876277-6-rw@kernel.org> (raw)
In-Reply-To: <20261201141207.2876277-1-rw@kernel.org>
From: Richard Weinberger <richard@nod.at>
Spin a SCHED_FIFO thread on one CPU next to a regular Linux thread
pinned to the same CPU, once normally and once with the scheduler
locked. Check that the Linux thread makes progress and that the
throttle counter in /proc/xenomai/sched/throttle went up about once
per window.
The test is skipped if RT throttling is not enabled.
Signed-off-by: Richard Weinberger <richard@nod.at>
---
configure.ac | 1 +
testsuite/smokey/Makefile.am | 2 +
testsuite/smokey/sched-throttle/Makefile.am | 8 +
.../smokey/sched-throttle/sched-throttle.c | 251 ++++++++++++++++++
4 files changed, 262 insertions(+)
create mode 100644 testsuite/smokey/sched-throttle/Makefile.am
create mode 100644 testsuite/smokey/sched-throttle/sched-throttle.c
diff --git a/configure.ac b/configure.ac
index d707fb11d..8aaae4589 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1073,6 +1073,7 @@ AC_CONFIG_FILES([ \
testsuite/smokey/arith/Makefile \
testsuite/smokey/dlopen/Makefile \
testsuite/smokey/sched-quota/Makefile \
+ testsuite/smokey/sched-throttle/Makefile \
testsuite/smokey/sched-tp/Makefile \
testsuite/smokey/setsched/Makefile \
testsuite/smokey/rtdm/Makefile \
diff --git a/testsuite/smokey/Makefile.am b/testsuite/smokey/Makefile.am
index c49a5d77f..ee6791fbb 100644
--- a/testsuite/smokey/Makefile.am
+++ b/testsuite/smokey/Makefile.am
@@ -33,6 +33,7 @@ COBALT_SUBDIRS = \
posix-select \
rtdm \
sched-quota \
+ sched-throttle \
sched-tp \
setsched \
sigdebug \
@@ -78,6 +79,7 @@ DIST_SUBDIRS = \
posix-select \
rtdm \
sched-quota \
+ sched-throttle \
sched-tp \
setsched \
sigdebug \
diff --git a/testsuite/smokey/sched-throttle/Makefile.am b/testsuite/smokey/sched-throttle/Makefile.am
new file mode 100644
index 000000000..2e7e03275
--- /dev/null
+++ b/testsuite/smokey/sched-throttle/Makefile.am
@@ -0,0 +1,8 @@
+
+noinst_LIBRARIES = libsched-throttle.a
+
+libsched_throttle_a_SOURCES = sched-throttle.c
+
+libsched_throttle_a_CPPFLAGS = \
+ @XENO_USER_CFLAGS@ \
+ -I$(top_srcdir)/include
diff --git a/testsuite/smokey/sched-throttle/sched-throttle.c b/testsuite/smokey/sched-throttle/sched-throttle.c
new file mode 100644
index 000000000..ec5afc4b2
--- /dev/null
+++ b/testsuite/smokey/sched-throttle/sched-throttle.c
@@ -0,0 +1,251 @@
+/*
+ * Real-time throttling test.
+ *
+ * Copyright (C) 2026 Richard Weinberger
+ *
+ * Released under the terms of GPLv2.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sched.h>
+#include <time.h>
+#include <pthread.h>
+#include <smokey/smokey.h>
+
+smokey_test_plugin(sched_throttle,
+ SMOKEY_ARGLIST(
+ SMOKEY_INT(cpu),
+ SMOKEY_INT(duration),
+ SMOKEY_BOOL(lock),
+ ),
+ "Check real-time throttling (CONFIG_XENO_OPT_SCHED_THROTTLE).\n"
+ "\tA SCHED_FIFO thread spins on one CPU for the given duration\n"
+ "\t(seconds, default 3), next to a regular Linux thread pinned to\n"
+ "\tthe same CPU. The test passes if the Linux thread makes progress\n"
+ "\twhile the real-time thread spins, and if the throttle counter of\n"
+ "\tthat CPU went up once per elapsed window. The test runs twice,\n"
+ "\tthe second time with the scheduler locked by the spinning\n"
+ "\tthread; lock=0 or lock=1 selects a single run."
+);
+
+#define PARAM_DIR "/sys/module/xenomai/parameters/"
+#define THROTTLE_VFILE "/proc/xenomai/sched/throttle"
+
+static volatile unsigned long linux_count;
+static volatile int linux_stop;
+static unsigned long linux_progress;
+static int spin_secs = 3;
+static int test_cpu;
+static int lock_sched;
+
+static int read_param(const char *name, long *val)
+{
+ FILE *fp;
+ int ret;
+
+ fp = fopen(name, "r");
+ if (fp == NULL)
+ return -errno;
+
+ ret = fscanf(fp, "%ld", val) == 1 ? 0 : -EINVAL;
+ fclose(fp);
+
+ return ret;
+}
+
+static int read_throttle_count(int cpu, unsigned long *count)
+{
+ unsigned long long period, budget;
+ char line[128], state[16];
+ unsigned long n;
+ int ret = -ENOENT;
+ FILE *fp;
+ int c;
+
+ fp = fopen(THROTTLE_VFILE, "r");
+ if (fp == NULL)
+ return -errno;
+
+ while (fgets(line, sizeof(line), fp)) {
+ if (sscanf(line, "%d %15s %llu %llu %lu",
+ &c, state, &period, &budget, &n) != 5)
+ continue; /* Header. */
+ if (c == cpu) {
+ *count = n;
+ ret = 0;
+ break;
+ }
+ }
+
+ fclose(fp);
+
+ return ret;
+}
+
+static void set_cpu(pthread_attr_t *attr, int cpu)
+{
+ cpu_set_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(cpu, &set);
+ pthread_attr_setaffinity_np(attr, sizeof(set), &set);
+}
+
+static void *linux_thread(void *arg)
+{
+ while (!linux_stop)
+ linux_count++;
+
+ return NULL;
+}
+
+static void *rt_thread(void *arg)
+{
+ struct timespec now, end;
+ unsigned long start;
+
+ if (lock_sched)
+ pthread_setmode_np(0, PTHREAD_LOCK_SCHED, NULL);
+
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ end.tv_sec += spin_secs;
+ start = linux_count;
+
+ do
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ while (now.tv_sec < end.tv_sec ||
+ (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
+
+ linux_progress = linux_count - start;
+
+ if (lock_sched)
+ pthread_setmode_np(PTHREAD_LOCK_SCHED, 0, NULL);
+
+ return NULL;
+}
+
+static int start_linux_thread(pthread_t *tid)
+{
+ pthread_attr_t attr;
+ int ret;
+
+ pthread_attr_init(&attr);
+ set_cpu(&attr, test_cpu);
+ ret = __STD(pthread_create(tid, &attr, linux_thread, NULL));
+ pthread_attr_destroy(&attr);
+
+ return -ret;
+}
+
+static int run_rt_thread(void)
+{
+ struct sched_param param = { .sched_priority = 1 };
+ pthread_attr_t attr;
+ pthread_t tid;
+ int ret;
+
+ pthread_attr_init(&attr);
+ pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
+ pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
+ pthread_attr_setschedparam(&attr, ¶m);
+ set_cpu(&attr, test_cpu);
+ ret = pthread_create(&tid, &attr, rt_thread, NULL);
+ pthread_attr_destroy(&attr);
+ if (ret)
+ return -ret;
+
+ return -pthread_join(tid, NULL);
+}
+
+static int run_spinner(int lock, long period_us)
+{
+ unsigned long before, after, expected;
+ pthread_t linux_tid;
+ int ret;
+
+ lock_sched = lock;
+ linux_stop = 0;
+ linux_progress = 0;
+
+ ret = read_throttle_count(test_cpu, &before);
+ if (ret)
+ return ret;
+
+ ret = start_linux_thread(&linux_tid);
+ if (ret)
+ return ret;
+
+ ret = run_rt_thread();
+
+ linux_stop = 1;
+ __STD(pthread_join(linux_tid, NULL));
+
+ if (ret)
+ return ret;
+
+ ret = read_throttle_count(test_cpu, &after);
+ if (ret)
+ return ret;
+
+ /* The first and last windows may be cut short. */
+ expected = spin_secs * 1000000UL / period_us;
+ if (expected > 2)
+ expected -= 2;
+ else
+ expected = 1;
+
+ smokey_trace("cpu %d%s: %lu throttles in %d s, linux progress %lu",
+ test_cpu, lock ? " (scheduler locked)" : "",
+ after - before, spin_secs, linux_progress);
+
+ if (!smokey_assert(linux_progress > 0))
+ return -EINVAL;
+
+ if (!smokey_assert(after - before >= expected))
+ return -EINVAL;
+
+ return 0;
+}
+
+static int run_sched_throttle(struct smokey_test *t,
+ int argc, char *const argv[])
+{
+ long period_us, runtime_us;
+ int ret;
+
+ smokey_parse_args(t, argc, argv);
+
+ if (SMOKEY_ARG_ISSET(sched_throttle, cpu))
+ test_cpu = SMOKEY_ARG_INT(sched_throttle, cpu);
+ if (SMOKEY_ARG_ISSET(sched_throttle, duration))
+ spin_secs = SMOKEY_ARG_INT(sched_throttle, duration);
+
+ if (read_param(PARAM_DIR "sched_rt_period_us", &period_us) ||
+ read_param(PARAM_DIR "sched_rt_runtime_us", &runtime_us)) {
+ smokey_note("sched_throttle: CONFIG_XENO_OPT_SCHED_THROTTLE disabled");
+ return -ENOSYS;
+ }
+
+ /*
+ * Without throttling, the spinning thread would lock up the
+ * CPU and trigger the watchdog.
+ */
+ if (runtime_us < 0 || runtime_us >= period_us) {
+ smokey_note("sched_throttle: throttling disabled at boot");
+ return -ENOSYS;
+ }
+
+ if (SMOKEY_ARG_ISSET(sched_throttle, lock))
+ return run_spinner(SMOKEY_ARG_BOOL(sched_throttle, lock),
+ period_us);
+
+ ret = run_spinner(0, period_us);
+ if (ret)
+ return ret;
+
+ return run_spinner(1, period_us);
+}
--
2.56.1
next prev parent reply other threads:[~2026-12-01 14:12 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 14:12 [RFC PATCH 0/5] RT throttling Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 1/5] cobalt/trace: add cobalt_sched_throttle tracepoint Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 2/5] cobalt/sched: add RT throttling Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 3/5] cobalt/sched-sporadic: stop budget accounting on " Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` [PATCH 4/5] cobalt/sched-quota: charge the running group " Richard Weinberger (sigma star gmbh)
2026-12-01 14:12 ` Richard Weinberger (sigma star gmbh) [this message]
2026-12-01 15:29 ` [RFC PATCH 0/5] " Jan Kiszka
2026-12-01 15:32 ` Richard Weinberger
2026-12-01 15:36 ` Jan Kiszka
2026-12-01 15:38 ` Richard Weinberger
2026-12-01 15:40 ` Jan Kiszka
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=20261201141207.2876277-6-rw@kernel.org \
--to=rw@kernel.org \
--cc=richard@nod.at \
--cc=upstream+xenomai@sigma-star.at \
--cc=xenomai@lists.linux.dev \
/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.