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 439 of file instr_time.h.

◆ INSTR_TIME_ADD

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

Definition at line 430 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 433 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 448 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 454 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 451 of file instr_time.h.

◆ INSTR_TIME_GET_NANOSEC

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

Definition at line 445 of file instr_time.h.

◆ INSTR_TIME_GT

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

Definition at line 442 of file instr_time.h.

◆ INSTR_TIME_IS_ZERO

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

Definition at line 419 of file instr_time.h.

◆ INSTR_TIME_SET_CURRENT

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

Definition at line 426 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 423 of file instr_time.h.

◆ INSTR_TIME_SET_ZERO

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

Definition at line 421 of file instr_time.h.

◆ INSTR_TIME_SUBTRACT

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

Definition at line 436 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 215 of file instr_time.h.

◆ PG_INSTR_SYSTEM_CLOCK_NAME

#define PG_INSTR_SYSTEM_CLOCK_NAME   "clock_gettime (CLOCK_REALTIME)"

Definition at line 216 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 178 of file instr_time.h.

179{
180#if PG_INSTR_TSC_CLOCK
183#endif
185}
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 402 of file instr_time.h.

403{
404 return pg_get_ticks_system();
405}
static instr_time pg_get_ticks_system(void)
Definition instr_time.h:220

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 408 of file instr_time.h.

409{
410 return pg_get_ticks_system();
411}

References pg_get_ticks_system().

◆ pg_get_ticks_system()

static instr_time pg_get_ticks_system ( void  )
inlinestatic

Definition at line 220 of file instr_time.h.

221{
223 struct timespec tmp;
224
226
228 now.ticks = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
229
230 return now;
231}
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1603
#define Assert(condition)
Definition c.h:943
#define PG_INSTR_SYSTEM_CLOCK
Definition instr_time.h:215
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 82 of file instr_time.c.

83{
85 return;
86
88 timing_initialized = true;
89}
static void set_ticks_per_ns_system(void)
Definition instr_time.c:137
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 299 of file instr_time.h.

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}
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 92 of file instr_time.c.

93{
95
96#if PG_INSTR_TSC_CLOCK
98
99 switch (source)
100 {
103 break;
105 timing_tsc_enabled = false;
106 break;
108 /* Tell caller TSC is not usable */
110 return false;
111 timing_tsc_enabled = true;
112 break;
113 }
114#endif
115
118 return true;
119}
int timing_clock_source
Definition instr_time.c:64
static void set_ticks_per_ns(void)
Definition instr_time.c:122
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 255 of file instr_time.h.

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}
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().