timers test fix and duplicate defines cleanup
Subsystems (3)
Shuah Khan
maintainercover timers test fix and duplicate defines cleanup
The first patch in this two patch fixes warn_unused_result compile time warning in posix_timers test.
The second patch removes local NSEC_PER_SEC and USEC_PER_SEC defines. NSEC_PER_SEC and USEC_PER_SEC are defines in several timers tests. These defines are inconsistent with variations of ULL, LL, etc. without any explanation why it is necessary.
These defines can be picked up from include/vdso/time64.h header file. In the interest of making it easier to maintain, remove the local defines. Include include/vdso/time64.h instead. This change will also make the defines consistent.
Shuah Khan (2):
selftests:timers: posix_timers: Fix warn_unused_result in
__fatal_error()
selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines
tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 4 +-- .../selftests/timers/alarmtimer-suspend.c | 2 +- .../selftests/timers/inconsistency-check.c | 2 +- tools/testing/selftests/timers/leap-a-day.c | 2 +- tools/testing/selftests/timers/mqueue-lat.c | 2 +- tools/testing/selftests/timers/nanosleep.c | 3 +-- tools/testing/selftests/timers/posix_timers.c | 27 ++++++++++--------- tools/testing/selftests/timers/raw_skew.c | 2 +- tools/testing/selftests/timers/set-2038.c | 3 +-- .../testing/selftests/timers/set-timer-lat.c | 3 +-- .../testing/selftests/timers/valid-adjtimex.c | 4 +-- 12 files changed, 26 insertions(+), 30 deletions(-)
Signature · 1 line
2.40.1
Shuah Khan
maintainerPATCH 1/2 selftests:timers: posix_timers: Fix warn_unused_result in __fatal_error()
__fatal_error routine doesn't check strerror_r() return value, which results in the following compile time warning:
posix_timers.c: In function ‘__fatal_error’:
posix_timers.c:31:9: warning: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
31 | strerror_r(errno, buf, sizeof(buf));
Fix this by adding a check for return value and error handling appropriate for the GNU-specific strerror_r() in use in __fatal_error(). Check if return string is null and handle accordingly.
From Linux strerror_r() manual page:
"The GNU-specific strerror_r() returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most buflen bytes are stored (the string may be truncated if buflen is too small and errnum is unknown). The string always includes a terminating null byte."
--- tools/testing/selftests/timers/posix_timers.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/timers/posix_timers.c +8 -4
@@ -26,13 +26,17 @@static void __fatal_error (const char *test,const char *name,const char *what) {char buf[64 ]; +char *ret_str =NULL ; -strerror_r (errno, buf,sizeof (buf)); + ret_str =strerror_r (errno, buf,sizeof (buf)); -if (name &&strlen (name)) -ksft_exit_fail_msg ("%s %s %s %s\n" , test, name, what, buf); +if (name &&strlen (name) && ret_str) +ksft_exit_fail_msg ("%s %s %s %s\n" , test, name, what, ret_str); +else if (ret_str) +ksft_exit_fail_msg ("%s %s %s\n" , test, what, ret_str);else -ksft_exit_fail_msg ("%s %s %s\n" , test, what, buf); +ksft_exit_fail_msg ("%s %s\n" , test, what); + }#define fatal_error (name, what) __fatal_error(__func__, name, what)
Signature · 1 line
2.40.1
Shuah Khan
maintainerPATCH 2/2 selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines
In reply to Shuah KhanRemove local NSEC_PER_SEC and USEC_PER_SEC defines. Pick them up from include/vdso/time64.h. This requires -I $(top_srcdir) to the timers Makefile to include the include/vdso/time64.h.
posix_timers test names the defines NSECS_PER_SEC and USECS_PER_SEC. Include the include/vdso/time64.h and change NSECS_PER_SEC and USECS_PER_SEC references to NSEC_PER_SEC and USEC_PER_SEC respectively.
--- tools/testing/selftests/timers/Makefile | 2 +- tools/testing/selftests/timers/adjtick.c | 4 +--- .../testing/selftests/timers/alarmtimer-suspend.c | 2 +- .../selftests/timers/inconsistency-check.c | 2 +- tools/testing/selftests/timers/leap-a-day.c | 2 +- tools/testing/selftests/timers/mqueue-lat.c | 2 +- tools/testing/selftests/timers/nanosleep.c | 3 +-- tools/testing/selftests/timers/posix_timers.c | 15 +++++++-------- tools/testing/selftests/timers/raw_skew.c | 2 +- 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 +--- 12 files changed, 18 insertions(+), 26 deletions(-)
tools/testing/selftests/timers/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -O3 -Wl,-no-as-needed -Wall +CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir) LDLIBS += -lrt -lpthread -lm # these are all "safe" tests that don't modify
tools/testing/selftests/timers/adjtick.c +1 -3
@@ -22,14 +22,12 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> +#include <include/vdso/time64.h> #include "../kselftest.h" #define CLOCK_MONOTONIC_RAW 4 -#define NSEC_PER_SEC 1000000000LL -#define USEC_PER_SEC 1000000 -#define MILLION 1000000long systick;
tools/testing/selftests/timers/alarmtimer-suspend.c +1 -1
@@ -28,6 +28,7 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> +#include <include/vdso/time64.h> #include "../kselftest.h" #define CLOCK_REALTIME 0
@@ -45,7 +46,6 @@#define NR_CLOCKIDS 12 -#define NSEC_PER_SEC 1000000000ULL#define UNREASONABLE_LAT (NSEC_PER_SEC * 5)/* hopefully we resume in 5 secs */ #define SUSPEND_SECS 15
tools/testing/selftests/timers/inconsistency-check.c +1 -1
@@ -28,10 +28,10 @@#include <sys/timex.h> #include <string.h> #include <signal.h> +#include <include/vdso/time64.h> #include "../kselftest.h" #define CALLS_PER_LOOP 64 -#define NSEC_PER_SEC 1000000000ULL#define CLOCK_REALTIME 0#define CLOCK_MONOTONIC 1
tools/testing/selftests/timers/leap-a-day.c +1 -1
@@ -48,9 +48,9 @@#include <string.h> #include <signal.h> #include <unistd.h> +#include <include/vdso/time64.h> #include "../kselftest.h" -#define NSEC_PER_SEC 1000000000ULL#define CLOCK_TAI 11time_t next_leap;
tools/testing/selftests/timers/mqueue-lat.c +1 -1
@@ -29,9 +29,9 @@#include <signal.h> #include <errno.h> #include <mqueue.h> +#include <include/vdso/time64.h> #include "../kselftest.h" -#define NSEC_PER_SEC 1000000000ULL#define TARGET_TIMEOUT 100000000/* 100ms in nanoseconds */ #define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */
tools/testing/selftests/timers/nanosleep.c +1 -2
@@ -27,10 +27,9 @@#include <sys/timex.h> #include <string.h> #include <signal.h> +#include <include/vdso/time64.h> #include "../kselftest.h" -#define NSEC_PER_SEC 1000000000ULL -#define CLOCK_REALTIME 0#define CLOCK_MONOTONIC 1#define CLOCK_PROCESS_CPUTIME_ID 2
tools/testing/selftests/timers/posix_timers.c +7 -8
@@ -15,13 +15,12 @@#include <string.h> #include <unistd.h> #include <time.h> +#include <include/vdso/time64.h> #include <pthread.h> #include "../kselftest.h" #define DELAY 2 -#define USECS_PER_SEC 1000000 -#define NSECS_PER_SEC 1000000000static void __fatal_error (const char *test,const char *name,const char *what) {
@@ -86,9 +85,9 @@ static int check_diff(struct timeval start, struct timeval end)long long diff; diff = end.tv_usec - start.tv_usec; - diff += (end.tv_sec - start.tv_sec) *USECS_PER_SEC ; + diff += (end.tv_sec - start.tv_sec) *USEC_PER_SEC ; -if (llabs (diff -DELAY *USECS_PER_SEC ) >USECS_PER_SEC /2 ) { +if (llabs (diff -DELAY *USEC_PER_SEC ) >USEC_PER_SEC /2 ) {printf ("Diff too high: %lld.." , diff);return -1 ; }
@@ -448,7 +447,7 @@ static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2) {int64_t diff; - diff =NSECS_PER_SEC * (int64_t )((int ) t1.tv_sec - (int ) t2.tv_sec); + diff =NSEC_PER_SEC * (int64_t )((int ) t1.tv_sec - (int ) t2.tv_sec); diff += ((int ) t1.tv_nsec - (int ) t2.tv_nsec);return diff; }
@@ -479,7 +478,7 @@ static void check_sigev_none(int which, const char *name)do {if (clock_gettime (which, &now))fatal_error (name,"clock_gettime()" ); - }while (calcdiff_ns (now, start) <NSECS_PER_SEC ); + }while (calcdiff_ns (now, start) <NSEC_PER_SEC );if (timer_gettime (timerid, &its))fatal_error (name,"timer_gettime()" );
@@ -536,7 +535,7 @@ static void check_gettime(int which, const char *name) wraps++; prev = its; - }while (calcdiff_ns (now, start) <NSECS_PER_SEC ); + }while (calcdiff_ns (now, start) <NSEC_PER_SEC );if (timer_delete (timerid))fatal_error (name,"timer_delete()" );
@@ -587,7 +586,7 @@ static void check_overrun(int which, const char *name)do {if (clock_gettime (which, &now))fatal_error (name,"clock_gettime()" ); - }while (calcdiff_ns (now, start) <NSECS_PER_SEC ); + }while (calcdiff_ns (now, start) <NSEC_PER_SEC );/* Unblock it, which should deliver a signal */ if (sigprocmask (SIG_UNBLOCK , &set,NULL ))
tools/testing/selftests/timers/raw_skew.c +1 -1
@@ -25,10 +25,10 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> +#include <include/vdso/time64.h> #include "../kselftest.h" #define CLOCK_MONOTONIC_RAW 4 -#define NSEC_PER_SEC 1000000000LL#define shift_right (x, s) ({ \ __typeof__(x) __x = (x); \
tools/testing/selftests/timers/set-2038.c +1 -2
@@ -27,10 +27,9 @@#include <unistd.h> #include <time.h> #include <sys/time.h> +#include <include/vdso/time64.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)
tools/testing/selftests/timers/set-timer-lat.c +1 -2
@@ -28,6 +28,7 @@#include <signal.h> #include <stdlib.h> #include <pthread.h> +#include <include/vdso/time64.h> #include "../kselftest.h" #define CLOCK_REALTIME 0
@@ -44,8 +45,6 @@#define CLOCK_TAI 11#define NR_CLOCKIDS 12 - -#define NSEC_PER_SEC 1000000000ULL#define UNRESONABLE_LATENCY 40000000/* 40ms in nanosecs */ #define TIMER_SECS 1
tools/testing/selftests/timers/valid-adjtimex.c +1 -3
@@ -29,11 +29,9 @@#include <string.h> #include <signal.h> #include <unistd.h> +#include <include/vdso/time64.h> #include "../kselftest.h" -#define NSEC_PER_SEC 1000000000LL -#define USEC_PER_SEC 1000000LL -#define ADJ_SETOFFSET 0x0100#include <sys/syscall.h>
Signature · 1 line
2.40.1
Re: [PATCH 2/2] selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines
Quoted 8 lines · Shuah Khan
Remove local
NSEC_PER_SECandUSEC_PER_SECdefines. Pick them up frominclude/vdso/time64.h. This requires -I $(top_srcdir) to the timersMakefileto include theinclude/vdso/time64.h.
posix_timerstest names the definesNSECS_PER_SECandUSECS_PER_SEC. Include theinclude/vdso/time64.hand changeNSECS_PER_SECandUSECS_PER_SECreferences toNSEC_PER_SECandUSEC_PER_SECrespectively.
Nit: You got the last bit switched there. This patch changes local NSEC_PER_SEC to the upstream defined NSECS_PER_SEC.
Overall no objection from me. I've always pushed to have the tests be mostly self-contained so they can be built outside of the kernel source, but at this point the current kselftest.h dependencies means it already takes some work, so this isn't introducing an undue hardship.
Other then the nit,
tools/testing/selftests/timers/adjtick.c line 29
Quoted 13 lines
tools/testing/selftests/timers/adjtick.c +1
-0@@ -22,14 +22,12 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> +#include <include/vdso/time64.h> #include "../kselftest.h" #define CLOCK_MONOTONIC_RAW 4
I suspect CLOCK_MONOTONIC_RAW (and the other clockid definitions) could be similarly removed here as well in a future patch?
thanks
-john
Shuah Khan
maintainerRe: [PATCH 1/2] selftests:timers: posix_timers: Fix warn_unused_result in __fatal_error()
In reply to Shuah KhanQuoted 42 lines · Shuah Khan
__fatal_errorroutine doesn't checkstrerror_r()return value, which results in the following compile time warning:
posix_timers.c: In function ‘__fatal_error’:
posix_timers.c:31:9: warning: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Wunused-result]31 | strerror_r(errno, buf, sizeof(buf));Fix this by adding a check for return value and error handling appropriate for the GNU-specific
strerror_r()in use in__fatal_error(). Check if return string is null and handle accordingly.From Linux
strerror_r()manual page:"The GNU-specific
strerror_r()returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most buflen bytes are stored (the string may be truncated if buflen is too small and errnum is unknown). The string always includes a terminating null byte."Signed-off-by Shuah Khan <[email protected]>--- tools/testing/selftests/timers/posix_timers.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)tools/testing/selftests/timers/posix_timers.c +3
-3@@ -26,13 +26,17 @@static void __fatal_error (const char *test,const char *name,const char *what) {char buf[64 ]; +char *ret_str =NULL ; -strerror_r (errno, buf,sizeof (buf)); + ret_str =strerror_r (errno, buf,sizeof (buf)); -if (name &&strlen (name)) -ksft_exit_fail_msg ("%s %s %s %s\n" , test, name, what, buf); +if (name &&strlen (name) && ret_str)
tools/testing/selftests/timers/posix_timers.c line 42
Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +5
-1+ksft_exit_fail_msg ("%s %s %s %s\n" , test, name, what, ret_str); + else if (ret_str) + ksft_exit_fail_msg("%s %s %s\n", test, what, ret_str); else -ksft_exit_fail_msg ("%s %s %s\n" , test, what, buf); + ksft_exit_fail_msg("%s %s\n", test, what); + }#define fatal_error (name, what) __fatal_error(__func__, name, what)
Any comments on this patch John?
thanks,
-- Shuah
Shuah Khan
maintainerRe: [PATCH 2/2] selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines
In reply to John StultzQuoted 6 lines · John Stultz
Quoted 5 lines · Shuah Khan
Remove local
NSEC_PER_SECandUSEC_PER_SECdefines. Pick them up frominclude/vdso/time64.h. This requires -I $(top_srcdir) to the timersMakefileto include theinclude/vdso/time64.h.
Quoted 6 lines
Quoted 3 lines
posix_timerstest names the definesNSECS_PER_SECandUSECS_PER_SEC. Include theinclude/vdso/time64.hand changeNSECS_PER_SECandUSECS_PER_SECreferences toNSEC_PER_SECandUSEC_PER_SECrespectively.Nit: You got the last bit switched there. This patch changes local
NSEC_PER_SECto the upstream definedNSECS_PER_SEC.
I think what IO have is correct. posix_timers defines them as NSECS_PER_SEC and USECS_PER_SEC and the header file doesn't have the extra S. It could use rephrasing thought to make it clear.
Is it okay to fix this when I apply the patch or would you like me to send v2?
Quoted 7 lines
Overall no objection from me. I've always pushed to have the tests be mostly self-contained so they can be built outside of the kernel source, but at this point the current
kselftest.hdependencies means it already takes some work, so this isn't introducing an undue hardship.
Yes. At this point it would be hard to build it outside. DO you think these defines can be part of uapi?
Quoted 13 lines
Other then the nit,
Acked-by John Stultz <[email protected]>Quoted 10 lines
tools/testing/selftests/timers/adjtick.c +1
-0@@ -22,14 +22,12 @@#include <sys/time.h> #include <sys/timex.h> #include <time.h> +#include <include/vdso/time64.h>
Quoted 6 lines
tools/testing/selftests/timers/adjtick.c line 29
Quoted 3 lines
tools/testing/selftests/timers/adjtick.c +0
-0#include "../kselftest.h" #define CLOCK_MONOTONIC_RAW 4I suspect
CLOCK_MONOTONIC_RAW(and the other clockid definitions) could be similarly removed here as well in a future patch?
Yes. It could be cleaned up. I will send a patch in for this.
thanks,
-- Shuah
Re: [PATCH 1/2] selftests:timers: posix_timers: Fix warn_unused_result in __fatal_error()
In reply to Shuah KhanQuoted 41 lines · Shuah Khan
__fatal_errorroutine doesn't checkstrerror_r()return value, which results in the following compile time warning:
posix_timers.c: In function ‘__fatal_error’:
posix_timers.c:31:9: warning: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
31 |strerror_r(errno, buf, sizeof(buf));Fix this by adding a check for return value and error handling appropriate for the GNU-specific
strerror_r()in use in__fatal_error(). Check if return string is null and handle accordingly.From Linux
strerror_r()manual page:"The GNU-specific
strerror_r()returns a pointer to a string containing the error message. This may be either a pointer to a string that the function stores in buf, or a pointer to some (immutable) static string (in which case buf is unused). If the function stores a string in buf, then at most buflen bytes are stored (the string may be truncated if buflen is too small and errnum is unknown). The string always includes a terminating null byte."Signed-off-by Shuah Khan <[email protected]>--- tools/testing/selftests/timers/posix_timers.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)tools/testing/selftests/timers/posix_timers.c +2
-2@@ -26,13 +26,17 @@static void __fatal_error (const char *test,const char *name,const char *what) {char buf[64 ]; +char *ret_str =NULL ; -strerror_r (errno, buf,sizeof (buf)); + ret_str =strerror_r (errno, buf,sizeof (buf)); -if (name &&strlen (name))
tools/testing/selftests/timers/posix_timers.c line 40
Quoted 10 lines
tools/testing/selftests/timers/posix_timers.c +6
-2-ksft_exit_fail_msg ("%s %s %s %s\n" , test, name, what, buf); +if (name &&strlen (name) && ret_str) +ksft_exit_fail_msg ("%s %s %s %s\n" , test, name, what, ret_str); +else if (ret_str) +ksft_exit_fail_msg ("%s %s %s\n" , test, what, ret_str);else - ksft_exit_fail_msg("%s %s %s\n", test, what, buf); +ksft_exit_fail_msg ("%s %s\n" , test, what); + }
John Stultz
maintainerRe: [PATCH 2/2] selftests: timers: Remove local NSEC_PER_SEC and USEC_PER_SEC defines
In reply to Shuah KhanQuoted 12 lines · Shuah Khan
Quoted 10 lines · John Stultz
Quoted 8 lines · Shuah Khan
Remove local
NSEC_PER_SECandUSEC_PER_SECdefines. Pick them up frominclude/vdso/time64.h. This requires -I $(top_srcdir) to the timersMakefileto include theinclude/vdso/time64.h.
posix_timerstest names the definesNSECS_PER_SECandUSECS_PER_SEC. Include theinclude/vdso/time64.hand changeNSECS_PER_SECandUSECS_PER_SECreferences toNSEC_PER_SECandUSEC_PER_SECrespectively.
Quoted 6 lines
Quoted 2 lines
Nit: You got the last bit switched there. This patch changes local
NSEC_PER_SECto the upstream definedNSECS_PER_SEC.I think what IO have is correct.
posix_timersdefines them asNSECS_PER_SECandUSECS_PER_SECand the header file doesn't have the extra S. It could use rephrasing thought to make it clear.
?
But the patch is removing the local non-plural NSEC_PER_SEC usage in posix_timers?
-#define NSEC_PER_SEC 1000000000LL
-#define USEC_PER_SEC 1000000
-
As the headers do have the values with the extra S.
So I'm confused (sort of my natural state :), but this is a minor nit, so apologies and just ignore me if I'm really getting it backwards here.
Quoted 2 lines
Is it okay to fix this when I apply the patch or would you like me to send v2?
I don't need a v2
Quoted 10 lines
Quoted 7 lines
Overall no objection from me. I've always pushed to have the tests be mostly self-contained so they can be built outside of the kernel source, but at this point the current
kselftest.hdependencies means it already takes some work, so this isn't introducing an undue hardship.Yes. At this point it would be hard to build it outside. DO you think these defines can be part of uapi?
Maybe, though they are so common I fret it would cause folks trouble with redefinition collisions.
Thanks for doing this cleanup!
-john
Quoted 8 lines · Shuah Khan
The first patch in this two patch fixes
warn_unused_resultcompile time warning inposix_timerstest.The second patch removes local
NSEC_PER_SECandUSEC_PER_SECdefines.NSEC_PER_SECandUSEC_PER_SECare defines in several timers tests. These defines are inconsistent with variations of ULL, LL, etc. without any explanation why it is necessary.
Quoted 4 lines
These defines can be picked up from
include/vdso/time64.hheader file. In the interest of making it easier to maintain, remove the local defines. Includeinclude/vdso/time64.hinstead. This change will also make the defines consistent.