Skip to contentSkip to conversation

syscalls/timer_settime01: adjust for rounding from nsec to usec

PATCH In review Thadeu Lima de Souza Cascardo 3 participants 8 messages Oct 6, 2020 – Oct 27, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/timer_settime01: adjust for rounding from nsec to usec

When using TIMER_ABSTIME, the value is added to the current clock time. However, usecs is used instead of nsecs, leading to a possible rounding up from tst_ts_to_us.

That rounding can lead to up to 500 nsecs of difference between the current time and the absolute time used for setting the timer. When reading the timer back, that same difference can be found, which leads to a test failure.

Accounting for that possible difference in the time allows the test to pass.

This can be easily reproducible by booting linux with clocksource=jiffies.

Fixes b34e243e85dd (syscalls/timer_settime01: Make sure the timer fires)
---
 testcases/kernel/syscalls/timer_settime/timer_settime01.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/timer_settime/timer_settime01.c +6 -1
index 67143e8f88b2..7ecf0c18d1c4 100644
@@ -149,10 +149,15 @@ static void run(unsigned int n)
 		if (TST_RET != 0) {
 			tst_res(TFAIL | TTERRNO, "timer_gettime(%s) failed",
 				get_clock_str(clock));
+		/*
+		 * With TIMER_ABSTIME, we add to the current clock after
+		 * rounding up by 500 ns to the value. Do the same here after
+		 * reading the timer back.
+		 */
 		} else if ((tst_its_get_interval_nsec(new_set) !=
 				tc->it_interval_tv_usec * 1000) ||
 			   (tst_its_get_value_nsec(new_set) >
-				MAX(tc->it_value_tv_usec * 1000, tc->it_interval_tv_usec * 1000))) {
+				MAX(tc->it_value_tv_usec * 1000 + 500, tc->it_interval_tv_usec * 1000))) {
 			tst_res(TFAIL | TTERRNO,
 				"timer_gettime(%s) reported bad values (%llu: %llu)",
 				get_clock_str(clock),
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 syscalls/timer_settime01: adjust for rounding from nsec to usec

Hi!
What about this change instead?

testcases/kernel/syscalls/timer_settime/timer_settime01.c +4 -2
index 67143e8f8..599ef2891 100644
@@ -132,11 +132,13 @@ static void run(unsigned int n)
 					get_clock_str(clock));
 				continue;
 			}
-			val += tst_ts_to_us(timenow);
+			tst_ts_add_us(timenow, val);
+			tst_its_set_value_from_ts(&new_set, timenow);
+		} else {
+			tst_its_set_value_from_us(&new_set, val);
 		}
 
 		tst_its_set_interval_from_us(&new_set, tc->it_interval_tv_usec);
-		tst_its_set_value_from_us(&new_set, val);
 
 		TEST(tv->timer_settime(timer, tc->flag, tst_its_get(&new_set), tst_its_get(tc->old_ptr)));
 

By adding the us to the timenow first and then setting the its.value from it we can avoid the rounding completely.

Signature · 2 lines
Cyril Hrubis
[email protected]
#2 lore raw

PATCH 1/1 syscalls/timer_settime01: adjust for rounding from nsec to usec

Hi!

Quoted 24 lines

What about this change instead?

testcases/kernel/syscalls/timer_settime/timer_settime01.c +4 -2
index 67143e8f8..599ef2891 100644
@@ -132,11 +132,13 @@ static void run(unsigned int n)
 					get_clock_str(clock));
 				continue;
 			}
-			val += tst_ts_to_us(timenow);
+			tst_ts_add_us(timenow, val);
+			tst_its_set_value_from_ts(&new_set, timenow);
+		} else {
+			tst_its_set_value_from_us(&new_set, val);
 		}
 
 		tst_its_set_interval_from_us(&new_set, tc->it_interval_tv_usec);
-		tst_its_set_value_from_us(&new_set, val);
 
 		TEST(tv->timer_settime(timer, tc->flag, tst_its_get(&new_set), tst_its_get(tc->old_ptr)));
 
Quoted 2 lines

By adding the us to the timenow first and then setting the its.value from it we can avoid the rounding completely.

Does this fix work for you?

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH 1/1 syscalls/timer_settime01: adjust for rounding from nsec to usec

Quoted 21 lines · Cyril Hrubis

Hi!

Quoted 20 lines

What about this change instead?

testcases/kernel/syscalls/timer_settime/timer_settime01.c +4 -2
index 67143e8f8..599ef2891 100644
@@ -132,11 +132,13 @@ static void run(unsigned int n)
 					get_clock_str(clock));
 				continue;
 			}
