1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*-------------------------------------------------------------------------
*
* instr_time.h
* portable high-precision interval timing
*
* This file provides an abstraction layer to hide portability issues in
* interval timing. On Unix we use clock_gettime(), and on Windows we use
* QueryPerformanceCounter(). These macros also give some breathing room to
* use other high-precision-timing APIs.
*
* The basic data type is instr_time, which all callers should treat as an
* opaque typedef. instr_time can store either an absolute time (of
* unspecified reference time) or an interval. The operations provided
* for it are:
*
* INSTR_TIME_IS_ZERO(t) is t equal to zero?
*
* INSTR_TIME_SET_ZERO(t) set t to zero (memset is acceptable too)
*
* INSTR_TIME_SET_CURRENT(t) set t to current time
*
* INSTR_TIME_SET_CURRENT_LAZY(t) set t to current time if t is zero,
* evaluates to whether t changed
*
* INSTR_TIME_ADD(x, y) x += y
*
* INSTR_TIME_SUBTRACT(x, y) x -= y
*
* INSTR_TIME_ACCUM_DIFF(x, y, z) x += (y - z)
*
* INSTR_TIME_GET_DOUBLE(t) convert t to double (in seconds)
*
* INSTR_TIME_GET_MILLISEC(t) convert t to double (in milliseconds)
*
* INSTR_TIME_GET_MICROSEC(t) convert t to uint64 (in microseconds)
*
* INSTR_TIME_GET_NANOSEC(t) convert t to uint64 (in nanoseconds)
*
* Note that INSTR_TIME_SUBTRACT and INSTR_TIME_ACCUM_DIFF convert
* absolute times to intervals. The INSTR_TIME_GET_xxx operations are
* only useful on intervals.
*
* When summing multiple measurements, it's recommended to leave the
* running sum in instr_time form (ie, use INSTR_TIME_ADD or
* INSTR_TIME_ACCUM_DIFF) and convert to a result format only at the end.
*
* Beware of multiple evaluations of the macro arguments.
*
*
* Copyright (c) 2001-2024, PostgreSQL Global Development Group
*
* src/include/portability/instr_time.h
*
*-------------------------------------------------------------------------
*/
/*
* We store interval times as an int64 integer on all platforms, as int64 is
* cheap to add/subtract, the most common operation for instr_time. The
* acquisition of time and converting to specific units of time is platform
* specific.
*
* To avoid users of the API relying on the integer representation, we wrap
* the 64bit integer in a struct.
*/
typedef struct instr_time
instr_time;
/* helpers macros used in platform specific code below */
/* Use clock_gettime() */
/*
* The best clockid to use according to the POSIX spec is CLOCK_MONOTONIC,
* since that will give reliable interval timing even in the face of changes
* to the system clock. However, POSIX doesn't require implementations to
* provide anything except CLOCK_REALTIME, so fall back to that if we don't
* find CLOCK_MONOTONIC.
*
* Also, some implementations have nonstandard clockids with better properties
* than CLOCK_MONOTONIC. In particular, as of macOS 10.12, Apple provides
* CLOCK_MONOTONIC_RAW which is both faster to read and higher resolution than
* their version of CLOCK_MONOTONIC.
*/
/* helper for INSTR_TIME_SET_CURRENT */
static inline instr_time
/* Use QueryPerformanceCounter() */
/* helper for INSTR_TIME_SET_CURRENT */
static inline instr_time
static inline double
/* WIN32 */
/*
* Common macros
*/
/* INSTR_TIME_H */