selftests: timers: print time values as long long
Subsystems (3)
PATCH 1/1 selftests: timers: print time values as long long
On 32-bit architectures with a 64-bit time_t, such as armhf in Debian 13, time_t, the tv_sec fields of struct timespec and struct timeval, their tv_usec field and the freq field of struct timex are long long, and printing them with %ld or %lu warns:
leap-a-day.c:142:69: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'time_t' {aka 'long long int'} [-Wformat=]
Cast the values to long long and print them with %lld. While at it, print tv_nsec, a long, with %ld instead of %lu, and cast it to long, as it is a long long on x32, and print the unsigned long frequency in rtcpie with %lu.
---
Found with a 32-bit build of the selftests with a 64-bit time_t (gcc 16 and clang 21 with -m32 -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64); KernelCI's 32-bit arm builds show the same warnings. The changed files build without format warnings there now, and the x86_64 builds don't change. The other selftests get the same kind of fix in separate patches.
.../selftests/timers/alarmtimer-suspend.c | 9 +++--- .../selftests/timers/inconsistency-check.c | 5 ++-- tools/testing/selftests/timers/leap-a-day.c | 28 ++++++++++--------- tools/testing/selftests/timers/rtcpie.c | 4 +-- tools/testing/selftests/timers/threadtest.c | 6 ++-- .../testing/selftests/timers/valid-adjtimex.c | 14 ++++++---- 6 files changed, 37 insertions(+), 29 deletions(-)
tools/testing/selftests/timers/alarmtimer-suspend.c +5 -4
@@ -60,8 +60,8 @@ void sigalarm(int signo) delta_ns =timespec_sub (start_time, ts); delta_ns -=NSEC_PER_SEC *SUSPEND_SECS * alarmcount; -printf ("ALARM(%i): %ld:%ld latency: %lld ns " , alarmcount, ts.tv_sec, - ts.tv_nsec, delta_ns); +printf ("ALARM(%i): %lld:%ld latency: %lld ns " , alarmcount, + (long long )ts.tv_sec, (long )ts.tv_nsec, delta_ns);if (delta_ns >UNREASONABLE_LAT ) {printf ("[FAIL]\n" );
@@ -103,8 +103,9 @@ int main(void) }clock_gettime (alarm_clock_id, &start_time); -printf ("Start time (%s): %ld:%ld\n" ,clock_name (alarm_clock_id), - start_time.tv_sec, start_time.tv_nsec); +printf ("Start time (%s): %lld:%ld\n" ,clock_name (alarm_clock_id), + (long long )start_time.tv_sec, + (long )start_time.tv_nsec);printf ("Setting alarm for every %i seconds\n" ,SUSPEND_SECS ); its1.it_value = start_time; its1.it_value.tv_sec +=SUSPEND_SECS ;
tools/testing/selftests/timers/inconsistency-check.c +3 -2
@@ -86,8 +86,9 @@ int consistency_test(int clock_type, unsigned long seconds)for (i =0 ; i <CALLS_PER_LOOP ; i++) {if (i == inconsistent)ksft_print_msg ("--------------------\n" ); -ksft_print_msg ("%lu:%lu\n" , list[i].tv_sec, - list[i].tv_nsec); +ksft_print_msg ("%lld:%ld\n" , + (long long )list[i].tv_sec, + (long )list[i].tv_nsec);if (i == inconsistent +1 )ksft_print_msg ("--------------------\n" ); }
tools/testing/selftests/timers/leap-a-day.c +15 -13
@@ -139,20 +139,21 @@ void sigalarm(int signo) ret =adjtimex (&tx);if (tx.time.tv_sec < next_leap) { -printf ("Error: Early timer expiration! (Should be %ld)\n" , next_leap); +printf ("Error: Early timer expiration! (Should be %lld)\n" , + (long long )next_leap); error_found =1 ; -printf ("adjtimex: %10ld sec + %6ld us (%i)\t%s\n" , - tx.time.tv_sec, - tx.time.tv_usec, +printf ("adjtimex: %10lld sec + %6lld us (%i)\t%s\n" , + (long long )tx.time.tv_sec, + (long long )tx.time.tv_usec, tx.tai,time_state_str (ret)); }if (ret !=TIME_WAIT ) {printf ("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n" ); error_found =1 ; -printf ("adjtimex: %10ld sec + %6ld us (%i)\t%s\n" , - tx.time.tv_sec, - tx.time.tv_usec, +printf ("adjtimex: %10lld sec + %6lld us (%i)\t%s\n" , + (long long )tx.time.tv_sec, + (long long )tx.time.tv_usec, tx.tai,time_state_str (ret)); }
@@ -291,7 +292,8 @@ int main(int argc, char **argv)printf ("Scheduling leap second for %s" ,ctime (&next_leap));/* Set up timer */ -printf ("Setting timer for %ld - %s" , next_leap,ctime (&next_leap)); +printf ("Setting timer for %lld - %s" , (long long )next_leap, +ctime (&next_leap));memset (&se,0 ,sizeof (se)); se.sigev_notify =SIGEV_SIGNAL ; se.sigev_signo = signum;
@@ -339,17 +341,17 @@ int main(int argc, char **argv)if (tai_time) {clock_gettime (CLOCK_TAI , &tai); -printf ("%ld sec, %9ld ns\t%s\n" , - tai.tv_sec, - tai.tv_nsec, +printf ("%lld sec, %9ld ns\t%s\n" , + (long long )tai.tv_sec, + (long )tai.tv_nsec,time_state_str (ret)); }else {ctime_r (&tx.time.tv_sec, buf); buf[strlen (buf)-1 ] =0 ;/*remove trailing\n */ -printf ("%s + %6ld us (%i)\t%s\n" , +printf ("%s + %6lld us (%i)\t%s\n" , buf, - tx.time.tv_usec, + (long long )tx.time.tv_usec, tx.tai,time_state_str (ret)); }
tools/testing/selftests/timers/rtcpie.c +2 -2
@@ -111,8 +111,8 @@ int main(int argc, char **argv)timersub (&end, &start, &diff);if (diff.tv_sec >0 || diff.tv_usec > ((1000000L / tmp) *1.10 )) { -fprintf (stderr,"\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n" , - diff.tv_sec, diff.tv_usec, +fprintf (stderr,"\nPIE delta error: %lld.%06lld should be close to 0.%06lu\n" , + (long long )diff.tv_sec, (long long )diff.tv_usec, (1000000L / tmp));fflush (stdout);exit (-1 );
tools/testing/selftests/timers/threadtest.c +4 -2
@@ -63,7 +63,8 @@ void checklist(const struct timespec *list, int size)for (j =0 ; j < size; j++) {if (j == i)printf ("---------------\n" ); -printf ("%lu:%lu\n" , list[j].tv_sec, list[j].tv_nsec); +printf ("%lld:%ld\n" , (long long )list[j].tv_sec, + (long )list[j].tv_nsec);if (j == i+1 )printf ("---------------\n" ); }
@@ -162,7 +163,8 @@ int main(int argc, char **argv) start =time (0 );strftime (buf,255 ,"%a, %d %b %Y %T %z" ,localtime (&start));printf ("%s\n" , buf); -printf ("Testing consistency with %i threads for %ld seconds: " , thread_count, runtime); +printf ("Testing consistency with %i threads for %lld seconds: " , + thread_count, (long long )runtime);fflush (stdout);/* spawn */
tools/testing/selftests/timers/valid-adjtimex.c +8 -6
@@ -136,8 +136,8 @@ int validate_freq(void) tx.modes =0 ; ret =adjtimex (&tx);if (tx.freq != valid_freq[i]) { -printf ("Warning: freq value %ld not what we set it (%ld)!\n" , - tx.freq, valid_freq[i]); +printf ("Warning: freq value %lld not what we set it (%ld)!\n" , + (long long )tx.freq, valid_freq[i]); } }for (i =0 ; i <NUM_FREQ_OUTOFRANGE ; i++) {
@@ -156,8 +156,8 @@ int validate_freq(void) ret =adjtimex (&tx);if (tx.freq == outofrange_freq[i]) {printf ("[FAIL]\n" ); -printf ("ERROR: out of range value %ld actually set!\n" , - tx.freq); +printf ("ERROR: out of range value %lld actually set!\n" , + (long long )tx.freq); pass =-1 ; goto out; }
@@ -218,7 +218,8 @@ int set_offset(long long offset, int use_nano) ret =clock_adjtime (CLOCK_REALTIME , &tmx);if (ret <0 ) { -printf ("(sec: %ld usec: %ld) " , tmx.time.tv_sec, tmx.time.tv_usec); +printf ("(sec: %lld usec: %lld) " , (long long )tmx.time.tv_sec, + (long long )tmx.time.tv_usec);printf ("[FAIL]\n" );return -1 ; }
@@ -238,7 +239,8 @@ int set_bad_offset(long sec, long usec, int use_nano) tmx.time.tv_usec = usec; ret =clock_adjtime (CLOCK_REALTIME , &tmx);if (ret >=0 ) { -printf ("Invalid (sec: %ld usec: %ld) did not fail! " , tmx.time.tv_sec, tmx.time.tv_usec); +printf ("Invalid (sec: %lld usec: %lld) did not fail! " , + (long long )tmx.time.tv_sec, (long long )tmx.time.tv_usec);printf ("[FAIL]\n" );return -1 ; }
Signature · 1 line
2.53.0
[tip: timers/core] selftests: timers: Print time values as long long
The following commit has been merged into the timers/core branch of tip:
The following commit has been merged into the timers/core branch of tip:
Commit-ID: 954394fe6221327e7c5a05869f958584f277fc11 Gitweb: https://git.kernel.org/tip/954394fe6221327e7c5a05869f958584f277fc11 Author: Sasha Levin <[email protected]> AuthorDate: Tue, 03 Nov 2026 10:54:42 -05:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Mon, 09 Nov 2026 20:21:47 +01:00
selftests: timers: Print time values as long long
On 32-bit architectures with a 64-bit time_t, such as armhf in Debian 13, time_t, the tv_sec fields of struct timespec and struct timeval, their tv_usec field and the freq field of struct timex are long long, and printing them with %ld or %lu warns:
leap-a-day.c:142:69: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'time_t' {aka 'long long int'} [-Wformat=]
Cast the values to long long and print them with %lld. While at it, print tv_nsec, a long, with %ld instead of %lu, and cast it to long, as it is a long long on x32, and print the unsigned long frequency in rtcpie with %lu.
--- tools/testing/selftests/timers/alarmtimer-suspend.c | 9 ++-- tools/testing/selftests/timers/inconsistency-check.c | 5 +- tools/testing/selftests/timers/leap-a-day.c | 28 +++++------ tools/testing/selftests/timers/rtcpie.c | 4 +- tools/testing/selftests/timers/threadtest.c | 6 +- tools/testing/selftests/timers/valid-adjtimex.c | 14 +++--- 6 files changed, 37 insertions(+), 29 deletions(-)
tools/testing/selftests/timers/alarmtimer-suspend.c +5 -4
@@ -60,8 +60,8 @@ void sigalarm(int signo) delta_ns =timespec_sub (start_time, ts); delta_ns -=NSEC_PER_SEC *SUSPEND_SECS * alarmcount; -printf ("ALARM(%i): %ld:%ld latency: %lld ns " , alarmcount, ts.tv_sec, - ts.tv_nsec, delta_ns); +printf ("ALARM(%i): %lld:%ld latency: %lld ns " , alarmcount, + (long long )ts.tv_sec, (long )ts.tv_nsec, delta_ns);if (delta_ns >UNREASONABLE_LAT ) {printf ("[FAIL]\n" );
@@ -103,8 +103,9 @@ int main(void) }clock_gettime (alarm_clock_id, &start_time); -printf ("Start time (%s): %ld:%ld\n" ,clock_name (alarm_clock_id), - start_time.tv_sec, start_time.tv_nsec); +printf ("Start time (%s): %lld:%ld\n" ,clock_name (alarm_clock_id), + (long long )start_time.tv_sec, + (long )start_time.tv_nsec);printf ("Setting alarm for every %i seconds\n" ,SUSPEND_SECS ); its1.it_value = start_time; its1.it_value.tv_sec +=SUSPEND_SECS ;
tools/testing/selftests/timers/inconsistency-check.c +3 -2
@@ -86,8 +86,9 @@ int consistency_test(int clock_type, unsigned long seconds)for (i =0 ; i <CALLS_PER_LOOP ; i++) {if (i == inconsistent)ksft_print_msg ("--------------------\n" ); -ksft_print_msg ("%lu:%lu\n" , list[i].tv_sec, - list[i].tv_nsec); +ksft_print_msg ("%lld:%ld\n" , + (long long )list[i].tv_sec, + (long )list[i].tv_nsec);if (i == inconsistent +1 )ksft_print_msg ("--------------------\n" ); }
tools/testing/selftests/timers/leap-a-day.c +15 -13
@@ -139,20 +139,21 @@ void sigalarm(int signo) ret =adjtimex (&tx);if (tx.time.tv_sec < next_leap) { -printf ("Error: Early timer expiration! (Should be %ld)\n" , next_leap); +printf ("Error: Early timer expiration! (Should be %lld)\n" , + (long long )next_leap); error_found =1 ; -printf ("adjtimex: %10ld sec + %6ld us (%i)\t%s\n" , - tx.time.tv_sec, - tx.time.tv_usec, +printf ("adjtimex: %10lld sec + %6lld us (%i)\t%s\n" , + (long long )tx.time.tv_sec, + (long long )tx.time.tv_usec, tx.tai,time_state_str (ret)); }if (ret !=TIME_WAIT ) {printf ("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n" ); error_found =1 ; -printf ("adjtimex: %10ld sec + %6ld us (%i)\t%s\n" , - tx.time.tv_sec, - tx.time.tv_usec, +printf ("adjtimex: %10lld sec + %6lld us (%i)\t%s\n" , + (long long )tx.time.tv_sec, + (long long )tx.time.tv_usec, tx.tai,time_state_str (ret)); }
@@ -291,7 +292,8 @@ int main(int argc, char **argv)printf ("Scheduling leap second for %s" ,ctime (&next_leap));/* Set up timer */ -printf ("Setting timer for %ld - %s" , next_leap,ctime (&next_leap)); +printf ("Setting timer for %lld - %s" , (long long )next_leap, +ctime (&next_leap));memset (&se,0 ,sizeof (se)); se.sigev_notify =SIGEV_SIGNAL ; se.sigev_signo = signum;
@@ -339,17 +341,17 @@ int main(int argc, char **argv)if (tai_time) {clock_gettime (CLOCK_TAI , &tai); -printf ("%ld sec, %9ld ns\t%s\n" , - tai.tv_sec, - tai.tv_nsec, +printf ("%lld sec, %9ld ns\t%s\n" , + (long long )tai.tv_sec, + (long )tai.tv_nsec,time_state_str (ret)); }else {ctime_r (&tx.time.tv_sec, buf); buf[strlen (buf)-1 ] =0 ;/*remove trailing\n */ -printf ("%s + %6ld us (%i)\t%s\n" , +printf ("%s + %6lld us (%i)\t%s\n" , buf, - tx.time.tv_usec, + (long long )tx.time.tv_usec, tx.tai,time_state_str (ret)); }
tools/testing/selftests/timers/rtcpie.c +2 -2
@@ -111,8 +111,8 @@ int main(int argc, char **argv)timersub (&end, &start, &diff);if (diff.tv_sec >0 || diff.tv_usec > ((1000000L / tmp) *1.10 )) { -fprintf (stderr,"\nPIE delta error: %ld.%06ld should be close to 0.%06ld\n" , - diff.tv_sec, diff.tv_usec, +fprintf (stderr,"\nPIE delta error: %lld.%06lld should be close to 0.%06lu\n" , + (long long )diff.tv_sec, (long long )diff.tv_usec, (1000000L / tmp));fflush (stdout);exit (-1 );
tools/testing/selftests/timers/threadtest.c +4 -2
@@ -63,7 +63,8 @@ void checklist(const struct timespec *list, int size)for (j =0 ; j < size; j++) {if (j == i)printf ("---------------\n" ); -printf ("%lu:%lu\n" , list[j].tv_sec, list[j].tv_nsec); +printf ("%lld:%ld\n" , (long long )list[j].tv_sec, + (long )list[j].tv_nsec);if (j == i+1 )printf ("---------------\n" ); }
@@ -162,7 +163,8 @@ int main(int argc, char **argv) start =time (0 );strftime (buf,255 ,"%a, %d %b %Y %T %z" ,localtime (&start));printf ("%s\n" , buf); -printf ("Testing consistency with %i threads for %ld seconds: " , thread_count, runtime); +printf ("Testing consistency with %i threads for %lld seconds: " , + thread_count, (long long )runtime);fflush (stdout);/* spawn */
tools/testing/selftests/timers/valid-adjtimex.c +8 -6
@@ -136,8 +136,8 @@ int validate_freq(void) tx.modes =0 ; ret =adjtimex (&tx);if (tx.freq != valid_freq[i]) { -printf ("Warning: freq value %ld not what we set it (%ld)!\n" , - tx.freq, valid_freq[i]); +printf ("Warning: freq value %lld not what we set it (%ld)!\n" , + (long long )tx.freq, valid_freq[i]); } }for (i =0 ; i <NUM_FREQ_OUTOFRANGE ; i++) {
@@ -156,8 +156,8 @@ int validate_freq(void) ret =adjtimex (&tx);if (tx.freq == outofrange_freq[i]) {printf ("[FAIL]\n" ); -printf ("ERROR: out of range value %ld actually set!\n" , - tx.freq); +printf ("ERROR: out of range value %lld actually set!\n" , + (long long )tx.freq); pass =-1 ; goto out; }
@@ -218,7 +218,8 @@ int set_offset(long long offset, int use_nano) ret =clock_adjtime (CLOCK_REALTIME , &tmx);if (ret <0 ) { -printf ("(sec: %ld usec: %ld) " , tmx.time.tv_sec, tmx.time.tv_usec); +printf ("(sec: %lld usec: %lld) " , (long long )tmx.time.tv_sec, + (long long )tmx.time.tv_usec);printf ("[FAIL]\n" );return -1 ; }
@@ -238,7 +239,8 @@ int set_bad_offset(long sec, long usec, int use_nano) tmx.time.tv_usec = usec; ret =clock_adjtime (CLOCK_REALTIME , &tmx);if (ret >=0 ) { -printf ("Invalid (sec: %ld usec: %ld) did not fail! " , tmx.time.tv_sec, tmx.time.tv_usec); +printf ("Invalid (sec: %lld usec: %lld) did not fail! " , + (long long )tmx.time.tv_sec, (long long )tmx.time.tv_usec);printf ("[FAIL]\n" );return -1 ; }