From 3eea80de37049bd453cf5724951c18505d557a99 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Thu, 2 Apr 2026 10:29:06 +0800 Subject: [PATCH v5 2/2] pg_test_timing: use int64 for largest observed time delta pg_test_timing obtains timing deltas with INSTR_TIME_GET_NANOSEC(), which returns a nanosecond value that should be stored in int64. Adjust diff and largest_diff to use int64 accordingly. Also fix the corresponding printf format specifiers so these values are printed correctly. Author: Chao Li Reviewed-by: Lukas Fittl Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/F780CEEB-A237-4302-9F55-60E9D8B6533D@gmail.com --- src/bin/pg_test_timing/pg_test_timing.c | 28 ++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/bin/pg_test_timing/pg_test_timing.c b/src/bin/pg_test_timing/pg_test_timing.c index 5eb20e091b1..340b529e972 100644 --- a/src/bin/pg_test_timing/pg_test_timing.c +++ b/src/bin/pg_test_timing/pg_test_timing.c @@ -18,14 +18,14 @@ static unsigned int test_duration = 3; static double max_rprct = 99.99; /* record duration in powers of 2 nanoseconds */ -static long long int histogram[32]; +static long long int histogram[64]; /* record counts of first 10K durations directly */ #define NUM_DIRECT 10000 static long long int direct_histogram[NUM_DIRECT]; /* separately record highest observed duration */ -static int32 largest_diff; +static int64 largest_diff; static long long int largest_diff_count; @@ -262,8 +262,8 @@ test_timing(unsigned int duration, TimingClockSourceType source, bool fast_timin while (INSTR_TIME_GT(end_time, cur)) { - int32 diff, - bits; + int64 diff; + int32 bits; instr_time diff_time; prev = cur; @@ -281,13 +281,17 @@ test_timing(unsigned int duration, TimingClockSourceType source, bool fast_timin if (unlikely(diff < 0)) { fprintf(stderr, _("Detected clock going backwards in time.\n")); - fprintf(stderr, _("Time warp: %d ns\n"), diff); + fprintf(stderr, _("Time warp: " INT64_FORMAT " ns\n"), diff); exit(1); } /* What is the highest bit in the time diff? */ if (diff > 0) - bits = pg_leftmost_one_pos32(diff) + 1; + { + bits = pg_leftmost_one_pos64(diff) + 1; + /* histogram should be define large enough */ + Assert(bits < lengthof(histogram)); + } else bits = 0; @@ -324,7 +328,7 @@ test_timing(unsigned int duration, TimingClockSourceType source, bool fast_timin static void output(uint64 loop_count) { - int max_bit = 31; + int max_bit = lengthof(histogram) - 1; const char *header1 = _("<= ns"); const char *header1b = _("ns"); const char *header2 = /* xgettext:no-c-format */ _("% of total"); @@ -342,7 +346,7 @@ output(uint64 loop_count) max_bit--; /* set minimum column widths */ - len1 = Max(8, len1); + len1 = Max(19, len1); len2 = Max(10, len2); len3 = Max(10, len3); len4 = Max(10, len4); @@ -360,8 +364,8 @@ output(uint64 loop_count) double prct = (double) histogram[i] * 100 / loop_count; rprct += prct; - printf("%*ld %*.4f %*.4f %*lld\n", - len1, (1L << i) - 1, + printf("%*llu %*.4f %*.4f %*lld\n", + len1, (1ULL << i) - 1, len2, prct, len3, rprct, len4, histogram[i]); @@ -409,8 +413,8 @@ output(uint64 loop_count) double prct = (double) largest_diff_count * 100 / loop_count; printf("...\n"); - printf("%*d %*.4f %*.4f %*lld\n", - len1, largest_diff, + printf("%*lld %*.4f %*.4f %*lld\n", + len1, (long long) largest_diff, len2, prct, len3, 100.0, len4, largest_diff_count); -- 2.50.1 (Apple Git-155)