PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_test_timing.c
Go to the documentation of this file.
1/*
2 * pg_test_timing.c
3 * tests overhead of timing calls and their monotonicity: that
4 * they always move forward
5 */
6
7#include "postgres_fe.h"
8
9#include <limits.h>
10
11#include "getopt_long.h"
12#include "port/pg_bitutils.h"
14
15static const char *progname;
16
17static unsigned int test_duration = 3;
18static double max_rprct = 99.99;
19
20/* record duration in powers of 2 nanoseconds */
21static long long int histogram[32];
22
23/* record counts of first 10K durations directly */
24#define NUM_DIRECT 10000
25static long long int direct_histogram[NUM_DIRECT];
26
27/* separately record highest observed duration */
29static long long int largest_diff_count;
30
31
32static void handle_args(int argc, char *argv[]);
33static void test_system_timing(void);
34#if PG_INSTR_TSC_CLOCK
35static void test_tsc_timing(void);
36#endif
38static void output(uint64 loop_count);
39
40int
41main(int argc, char *argv[])
42{
43 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_test_timing"));
44 progname = get_progname(argv[0]);
45
46 handle_args(argc, argv);
47
48 /* initialize timing infrastructure (required for INSTR_* calls) */
50
52
53#if PG_INSTR_TSC_CLOCK
55#endif
56
57 return 0;
58}
59
60static void
61handle_args(int argc, char *argv[])
62{
63 static struct option long_options[] = {
64 {"duration", required_argument, NULL, 'd'},
65 {"cutoff", required_argument, NULL, 'c'},
66 {NULL, 0, NULL, 0}
67 };
68
69 int option; /* Command line option */
70 int optindex = 0; /* used by getopt_long */
71 unsigned long optval; /* used for option parsing */
72 char *endptr;
73
74 if (argc > 1)
75 {
76 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
77 {
78 printf(_("Usage: %s [-d DURATION] [-c CUTOFF]\n"), progname);
79 exit(0);
80 }
81 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
82 {
83 puts("pg_test_timing (PostgreSQL) " PG_VERSION);
84 exit(0);
85 }
86 }
87
88 while ((option = getopt_long(argc, argv, "d:c:",
89 long_options, &optindex)) != -1)
90 {
91 switch (option)
92 {
93 case 'd':
94 errno = 0;
95 optval = strtoul(optarg, &endptr, 10);
96
97 if (endptr == optarg || *endptr != '\0' ||
98 errno != 0 || optval != (unsigned int) optval)
99 {
100 fprintf(stderr, _("%s: invalid argument for option %s\n"),
101 progname, "--duration");
102 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
103 exit(1);
104 }
105
106 test_duration = (unsigned int) optval;
107 if (test_duration == 0)
108 {
109 fprintf(stderr, _("%s: %s must be in range %u..%u\n"),
110 progname, "--duration", 1, UINT_MAX);
111 exit(1);
112 }
113 break;
114
115 case 'c':
116 errno = 0;
117 max_rprct = strtod(optarg, &endptr);
118
119 if (endptr == optarg || *endptr != '\0' || errno != 0)
120 {
121 fprintf(stderr, _("%s: invalid argument for option %s\n"),
122 progname, "--cutoff");
123 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
124 exit(1);
125 }
126
128 {
129 fprintf(stderr, _("%s: %s must be in range %u..%u\n"),
130 progname, "--cutoff", 0, 100);
131 exit(1);
132 }
133 break;
134
135 default:
136 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
137 progname);
138 exit(1);
139 break;
140 }
141 }
142
143 if (argc > optind)
144 {
146 _("%s: too many command-line arguments (first is \"%s\")\n"),
147 progname, argv[optind]);
148 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
149 progname);
150 exit(1);
151 }
152
153 printf(ngettext("Testing timing overhead for %u second.\n\n",
154 "Testing timing overhead for %u seconds.\n\n",
157}
158
159/*
160 * This tests default (non-fast) timing code. A clock source for that is
161 * always available. Hence, we can unconditionally output the result.
162 */
163static void
171
172/*
173 * If on a supported architecture, test the TSC clock source. This clock
174 * source is not always available. In that case we print an informational
175 * message indicating as such.
176 *
177 * We first emit "slow" timings (RDTSCP on x86), which are used for higher
178 * precision measurements when the TSC clock source is enabled. We emit
179 * "fast" timings second (RDTSC on x86), which is used for faster timing
180 * measurements with lower precision.
181 */
182#if PG_INSTR_TSC_CLOCK
183static void
184test_tsc_timing(void)
185{
188
189 printf("\n");
191 if (loop_count > 0)
192 {
194 printf("\n");
195
196 /* Now, emit fast timing measurements */
199 printf("\n");
200
201 printf(_("TSC frequency in use: %u kHz\n"), timing_tsc_frequency_khz);
202
204 if (calibrated_freq > 0)
205 printf(_("TSC frequency from calibration: %u kHz\n"), calibrated_freq);
206 else
207 printf(_("TSC calibration did not converge\n"));
208
211 printf(_("TSC clock source will be used by default, unless timing_clock_source is set to 'system'.\n"));
212 else
213 printf(_("TSC clock source will not be used by default, unless timing_clock_source is set to 'tsc'.\n"));
214 }
215 else
216 printf(_("TSC clock source is not usable. Likely unable to determine TSC frequency. Are you running in an unsupported virtualized environment?\n"));
217}
218#endif
219
220static uint64
222{
223 uint64 loop_count = 0;
225 end_time,
226 prev,
227 cur;
228 const char *time_source = NULL;
229
231 return 0;
232
234
235#if PG_INSTR_TSC_CLOCK
238#endif
239
240 if (fast_timing)
241 printf(_("Fast clock source: %s\n"), time_source);
243 printf(_("System clock source: %s\n"), time_source);
244 else
245 printf(_("Clock source: %s\n"), time_source);
246
247 /*
248 * Pre-zero the statistics data structures. They're already zero by
249 * default, but this helps bring them into processor cache and avoid
250 * possible timing glitches due to COW behavior.
251 */
253 memset(histogram, 0, sizeof(histogram));
254 largest_diff = 0;
256
258 cur = start_time;
259
262
263 while (INSTR_TIME_GT(end_time, cur))
264 {
265 int32 diff,
266 bits;
268
269 prev = cur;
270
271 if (fast_timing)
273 else
275
276 diff_time = cur;
279
280 /* Did time go backwards? */
281 if (unlikely(diff < 0))
282 {
283 fprintf(stderr, _("Detected clock going backwards in time.\n"));
284 fprintf(stderr, _("Time warp: %d ms\n"), diff);
285 exit(1);
286 }
287
288 /* What is the highest bit in the time diff? */
289 if (diff > 0)
290 bits = pg_leftmost_one_pos32(diff) + 1;
291 else
292 bits = 0;
293
294 /* Update appropriate duration bucket */
295 histogram[bits]++;
296
297 /* Update direct histogram of time diffs */
298 if (diff < NUM_DIRECT)
300
301 /* Also track the largest observed duration, even if >= NUM_DIRECT */
302 if (diff > largest_diff)
303 {
306 }
307 else if (diff == largest_diff)
309
310 loop_count++;
311 }
312
313 /* Refresh end time to be the actual time spent (vs the target end time) */
315
317
318 printf(_("Average loop time including overhead: %0.2f ns\n"),
320
321 return loop_count;
322}
323
324static void
326{
327 int max_bit = 31;
328 const char *header1 = _("<= ns");
329 const char *header1b = _("ns");
330 const char *header2 = /* xgettext:no-c-format */ _("% of total");
331 const char *header3 = /* xgettext:no-c-format */ _("running %");
332 const char *header4 = _("count");
333 int len1 = strlen(header1);
334 int len2 = strlen(header2);
335 int len3 = strlen(header3);
336 int len4 = strlen(header4);
337 double rprct;
338 bool stopped = false;
339
340 /* find highest bit value */
341 while (max_bit > 0 && histogram[max_bit] == 0)
342 max_bit--;
343
344 /* set minimum column widths */
345 len1 = Max(8, len1);
346 len2 = Max(10, len2);
347 len3 = Max(10, len3);
348 len4 = Max(10, len4);
349
350 printf(_("Histogram of timing durations:\n"));
351 printf("%*s %*s %*s %*s\n",
352 len1, header1,
353 len2, header2,
354 len3, header3,
355 len4, header4);
356
357 rprct = 0;
358 for (int i = 0; i <= max_bit; i++)
359 {
360 double prct = (double) histogram[i] * 100 / loop_count;
361
362 rprct += prct;
363 printf("%*ld %*.4f %*.4f %*lld\n",
364 len1, (1L << i) - 1,
365 len2, prct,
366 len3, rprct,
367 len4, histogram[i]);
368 }
369
370 printf(_("\nObserved timing durations up to %.4f%%:\n"), max_rprct);
371 printf("%*s %*s %*s %*s\n",
372 len1, header1b,
373 len2, header2,
374 len3, header3,
375 len4, header4);
376
377 rprct = 0;
378 for (int i = 0; i < NUM_DIRECT; i++)
379 {
380 if (direct_histogram[i])
381 {
382 double prct = (double) direct_histogram[i] * 100 / loop_count;
383 bool print_it = !stopped;
384
385 rprct += prct;
386
387 /* if largest diff is < NUM_DIRECT, be sure we print it */
388 if (i == largest_diff)
389 {
390 if (stopped)
391 printf("...\n");
392 print_it = true;
393 }
394
395 if (print_it)
396 printf("%*d %*.4f %*.4f %*lld\n",
397 len1, i,
398 len2, prct,
399 len3, rprct,
401 if (rprct >= max_rprct)
402 stopped = true;
403 }
404 }
405
406 /* print largest diff when it's outside the array range */
408 {
409 double prct = (double) largest_diff_count * 100 / loop_count;
410
411 printf("...\n");
412 printf("%*d %*.4f %*.4f %*lld\n",
413 len1, largest_diff,
414 len2, prct,
415 len3, 100.0,
417 }
418}
#define ngettext(s, p, n)
Definition c.h:1270
#define Max(x, y)
Definition c.h:1085
#define PG_TEXTDOMAIN(domain)
Definition c.h:1303
int32_t int32
Definition c.h:620
uint64_t uint64
Definition c.h:625
#define unlikely(x)
Definition c.h:438
uint32_t uint32
Definition c.h:624
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition exec.c:430
int main(void)
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
struct cursor * cur
Definition ecpg.c:29
#define _(x)
Definition elog.c:95
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition getopt_long.c:60
#define required_argument
Definition getopt_long.h:26
FILE * output
void pg_initialize_timing(void)
Definition instr_time.c:82
int32 timing_tsc_frequency_khz
Definition instr_time.c:67
bool pg_set_timing_clock_source(TimingClockSourceType source)
Definition instr_time.c:92
#define PG_INSTR_SYSTEM_CLOCK_NAME
Definition instr_time.h:216
#define INSTR_TIME_SET_CURRENT(t)
Definition instr_time.h:426
#define INSTR_TIME_GT(x, y)
Definition instr_time.h:442
#define INSTR_TIME_GET_NANOSEC(t)
Definition instr_time.h:445
#define INSTR_TIME_GET_DOUBLE(t)
Definition instr_time.h:448
#define INSTR_TIME_SET_CURRENT_FAST(t)
Definition instr_time.h:423
#define INSTR_TIME_SUBTRACT(x, y)
Definition instr_time.h:436
#define INSTR_TIME_ADD_NANOSEC(t, n)
Definition instr_time.h:433
static TimingClockSourceType pg_current_timing_clock_source(void)
Definition instr_time.h:178
TimingClockSourceType
Definition instr_time.h:124
@ TIMING_CLOCK_SOURCE_SYSTEM
Definition instr_time.h:126
@ TIMING_CLOCK_SOURCE_AUTO
Definition instr_time.h:125
int i
Definition isn.c:77
static int pg_leftmost_one_pos32(uint32 word)
Definition pg_bitutils.h:41
static time_t start_time
Definition pg_ctl.c:96
PGDLLIMPORT int optind
Definition getopt.c:47
PGDLLIMPORT char * optarg
Definition getopt.c:49
static rewind_source * source
Definition pg_rewind.c:89
static long long int histogram[32]
static void test_system_timing(void)
static int32 largest_diff
static void handle_args(int argc, char *argv[])
static long long int largest_diff_count
static uint64 test_timing(unsigned int duration, TimingClockSourceType source, bool fast_timing)
static double max_rprct
#define NUM_DIRECT
static const char * progname
static unsigned int test_duration
static long long int direct_histogram[NUM_DIRECT]
static int64 end_time
Definition pgbench.c:176
static int duration
Definition pgbench.c:175
const char * get_progname(const char *argv0)
Definition path.c:652
#define printf(...)
Definition port.h:266
static int fb(int x)
#define NS_PER_S
Definition uuid.c:31