mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests: timers: print time values as long long
@ 2026-11-03 15:54 Sasha Levin
  0 siblings, 0 replies; only message in thread
From: Sasha Levin @ 2026-11-03 15:54 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Anna-Maria Behnsen, Frederic Weisbecker
  Cc: Stephen Boyd, Miroslav Lichvar, Shuah Khan, linux-kernel,
	linux-kselftest

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.

Signed-off-by: Sasha Levin <sashal@kernel.org>
---
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(-)

diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c
index 120b3ce8b39e8..c1d87cbfee27e 100644
--- a/tools/testing/selftests/timers/alarmtimer-suspend.c
+++ b/tools/testing/selftests/timers/alarmtimer-suspend.c
@@ -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;
diff --git a/tools/testing/selftests/timers/inconsistency-check.c b/tools/testing/selftests/timers/inconsistency-check.c
index d7982ac4bd18b..e8012fed2280c 100644
--- a/tools/testing/selftests/timers/inconsistency-check.c
+++ b/tools/testing/selftests/timers/inconsistency-check.c
@@ -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");
 			}
diff --git a/tools/testing/selftests/timers/leap-a-day.c b/tools/testing/selftests/timers/leap-a-day.c
index b93cb5714b37b..e932e13071ed2 100644
--- a/tools/testing/selftests/timers/leap-a-day.c
+++ b/tools/testing/selftests/timers/leap-a-day.c
@@ -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));
 			}
diff --git a/tools/testing/selftests/timers/rtcpie.c b/tools/testing/selftests/timers/rtcpie.c
index 4ba42d198b7ec..5d88c78e3bdbc 100644
--- a/tools/testing/selftests/timers/rtcpie.c
+++ b/tools/testing/selftests/timers/rtcpie.c
@@ -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);
diff --git a/tools/testing/selftests/timers/threadtest.c b/tools/testing/selftests/timers/threadtest.c
index 60b8b21bf7824..fbf5d4cd80792 100644
--- a/tools/testing/selftests/timers/threadtest.c
+++ b/tools/testing/selftests/timers/threadtest.c
@@ -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 */
diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index f641d5fb09023..c666a327d0580 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -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;
 	}
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-03 15:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 15:54 [PATCH] selftests: timers: print time values as long long Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®