PostgreSQL Source Code git master
Loading...
Searching...
No Matches
instr_time.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * instr_time.h
4 * portable high-precision interval timing
5 *
6 * This file provides an abstraction layer to hide portability issues in
7 * interval timing. On x86 we use the RDTSC/RDTSCP instruction directly in
8 * certain cases, or alternatively clock_gettime() on Unix-like systems and
9 * QueryPerformanceCounter() on Windows. These macros also give some breathing
10 * room to use other high-precision-timing APIs.
11 *
12 * The basic data type is instr_time, which all callers should treat as an
13 * opaque typedef. instr_time can store either an absolute time (of
14 * unspecified reference time) or an interval. The operations provided
15 * for it are:
16 *
17 * INSTR_TIME_IS_ZERO(t) is t equal to zero?
18 *
19 * INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
20 *
21 * INSTR_TIME_SET_CURRENT_FAST(t) set t to current time without waiting
22 * for instructions in out-of-order window
23 *
24 * INSTR_TIME_SET_CURRENT(t) set t to current time while waiting for
25 * instructions in OOO to retire
26 *
27 *
28 * INSTR_TIME_ADD(x, y) x += y
29 *
30 * INSTR_TIME_ADD_NANOSEC(t, n) t += n in nanoseconds (converts to ticks)
31 *
32 * INSTR_TIME_SUBTRACT(x, y) x -= y
33 *
34 * INSTR_TIME_ACCUM_DIFF(x, y, z) x += (y - z)
35 *
36 * INSTR_TIME_GT(x, y) x > y
37 *
38 * INSTR_TIME_GET_DOUBLE(t) convert t to double (in seconds)
39 *
40 * INSTR_TIME_GET_MILLISEC(t) convert t to double (in milliseconds)
41 *
42 * INSTR_TIME_GET_MICROSEC(t) convert t to int64 (in microseconds)
43 *
44 * INSTR_TIME_GET_NANOSEC(t) convert t to int64 (in nanoseconds)
45 *
46 * Note that INSTR_TIME_SUBTRACT and INSTR_TIME_ACCUM_DIFF convert
47 * absolute times to intervals. The INSTR_TIME_GET_xxx operations are
48 * only useful on intervals.
49 *
50 * When summing multiple measurements, it's recommended to leave the
51 * running sum in instr_time form (ie, use INSTR_TIME_ADD or
52 * INSTR_TIME_ACCUM_DIFF) and convert to a result format only at the end.
53 *
54 * Beware of multiple evaluations of the macro arguments.
55 *
56 *
57 * Copyright (c) 2001-2026, PostgreSQL Global Development Group
58 *
59 * src/include/portability/instr_time.h
60 *
61 *-------------------------------------------------------------------------
62 */
63#ifndef INSTR_TIME_H
64#define INSTR_TIME_H
65
66
67/*
68 * We store interval times as an int64 integer on all platforms, as int64 is
69 * cheap to add/subtract, the most common operation for instr_time. The
70 * acquisition of time and converting to specific units of time is platform
71 * specific.
72 *
73 * To avoid users of the API relying on the integer representation, we wrap
74 * the 64bit integer in a struct.
75 */
76typedef struct instr_time
77{
78 int64 ticks; /* in platforms specific unit */
80
81
82/* helpers macros used in platform specific code below */
83
84#define NS_PER_S INT64CONST(1000000000)
85#define NS_PER_MS INT64CONST(1000000)
86#define NS_PER_US INT64CONST(1000)
87
88/* Shift amount for fixed-point ticks-to-nanoseconds conversion. */
89#define TICKS_TO_NS_SHIFT 14
90
91/*
92 * PG_INSTR_TICKS_TO_NS controls whether pg_ticks_to_ns/pg_ns_to_ticks needs to
93 * check ticks_per_ns_scaled and potentially convert ticks <=> nanoseconds.
94 *
95 * PG_INSTR_TSC_CLOCK controls whether the TSC clock source is compiled in, and
96 * potentially used based on timing_tsc_enabled.
97 */
98#if defined(__x86_64__) || defined(_M_X64)
99#define PG_INSTR_TICKS_TO_NS 1
100#define PG_INSTR_TSC_CLOCK 1
101#elif defined(WIN32)
102#define PG_INSTR_TICKS_TO_NS 1
103#define PG_INSTR_TSC_CLOCK 0
104#else
105#define PG_INSTR_TICKS_TO_NS 0
106#define PG_INSTR_TSC_CLOCK 0
107#endif
108
109/*
110 * Variables used to translate ticks to nanoseconds, initialized by
111 * pg_initialize_timing and adjusted by pg_set_timing_clock_source calls or
112 * changes of the "timing_clock_source" GUC.
113 *
114 * Note that changing these values after setting an instr_time and before
115 * reading/converting it will lead to incorrect results. This is technically
116 * possible because the GUC can be changed at runtime, but unlikely, and we
117 * allow changing this at runtime to simplify testing of different sources.
118 */
122
131
133
134/*
135 * Initialize timing infrastructure
136 *
137 * This must be called at least once before using INSTR_TIME_SET_CURRENT*
138 * macros.
139 *
140 * If you want to use the TSC clock source in a client program,
141 * pg_set_timing_clock_source() needs to also be called.
142 */
143extern void pg_initialize_timing(void);
144
145/*
146 * Sets the time source to be used. Mainly intended for frontend programs,
147 * the backend should set it via the timing_clock_source GUC instead.
148 *
149 * Returns false if the clock source could not be set, for example when TSC
150 * is not available despite being explicitly set.
151 */
153
154/* Whether to actually use TSC based on availability and GUC settings. */
156
157/*
158 * TSC frequency in kHz, set during initialization.
159 *
160 * -1 = not yet initialized, 0 = TSC not usable, >0 = frequency in kHz.
161 */
163
164#if PG_INSTR_TSC_CLOCK
165
166extern void pg_initialize_timing_tsc(void);
167
169
170#endif /* PG_INSTR_TSC_CLOCK */
171
172/*
173 * Returns the current timing clock source effectively in use, resolving
174 * TIMING_CLOCK_SOURCE_AUTO to either TIMING_CLOCK_SOURCE_SYSTEM or
175 * TIMING_CLOCK_SOURCE_TSC.
176 */
177static inline TimingClockSourceType
179{
180#if PG_INSTR_TSC_CLOCK
183#endif
185}
186
187#ifndef WIN32
188
189/* On POSIX, use clock_gettime() for system clock source */
190
191#include <time.h>
192
193/*
194 * The best clockid to use according to the POSIX spec is CLOCK_MONOTONIC,
195 * since that will give reliable interval timing even in the face of changes
196 * to the system clock. However, POSIX doesn't require implementations to
197 * provide anything except CLOCK_REALTIME, so fall back to that if we don't
198 * find CLOCK_MONOTONIC.
199 *
200 * Also, some implementations have nonstandard clockids with better properties
201 * than CLOCK_MONOTONIC. In particular, as of macOS 10.12, Apple provides
202 * CLOCK_MONOTONIC_RAW which is both faster to read and higher resolution than
203 * their version of CLOCK_MONOTONIC.
204 *
205 * Note this does not get used in case the TSC clock source logic is used,
206 * which directly calls architecture specific timing instructions (e.g. RDTSC).
207 */
208#if defined(__darwin__) && defined(CLOCK_MONOTONIC_RAW)
209#define PG_INSTR_SYSTEM_CLOCK CLOCK_MONOTONIC_RAW
210#define PG_INSTR_SYSTEM_CLOCK_NAME "clock_gettime (CLOCK_MONOTONIC_RAW)"
211#elif defined(CLOCK_MONOTONIC)
212#define PG_INSTR_SYSTEM_CLOCK CLOCK_MONOTONIC
213#define PG_INSTR_SYSTEM_CLOCK_NAME "clock_gettime (CLOCK_MONOTONIC)"
214#else
215#define PG_INSTR_SYSTEM_CLOCK CLOCK_REALTIME
216#define PG_INSTR_SYSTEM_CLOCK_NAME "clock_gettime (CLOCK_REALTIME)"
217#endif
218
219static inline instr_time
221{
223 struct timespec tmp;
224
226
228 now.ticks = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
229
230 return now;
231}
232
233#else /* WIN32 */
234
235/* On Windows, use QueryPerformanceCounter() for system clock source */
236
237#define PG_INSTR_SYSTEM_CLOCK_NAME "QueryPerformanceCounter"
238static inline instr_time
240{
242 LARGE_INTEGER tmp;
243
245
247 now.ticks = tmp.QuadPart;
248
249 return now;
250}
251
252#endif /* WIN32 */
253
254static inline int64
256{
257#if PG_INSTR_TICKS_TO_NS
258 int64 ns = 0;
259
261
262 /*
263 * Avoid doing work if we don't use scaled ticks, e.g. system clock on
264 * Unix (in that case ticks is counted in nanoseconds)
265 */
266 if (ticks_per_ns_scaled == 0)
267 return ticks;
268
269 /*
270 * Would multiplication overflow? If so perform computation in two parts.
271 */
272 if (unlikely(ticks > (int64) max_ticks_no_overflow))
273 {
274 /*
275 * To avoid overflow, first scale total ticks down by the fixed
276 * factor, and *afterwards* multiply them by the frequency-based scale
277 * factor.
278 *
279 * The remaining ticks can follow the regular formula, since they
280 * won't overflow.
281 */
282 int64 count = ticks >> TICKS_TO_NS_SHIFT;
283
284 ns = count * ticks_per_ns_scaled;
285 ticks -= (count << TICKS_TO_NS_SHIFT);
286 }
287
288 ns += (ticks * ticks_per_ns_scaled) >> TICKS_TO_NS_SHIFT;
289
290 return ns;
291#else
293
294 return ticks;
295#endif /* PG_INSTR_TICKS_TO_NS */
296}
297
298static inline int64
300{
301#if PG_INSTR_TICKS_TO_NS
302 int64 ticks = 0;
303
305
306 /*
307 * If ticks_per_ns_scaled is zero, ticks are already in nanoseconds (e.g.
308 * system clock on Unix).
309 */
310 if (ticks_per_ns_scaled == 0)
311 return ns;
312
313 /*
314 * The reverse of pg_ticks_to_ns to avoid a similar overflow problem.
315 */
316 if (unlikely(ns > (INT64_MAX >> TICKS_TO_NS_SHIFT)))
317 {
318 int64 count = ns / ticks_per_ns_scaled;
319
320 ticks = count << TICKS_TO_NS_SHIFT;
321 ns -= count * ticks_per_ns_scaled;
322 }
323
324 ticks += (ns << TICKS_TO_NS_SHIFT) / ticks_per_ns_scaled;
325
326 return ticks;
327#else
329
330 return ns;
331#endif /* PG_INSTR_TICKS_TO_NS */
332}
333
334#if PG_INSTR_TSC_CLOCK
335
336#define PG_INSTR_TSC_CLOCK_NAME_FAST "RDTSC"
337#define PG_INSTR_TSC_CLOCK_NAME "RDTSCP"
338
339#ifdef _MSC_VER
340#include <intrin.h>
341#endif /* defined(_MSC_VER) */
342
343/* Helpers to abstract compiler differences for reading the x86 TSC. */
344static inline int64
345pg_rdtsc(void)
346{
347#ifdef _MSC_VER
348 return __rdtsc();
349#else
350 return __builtin_ia32_rdtsc();
351#endif /* defined(_MSC_VER) */
352}
353
354static inline int64
355pg_rdtscp(void)
356{
357 uint32 unused;
358
359#ifdef _MSC_VER
360 return __rdtscp(&unused);
361#else
362 return __builtin_ia32_rdtscp(&unused);
363#endif /* defined(_MSC_VER) */
364}
365
366/*
367 * Marked always_inline due to a shortcoming in gcc's heuristics leading to
368 * only inlining the function partially.
369 * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124795
370 */
372pg_get_ticks(void)
373{
375 {
377
378 now.ticks = pg_rdtscp();
379 return now;
380 }
381
382 return pg_get_ticks_system();
383}
384
387{
389 {
391
392 now.ticks = pg_rdtsc();
393 return now;
394 }
395
396 return pg_get_ticks_system();
397}
398
399#else
400
403{
404 return pg_get_ticks_system();
405}
406
409{
410 return pg_get_ticks_system();
411}
412
413#endif /* PG_INSTR_TSC_CLOCK */
414
415/*
416 * Common macros
417 */
418
419#define INSTR_TIME_IS_ZERO(t) ((t).ticks == 0)
420
421#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)
422
423#define INSTR_TIME_SET_CURRENT_FAST(t) \
424 ((t) = pg_get_ticks_fast())
425
426#define INSTR_TIME_SET_CURRENT(t) \
427 ((t) = pg_get_ticks())
428
429
430#define INSTR_TIME_ADD(x,y) \
431 ((x).ticks += (y).ticks)
432
433#define INSTR_TIME_ADD_NANOSEC(t, n) \
434 ((t).ticks += pg_ns_to_ticks(n))
435
436#define INSTR_TIME_SUBTRACT(x,y) \
437 ((x).ticks -= (y).ticks)
438
439#define INSTR_TIME_ACCUM_DIFF(x,y,z) \
440 ((x).ticks += (y).ticks - (z).ticks)
441
442#define INSTR_TIME_GT(x,y) \
443 ((x).ticks > (y).ticks)
444
445#define INSTR_TIME_GET_NANOSEC(t) \
446 (pg_ticks_to_ns((t).ticks))
447
448#define INSTR_TIME_GET_DOUBLE(t) \
449 ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_S)
450
451#define INSTR_TIME_GET_MILLISEC(t) \
452 ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_MS)
453
454#define INSTR_TIME_GET_MICROSEC(t) \
455 (INSTR_TIME_GET_NANOSEC(t) / NS_PER_US)
456
457#endif /* INSTR_TIME_H */
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1603
#define PGDLLIMPORT
Definition c.h:1421
#define likely(x)
Definition c.h:437
#define Assert(condition)
Definition c.h:943
int64_t int64
Definition c.h:621
#define pg_attribute_always_inline
Definition c.h:305
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
static pg_attribute_always_inline instr_time pg_get_ticks_fast(void)
Definition instr_time.h:408
#define TICKS_TO_NS_SHIFT
Definition instr_time.h:89
PGDLLIMPORT int timing_clock_source
Definition instr_time.c:64
#define PG_INSTR_SYSTEM_CLOCK
Definition instr_time.h:215
PGDLLIMPORT uint64 max_ticks_no_overflow
Definition instr_time.c:62
void pg_initialize_timing(void)
Definition instr_time.c:82
static instr_time pg_get_ticks_system(void)
Definition instr_time.h:220
PGDLLIMPORT bool timing_initialized
Definition instr_time.c:63
#define NS_PER_S
Definition instr_time.h:84
static TimingClockSourceType pg_current_timing_clock_source(void)
Definition instr_time.h:178
PGDLLIMPORT bool timing_tsc_enabled
Definition instr_time.c:66
static int64 pg_ns_to_ticks(int64 ns)
Definition instr_time.h:299
static int64 pg_ticks_to_ns(int64 ticks)
Definition instr_time.h:255
TimingClockSourceType
Definition instr_time.h:124
@ TIMING_CLOCK_SOURCE_SYSTEM
Definition instr_time.h:126
@ TIMING_CLOCK_SOURCE_AUTO
Definition instr_time.h:125
PGDLLIMPORT uint64 ticks_per_ns_scaled
Definition instr_time.c:61
PGDLLIMPORT int32 timing_tsc_frequency_khz
Definition instr_time.c:67
bool pg_set_timing_clock_source(TimingClockSourceType source)
Definition instr_time.c:92
static pg_attribute_always_inline instr_time pg_get_ticks(void)
Definition instr_time.h:402
static rewind_source * source
Definition pg_rewind.c:89
static int fb(int x)
int64 ticks
Definition instr_time.h:78