PostgreSQL Source Code git master
Loading...
Searching...
No Matches
instr_time.h File Reference
#include <time.h>
Include dependency graph for instr_time.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  instr_time
 

Macros

#define NS_PER_S   INT64CONST(1000000000)
 
#define NS_PER_MS   INT64CONST(1000000)
 
#define NS_PER_US   INT64CONST(1000)
 
#define TICKS_TO_NS_SHIFT   14
 
#define PG_INSTR_TICKS_TO_NS   0
 
#define PG_INSTR_TSC_CLOCK   0
 
#define PG_INSTR_SYSTEM_CLOCK   CLOCK_REALTIME
 
#define PG_INSTR_SYSTEM_CLOCK_NAME   "clock_gettime (CLOCK_REALTIME)"
 
#define INSTR_TIME_IS_ZERO(t)   ((t).ticks == 0)
 
#define INSTR_TIME_SET_ZERO(t)   ((t).ticks = 0)
 
#define INSTR_TIME_SET_CURRENT_FAST(t)    ((t) = pg_get_ticks_fast())
 
#define INSTR_TIME_SET_CURRENT(t)    ((t) = pg_get_ticks())
 
#define INSTR_TIME_ADD(x, y)    ((x).ticks += (y).ticks)
 
#define INSTR_TIME_ADD_NANOSEC(t, n)    ((t).ticks += pg_ns_to_ticks(n))
 
#define INSTR_TIME_SUBTRACT(x, y)    ((x).ticks -= (y).ticks)
 
#define INSTR_TIME_ACCUM_DIFF(x, y, z)    ((x).ticks += (y).ticks - (z).ticks)
 
#define INSTR_TIME_GT(x, y)    ((x).ticks > (y).ticks)
 
#define INSTR_TIME_GET_NANOSEC(t)    (pg_ticks_to_ns((t).ticks))
 
#define INSTR_TIME_GET_DOUBLE(t)    ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_S)
 
#define INSTR_TIME_GET_MILLISEC(t)    ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_MS)
 
#define INSTR_TIME_GET_MICROSEC(t)    (INSTR_TIME_GET_NANOSEC(t) / NS_PER_US)
 

Typedefs

typedef struct instr_time instr_time
 

Enumerations

enum  TimingClockSourceType { TIMING_CLOCK_SOURCE_AUTO , TIMING_CLOCK_SOURCE_SYSTEM }
 

Functions

void pg_initialize_timing (void)
 
bool pg_set_timing_clock_source (TimingClockSourceType source)
 
static TimingClockSourceType pg_current_timing_clock_source (void)
 
static instr_time pg_get_ticks_system (void)
 
static int64 pg_ticks_to_ns (int64 ticks)
 
static int64 pg_ns_to_ticks (int64 ns)
 
static pg_attribute_always_inline instr_time pg_get_ticks (void)
 
static pg_attribute_always_inline instr_time pg_get_ticks_fast (void)
 

Variables

PGDLLIMPORT uint64 ticks_per_ns_scaled
 
PGDLLIMPORT uint64 max_ticks_no_overflow
 
PGDLLIMPORT bool timing_initialized
 
PGDLLIMPORT int timing_clock_source
 
PGDLLIMPORT bool timing_tsc_enabled
 
PGDLLIMPORT int32 timing_tsc_frequency_khz
 

Macro Definition Documentation

◆ INSTR_TIME_ACCUM_DIFF

#define INSTR_TIME_ACCUM_DIFF (   x,
  y,
 
)     ((x).ticks += (y).ticks - (z).ticks)

Definition at line 447 of file instr_time.h.

◆ INSTR_TIME_ADD

#define INSTR_TIME_ADD (   x,
  y 
)     ((x).ticks += (y).ticks)

Definition at line 438 of file instr_time.h.

◆ INSTR_TIME_ADD_NANOSEC

