From f477523f80ff41271131c430df5b1408902b8aab Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 9 Apr 2026 11:55:58 -0400
Subject: [PATCH v26a 2/2] fixup! instrumentation: Show additional TSC clock
 source info in pg_test_timing

---
 src/include/portability/instr_time.h    |  2 ++
 src/common/instr_time.c                 | 18 +++++++++++++++--
 src/port/pg_cpu_x86.c                   |  3 ---
 src/bin/pg_test_timing/pg_test_timing.c | 27 +++++++++++++++++--------
 4 files changed, 37 insertions(+), 13 deletions(-)

diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h
index 4917728afc3..46307fd47a9 100644
--- a/src/include/portability/instr_time.h
+++ b/src/include/portability/instr_time.h
@@ -161,6 +161,7 @@ extern PGDLLIMPORT bool timing_tsc_enabled;
  */
 extern PGDLLIMPORT int32 timing_tsc_frequency_khz;
 
+
 #if PG_INSTR_TSC_CLOCK
 
 extern void pg_initialize_timing_tsc(void);
@@ -178,6 +179,7 @@ extern const TscClockSourceInfo *pg_timing_tsc_clock_source_info(void);
 
 #endif							/* PG_INSTR_TSC_CLOCK */
 
+
 /*
  * Returns the current timing clock source effectively in use, resolving
  * TIMING_CLOCK_SOURCE_AUTO to either TIMING_CLOCK_SOURCE_SYSTEM or
diff --git a/src/common/instr_time.c b/src/common/instr_time.c
index 2523d6a0df6..0db19673490 100644
--- a/src/common/instr_time.c
+++ b/src/common/instr_time.c
@@ -208,9 +208,23 @@ tsc_detect_frequency(void)
 	tsc_info.frequency_khz = 0;
 	tsc_info.frequency_source[0] = '\0';
 
+	strlcat(tsc_info.frequency_source, "x86",
+			sizeof(tsc_info.frequency_source));
+
 	/* We require RDTSCP support and an invariant TSC, bail if not available */
-	if (!x86_feature_available(PG_RDTSCP) || !x86_feature_available(PG_TSC_INVARIANT))
+	if (!x86_feature_available(PG_RDTSCP))
+	{
+		strlcat(tsc_info.frequency_source, ", no rdtscp",
+				sizeof(tsc_info.frequency_source));
 		return;
+	}
+
+	if (!x86_feature_available(PG_TSC_INVARIANT))
+	{
+		strlcat(tsc_info.frequency_source, ", not invariant",
+				sizeof(tsc_info.frequency_source));
+		return;
+	}
 
 	/* Determine speed at which the TSC advances */
 	timing_tsc_frequency_khz = x86_tsc_frequency_khz(tsc_info.frequency_source,
@@ -231,7 +245,7 @@ tsc_detect_frequency(void)
 	timing_tsc_frequency_khz = tsc_info.calibrated_frequency_khz;
 	if (timing_tsc_frequency_khz > 0)
 	{
-		strlcpy(tsc_info.frequency_source, "x86, calibration",
+		strlcat(tsc_info.frequency_source, ", calibration",
 				sizeof(tsc_info.frequency_source));
 		tsc_info.frequency_khz = timing_tsc_frequency_khz;
 	}
diff --git a/src/port/pg_cpu_x86.c b/src/port/pg_cpu_x86.c
index c097807fc7f..56925e9aa51 100644
--- a/src/port/pg_cpu_x86.c
+++ b/src/port/pg_cpu_x86.c
@@ -169,9 +169,6 @@ x86_tsc_frequency_khz(char *source, size_t source_len)
 {
 	unsigned int reg[4] = {0};
 
-	if (source)
-		strlcpy(source, "x86", source_len);
-
 	/*
 	 * If we're inside a virtual machine, try to fetch the TSC frequency from
 	 * the hypervisor, using a hypervisor specific method.
diff --git a/src/bin/pg_test_timing/pg_test_timing.c b/src/bin/pg_test_timing/pg_test_timing.c
index ef5dd90c41f..5cb551fa7b0 100644
--- a/src/bin/pg_test_timing/pg_test_timing.c
+++ b/src/bin/pg_test_timing/pg_test_timing.c
@@ -197,17 +197,28 @@ test_tsc_timing(void)
 		loop_count = test_timing(test_duration, TIMING_CLOCK_SOURCE_TSC, true);
 		output(loop_count);
 		printf("\n");
+	}
 
-		info = pg_timing_tsc_clock_source_info();
-		printf(_("TSC frequency in use: %d kHz\n"), info->frequency_khz);
-		if (info->frequency_source[0] != '\0')
-			printf(_("TSC frequency source: %s\n"), info->frequency_source);
+	/*
+	 * Report TSC information regardless of whether it was usable, makes
+	 * debugging a lot easier.
+	 */
+	info = pg_timing_tsc_clock_source_info();
+	if (info->frequency_source[0] != '\0')
+		printf(_("TSC frequency source: %s\n"), info->frequency_source);
+	printf(_("TSC frequency in use: %d kHz\n"), info->frequency_khz);
 
-		if (info->calibrated_frequency_khz > 0)
-			printf(_("TSC frequency from calibration: %d kHz\n"), info->calibrated_frequency_khz);
-		else
-			printf(_("TSC calibration did not converge\n"));
+	if (info->calibrated_frequency_khz > 0)
+		printf(_("TSC frequency from calibration: %d kHz\n"), info->calibrated_frequency_khz);
+	else
+		printf(_("TSC calibration did not converge\n"));
 
+	/*
+	 * Report whether TSC was usable and, if so, whether it will be used
+	 * automatically.
+	 */
+	if (loop_count > 0)
+	{
 		pg_set_timing_clock_source(TIMING_CLOCK_SOURCE_AUTO);
 		if (pg_current_timing_clock_source() == TIMING_CLOCK_SOURCE_TSC)
 			printf(_("TSC clock source will be used by default, unless timing_clock_source is set to 'system'.\n"));
-- 
2.53.0.1.gb2826b52eb

