From b4b66151a71445f3a71574136ddc82968c7b175e Mon Sep 17 00:00:00 2001 From: Jiangshan Yi Date: Thu, 2 Jul 2026 17:39:15 +0800 Subject: selftests: timers: leap-a-day: Fix -w option and update usage comment Commit 98b74e1f3104 ("kselftests: timers: leap-a-day: Change default arguments to help test runs") replaced the -s option with -w and made "wait for the leap second" the non-default behaviour, but it only updated the switch/case handling. Two things were left inconsistent: - The getopt() option string still lists 's' instead of 'w', so passing -w is rejected as an invalid option and the new behaviour cannot be selected at all. - The file header comment still documents the removed -s option and an outdated default for -i. Fix the getopt() string to accept 'w' (matching the existing case 'w': handler) and update the header comment to describe -w, -t and the current -i default. Fixes: 98b74e1f3104 ("kselftests: timers: leap-a-day: Change default arguments to help test runs") Signed-off-by: Jiangshan Yi Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260702093915.2652638-1-yijiangshan@kylinos.cn --- tools/testing/selftests/timers/leap-a-day.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/leap-a-day.c b/tools/testing/selftests/timers/leap-a-day.c index 3568cfb3e815..97c8c66bea88 100644 --- a/tools/testing/selftests/timers/leap-a-day.c +++ b/tools/testing/selftests/timers/leap-a-day.c @@ -9,16 +9,19 @@ * kernel's leap-second behavior, as well as how well applications * handle the leap-second discontinuity. * - * Usage: leap-a-day [-s] [-i ] + * Usage: leap-a-day [-w] [-i ] [-t] * * Options: - * -s: Each iteration, set the date to 10 seconds before midnight GMT. - * This speeds up the number of leapsecond transitions tested, - * but because it calls settimeofday frequently, advancing the - * time by 24 hours every ~16 seconds, it may cause application - * disruption. + * -w: Only set the leap-second flag and wait for the leap second + * each iteration, instead of advancing the time. By default the + * date is set to 10 seconds before midnight GMT, which speeds up + * the number of leapsecond transitions tested, but because it + * calls settimeofday frequently, advancing the time by 24 hours + * every ~16 seconds, it may cause application disruption. * - * -i: Number of iterations to run (default: infinite) + * -i: Number of iterations to run (-1 = infinite, default: 10) + * + * -t: Print TAI time. * * Other notes: Disabling NTP prior to running this is advised, as the two * may conflict in their commands to the kernel. @@ -186,7 +189,7 @@ int main(int argc, char **argv) int opt; /* Process arguments */ - while ((opt = getopt(argc, argv, "sti:")) != -1) { + while ((opt = getopt(argc, argv, "wti:")) != -1) { switch (opt) { case 'w': printf("Only setting leap-flag, not changing time. It could take up to a day for leap to trigger.\n"); -- cgit From f44ce7fdbdd00a596455b8689b62c12d5530d610 Mon Sep 17 00:00:00 2001 From: Wake Liu Date: Wed, 10 Jun 2026 01:47:21 +0000 Subject: selftests: timers: Partially revert "Remove local NSEC_PER_SEC and USEC_PER_SEC defines" This partially reverts commit 80fa614e2fbc ("selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines"). The original commit removed local definitions of NSEC_PER_SEC and USEC_PER_SEC in favor of including . However, NSEC_PER_SEC in vdso/time64.h is defined as 1000000000L, which is 32-bit on 32-bit architectures. This causes integer overflow warnings in several timer tests when doing arithmetic like NSEC_PER_SEC * 10 on 32-bit systems. To fix this, restore the local definitions of NSEC_PER_SEC and USEC_PER_SEC in the test files, but use "LL" suffix consistently (1000000000LL and 1000000LL) to ensure 64-bit arithmetic and avoid overflows. We keep the cleanup from the original commit that renamed plural definitions (NSECS_PER_SEC/USECS_PER_SEC) to singular ones in posix_timers.c, but we now define them locally there as well. This also removes the dependency of the selftests on the internal kernel header . Signed-off-by: Wake Liu Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260610014721.718362-1-wakel@google.com --- tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 5 +++-- tools/testing/selftests/timers/alarmtimer-suspend.c | 3 ++- tools/testing/selftests/timers/inconsistency-check.c | 3 ++- tools/testing/selftests/timers/leap-a-day.c | 3 ++- tools/testing/selftests/timers/mqueue-lat.c | 3 ++- tools/testing/selftests/timers/nanosleep.c | 3 ++- tools/testing/selftests/timers/nsleep-lat.c | 3 ++- tools/testing/selftests/timers/posix_timers.c | 4 +++- tools/testing/selftests/timers/raw_skew.c | 3 ++- tools/testing/selftests/timers/set-2038.c | 3 ++- tools/testing/selftests/timers/set-timer-lat.c | 3 ++- tools/testing/selftests/timers/valid-adjtimex.c | 4 +++- 13 files changed, 28 insertions(+), 14 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/Makefile b/tools/testing/selftests/timers/Makefile index 32203593c62e..0e73a16874c4 100644 --- a/tools/testing/selftests/timers/Makefile +++ b/tools/testing/selftests/timers/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir) +CFLAGS += -O3 -Wl,-no-as-needed -Wall LDLIBS += -lrt -lpthread -lm # these are all "safe" tests that don't modify diff --git a/tools/testing/selftests/timers/adjtick.c b/tools/testing/selftests/timers/adjtick.c index 5b3ef708d6e9..22d274d5520f 100644 --- a/tools/testing/selftests/timers/adjtick.c +++ b/tools/testing/selftests/timers/adjtick.c @@ -22,10 +22,11 @@ #include #include #include -#include - #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL + #define MILLION 1000000 long systick; diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c index aa66c805f6a4..d55d5b0377c6 100644 --- a/tools/testing/selftests/timers/alarmtimer-suspend.c +++ b/tools/testing/selftests/timers/alarmtimer-suspend.c @@ -28,10 +28,11 @@ #include #include #include -#include #include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15 diff --git a/tools/testing/selftests/timers/inconsistency-check.c b/tools/testing/selftests/timers/inconsistency-check.c index e53e63e18683..9ab7066b4e32 100644 --- a/tools/testing/selftests/timers/inconsistency-check.c +++ b/tools/testing/selftests/timers/inconsistency-check.c @@ -28,9 +28,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 diff --git a/tools/testing/selftests/timers/leap-a-day.c b/tools/testing/selftests/timers/leap-a-day.c index 97c8c66bea88..71b49734e08c 100644 --- a/tools/testing/selftests/timers/leap-a-day.c +++ b/tools/testing/selftests/timers/leap-a-day.c @@ -51,9 +51,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + #define CLOCK_TAI 11 time_t next_leap; diff --git a/tools/testing/selftests/timers/mqueue-lat.c b/tools/testing/selftests/timers/mqueue-lat.c index c0d9368e4fca..ce8700c95cac 100644 --- a/tools/testing/selftests/timers/mqueue-lat.c +++ b/tools/testing/selftests/timers/mqueue-lat.c @@ -29,9 +29,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + #define TARGET_TIMEOUT 100000000 /* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */ diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index a054680b3372..ceff18149a48 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -27,9 +27,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c index a7ba1eb1e21b..15f6493ca7b5 100644 --- a/tools/testing/selftests/timers/nsleep-lat.c +++ b/tools/testing/selftests/timers/nsleep-lat.c @@ -24,9 +24,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */ /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c index 2f3bac9fc6e8..fa168d802f9e 100644 --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -16,12 +16,14 @@ #include #include #include -#include #include #include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL + #define DELAY 2 static void __fatal_error(const char *test, const char *name, const char *what) diff --git a/tools/testing/selftests/timers/raw_skew.c b/tools/testing/selftests/timers/raw_skew.c index a7bae7d80916..2dd16cb4cdd0 100644 --- a/tools/testing/selftests/timers/raw_skew.c +++ b/tools/testing/selftests/timers/raw_skew.c @@ -25,9 +25,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + #define shift_right(x, s) ({ \ __typeof__(x) __x = (x); \ __typeof__(s) __s = (s); \ diff --git a/tools/testing/selftests/timers/set-2038.c b/tools/testing/selftests/timers/set-2038.c index ecc171de4728..c1235638406d 100644 --- a/tools/testing/selftests/timers/set-2038.c +++ b/tools/testing/selftests/timers/set-2038.c @@ -27,9 +27,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + #define KTIME_MAX ((long long)~((unsigned long long)1 << 63)) #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) diff --git a/tools/testing/selftests/timers/set-timer-lat.c b/tools/testing/selftests/timers/set-timer-lat.c index 44d2e3614fa5..e092c18befff 100644 --- a/tools/testing/selftests/timers/set-timer-lat.c +++ b/tools/testing/selftests/timers/set-timer-lat.c @@ -28,9 +28,10 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL + /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c index e1e56d3097d6..dc2559eb11a5 100644 --- a/tools/testing/selftests/timers/valid-adjtimex.c +++ b/tools/testing/selftests/timers/valid-adjtimex.c @@ -29,9 +29,11 @@ #include #include #include -#include #include "kselftest.h" +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL + #define ADJ_SETOFFSET 0x0100 #include -- cgit From 431012dd0a291bc135eb88d8a62d7424576800fa Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:37 +0200 Subject: selftests: Add clock-helpers.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Multiple selftests duplicate various clock-related functionality. Add a new header to collect and deduplicate those. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-6-910cbd485390@linutronix.de --- tools/testing/selftests/clock-helpers.h | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tools/testing/selftests/clock-helpers.h (limited to 'tools/testing') diff --git a/tools/testing/selftests/clock-helpers.h b/tools/testing/selftests/clock-helpers.h new file mode 100644 index 000000000000..01451f538e71 --- /dev/null +++ b/tools/testing/selftests/clock-helpers.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __CLOCK_HELPERS_H +#define __CLOCK_HELPERS_H + +#include +#include + +#define MSEC_PER_SEC 1000LL +#define USEC_PER_MSEC 1000LL +#define NSEC_PER_USEC 1000LL +#define NSEC_PER_MSEC 1000000LL +#define USEC_PER_SEC 1000000LL +#define NSEC_PER_SEC 1000000000LL +#define PSEC_PER_SEC 1000000000000LL +#define FSEC_PER_SEC 1000000000000000LL + +#ifndef CLOCK_AUX +#define CLOCK_AUX 16 +#endif + +#ifndef MAX_AUX_CLOCKS +#define MAX_AUX_CLOCKS 8 +#endif + +#ifndef CLOCK_AUX_LAST +#define CLOCK_AUX_LAST (CLOCK_AUX + MAX_AUX_CLOCKS - 1) +#endif + +__attribute__((unused)) +static inline const char *clock_name(clockid_t clockid) +{ + switch (clockid) { + case CLOCK_REALTIME: + return "CLOCK_REALTIME"; + case CLOCK_MONOTONIC: + return "CLOCK_MONOTONIC"; + case CLOCK_PROCESS_CPUTIME_ID: + return "CLOCK_PROCESS_CPUTIME_ID"; + case CLOCK_THREAD_CPUTIME_ID: + return "CLOCK_THREAD_CPUTIME_ID"; + case CLOCK_MONOTONIC_RAW: + return "CLOCK_MONOTONIC_RAW"; + case CLOCK_REALTIME_COARSE: + return "CLOCK_REALTIME_COARSE"; + case CLOCK_MONOTONIC_COARSE: + return "CLOCK_MONOTONIC_COARSE"; + case CLOCK_BOOTTIME: + return "CLOCK_BOOTTIME"; + case CLOCK_REALTIME_ALARM: + return "CLOCK_REALTIME_ALARM"; + case CLOCK_BOOTTIME_ALARM: + return "CLOCK_BOOTTIME_ALARM"; + case CLOCK_TAI: + return "CLOCK_TAI"; + case CLOCK_AUX + 0: + return "CLOCK_AUX0"; + case CLOCK_AUX + 1: + return "CLOCK_AUX1"; + case CLOCK_AUX + 2: + return "CLOCK_AUX2"; + case CLOCK_AUX + 3: + return "CLOCK_AUX3"; + case CLOCK_AUX + 4: + return "CLOCK_AUX4"; + case CLOCK_AUX + 5: + return "CLOCK_AUX5"; + case CLOCK_AUX + 6: + return "CLOCK_AUX6"; + case CLOCK_AUX + 7: + return "CLOCK_AUX7"; + }; + return "UNKNOWN_CLOCKID"; +} + +#endif /* __CLOCK_HELPERS_H */ -- cgit From 54200b87489b39f9437a91e2156c4c2d90412704 Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:38 +0200 Subject: selftests: timers: Use clock_name() and constants from clock-helpers.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the many repetitions of custom clockid_t to string translation with the clock_name() helper from clock-helpers.h. Also drop the custom ?SEC_PER_?SEC definitions. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-7-910cbd485390@linutronix.de --- tools/testing/selftests/timers/adjtick.c | 5 ++- .../testing/selftests/timers/alarmtimer-suspend.c | 38 ++------------------ .../testing/selftests/timers/inconsistency-check.c | 38 +++----------------- tools/testing/selftests/timers/leap-a-day.c | 3 +- tools/testing/selftests/timers/mqueue-lat.c | 3 +- tools/testing/selftests/timers/nanosleep.c | 42 ++++------------------ tools/testing/selftests/timers/nsleep-lat.c | 36 ++----------------- tools/testing/selftests/timers/posix_timers.c | 36 ++++++++++--------- tools/testing/selftests/timers/raw_skew.c | 3 +- tools/testing/selftests/timers/set-2038.c | 3 +- tools/testing/selftests/timers/set-timer-lat.c | 41 +++------------------ tools/testing/selftests/timers/valid-adjtimex.c | 4 +-- 12 files changed, 47 insertions(+), 205 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/adjtick.c b/tools/testing/selftests/timers/adjtick.c index 22d274d5520f..68009a6d6de8 100644 --- a/tools/testing/selftests/timers/adjtick.c +++ b/tools/testing/selftests/timers/adjtick.c @@ -22,10 +22,9 @@ #include #include #include -#include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL -#define USEC_PER_SEC 1000000LL +#include "clock-helpers.h" +#include "kselftest.h" #define MILLION 1000000 diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c index d55d5b0377c6..120b3ce8b39e 100644 --- a/tools/testing/selftests/timers/alarmtimer-suspend.c +++ b/tools/testing/selftests/timers/alarmtimer-suspend.c @@ -29,10 +29,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - #define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15 @@ -40,37 +39,6 @@ int alarmcount; int alarm_clock_id; struct timespec start_time; - -char *clockstring(int clockid) -{ - switch (clockid) { - case CLOCK_REALTIME: - return "CLOCK_REALTIME"; - case CLOCK_MONOTONIC: - return "CLOCK_MONOTONIC"; - case CLOCK_PROCESS_CPUTIME_ID: - return "CLOCK_PROCESS_CPUTIME_ID"; - case CLOCK_THREAD_CPUTIME_ID: - return "CLOCK_THREAD_CPUTIME_ID"; - case CLOCK_MONOTONIC_RAW: - return "CLOCK_MONOTONIC_RAW"; - case CLOCK_REALTIME_COARSE: - return "CLOCK_REALTIME_COARSE"; - case CLOCK_MONOTONIC_COARSE: - return "CLOCK_MONOTONIC_COARSE"; - case CLOCK_BOOTTIME: - return "CLOCK_BOOTTIME"; - case CLOCK_REALTIME_ALARM: - return "CLOCK_REALTIME_ALARM"; - case CLOCK_BOOTTIME_ALARM: - return "CLOCK_BOOTTIME_ALARM"; - case CLOCK_TAI: - return "CLOCK_TAI"; - } - return "UNKNOWN_CLOCKID"; -} - - long long timespec_sub(struct timespec a, struct timespec b) { long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec; @@ -130,12 +98,12 @@ int main(void) alarmcount = 0; if (timer_create(alarm_clock_id, &se, &tm1) == -1) { printf("timer_create failed, %s unsupported?: %s\n", - clockstring(alarm_clock_id), strerror(errno)); + clock_name(alarm_clock_id), strerror(errno)); break; } clock_gettime(alarm_clock_id, &start_time); - printf("Start time (%s): %ld:%ld\n", clockstring(alarm_clock_id), + printf("Start time (%s): %ld:%ld\n", clock_name(alarm_clock_id), start_time.tv_sec, start_time.tv_nsec); printf("Setting alarm for every %i seconds\n", SUSPEND_SECS); its1.it_value = start_time; diff --git a/tools/testing/selftests/timers/inconsistency-check.c b/tools/testing/selftests/timers/inconsistency-check.c index 9ab7066b4e32..d7982ac4bd18 100644 --- a/tools/testing/selftests/timers/inconsistency-check.c +++ b/tools/testing/selftests/timers/inconsistency-check.c @@ -28,44 +28,14 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 #define CALLS_PER_LOOP 64 -char *clockstring(int clockid) -{ - switch (clockid) { - case CLOCK_REALTIME: - return "CLOCK_REALTIME"; - case CLOCK_MONOTONIC: - return "CLOCK_MONOTONIC"; - case CLOCK_PROCESS_CPUTIME_ID: - return "CLOCK_PROCESS_CPUTIME_ID"; - case CLOCK_THREAD_CPUTIME_ID: - return "CLOCK_THREAD_CPUTIME_ID"; - case CLOCK_MONOTONIC_RAW: - return "CLOCK_MONOTONIC_RAW"; - case CLOCK_REALTIME_COARSE: - return "CLOCK_REALTIME_COARSE"; - case CLOCK_MONOTONIC_COARSE: - return "CLOCK_MONOTONIC_COARSE"; - case CLOCK_BOOTTIME: - return "CLOCK_BOOTTIME"; - case CLOCK_REALTIME_ALARM: - return "CLOCK_REALTIME_ALARM"; - case CLOCK_BOOTTIME_ALARM: - return "CLOCK_BOOTTIME_ALARM"; - case CLOCK_TAI: - return "CLOCK_TAI"; - } - return "UNKNOWN_CLOCKID"; -} - /* returns 1 if a <= b, 0 otherwise */ static inline int in_order(struct timespec a, struct timespec b) { @@ -172,15 +142,15 @@ int main(int argc, char *argv[]) for (clockid = userclock; clockid < maxclocks; clockid++) { if (clockid == CLOCK_HWSPECIFIC || clock_gettime(clockid, &ts)) { - ksft_test_result_skip("%-31s\n", clockstring(clockid)); + ksft_test_result_skip("%-31s\n", clock_name(clockid)); continue; } if (consistency_test(clockid, runtime)) { - ksft_test_result_fail("%-31s\n", clockstring(clockid)); + ksft_test_result_fail("%-31s\n", clock_name(clockid)); ksft_exit_fail(); } else { - ksft_test_result_pass("%-31s\n", clockstring(clockid)); + ksft_test_result_pass("%-31s\n", clock_name(clockid)); } } ksft_exit_pass(); diff --git a/tools/testing/selftests/timers/leap-a-day.c b/tools/testing/selftests/timers/leap-a-day.c index 71b49734e08c..b93cb5714b37 100644 --- a/tools/testing/selftests/timers/leap-a-day.c +++ b/tools/testing/selftests/timers/leap-a-day.c @@ -51,10 +51,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - #define CLOCK_TAI 11 time_t next_leap; diff --git a/tools/testing/selftests/timers/mqueue-lat.c b/tools/testing/selftests/timers/mqueue-lat.c index ce8700c95cac..fa4c3e3f58fe 100644 --- a/tools/testing/selftests/timers/mqueue-lat.c +++ b/tools/testing/selftests/timers/mqueue-lat.c @@ -29,10 +29,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - #define TARGET_TIMEOUT 100000000 /* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */ diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index ceff18149a48..7df86dfc897f 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -27,44 +27,14 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 #define UNSUPPORTED 0xf00f -char *clockstring(int clockid) -{ - switch (clockid) { - case CLOCK_REALTIME: - return "CLOCK_REALTIME"; - case CLOCK_MONOTONIC: - return "CLOCK_MONOTONIC"; - case CLOCK_PROCESS_CPUTIME_ID: - return "CLOCK_PROCESS_CPUTIME_ID"; - case CLOCK_THREAD_CPUTIME_ID: - return "CLOCK_THREAD_CPUTIME_ID"; - case CLOCK_MONOTONIC_RAW: - return "CLOCK_MONOTONIC_RAW"; - case CLOCK_REALTIME_COARSE: - return "CLOCK_REALTIME_COARSE"; - case CLOCK_MONOTONIC_COARSE: - return "CLOCK_MONOTONIC_COARSE"; - case CLOCK_BOOTTIME: - return "CLOCK_BOOTTIME"; - case CLOCK_REALTIME_ALARM: - return "CLOCK_REALTIME_ALARM"; - case CLOCK_BOOTTIME_ALARM: - return "CLOCK_BOOTTIME_ALARM"; - case CLOCK_TAI: - return "CLOCK_TAI"; - }; - return "UNKNOWN_CLOCKID"; -} - /* returns 1 if a <= b, 0 otherwise */ static inline int in_order(struct timespec a, struct timespec b) { @@ -182,7 +152,7 @@ int main(int argc, char **argv) if (clockid == CLOCK_PROCESS_CPUTIME_ID || clockid == CLOCK_THREAD_CPUTIME_ID || clockid == CLOCK_HWSPECIFIC) { - ksft_test_result_skip("%-31s\n", clockstring(clockid)); + ksft_test_result_skip("%-31s\n", clock_name(clockid)); continue; } @@ -192,21 +162,21 @@ int main(int argc, char **argv) while (length <= (NSEC_PER_SEC * 10)) { ret = nanosleep_test(clockid, length); if (ret == UNSUPPORTED) { - ksft_test_result_skip("%-31s\n", clockstring(clockid)); + ksft_test_result_skip("%-31s\n", clock_name(clockid)); goto next; } if (ret < 0) { - ksft_test_result_fail("%-31s\n", clockstring(clockid)); + ksft_test_result_fail("%-31s\n", clock_name(clockid)); ksft_exit_fail(); } length *= 100; } ret = nanosleep_test_remaining(clockid); if (ret < 0) { - ksft_test_result_fail("%-31s\n", clockstring(clockid)); + ksft_test_result_fail("%-31s\n", clock_name(clockid)); ksft_exit_fail(); } - ksft_test_result_pass("%-31s\n", clockstring(clockid)); + ksft_test_result_pass("%-31s\n", clock_name(clockid)); next: ret = 0; } diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c index 15f6493ca7b5..d22973c87f21 100644 --- a/tools/testing/selftests/timers/nsleep-lat.c +++ b/tools/testing/selftests/timers/nsleep-lat.c @@ -24,10 +24,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */ /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ @@ -35,35 +34,6 @@ #define UNSUPPORTED 0xf00f -char *clockstring(int clockid) -{ - switch (clockid) { - case CLOCK_REALTIME: - return "CLOCK_REALTIME"; - case CLOCK_MONOTONIC: - return "CLOCK_MONOTONIC"; - case CLOCK_PROCESS_CPUTIME_ID: - return "CLOCK_PROCESS_CPUTIME_ID"; - case CLOCK_THREAD_CPUTIME_ID: - return "CLOCK_THREAD_CPUTIME_ID"; - case CLOCK_MONOTONIC_RAW: - return "CLOCK_MONOTONIC_RAW"; - case CLOCK_REALTIME_COARSE: - return "CLOCK_REALTIME_COARSE"; - case CLOCK_MONOTONIC_COARSE: - return "CLOCK_MONOTONIC_COARSE"; - case CLOCK_BOOTTIME: - return "CLOCK_BOOTTIME"; - case CLOCK_REALTIME_ALARM: - return "CLOCK_REALTIME_ALARM"; - case CLOCK_BOOTTIME_ALARM: - return "CLOCK_BOOTTIME_ALARM"; - case CLOCK_TAI: - return "CLOCK_TAI"; - }; - return "UNKNOWN_CLOCKID"; -} - struct timespec timespec_add(struct timespec ts, unsigned long long ns) { ts.tv_nsec += ns; @@ -156,10 +126,10 @@ int main(int argc, char **argv) } if (ret == UNSUPPORTED) { - ksft_test_result_skip("%s\n", clockstring(clockid)); + ksft_test_result_skip("%s\n", clock_name(clockid)); } else { ksft_test_result(ret >= 0, "%s\n", - clockstring(clockid)); + clock_name(clockid)); } } diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c index fa168d802f9e..a92d4b957747 100644 --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -19,11 +19,9 @@ #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL -#define USEC_PER_SEC 1000000LL - #define DELAY 2 static void __fatal_error(const char *test, const char *name, const char *what) @@ -143,8 +141,9 @@ static void check_itimer(int which, const char *name) ksft_test_result(check_diff(start, end) == 0, "%s\n", name); } -static void check_timer_create(int which, const char *name) +static void check_timer_create(int which) { + const char *name = clock_name(which); struct timespec start, end; struct itimerspec val = { .it_value.tv_sec = DELAY, @@ -457,8 +456,9 @@ static void check_delete(void) ksft_test_result(!tsig.signals, "check_delete\n"); } -static void check_sigev_none(int which, const char *name) +static void check_sigev_none(int which) { + const char *name = clock_name(which); struct timespec start, now; struct itimerspec its; struct sigevent sev; @@ -495,8 +495,9 @@ static void check_sigev_none(int which, const char *name) "check_sigev_none %s\n", name); } -static void check_gettime(int which, const char *name) +static void check_gettime(int which) { + const char *name = clock_name(which); struct itimerspec its, prev; struct timespec start, now; struct sigevent sev; @@ -548,8 +549,9 @@ static void check_gettime(int which, const char *name) ksft_test_result(wraps > 1, "check_gettime %s\n", name); } -static void check_overrun(int which, const char *name) +static void check_overrun(int which) { + const char *name = clock_name(which); struct timespec start, now; struct tmrsig tsig = { }; struct itimerspec its; @@ -691,7 +693,7 @@ int main(int argc, char **argv) check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); check_itimer(ITIMER_PROF, "ITIMER_PROF"); check_itimer(ITIMER_REAL, "ITIMER_REAL"); - check_timer_create(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); + check_timer_create(CLOCK_THREAD_CPUTIME_ID); /* * It's unfortunately hard to reliably test a timer expiration @@ -702,7 +704,7 @@ int main(int argc, char **argv) * to ensure true parallelism. So test only one thread until we * find a better solution. */ - check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); + check_timer_create(CLOCK_PROCESS_CPUTIME_ID); check_timer_distribution(); if (run_sig_ign_tests) { @@ -710,18 +712,18 @@ int main(int argc, char **argv) check_sig_ign(1); check_rearm(); check_delete(); - check_sigev_none(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); - check_sigev_none(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); - check_gettime(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); - check_gettime(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); - check_gettime(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); + check_sigev_none(CLOCK_MONOTONIC); + check_sigev_none(CLOCK_PROCESS_CPUTIME_ID); + check_gettime(CLOCK_MONOTONIC); + check_gettime(CLOCK_PROCESS_CPUTIME_ID); + check_gettime(CLOCK_THREAD_CPUTIME_ID); } else { ksft_print_msg("Skipping SIG_IGN tests on kernel < 6.13\n"); } - check_overrun(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); - check_overrun(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); - check_overrun(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); + check_overrun(CLOCK_MONOTONIC); + check_overrun(CLOCK_PROCESS_CPUTIME_ID); + check_overrun(CLOCK_THREAD_CPUTIME_ID); ksft_finished(); } diff --git a/tools/testing/selftests/timers/raw_skew.c b/tools/testing/selftests/timers/raw_skew.c index 2dd16cb4cdd0..0c87a8fb0d7f 100644 --- a/tools/testing/selftests/timers/raw_skew.c +++ b/tools/testing/selftests/timers/raw_skew.c @@ -25,10 +25,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - #define shift_right(x, s) ({ \ __typeof__(x) __x = (x); \ __typeof__(s) __s = (s); \ diff --git a/tools/testing/selftests/timers/set-2038.c b/tools/testing/selftests/timers/set-2038.c index c1235638406d..f522a3035ec6 100644 --- a/tools/testing/selftests/timers/set-2038.c +++ b/tools/testing/selftests/timers/set-2038.c @@ -27,10 +27,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - #define KTIME_MAX ((long long)~((unsigned long long)1 << 63)) #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) diff --git a/tools/testing/selftests/timers/set-timer-lat.c b/tools/testing/selftests/timers/set-timer-lat.c index e092c18befff..79ddba25d314 100644 --- a/tools/testing/selftests/timers/set-timer-lat.c +++ b/tools/testing/selftests/timers/set-timer-lat.c @@ -28,10 +28,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL - /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 @@ -44,36 +43,6 @@ struct timespec start_time; long long max_latency_ns; int timer_fired_early; -char *clockstring(int clockid) -{ - switch (clockid) { - case CLOCK_REALTIME: - return "CLOCK_REALTIME"; - case CLOCK_MONOTONIC: - return "CLOCK_MONOTONIC"; - case CLOCK_PROCESS_CPUTIME_ID: - return "CLOCK_PROCESS_CPUTIME_ID"; - case CLOCK_THREAD_CPUTIME_ID: - return "CLOCK_THREAD_CPUTIME_ID"; - case CLOCK_MONOTONIC_RAW: - return "CLOCK_MONOTONIC_RAW"; - case CLOCK_REALTIME_COARSE: - return "CLOCK_REALTIME_COARSE"; - case CLOCK_MONOTONIC_COARSE: - return "CLOCK_MONOTONIC_COARSE"; - case CLOCK_BOOTTIME: - return "CLOCK_BOOTTIME"; - case CLOCK_REALTIME_ALARM: - return "CLOCK_REALTIME_ALARM"; - case CLOCK_BOOTTIME_ALARM: - return "CLOCK_BOOTTIME_ALARM"; - case CLOCK_TAI: - return "CLOCK_TAI"; - } - return "UNKNOWN_CLOCKID"; -} - - long long timespec_sub(struct timespec a, struct timespec b) { long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec; @@ -104,7 +73,7 @@ void sigalarm(int signo) void describe_timer(int flags, int interval) { printf("%-22s %s %s ", - clockstring(clock_id), + clock_name(clock_id), flags ? "ABSTIME":"RELTIME", interval ? "PERIODIC":"ONE-SHOT"); } @@ -130,12 +99,12 @@ int setup_timer(int clock_id, int flags, int interval, timer_t *tm1) if ((clock_id == CLOCK_REALTIME_ALARM) || (clock_id == CLOCK_BOOTTIME_ALARM)) { printf("%-22s %s missing CAP_WAKE_ALARM? : [UNSUPPORTED]\n", - clockstring(clock_id), + clock_name(clock_id), flags ? "ABSTIME":"RELTIME"); /* Indicate timer isn't set, so caller doesn't wait */ return 1; } - printf("%s - timer_create() failed\n", clockstring(clock_id)); + printf("%s - timer_create() failed\n", clock_name(clock_id)); return -1; } @@ -152,7 +121,7 @@ int setup_timer(int clock_id, int flags, int interval, timer_t *tm1) err = timer_settime(*tm1, flags, &its1, &its2); if (err) { - printf("%s - timer_settime() failed\n", clockstring(clock_id)); + printf("%s - timer_settime() failed\n", clock_name(clock_id)); return -1; } diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c index dc2559eb11a5..f641d5fb0902 100644 --- a/tools/testing/selftests/timers/valid-adjtimex.c +++ b/tools/testing/selftests/timers/valid-adjtimex.c @@ -29,11 +29,9 @@ #include #include #include +#include "clock-helpers.h" #include "kselftest.h" -#define NSEC_PER_SEC 1000000000LL -#define USEC_PER_SEC 1000000LL - #define ADJ_SETOFFSET 0x0100 #include -- cgit From 142a2d312e7bb8e5ad524a4f2115523cf4904732 Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:39 +0200 Subject: selftests: timers: nanosleep: Drop output alignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The padding of the clock name is pointless as nothing else is printed. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-8-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nanosleep.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index 7df86dfc897f..629e800135ad 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -152,7 +152,7 @@ int main(int argc, char **argv) if (clockid == CLOCK_PROCESS_CPUTIME_ID || clockid == CLOCK_THREAD_CPUTIME_ID || clockid == CLOCK_HWSPECIFIC) { - ksft_test_result_skip("%-31s\n", clock_name(clockid)); + ksft_test_result_skip("%s\n", clock_name(clockid)); continue; } @@ -162,21 +162,21 @@ int main(int argc, char **argv) while (length <= (NSEC_PER_SEC * 10)) { ret = nanosleep_test(clockid, length); if (ret == UNSUPPORTED) { - ksft_test_result_skip("%-31s\n", clock_name(clockid)); + ksft_test_result_skip("%s\n", clock_name(clockid)); goto next; } if (ret < 0) { - ksft_test_result_fail("%-31s\n", clock_name(clockid)); + ksft_test_result_fail("%s\n", clock_name(clockid)); ksft_exit_fail(); } length *= 100; } ret = nanosleep_test_remaining(clockid); if (ret < 0) { - ksft_test_result_fail("%-31s\n", clock_name(clockid)); + ksft_test_result_fail("%s\n", clock_name(clockid)); ksft_exit_fail(); } - ksft_test_result_pass("%-31s\n", clock_name(clockid)); + ksft_test_result_pass("%s\n", clock_name(clockid)); next: ret = 0; } -- cgit From 2999620932be363a97c1028f5636deefe08adb06 Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:40 +0200 Subject: selftests: timers: nanosleep: Explicitly list the tested clocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test relies on the numeric values of the clockids. As not all clocks are meant to be tested, this requires skipping some clocks unconditionally, cluttering the result. Use an equivalent, explicit list instead. In addition to CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID and CLOCK_HWSPECIFIC which were unconditionally skipped before, this also unconditionally skips CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE. clock_nanosleep() is not supported for those. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-9-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nanosleep.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index 629e800135ad..9dd662226261 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -30,9 +30,6 @@ #include "clock-helpers.h" #include "kselftest.h" -/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ -#define CLOCK_HWSPECIFIC 10 - #define UNSUPPORTED 0xf00f /* returns 1 if a <= b, 0 otherwise */ @@ -141,20 +138,21 @@ int main(int argc, char **argv) { long long length; int clockid, ret; - int max_clocks = CLOCK_TAI + 1; - ksft_print_header(); - ksft_set_plan(max_clocks); + static const clockid_t tested_clocks[] = { + CLOCK_REALTIME, + CLOCK_MONOTONIC, + CLOCK_BOOTTIME, + CLOCK_BOOTTIME_ALARM, + CLOCK_REALTIME_ALARM, + CLOCK_TAI, + }; - for (clockid = CLOCK_REALTIME; clockid < max_clocks; clockid++) { + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(tested_clocks)); - /* Skip cputime clockids since nanosleep won't increment cputime */ - if (clockid == CLOCK_PROCESS_CPUTIME_ID || - clockid == CLOCK_THREAD_CPUTIME_ID || - clockid == CLOCK_HWSPECIFIC) { - ksft_test_result_skip("%s\n", clock_name(clockid)); - continue; - } + for (size_t clock_index = 0; clock_index < ARRAY_SIZE(tested_clocks); clock_index++) { + clockid = tested_clocks[clock_index]; fflush(stdout); -- cgit From bfe5bf0c5fab93f90d851688fe2d65590339ecec Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:41 +0200 Subject: selftests: timers: nanosleep: Reuse kselftest error numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test currently defines its own custom error numbers. These mirror the semantics from the standard KFST_* constants. To make the code easier to read, use the standard result constants. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-10-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nanosleep.c | 36 ++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index 9dd662226261..0a2ca1791f27 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -30,8 +30,6 @@ #include "clock-helpers.h" #include "kselftest.h" -#define UNSUPPORTED 0xf00f - /* returns 1 if a <= b, 0 otherwise */ static inline int in_order(struct timespec a, struct timespec b) { @@ -60,15 +58,15 @@ int nanosleep_test(int clockid, long long ns) /* First check abs time */ if (clock_gettime(clockid, &now)) - return UNSUPPORTED; + return KSFT_SKIP; target = timespec_add(now, ns); if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL)) - return UNSUPPORTED; + return KSFT_SKIP; clock_gettime(clockid, &now); if (!in_order(target, now)) - return -1; + return KSFT_FAIL; /* Second check reltime */ clock_gettime(clockid, &now); @@ -80,8 +78,8 @@ int nanosleep_test(int clockid, long long ns) clock_gettime(clockid, &now); if (!in_order(target, now)) - return -1; - return 0; + return KSFT_FAIL; + return KSFT_PASS; } static void dummy_event_handler(int val) @@ -100,38 +98,38 @@ static int nanosleep_test_remaining(int clockid) sa.sa_handler = dummy_event_handler; ret = sigaction(SIGALRM, &sa, NULL); if (ret) - return -1; + return KSFT_FAIL; ret = timer_create(clockid, NULL, &timer); if (ret) - return -1; + return KSFT_FAIL; itimer.it_value.tv_nsec = NSEC_PER_SEC / 4; ret = timer_settime(timer, 0, &itimer, NULL); if (ret) - return -1; + return KSFT_FAIL; rqtp.tv_nsec = NSEC_PER_SEC / 2; ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp); if (ret != EINTR) - return -1; + return KSFT_FAIL; ret = timer_delete(timer); if (ret) - return -1; + return KSFT_FAIL; sa.sa_handler = SIG_DFL; ret = sigaction(SIGALRM, &sa, NULL); if (ret) - return -1; + return KSFT_FAIL; if (!in_order((struct timespec) {}, rmtp)) - return -1; + return KSFT_FAIL; if (!in_order(rmtp, rqtp)) - return -1; + return KSFT_FAIL; - return 0; + return KSFT_PASS; } int main(int argc, char **argv) @@ -159,18 +157,18 @@ int main(int argc, char **argv) length = 10; while (length <= (NSEC_PER_SEC * 10)) { ret = nanosleep_test(clockid, length); - if (ret == UNSUPPORTED) { + if (ret == KSFT_SKIP) { ksft_test_result_skip("%s\n", clock_name(clockid)); goto next; } - if (ret < 0) { + if (ret == KSFT_FAIL) { ksft_test_result_fail("%s\n", clock_name(clockid)); ksft_exit_fail(); } length *= 100; } ret = nanosleep_test_remaining(clockid); - if (ret < 0) { + if (ret == KSFT_FAIL) { ksft_test_result_fail("%s\n", clock_name(clockid)); ksft_exit_fail(); } -- cgit From 061341a568146000de8d41791bf7612b2f6813b5 Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:42 +0200 Subject: selftests: timers: nanosleep: Move all single clock tests out of the loop in main() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make the code easier to read by avoiding a goto. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-11-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nanosleep.c | 51 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index 0a2ca1791f27..5037481516df 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -132,10 +132,35 @@ static int nanosleep_test_remaining(int clockid) return KSFT_PASS; } +static void nanosleep_test_clock(clockid_t clockid) +{ + long long length = 10; + int ret; + + while (length <= (NSEC_PER_SEC * 10)) { + ret = nanosleep_test(clockid, length); + if (ret == KSFT_SKIP) { + ksft_test_result_skip("%s\n", clock_name(clockid)); + return; + } + if (ret == KSFT_FAIL) { + ksft_test_result_fail("%s\n", clock_name(clockid)); + ksft_exit_fail(); + } + length *= 100; + } + + ret = nanosleep_test_remaining(clockid); + if (ret == KSFT_FAIL) { + ksft_test_result_fail("%s\n", clock_name(clockid)); + ksft_exit_fail(); + } + ksft_test_result_pass("%s\n", clock_name(clockid)); +} + int main(int argc, char **argv) { - long long length; - int clockid, ret; + int clockid; static const clockid_t tested_clocks[] = { CLOCK_REALTIME, @@ -154,27 +179,7 @@ int main(int argc, char **argv) fflush(stdout); - length = 10; - while (length <= (NSEC_PER_SEC * 10)) { - ret = nanosleep_test(clockid, length); - if (ret == KSFT_SKIP) { - ksft_test_result_skip("%s\n", clock_name(clockid)); - goto next; - } - if (ret == KSFT_FAIL) { - ksft_test_result_fail("%s\n", clock_name(clockid)); - ksft_exit_fail(); - } - length *= 100; - } - ret = nanosleep_test_remaining(clockid); - if (ret == KSFT_FAIL) { - ksft_test_result_fail("%s\n", clock_name(clockid)); - ksft_exit_fail(); - } - ksft_test_result_pass("%s\n", clock_name(clockid)); -next: - ret = 0; + nanosleep_test_clock(clockid); } ksft_exit_pass(); } -- cgit From f2ff28a52255f04ab0de16a7d298e35360852890 Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:43 +0200 Subject: selftests: timers: nanosleep: Explicitly handle timer_delete() failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An upcoming patch will remove the early exit on failure of a single testcase. If a test fails because a timeout timer could not be deleted, then that timeout timer might interfere with the following tettcases. Exit the whole test case with a clear error message in this case. Other failures are not problematic: * If timer_settime() fails, a harmless unarmed timer is left around. * The SIGALRM handler reconfigured for each call to nanosleep_test_remaining() anyways. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-12-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nanosleep.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index 5037481516df..dd899dcfb2cb 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -111,11 +111,14 @@ static int nanosleep_test_remaining(int clockid) rqtp.tv_nsec = NSEC_PER_SEC / 2; ret = clock_nanosleep(clockid, 0, &rqtp, &rmtp); - if (ret != EINTR) - return KSFT_FAIL; - ret = timer_delete(timer); - if (ret) + if (timer_delete(timer)) { + ksft_exit_fail_msg("Unable to delete the timeout timer for %s. " + "This might interfere with following testcases.\n", + clock_name(clockid)); + } + + if (ret != EINTR) return KSFT_FAIL; sa.sa_handler = SIG_DFL; -- cgit From a1211288234058b0d13e2cc4a910b556c5308fdd Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:44 +0200 Subject: selftests: timers: nanosleep: Report each test separately MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the test for the sleep functionality itself and the one for the remaining time are reported together. This makes the test output a bit confusing, especially as the upcoming tests for auxiliary clocks will have different results between the two parts. Report each test part on its own. This also allows continuing testing other clocks after a single one has failed. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-13-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nanosleep.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c index dd899dcfb2cb..b45e4c855259 100644 --- a/tools/testing/selftests/timers/nanosleep.c +++ b/tools/testing/selftests/timers/nanosleep.c @@ -142,23 +142,18 @@ static void nanosleep_test_clock(clockid_t clockid) while (length <= (NSEC_PER_SEC * 10)) { ret = nanosleep_test(clockid, length); - if (ret == KSFT_SKIP) { - ksft_test_result_skip("%s\n", clock_name(clockid)); + if (ret != KSFT_PASS) { + ksft_test_result_report(ret, "%s\n", clock_name(clockid)); + ksft_test_result_skip("%s (remaining)\n", clock_name(clockid)); return; } - if (ret == KSFT_FAIL) { - ksft_test_result_fail("%s\n", clock_name(clockid)); - ksft_exit_fail(); - } + length *= 100; } + ksft_test_result_pass("%s\n", clock_name(clockid)); ret = nanosleep_test_remaining(clockid); - if (ret == KSFT_FAIL) { - ksft_test_result_fail("%s\n", clock_name(clockid)); - ksft_exit_fail(); - } - ksft_test_result_pass("%s\n", clock_name(clockid)); + ksft_test_result_report(ret, "%s (remaining)\n", clock_name(clockid)); } int main(int argc, char **argv) @@ -175,7 +170,7 @@ int main(int argc, char **argv) }; ksft_print_header(); - ksft_set_plan(ARRAY_SIZE(tested_clocks)); + ksft_set_plan(ARRAY_SIZE(tested_clocks) * 2); for (size_t clock_index = 0; clock_index < ARRAY_SIZE(tested_clocks); clock_index++) { clockid = tested_clocks[clock_index]; @@ -184,5 +179,5 @@ int main(int argc, char **argv) nanosleep_test_clock(clockid); } - ksft_exit_pass(); + ksft_finished(); } -- cgit From a00f90877dcd4176da3564f5f126547aa3ddfead Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:45 +0200 Subject: selftests: timers: nsleep-lat: Use NSEC_PER_MSEC define for unreasonable latency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the predefined symbol to make the code easier to understand. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-14-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nsleep-lat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c index d22973c87f21..d46f17404439 100644 --- a/tools/testing/selftests/timers/nsleep-lat.c +++ b/tools/testing/selftests/timers/nsleep-lat.c @@ -27,7 +27,7 @@ #include "clock-helpers.h" #include "kselftest.h" -#define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */ +#define UNRESONABLE_LATENCY (40 * NSEC_PER_MSEC) /* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ #define CLOCK_HWSPECIFIC 10 -- cgit From 187ac443ed51e04dcf0df98d270863c3f54af8fc Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:46 +0200 Subject: selftests: timers: nsleep-lat: Explicitly list the tested clocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test relies on the numeric values of the clockids. As not all clocks are meant to be tested, this requires skipping some clocks unconditionally, cluttering the result. Use an equivalent, explicit list instead. In addition to CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID and CLOCK_HWSPECIFIC which were unconditionally skipped before, this also unconditionally skips CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE. clock_nanosleep() is not supported for those. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-15-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nsleep-lat.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c index d46f17404439..af47c28f086b 100644 --- a/tools/testing/selftests/timers/nsleep-lat.c +++ b/tools/testing/selftests/timers/nsleep-lat.c @@ -29,9 +29,6 @@ #define UNRESONABLE_LATENCY (40 * NSEC_PER_MSEC) -/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ -#define CLOCK_HWSPECIFIC 10 - #define UNSUPPORTED 0xf00f struct timespec timespec_add(struct timespec ts, unsigned long long ns) @@ -97,24 +94,25 @@ int nanosleep_lat_test(int clockid, long long ns) return 0; } -#define SKIPPED_CLOCK_COUNT 3 - int main(int argc, char **argv) { long long length; int clockid, ret; - int max_clocks = CLOCK_TAI + 1; - ksft_print_header(); - ksft_set_plan(max_clocks - CLOCK_REALTIME - SKIPPED_CLOCK_COUNT); + static const clockid_t tested_clocks[] = { + CLOCK_REALTIME, + CLOCK_MONOTONIC, + CLOCK_BOOTTIME, + CLOCK_BOOTTIME_ALARM, + CLOCK_REALTIME_ALARM, + CLOCK_TAI, + }; - for (clockid = CLOCK_REALTIME; clockid < max_clocks; clockid++) { + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(tested_clocks)); - /* Skip cputime clockids since nanosleep won't increment cputime */ - if (clockid == CLOCK_PROCESS_CPUTIME_ID || - clockid == CLOCK_THREAD_CPUTIME_ID || - clockid == CLOCK_HWSPECIFIC) - continue; + for (size_t clock_index = 0; clock_index < ARRAY_SIZE(tested_clocks); clock_index++) { + clockid = tested_clocks[clock_index]; length = 10; while (length <= (NSEC_PER_SEC * 10)) { -- cgit From 0f474ee80c7ba1cab784c12a12232c53db5ad1fb Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:47 +0200 Subject: selftests: timers: nsleep-lat: Reuse kselftest error numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test currently defines its own custom error numbers. These mirror the semantics from the standard KFST_* constants. To make the code easier to read, use the standard result constants. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-16-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nsleep-lat.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c index af47c28f086b..266ca0cc37ec 100644 --- a/tools/testing/selftests/timers/nsleep-lat.c +++ b/tools/testing/selftests/timers/nsleep-lat.c @@ -29,8 +29,6 @@ #define UNRESONABLE_LATENCY (40 * NSEC_PER_MSEC) -#define UNSUPPORTED 0xf00f - struct timespec timespec_add(struct timespec ts, unsigned long long ns) { ts.tv_nsec += ns; @@ -60,9 +58,9 @@ int nanosleep_lat_test(int clockid, long long ns) target.tv_nsec = ns%NSEC_PER_SEC; if (clock_gettime(clockid, &start)) - return UNSUPPORTED; + return KSFT_SKIP; if (clock_nanosleep(clockid, 0, &target, NULL)) - return UNSUPPORTED; + return KSFT_SKIP; count = 10; @@ -74,7 +72,7 @@ int nanosleep_lat_test(int clockid, long long ns) if (((timespec_sub(start, end)/count)-ns) > UNRESONABLE_LATENCY) { ksft_print_msg("Large rel latency: %lld ns :", (timespec_sub(start, end)/count)-ns); - return -1; + return KSFT_FAIL; } /* Next check absolute latency */ @@ -88,10 +86,10 @@ int nanosleep_lat_test(int clockid, long long ns) if (latency/count > UNRESONABLE_LATENCY) { ksft_print_msg("Large abs latency: %lld ns :", latency/count); - return -1; + return KSFT_FAIL; } - return 0; + return KSFT_PASS; } int main(int argc, char **argv) @@ -123,12 +121,7 @@ int main(int argc, char **argv) } - if (ret == UNSUPPORTED) { - ksft_test_result_skip("%s\n", clock_name(clockid)); - } else { - ksft_test_result(ret >= 0, "%s\n", - clock_name(clockid)); - } + ksft_test_result_report(ret, "%s\n", clock_name(clockid)); } ksft_finished(); -- cgit From 4fa377c19e111c539a530a8200996b911ceff9ff Mon Sep 17 00:00:00 2001 From: "Thomas Weißschuh (Schneider Electric)" Date: Mon, 3 Aug 2026 12:04:48 +0200 Subject: selftests: timers: nsleep-lat: Check all calls to clock_nanosleep() and clock_gettime() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both these functions can fail. The first calls to those functions are already checked and result in KSFT_SKIP. If they start failing afterwards unexpectedly, report a hard error. Signed-off-by: Thomas Weißschuh (Schneider Electric) Signed-off-by: Thomas Gleixner Acked-by: John Stultz Link: https://patch.msgid.link/20260803-auxclock-nanosleep-prep-v2-17-910cbd485390@linutronix.de --- tools/testing/selftests/timers/nsleep-lat.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'tools/testing') diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c index 266ca0cc37ec..5de0051ac8e3 100644 --- a/tools/testing/selftests/timers/nsleep-lat.c +++ b/tools/testing/selftests/timers/nsleep-lat.c @@ -65,10 +65,16 @@ int nanosleep_lat_test(int clockid, long long ns) count = 10; /* First check relative latency */ - clock_gettime(clockid, &start); - for (i = 0; i < count; i++) - clock_nanosleep(clockid, 0, &target, NULL); - clock_gettime(clockid, &end); + if (clock_gettime(clockid, &start)) + return KSFT_FAIL; + + for (i = 0; i < count; i++) { + if (clock_nanosleep(clockid, 0, &target, NULL)) + return KSFT_FAIL; + } + + if (clock_gettime(clockid, &end)) + return KSFT_FAIL; if (((timespec_sub(start, end)/count)-ns) > UNRESONABLE_LATENCY) { ksft_print_msg("Large rel latency: %lld ns :", (timespec_sub(start, end)/count)-ns); @@ -77,10 +83,13 @@ int nanosleep_lat_test(int clockid, long long ns) /* Next check absolute latency */ for (i = 0; i < count; i++) { - clock_gettime(clockid, &start); + if (clock_gettime(clockid, &start)) + return KSFT_FAIL; target = timespec_add(start, ns); - clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL); - clock_gettime(clockid, &end); + if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL)) + return KSFT_FAIL; + if (clock_gettime(clockid, &end)) + return KSFT_FAIL; latency += timespec_sub(target, end); } -- cgit