-			val += tst_ts_to_us(timenow);
+			tst_ts_add_us(timenow, val);
+			tst_its_set_value_from_ts(&new_set, timenow);
+		} else {
+			tst_its_set_value_from_us(&new_set, val);
 		}
 
 		tst_its_set_interval_from_us(&new_set, tc->it_interval_tv_usec);
-		tst_its_set_value_from_us(&new_set, val);
 
Quoted 9 lines
Quoted 6 lines
testcases/kernel/syscalls/timer_settime/timer_settime01.c +0 -0
 		TEST(tv->timer_settime(timer, tc->flag, tst_its_get(&new_set), tst_its_get(tc->old_ptr)));
 

By adding the us to the timenow first and then setting the its.value from it we can avoid the rounding completely.

Does this fix work for you?

Sorry, I was out on vacation. This fixes it for me when using clocksource=jiffies.

Thanks.
Cascardo.

Quoted 3 lines
Signature · 2 lines
Cyril Hrubis
[email protected]
#4 lore raw

PATCH 1/1 syscalls/timer_settime01: adjust for rounding from nsec to usec

Hi!

Quoted 2 lines

Sorry, I was out on vacation. This fixes it for me when using clocksource=jiffies.

Thanks a lot for testing, pushed.

Signature · 2 lines
Cyril Hrubis
[email protected]
#5 lore raw
In reply to Thadeu Lima de Souza Cascardo

Hi all,

the patch from Chris fixes the below issue on s390x:

timer_settime01.c:96: TINFO: Testing for using absolute time:
timer_settime01.c:169: TPASS: timer_settime(CLOCK_REALTIME) passed timer_settime01.c:169: TPASS: timer_settime(CLOCK_MONOTONIC) passed timer_settime01.c:169: TPASS: timer_settime(CLOCK_PROCESS_CPUTIME_ID) passed timer_settime01.c:169: TPASS: timer_settime(CLOCK_THREAD_CPUTIME_ID) passed timer_settime01.c:169: TPASS: timer_settime(CLOCK_BOOTTIME) passed timer_settime01.c:111: TCONF: CLOCK_BOOTTIME_ALARM unsupported: EOPNOTSUPP (95) timer_settime01.c:111: TCONF: CLOCK_REALTIME_ALARM unsupported: EOPNOTSUPP (95) timer_settime01.c:156: TFAIL: timer_gettime(CLOCK_TAI) reported bad values (0: 50000019): SUCCESS (0) timer_settime01.c:169: TPASS: timer_settime(CLOCK_TAI) passed

and

timer_settime01.c:96: TINFO: Testing for using absolute time:
timer_settime01.c:169: TPASS: timer_settime(CLOCK_REALTIME) passed
timer_settime01.c:169: TPASS: timer_settime(CLOCK_MONOTONIC) passed timer_settime01.c:169: TPASS: timer_settime(CLOCK_PROCESS_CPUTIME_ID) passed timer_settime01.c:169: TPASS: timer_settime(CLOCK_THREAD_CPUTIME_ID) passed timer_settime01.c:156: TFAIL: timer_gettime(CLOCK_BOOTTIME) reported bad values (0: 50000097): SUCCESS (0) timer_settime01.c:169: TPASS: timer_settime(CLOCK_BOOTTIME) passed
timer_settime01.c:111: TCONF: CLOCK_BOOTTIME_ALARM unsupported: EOPNOTSUPP (95) timer_settime01.c:111: TCONF: CLOCK_REALTIME_ALARM unsupported: EOPNOTSUPP (95) timer_settime01.c:169: TPASS: timer_settime(CLOCK_TAI) passed

Regards
Alex

#6 lore raw

Hi!

Quoted 1 line

the patch from Chris fixes the below issue on s390x:

And what about the simpler solution I've proposed in the reply, does that one work fine as well?

Signature · 2 lines
Cyril Hrubis
[email protected]
#7 lore raw
Quoted 5 lines · Cyril Hrubis

Hi!

Quoted 1 line

the patch from Chris fixes the below issue on s390x:

And what about the simpler solution I've proposed in the reply, does that one work fine as well?

Hi Cyril,

first, sorry for butchering your name :/

Second, I tested now both patches on a heavy loaded system (with stress-ng) and both seem to solve my issues. I ran a test 1000 times (-i 1000) and couldn't reproduce it anymore.

Regards
Alex