|
4 | 4 | * portable high-precision interval timing
|
5 | 5 | *
|
6 | 6 | * This file provides an abstraction layer to hide portability issues in
|
7 |
| - * interval timing. On Unix we use gettimeofday(), but on Windows that |
8 |
| - * gives a low-precision result so we must use QueryPerformanceCounter() |
9 |
| - * instead. These macros also give some breathing room to use other |
10 |
| - * high-precision-timing APIs on yet other platforms. |
| 7 | + * interval timing. On Unix we use clock_gettime() if available, else |
| 8 | + * gettimeofday(). On Windows, gettimeofday() gives a low-precision result |
| 9 | + * so we must use QueryPerformanceCounter() instead. These macros also give |
| 10 | + * some breathing room to use other high-precision-timing APIs. |
11 | 11 | *
|
12 | 12 | * The basic data type is instr_time, which all callers should treat as an
|
13 | 13 | * opaque typedef. instr_time can store either an absolute time (of
|
|
54 | 54 |
|
55 | 55 | #ifndef WIN32
|
56 | 56 |
|
| 57 | +#ifdef HAVE_CLOCK_GETTIME |
| 58 | + |
| 59 | +/* Use clock_gettime() */ |
| 60 | + |
| 61 | +#include <time.h> |
| 62 | + |
| 63 | +/* |
| 64 | + * The best clockid to use according to the POSIX spec is CLOCK_MONOTONIC, |
| 65 | + * since that will give reliable interval timing even in the face of changes |
| 66 | + * to the system clock. However, POSIX doesn't require implementations to |
| 67 | + * provide anything except CLOCK_REALTIME, so fall back to that if we don't |
| 68 | + * find CLOCK_MONOTONIC. |
| 69 | + * |
| 70 | + * Also, some implementations have nonstandard clockids with better properties |
| 71 | + * than CLOCK_MONOTONIC. In particular, as of macOS 10.12, Apple provides |
| 72 | + * CLOCK_MONOTONIC_RAW which is both faster to read and higher resolution than |
| 73 | + * their version of CLOCK_MONOTONIC. |
| 74 | + */ |
| 75 | +#if defined(__darwin__) && defined(CLOCK_MONOTONIC_RAW) |
| 76 | +#define PG_INSTR_CLOCK CLOCK_MONOTONIC_RAW |
| 77 | +#elif defined(CLOCK_MONOTONIC) |
| 78 | +#define PG_INSTR_CLOCK CLOCK_MONOTONIC |
| 79 | +#else |
| 80 | +#define PG_INSTR_CLOCK CLOCK_REALTIME |
| 81 | +#endif |
| 82 | + |
| 83 | +typedef struct timespec instr_time; |
| 84 | + |
| 85 | +#define INSTR_TIME_IS_ZERO(t) ((t).tv_nsec == 0 && (t).tv_sec == 0) |
| 86 | + |
| 87 | +#define INSTR_TIME_SET_ZERO(t) ((t).tv_sec = 0, (t).tv_nsec = 0) |
| 88 | + |
| 89 | +#define INSTR_TIME_SET_CURRENT(t) ((void) clock_gettime(PG_INSTR_CLOCK, &(t))) |
| 90 | + |
| 91 | +#define INSTR_TIME_ADD(x,y) \ |
| 92 | + do { \ |
| 93 | + (x).tv_sec += (y).tv_sec; \ |
| 94 | + (x).tv_nsec += (y).tv_nsec; \ |
| 95 | + /* Normalize */ \ |
| 96 | + while ((x).tv_nsec >= 1000000000) \ |
| 97 | + { \ |
| 98 | + (x).tv_nsec -= 1000000000; \ |
| 99 | + (x).tv_sec++; \ |
| 100 | + } \ |
| 101 | + } while (0) |
| 102 | + |
| 103 | +#define INSTR_TIME_SUBTRACT(x,y) \ |
| 104 | + do { \ |
| 105 | + (x).tv_sec -= (y).tv_sec; \ |
| 106 | + (x).tv_nsec -= (y).tv_nsec; \ |
| 107 | + /* Normalize */ \ |
| 108 | + while ((x).tv_nsec < 0) \ |
| 109 | + { \ |
| 110 | + (x).tv_nsec += 1000000000; \ |
| 111 | + (x).tv_sec--; \ |
| 112 | + } \ |
| 113 | + } while (0) |
| 114 | + |
| 115 | +#define INSTR_TIME_ACCUM_DIFF(x,y,z) \ |
| 116 | + do { \ |
| 117 | + (x).tv_sec += (y).tv_sec - (z).tv_sec; \ |
| 118 | + (x).tv_nsec += (y).tv_nsec - (z).tv_nsec; \ |
| 119 | + /* Normalize after each add to avoid overflow/underflow of tv_nsec */ \ |
| 120 | + while ((x).tv_nsec < 0) \ |
| 121 | + { \ |
| 122 | + (x).tv_nsec += 1000000000; \ |
| 123 | + (x).tv_sec--; \ |
| 124 | + } \ |
| 125 | + while ((x).tv_nsec >= 1000000000) \ |
| 126 | + { \ |
| 127 | + (x).tv_nsec -= 1000000000; \ |
| 128 | + (x).tv_sec++; \ |
| 129 | + } \ |
| 130 | + } while (0) |
| 131 | + |
| 132 | +#define INSTR_TIME_GET_DOUBLE(t) \ |
| 133 | + (((double) (t).tv_sec) + ((double) (t).tv_nsec) / 1000000000.0) |
| 134 | + |
| 135 | +#define INSTR_TIME_GET_MILLISEC(t) \ |
| 136 | + (((double) (t).tv_sec * 1000.0) + ((double) (t).tv_nsec) / 1000000.0) |
| 137 | + |
| 138 | +#define INSTR_TIME_GET_MICROSEC(t) \ |
| 139 | + (((uint64) (t).tv_sec * (uint64) 1000000) + (uint64) ((t).tv_nsec / 1000)) |
| 140 | + |
| 141 | +#else /* !HAVE_CLOCK_GETTIME */ |
| 142 | + |
| 143 | +/* Use gettimeofday() */ |
| 144 | + |
57 | 145 | #include <sys/time.h>
|
58 | 146 |
|
59 | 147 | typedef struct timeval instr_time;
|
@@ -113,8 +201,13 @@ typedef struct timeval instr_time;
|
113 | 201 |
|
114 | 202 | #define INSTR_TIME_GET_MICROSEC(t) \
|
115 | 203 | (((uint64) (t).tv_sec * (uint64) 1000000) + (uint64) (t).tv_usec)
|
| 204 | + |
| 205 | +#endif /* HAVE_CLOCK_GETTIME */ |
| 206 | + |
116 | 207 | #else /* WIN32 */
|
117 | 208 |
|
| 209 | +/* Use QueryPerformanceCounter() */ |
| 210 | + |
118 | 211 | typedef LARGE_INTEGER instr_time;
|
119 | 212 |
|
120 | 213 | #define INSTR_TIME_IS_ZERO(t) ((t).QuadPart == 0)
|
@@ -149,6 +242,7 @@ GetTimerFrequency(void)
|
149 | 242 | QueryPerformanceFrequency(&f);
|
150 | 243 | return (double) f.QuadPart;
|
151 | 244 | }
|
| 245 | + |
152 | 246 | #endif /* WIN32 */
|
153 | 247 |
|
154 | 248 | #endif /* INSTR_TIME_H */
|
0 commit comments