#define INSTR_TIME_ADD_NANOSEC (   t,
 
)     ((t).ticks += pg_ns_to_ticks(n))

Definition at line 441 of file instr_time.h.

◆ INSTR_TIME_GET_DOUBLE

#define INSTR_TIME_GET_DOUBLE (   t)     ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_S)

Definition at line 456 of file instr_time.h.

◆ INSTR_TIME_GET_MICROSEC

#define INSTR_TIME_GET_MICROSEC (   t)     (INSTR_TIME_GET_NANOSEC(t) / NS_PER_US)

Definition at line 462 of file instr_time.h.

◆ INSTR_TIME_GET_MILLISEC

#define INSTR_TIME_GET_MILLISEC (   t)     ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_MS)

Definition at line 459 of file instr_time.h.

◆ INSTR_TIME_GET_NANOSEC

#define INSTR_TIME_GET_NANOSEC (   t)     (pg_ticks_to_ns((t).ticks))

Definition at line 453 of file instr_time.h.

◆ INSTR_TIME_GT

#define INSTR_TIME_GT (   x,
  y 
)     ((x).ticks > (y).ticks)

Definition at line 450 of file instr_time.h.

◆ INSTR_TIME_IS_ZERO

#define INSTR_TIME_IS_ZERO (   t)    ((t).ticks == 0)

Definition at line 427 of file instr_time.h.

◆ INSTR_TIME_SET_CURRENT

#define INSTR_TIME_SET_CURRENT (   t)     ((t) = pg_get_ticks())

Definition at line 434 of file instr_time.h.

◆ INSTR_TIME_SET_CURRENT_FAST

#define INSTR_TIME_SET_CURRENT_FAST (   t)     ((t) = pg_get_ticks_fast())

Definition at line 431 of file instr_time.h.

◆ INSTR_TIME_SET_ZERO

#define INSTR_TIME_SET_ZERO (   t)    ((t).ticks = 0)

Definition at line 429 of file instr_time.h.

◆ INSTR_TIME_SUBTRACT

#define INSTR_TIME_SUBTRACT (   x,
  y 
)     ((x).ticks -= (y).ticks)

Definition at line 444 of file instr_time.h.

◆ NS_PER_MS

#define NS_PER_MS   INT64CONST(1000000)

Definition at line 85 of file instr_time.h.

◆ NS_PER_S

#define NS_PER_S   INT64CONST(1000000000)

Definition at line 84 of file instr_time.h.

◆ NS_PER_US

#define NS_PER_US   INT64CONST(1000)

Definition at line 86 of file instr_time.h.

◆ PG_INSTR_SYSTEM_CLOCK

#define PG_INSTR_SYSTEM_CLOCK   CLOCK_REALTIME

Definition at line 223 of file instr_time.h.

◆ PG_INSTR_SYSTEM_CLOCK_NAME

#define PG_INSTR_SYSTEM_CLOCK_NAME   "clock_gettime (CLOCK_REALTIME)"

Definition at line 224 of file instr_time.h.

◆ PG_INSTR_TICKS_TO_NS

#define PG_INSTR_TICKS_TO_NS   0

Definition at line 105 of file instr_time.h.

◆ PG_INSTR_TSC_CLOCK

#define PG_INSTR_TSC_CLOCK   0

Definition at line 106 of file instr_time.h.

◆ TICKS_TO_NS_SHIFT

#define TICKS_TO_NS_SHIFT   14

Definition at line 89 of file instr_time.h.

Typedef Documentation

◆ instr_time

Enumeration Type Documentation

◆ TimingClockSourceType

Enumerator
TIMING_CLOCK_SOURCE_AUTO 
TIMING_CLOCK_SOURCE_SYSTEM 

Definition at line 123 of file instr_time.h.

