Skip to contentSkip to conversation

selftests: timers: Use CPU time for CPU timers in posix_timers test

PATCH Superseded Wake Liu 2 participants 4 messages Jul 8 – Jul 21

Subsystems (3)
lore
Share & tools
lore

Wake Liu

author
#0 lore raw

PATCH 1/1 selftests: timers: Use CPU time for CPU timers in posix_timers test

The posix_timers test compares the elapsed real time (GTOD) with the expected timer delay to verify if the timer expired. This works fine for real-time timers (ITIMER_REAL), but is flaky for CPU-time timers (ITIMER_PROF, ITIMER_VIRTUAL, and CLOCK_*_CPUTIME_ID) on loaded systems (like shared test runners) because the test process might be scheduled out, resulting in elapsed real time being larger than the consumed CPU time.

Fix this by using the appropriate measurement method for each timer:
- Use CPU process/thread time (via clock_gettime or getrusage) for CPU timers.
- Keep using monotonic clock (real time) for real-time timers.

This makes the test robust against scheduling delays.

---
 tools/testing/selftests/timers/posix_timers.c | 65 +++++++++++++------
 1 file changed, 46 insertions(+), 19 deletions(-)
tools/testing/selftests/timers/posix_timers.c +46 -19
index 6f78b6068589..55c5f96ea6c8 100644
@@ -79,16 +79,39 @@ static void sig_handler(int nr)
 	done = 1;
 }
 
