Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796134344; b=B/HLGEU56mJpbrNg5Lgz4UoKu4sroLx1zeQxgPSUC0UjnosHup+alpA7LWq6gAw09Inv Vktn/qbw4MIBOjIAMtTKs3n3pIlMVwXK72qAH3w/6XeS3tKzqEuP06Ji9wy4KFT60Ppm4 ZMzomdYNWpjvwpKcULTleqjvQKc+thqhDqcxFMWxNAgyZ87aTfOoPAy3jkq6thA7PGts6 GEeJDYG+eg/Sbx0TCSafD8HK8dMTPSxm3TjXIoj2AH+Abm+uJuINk7AMV7BDQg0mHM14H QXgOFVvE+Ll64GKzGoJnlIy3UtJSKa8BzHsdp6XWir9WAT2BVfITMtQO0aW7y9riVbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134344; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=ThqbXGBq0lpLXJ4TaSikRbc3HI+Ds/u7jh3Kaugdylo=; b=IKJU/bBqSSramnnmrZSd/XkLPJW201/C/v76Un1Bo0bzFo15FCQOfPrVYKNo6vJsMY9K hNb5UCpAP25dzBNulgOXRc5jIgKzcgawbVGxtem8NiP8c1OR3eu2pPGZI9BXr7I4kQ8Jf RZ9erZ9dtC2Y3mhYAef1S8CZ97JnQxZUaOLFgjimiVR+gpjQCsArR8HHGUHsWnfAFTFyU 9Ubk3YuDllvEtC4JJuCQAeLjDPYjKdsKsv+2Pf5msfM1altQBWgqB1g/koulCByKR1zkb mtR6FlLSeijj4AUpZ1PL/WnEx5BsDwBltu61OLEuZWio2HzsOAAU6F4PXHtFZJqtojw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=exxYNcDj; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D71D9606A333 for ; Tue, 1 Dec 2026 14:12:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=exxYNcDj Received: by smtp.kernel.org (Postfix) with ESMTPSA id 927CF1F00893; Tue, 1 Dec 2026 14:12:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796134344; bh=ThqbXGBq0lpLXJ4TaSikRbc3HI+Ds/u7jh3Kaugdylo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=exxYNcDjwKNEZLBVL90uCX/ti95zC0WAVf7S9vNRpzzB5Xmr0jBfeMAlihe7sDAF2 nLLRtAlNMES9hrCcLtBzjgDtIqe3a0NOdJjpdHu3Wek1CYYVwhaoH5OvfOy4c58CnI MQ6Co/NuUXGpX71SdxxM9khq+T3LM2mvs+yNSiNQRcjfscmWGZdFXOx17IGe2GlfTw 3FxF3qa8USwwugRvzBtWIwrPVyn6eqBVGTaYq/J2AOGdhpNWb+K/z4pSbaQaEScVvc m1pp79uPul4O1U6lwciaXjWoRSiBw0AozVDufWpFnmlqA4sBmnKaU3koAdsQlVeHhR G2JcHL8BLOuKw== From: "Richard Weinberger (sigma star gmbh)" To: xenomai@lists.linux.dev Cc: upstream+xenomai@sigma-star.at, Richard Weinberger Subject: [PATCH 5/5] testsuite/smokey: add sched-throttle test Date: Tue, 1 Dec 2026 15:12:07 +0100 Message-ID: <20261201141207.2876277-6-rw@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261201141207.2876277-1-rw@kernel.org> References: <20261201141207.2876277-1-rw@kernel.org> X-Mailing-List: xenomai@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Richard Weinberger 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 --- 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 +#include +#include +#include +#include +#include +#include +#include +#include + +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