124{
127#if PG_INSTR_TSC_CLOCK
129#endif
TimingClockSourceType
Definition instr_time.h:124
@ TIMING_CLOCK_SOURCE_SYSTEM
Definition instr_time.h:126
@ TIMING_CLOCK_SOURCE_AUTO
Definition instr_time.h:125
static int fb(int x)

Function Documentation

◆ pg_current_timing_clock_source()

static TimingClockSourceType pg_current_timing_clock_source ( void  )
inlinestatic

Definition at line 186 of file instr_time.h.

187{
188#if PG_INSTR_TSC_CLOCK
191#endif
193}
PGDLLIMPORT bool timing_tsc_enabled
Definition instr_time.c:66

References fb(), TIMING_CLOCK_SOURCE_SYSTEM, and timing_tsc_enabled.

Referenced by show_timing_clock_source(), and test_timing().

◆ pg_get_ticks()

static pg_attribute_always_inline instr_time pg_get_ticks ( void  )
static

Definition at line 410 of file instr_time.h.

411{
412 return pg_get_ticks_system();
413}
static instr_time pg_get_ticks_system(void)
Definition instr_time.h:228

References pg_get_ticks_system().

◆ pg_get_ticks_fast()

static pg_attribute_always_inline instr_time pg_get_ticks_fast ( void  )
static

Definition at line 416 of file instr_time.h.

417{
418 return pg_get_ticks_system();
419}

References pg_get_ticks_system().

◆ pg_get_ticks_system()

static instr_time pg_get_ticks_system ( void  )
inlinestatic

Definition at line 228 of file instr_time.h.

229{
231 struct timespec tmp;
232
234
236 now.ticks = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
237
238 return now;
239}
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1613
#define Assert(condition)
Definition c.h:943
#define PG_INSTR_SYSTEM_CLOCK
Definition instr_time.h:223
PGDLLIMPORT bool timing_initialized
Definition instr_time.c:63
#define NS_PER_S
Definition instr_time.h:84

References Assert, fb(), now(), NS_PER_S, PG_INSTR_SYSTEM_CLOCK, and timing_initialized.

Referenced by pg_get_ticks(), and pg_get_ticks_fast().

◆ pg_initialize_timing()

void pg_initialize_timing ( void  )
extern

Definition at line 84 of file instr_time.c.

85{
87 return;
88
90 timing_initialized = true;
91}
static void set_ticks_per_ns_system(void)
Definition instr_time.c:139
bool timing_initialized
Definition instr_time.c:63

References set_ticks_per_ns_system(), and timing_initialized.

Referenced by InitProcessGlobals(), main(), main(), and regression_main().

◆ pg_ns_to_ticks()

static int64 pg_ns_to_ticks ( int64  ns)
inlinestatic

Definition at line 307 of file instr_time.h.

308{
309#if PG_INSTR_TICKS_TO_NS
310 int64 ticks = 0;
311
313
314 /*
315 * If ticks_per_ns_scaled is zero, ticks are already in nanoseconds (e.g.
316 * system clock on Unix).
317 */
318 if (ticks_per_ns_scaled == 0)
319 return ns;
320
321 /*
322 * The reverse of pg_ticks_to_ns to avoid a similar overflow problem.
323 */
324 if (unlikely(ns > (INT64_MAX >> TICKS_TO_NS_SHIFT)))
325 {
326 int64 count = ns / ticks_per_ns_scaled;
327
328 ticks = count << TICKS_TO_NS_SHIFT;
329 ns -= count * ticks_per_ns_scaled;
330 }
331
332 ticks += (ns << TICKS_TO_NS_SHIFT) / ticks_per_ns_scaled;
333
334 return ticks;
335#else
337
338 return ns;
339#endif /* PG_INSTR_TICKS_TO_NS */
340}
int64_t int64
Definition c.h:621
#define unlikely(x)
Definition c.h:438
#define TICKS_TO_NS_SHIFT
Definition instr_time.h:89
PGDLLIMPORT uint64 ticks_per_ns_scaled
Definition instr_time.c:61

References Assert, fb(), ticks_per_ns_scaled, TICKS_TO_NS_SHIFT, timing_initialized, and unlikely.

◆ pg_set_timing_clock_source()

bool pg_set_timing_clock_source ( TimingClockSourceType  source)
extern

Definition at line 94 of file instr_time.c.

95{
97
98#if PG_INSTR_TSC_CLOCK
100
101 switch (source)
102 {
105 break;
107 timing_tsc_enabled = false;
108 break;
110 /* Tell caller TSC is not usable */
112 return false;
113 timing_tsc_enabled = true;
114 break;
115 }
116#endif
117
120 return true;
121}
int timing_clock_source
Definition instr_time.c:64
static void set_ticks_per_ns(void)
Definition instr_time.c:124
int32 timing_tsc_frequency_khz
Definition instr_time.c:67
bool timing_tsc_enabled
Definition instr_time.c:66
static rewind_source * source
Definition pg_rewind.c:89

References Assert, fb(), set_ticks_per_ns(), source, timing_clock_source, TIMING_CLOCK_SOURCE_AUTO, TIMING_CLOCK_SOURCE_SYSTEM, timing_initialized, timing_tsc_enabled, and timing_tsc_frequency_khz.

Referenced by assign_timing_clock_source(), and test_timing().

◆ pg_ticks_to_ns()

static int64 pg_ticks_to_ns ( int64  ticks)
inlinestatic

Definition at line 263 of file instr_time.h.

264{
265#if PG_INSTR_TICKS_TO_NS
266 int64 ns = 0;
267
269
270 /*
271 * Avoid doing work if we don't use scaled ticks, e.g. system clock on
272 * Unix (in that case ticks is counted in nanoseconds)
273 */
274 if (ticks_per_ns_scaled == 0)
275 return ticks;
276
277 /*
278 * Would multiplication overflow? If so perform computation in two parts.
279 */
280 if (unlikely(ticks > (int64) max_ticks_no_overflow))
281 {
282 /*
283 * To avoid overflow, first scale total ticks down by the fixed
284 * factor, and *afterwards* multiply them by the frequency-based scale
285 * factor.
286 *
287 * The remaining ticks can follow the regular formula, since they
288 * won't overflow.
289 */
290 int64 count = ticks >> TICKS_TO_NS_SHIFT;
291
292 ns = count * ticks_per_ns_scaled;
293 ticks -= (count << TICKS_TO_NS_SHIFT);
294 }
295
296 ns += (ticks * ticks_per_ns_scaled) >> TICKS_TO_NS_SHIFT;
297
298 return ns;
299#else
301
302 return ticks;
303#endif /* PG_INSTR_TICKS_TO_NS */
304}
PGDLLIMPORT uint64 max_ticks_no_overflow
Definition instr_time.c:62

References Assert, max_ticks_no_overflow, ticks_per_ns_scaled, TICKS_TO_NS_SHIFT, timing_initialized, and unlikely.

Variable Documentation

◆ max_ticks_no_overflow

PGDLLIMPORT uint64 max_ticks_no_overflow
extern

Definition at line 62 of file instr_time.c.

Referenced by pg_ticks_to_ns(), and set_ticks_per_ns_system().

◆ ticks_per_ns_scaled

PGDLLIMPORT uint64 ticks_per_ns_scaled
extern

◆ timing_clock_source

PGDLLIMPORT int timing_clock_source
extern

Definition at line 64 of file instr_time.c.

Referenced by pg_set_timing_clock_source(), and show_timing_clock_source().

◆ timing_initialized

◆ timing_tsc_enabled

PGDLLIMPORT bool timing_tsc_enabled
extern

◆ timing_tsc_frequency_khz

PGDLLIMPORT int32 timing_tsc_frequency_khz
extern

Definition at line 67 of file instr_time.c.

Referenced by check_timing_clock_source(), and pg_set_timing_clock_source().