-/*
- * Check the expected timer expiration matches the GTOD elapsed delta since
- * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
- */
-static int check_diff(struct timeval start, struct timeval end)
+#include <sys/resource.h>
+
+static long long get_clock_time_us(clockid_t clock_id)
+{
+	struct timespec ts;
+	if (clock_gettime(clock_id, &ts) < 0)
+		return -1;
+	return (long long)ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
+}
+
+static long long get_itimer_time_us(int which)
+{
+	struct rusage usage;
+
+	switch (which) {
+	case ITIMER_REAL:
+		return get_clock_time_us(CLOCK_MONOTONIC);
+	case ITIMER_PROF:
+		return get_clock_time_us(CLOCK_PROCESS_CPUTIME_ID);
+	case ITIMER_VIRTUAL:
+		if (getrusage(RUSAGE_SELF, &usage) < 0)
+			return -1;
+		return (long long)usage.ru_utime.tv_sec * USECS_PER_SEC + usage.ru_utime.tv_usec;
+	default:
+		return -1;
+	}
+}
+
+static int check_diff_us(long long start, long long end)
 {
 	long long diff;
 
-	diff = end.tv_usec - start.tv_usec;
-	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
+	diff = end - start;
 
 	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
 		printf("Diff too high: %lld..", diff);
@@ -100,7 +123,7 @@ static int check_diff(struct timeval start, struct timeval end)
 
 static void check_itimer(int which, const char *name)
 {
-	struct timeval start, end;
+	long long start, end;
 	struct itimerval val = {
 		.it_value.tv_sec = DELAY,
 	};
@@ -114,8 +137,9 @@ static void check_itimer(int which, const char *name)
 	else if (which == ITIMER_REAL)
 		signal(SIGALRM, sig_handler);
 
-	if (gettimeofday(&start, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	start = get_itimer_time_us(which);
+	if (start < 0)
+		fatal_error(name, "get_itimer_time_us()");
 
 	if (setitimer(which, &val, NULL) < 0)
 		fatal_error(name, "setitimer()");
@@ -127,15 +151,16 @@ static void check_itimer(int which, const char *name)
 	else if (which == ITIMER_REAL)
 		idle_loop();
 
-	if (gettimeofday(&end, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	end = get_itimer_time_us(which);
+	if (end < 0)
+		fatal_error(name, "get_itimer_time_us()");
 
-	ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
+	ksft_test_result(check_diff_us(start, end) == 0, "%s\n", name);
 }
 
 static void check_timer_create(int which, const char *name)
 {
-	struct timeval start, end;
+	long long start, end;
 	struct itimerspec val = {
 		.it_value.tv_sec = DELAY,
 	};
@@ -149,18 +174,20 @@ static void check_timer_create(int which, const char *name)
 	if (signal(SIGALRM, sig_handler) == SIG_ERR)
 		fatal_error(name, "signal()");
 
-	if (gettimeofday(&start, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	start = get_clock_time_us(which);
+	if (start < 0)
+		fatal_error(name, "get_clock_time_us()");
 
 	if (timer_settime(id, 0, &val, NULL) < 0)
 		fatal_error(name, "timer_settime()");
 
 	user_loop();
 
-	if (gettimeofday(&end, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	end = get_clock_time_us(which);
+	if (end < 0)
+		fatal_error(name, "get_clock_time_us()");
 
-	ksft_test_result(check_diff(start, end) == 0,
+	ksft_test_result(check_diff_us(start, end) == 0,
 			 "timer_create() per %s\n", name);
 }
 
Signature · 1 line
2.55.0.795.g602f6c329a-goog
maintainer
#1 lore raw
Quoted 8 lines · Wake Liu

The posix_timers test compares the elapsed real time (GTOD) with the expected timer delay to verify if the timer expired. This works fine for real-time timers (ITIMER_REAL), but is flaky for CPU-time timers (ITIMER_PROF, ITIMER_VIRTUAL, and CLOCK_*_CPUTIME_ID) on loaded systems (like shared test runners) because the test process might be scheduled out, resulting in elapsed real time being larger than the consumed CPU time.

Quoted 6 lines

Fix this by using the appropriate measurement method for each timer:
- Use CPU process/thread time (via clock_gettime or getrusage) for CPU timers.
- Keep using monotonic clock (real time) for real-time timers.

This makes the test robust against scheduling delays.

Lacks Signed-off-by ....

tools/testing/selftests/timers/posix_timers.c static void sig_handler(int nr) · line 86

Quoted 5 lines
tools/testing/selftests/timers/posix_timers.c +5 -0
+#include <sys/resource.h>
+
+static long long get_clock_time_us(clockid_t clock_id)
+{
+	struct timespec ts;

Newline between declaration and code.

Quoted 20 lines
tools/testing/selftests/timers/posix_timers.c +20 -0
+	if (clock_gettime(clock_id, &ts) < 0)
+		return -1;
+	return (long long)ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
+}
+
+static long long get_itimer_time_us(int which)
+{
+	struct rusage usage;
+
+	switch (which) {
+	case ITIMER_REAL:
+		return get_clock_time_us(CLOCK_MONOTONIC);
+	case ITIMER_PROF:
+		return get_clock_time_us(CLOCK_PROCESS_CPUTIME_ID);
+	case ITIMER_VIRTUAL:
+		if (getrusage(RUSAGE_SELF, &usage) < 0)
+			return -1;
+		return (long long)usage.ru_utime.tv_sec * USECS_PER_SEC + usage.ru_utime.tv_usec;
+	default:
+		return -1;

tools/testing/selftests/timers/posix_timers.c static void sig_handler(int nr) · line 114

Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +5 -2
+	}
+}
+
+static int check_diff_us(long long start, long long end)
 {
 	long long diff;
 
-	diff = end.tv_usec - start.tv_usec;
-	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
+	diff = end - start;

Move that to the declaration line.

tools/testing/selftests/timers/posix_timers.c static int check_diff(struct timeval start, struct timeval end) · line 129

Quoted 13 lines
tools/testing/selftests/timers/posix_timers.c +4 -3
 	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
 		printf("Diff too high: %lld..", diff);
@@ -100,7 +123,7 @@ static int check_diff(struct timeval start, struct timeval end)
 
-	if (gettimeofday(&end, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	end = get_clock_time_us(which);
+	if (end < 0)
+		fatal_error(name, "get_clock_time_us()");
 
-	ksft_test_result(check_diff(start, end) == 0,
+	ksft_test_result(check_diff_us(start, end) == 0,
 			 "timer_create() per %s\n", name);

Please get rid of the line break.

author
#2 lore raw

PATCH v2 1/1 selftests: timers: Use CPU time for CPU timers in posix_timers test

The posix_timers test compares the elapsed real time (GTOD) with the expected timer delay to verify if the timer expired. This works fine for real-time timers (ITIMER_REAL), but is flaky for CPU-time timers (ITIMER_PROF, ITIMER_VIRTUAL, and CLOCK_*_CPUTIME_ID) on loaded systems (like shared test runners) because the test process might be scheduled out, resulting in elapsed real time being larger than the consumed CPU time.

Fix this by using the appropriate measurement method for each timer:
- Use CPU process/thread time (via clock_gettime or getrusage) for CPU timers.
- Keep using monotonic clock (real time) for real-time timers.

This makes the test robust against scheduling delays.

---
v2:
- Add Signed-off-by.
- Fix coding style issues:
- Add newline after variable declaration in get_clock_time_us().
- Merge variable declaration and assignment in check_diff_us().
- Remove unnecessary line break in ksft_test_result() call.

 tools/testing/selftests/timers/posix_timers.c | 67 +++++++++++++------
 1 file changed, 46 insertions(+), 21 deletions(-)
tools/testing/selftests/timers/posix_timers.c +46 -21
index 6f78b6068589..02eb9d5c769c 100644
@@ -79,16 +79,38 @@ static void sig_handler(int nr)
 	done = 1;
 }
 
-/*
- * Check the expected timer expiration matches the GTOD elapsed delta since
- * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
- */
-static int check_diff(struct timeval start, struct timeval end)
+#include <sys/resource.h>
+
+static long long get_clock_time_us(clockid_t clock_id)
+{
+	struct timespec ts;
+
+	if (clock_gettime(clock_id, &ts) < 0)
+		return -1;
+	return (long long)ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
+}
+
+static long long get_itimer_time_us(int which)
 {
-	long long diff;
+	struct rusage usage;
+
+	switch (which) {
+	case ITIMER_REAL:
+		return get_clock_time_us(CLOCK_MONOTONIC);
+	case ITIMER_PROF:
+		return get_clock_time_us(CLOCK_PROCESS_CPUTIME_ID);
+	case ITIMER_VIRTUAL:
+		if (getrusage(RUSAGE_SELF, &usage) < 0)
+			return -1;
+		return (long long)usage.ru_utime.tv_sec * USECS_PER_SEC + usage.ru_utime.tv_usec;
+	default:
+		return -1;
+	}
+}
 
-	diff = end.tv_usec - start.tv_usec;
-	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
+static int check_diff_us(long long start, long long end)
+{
+	long long diff = end - start;
 
 	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
 		printf("Diff too high: %lld..", diff);
@@ -100,7 +122,7 @@ static int check_diff(struct timeval start, struct timeval end)
 
 static void check_itimer(int which, const char *name)
 {
-	struct timeval start, end;
+	long long start, end;
 	struct itimerval val = {
 		.it_value.tv_sec = DELAY,
 	};
@@ -114,8 +136,9 @@ static void check_itimer(int which, const char *name)
 	else if (which == ITIMER_REAL)
 		signal(SIGALRM, sig_handler);
 
-	if (gettimeofday(&start, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	start = get_itimer_time_us(which);
+	if (start < 0)
+		fatal_error(name, "get_itimer_time_us()");
 
 	if (setitimer(which, &val, NULL) < 0)
 		fatal_error(name, "setitimer()");
@@ -127,15 +150,16 @@ static void check_itimer(int which, const char *name)
 	else if (which == ITIMER_REAL)
 		idle_loop();
 
-	if (gettimeofday(&end, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	end = get_itimer_time_us(which);
+	if (end < 0)
+		fatal_error(name, "get_itimer_time_us()");
 
-	ksft_test_result(check_diff(start, end) == 0, "%s\n", name);
+	ksft_test_result(check_diff_us(start, end) == 0, "%s\n", name);
 }
 
 static void check_timer_create(int which, const char *name)
 {
-	struct timeval start, end;
+	long long start, end;
 	struct itimerspec val = {
 		.it_value.tv_sec = DELAY,
 	};
@@ -149,19 +173,20 @@ static void check_timer_create(int which, const char *name)
 	if (signal(SIGALRM, sig_handler) == SIG_ERR)
 		fatal_error(name, "signal()");
 
-	if (gettimeofday(&start, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	start = get_clock_time_us(which);
+	if (start < 0)
+		fatal_error(name, "get_clock_time_us()");
 
 	if (timer_settime(id, 0, &val, NULL) < 0)
 		fatal_error(name, "timer_settime()");
 
 	user_loop();
 
-	if (gettimeofday(&end, NULL) < 0)
-		fatal_error(name, "gettimeofday()");
+	end = get_clock_time_us(which);
+	if (end < 0)
+		fatal_error(name, "get_clock_time_us()");
 
-	ksft_test_result(check_diff(start, end) == 0,
-			 "timer_create() per %s\n", name);
+	ksft_test_result(check_diff_us(start, end) == 0, "timer_create() per %s\n", name);
 }
 
 static pthread_t ctd_thread;
Signature · 1 line
2.55.0.229.g6434b31f56-goog
author
#3 lore raw
In reply to Thomas Gleixner

Hi Thomas,

Thanks for the review. I've addressed all the style issues and sent out PATCH v2.

Quoted 76 lines · Thomas Gleixner
Quoted 8 lines · Wake Liu

The posix_timers test compares the elapsed real time (GTOD) with the expected timer delay to verify if the timer expired. This works fine for real-time timers (ITIMER_REAL), but is flaky for CPU-time timers (ITIMER_PROF, ITIMER_VIRTUAL, and CLOCK_*_CPUTIME_ID) on loaded systems (like shared test runners) because the test process might be scheduled out, resulting in elapsed real time being larger than the consumed CPU time.

Quoted 6 lines

Fix this by using the appropriate measurement method for each timer:
- Use CPU process/thread time (via clock_gettime or getrusage) for CPU timers.
- Keep using monotonic clock (real time) for real-time timers.

This makes the test robust against scheduling delays.

Lacks Signed-off-by ....

tools/testing/selftests/timers/posix_timers.c static void sig_handler(int nr) · line 86

Quoted 5 lines
tools/testing/selftests/timers/posix_timers.c +5 -0
+#include <sys/resource.h>
+
+static long long get_clock_time_us(clockid_t clock_id)
+{
+     struct timespec ts;

Newline between declaration and code.

Quoted 20 lines
tools/testing/selftests/timers/posix_timers.c +20 -0
+     if (clock_gettime(clock_id, &ts) < 0)
+             return -1;
+     return (long long)ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
+}
+
+static long long get_itimer_time_us(int which)
+{
+     struct rusage usage;
+
+     switch (which) {
+     case ITIMER_REAL:
+             return get_clock_time_us(CLOCK_MONOTONIC);
+     case ITIMER_PROF:
+             return get_clock_time_us(CLOCK_PROCESS_CPUTIME_ID);
+     case ITIMER_VIRTUAL:
+             if (getrusage(RUSAGE_SELF, &usage) < 0)
+                     return -1;
+             return (long long)usage.ru_utime.tv_sec * USECS_PER_SEC + usage.ru_utime.tv_usec;
+     default:
+             return -1;

tools/testing/selftests/timers/posix_timers.c static void sig_handler(int nr) · line 114

Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +5 -2
+     }
+}
+
+static int check_diff_us(long long start, long long end)
 {
      long long diff;
 
-     diff = end.tv_usec - start.tv_usec;
-     diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
+     diff = end - start;

Move that to the declaration line.

tools/testing/selftests/timers/posix_timers.c static int check_diff(struct timeval start, struct timeval end) · line 129

Quoted 13 lines
      if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
              printf("Diff too high: %lld..", diff);
tools/testing/selftests/timers/posix_timers.c +4 -3
@@ -100,7 +123,7 @@ static int check_diff(struct timeval start, struct timeval end)
 
-     if (gettimeofday(&end, NULL) < 0)
-             fatal_error(name, "gettimeofday()");
+     end = get_clock_time_us(which);
+     if (end < 0)
+             fatal_error(name, "get_clock_time_us()");
 
-     ksft_test_result(check_diff(start, end) == 0,
+     ksft_test_result(check_diff_us(start, end) == 0,
                       "timer_create() per %s\n", name);

Please get rid of the line break.

Signature · 2 lines
Best Regards,
Wake Liu