[patch 06/45] selftests/timers/posix-timers: Validate timer_gettime()
From: Thomas Gleixner
Date: Tue Jun 06 2023 - 10:37:53 EST
timer_gettime() must return the correct expiry time for interval timers
even when the timer is not armed, which is the case when a signal is
pending but blocked.
Works correctly for regular posix timers, but not for posix CPU timers.
Add a selftest to validate the fixes.
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
tools/testing/selftests/timers/posix_timers.c | 60 +++++++++++++++++++++++++-
1 file changed, 59 insertions(+), 1 deletion(-)
--- a/tools/testing/selftests/timers/posix_timers.c
+++ b/tools/testing/selftests/timers/posix_timers.c
@@ -523,10 +523,65 @@ static void check_sigev_none(int which,
ksft_test_result_fail("check_sigev_none %s\n", name);
}
+static void check_gettime(int which, const char *name)
+{
+ struct itimerspec its, prev;
+ struct timespec start, now;
+ struct sigevent sev;
+ timer_t timerid;
+ int wraps = 0;
+ sigset_t set;
+
+ /* Block the signal */
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ if (sigprocmask(SIG_BLOCK, &set, NULL))
+ fatal_error(name, "sigprocmask(SIG_BLOCK)");
+
+ memset(&sev, 0, sizeof(sev));
+ sev.sigev_notify = SIGEV_SIGNAL;
+ sev.sigev_signo = SIGUSR1;
+
+ if (timer_create(which, &sev, &timerid))
+ fatal_error(name, "timer_create()");
+
+ /* Start the timer to expire in 100ms and 100ms intervals */
+ its.it_value.tv_sec = 0;
+ its.it_value.tv_nsec = 100000000;
+ its.it_interval.tv_sec = 0;
+ its.it_interval.tv_nsec = 100000000;
+ timer_settime(timerid, 0, &its, NULL);
+
+ if (timer_gettime(timerid, &prev))
+ fatal_error(name, "timer_gettime()");
+
+ if (clock_gettime(which, &start))
+ fatal_error(name, "clock_gettime()");
+
+ do {
+ if (clock_gettime(which, &now))
+ fatal_error(name, "clock_gettime()");
+ if (timer_gettime(timerid, &its))
+ fatal_error(name, "timer_gettime()");
+ if (its.it_value.tv_nsec > prev.it_value.tv_nsec)
+ wraps++;
+ prev = its;
+
+ } while (calcdiff_ns(now, start) < NSECS_PER_SEC);
+
+ if (timer_delete(timerid))
+ fatal_error(name, "timer_delete()");
+
+ if (wraps > 1)
+ ksft_test_result_pass("check_gettime %s\n", name);
+ else
+ ksft_test_result_fail("check_gettime %s\n", name);
+}
+
int main(int argc, char **argv)
{
ksft_print_header();
- ksft_set_plan(12);
+ ksft_set_plan(15);
check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL");
check_itimer(ITIMER_PROF, "ITIMER_PROF");
@@ -550,6 +605,9 @@ int main(int argc, char **argv)
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");
ksft_finished();
}