Skip to content

Commit 9dcc764

Browse files
committed
Minor tweaks for pg_test_timing.
Increase the size of the "direct" histogram to 10K elements, so that we can precisely track loop times up to 10 microseconds. (Going further than that seems pretty uninteresting, even for very old and slow machines.) Relabel "Per loop time" as "Average loop time" for clarity. Pre-zero the histogram arrays to make sure that they are loaded into processor cache and any copy-on-write overhead has happened before we enter the timing loop. Also use unlikely() to keep the compiler from thinking that the clock-went-backwards case is part of the hot loop. Neither of these hacks made a lot of difference on my own machine, but they seem like they might help on some platforms. Discussion: https://postgr.es/m/be0339cc-1ae1-4892-9445-8e6d8995a44d@eisentraut.org
1 parent 167ed80 commit 9dcc764

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

doc/src/sgml/ref/pgtesttiming.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ PostgreSQL documentation
161161
<para>
162162
<screen><![CDATA[
163163
Testing timing overhead for 3 seconds.
164-
Per loop time including overhead: 16.40 ns
164+
Average loop time including overhead: 16.40 ns
165165
Histogram of timing durations:
166166
<= ns % of total running % count
167167
0 0.0000 0.0000 0

src/bin/pg_test_timing/pg_test_timing.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ static double max_rprct = 99.99;
2020
/* record duration in powers of 2 nanoseconds */
2121
static long long int histogram[32];
2222

23-
/* record counts of first 1024 durations directly */
24-
#define NUM_DIRECT 1024
23+
/* record counts of first 10K durations directly */
24+
#define NUM_DIRECT 10000
2525
static long long int direct_histogram[NUM_DIRECT];
2626

2727
/* separately record highest observed duration */
@@ -161,6 +161,16 @@ test_timing(unsigned int duration)
161161
end_time,
162162
temp;
163163

164+
/*
165+
* Pre-zero the statistics data structures. They're already zero by
166+
* default, but this helps bring them into processor cache and avoid
167+
* possible timing glitches due to COW behavior.
168+
*/
169+
memset(direct_histogram, 0, sizeof(direct_histogram));
170+
memset(histogram, 0, sizeof(histogram));
171+
largest_diff = 0;
172+
largest_diff_count = 0;
173+
164174
total_time = duration > 0 ? duration * INT64CONST(1000000000) : 0;
165175

166176
INSTR_TIME_SET_CURRENT(start_time);
@@ -177,7 +187,7 @@ test_timing(unsigned int duration)
177187
diff = cur - prev;
178188

179189
/* Did time go backwards? */
180-
if (diff < 0)
190+
if (unlikely(diff < 0))
181191
{
182192
fprintf(stderr, _("Detected clock going backwards in time.\n"));
183193
fprintf(stderr, _("Time warp: %d ms\n"), diff);
@@ -215,7 +225,7 @@ test_timing(unsigned int duration)
215225

216226
INSTR_TIME_SUBTRACT(end_time, start_time);
217227

218-
printf(_("Per loop time including overhead: %0.2f ns\n"),
228+
printf(_("Average loop time including overhead: %0.2f ns\n"),
219229
INSTR_TIME_GET_DOUBLE(end_time) * 1e9 / loop_count);
220230

221231
return loop_count;

0 commit comments

Comments
 (0)