Skip to content

Commit 1d63f7d

Browse files
committed
Use clock_gettime(), if available, in instr_time measurements.
The advantage of clock_gettime() is that the API allows the result to be precise to nanoseconds, not just microseconds as in gettimeofday(). Now that it's routinely possible to do tens of plan node executions in 1us, we really need more precision than gettimeofday() can offer for EXPLAIN ANALYZE to accumulate statistics with. Some research shows that clock_gettime() is available on pretty nearly every modern Unix-ish platform, and as far as I have been able to test, it has about the same execution time as gettimeofday(), so there's no loss in switching over. (By the same token, this doesn't do anything to fix the fact that we really wish clock readings were faster. But there's enough win here to justify changing anyway.) A small side benefit is that on most platforms, we can use CLOCK_MONOTONIC instead of CLOCK_REALTIME and thereby render EXPLAIN impervious to concurrent resets of the system clock. (This means that code must not assume that the contents of struct instr_time have any well-defined interpretation as timestamps, but really that was true before.) Some platforms offer nonstandard clock IDs that might be of interest. This patch knows we should use CLOCK_MONOTONIC_RAW on macOS, because it provides more precision and is faster to read than their CLOCK_MONOTONIC. If there turn out to be many more cases where we need special rules, it might be appropriate to handle the selection of clock ID in configure, but for the moment that doesn't seem worth the trouble. Discussion: https://postgr.es/m/31856.1400021891@sss.pgh.pa.us
1 parent 67a8753 commit 1d63f7d

File tree

5 files changed

+163
-6
lines changed

5 files changed

+163
-6
lines changed

configure

+57-1
Original file line numberDiff line numberDiff line change
@@ -9055,6 +9055,62 @@ if test "$ac_res" != no; then :
90559055

90569056
fi
90579057

9058+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing clock_gettime" >&5
9059+
$as_echo_n "checking for library containing clock_gettime... " >&6; }
9060+
if ${ac_cv_search_clock_gettime+:} false; then :
9061+
$as_echo_n "(cached) " >&6
9062+
else
9063+
ac_func_search_save_LIBS=$LIBS
9064+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
9065+
/* end confdefs.h. */
9066+
9067+
/* Override any GCC internal prototype to avoid an error.
9068+
Use char because int might match the return type of a GCC
9069+
builtin and then its argument prototype would still apply. */
9070+
#ifdef __cplusplus
9071+
extern "C"
9072+
#endif
9073+
char clock_gettime ();
9074+
int
9075+
main ()
9076+
{
9077+
return clock_gettime ();
9078+
;
9079+
return 0;
9080+
}
9081+
_ACEOF
9082+
for ac_lib in '' rt posix4; do
9083+
if test -z "$ac_lib"; then
9084+
ac_res="none required"
9085+
else
9086+
ac_res=-l$ac_lib
9087+
LIBS="-l$ac_lib $ac_func_search_save_LIBS"
9088+
fi
9089+
if ac_fn_c_try_link "$LINENO"; then :
9090+
ac_cv_search_clock_gettime=$ac_res
9091+
fi
9092+
rm -f core conftest.err conftest.$ac_objext \
9093+
conftest$ac_exeext
9094+
if ${ac_cv_search_clock_gettime+:} false; then :
9095+
break
9096+
fi
9097+
done
9098+
if ${ac_cv_search_clock_gettime+:} false; then :
9099+
9100+
else
9101+
ac_cv_search_clock_gettime=no
9102+
fi
9103+
rm conftest.$ac_ext
9104+
LIBS=$ac_func_search_save_LIBS
9105+
fi
9106+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_clock_gettime" >&5
9107+
$as_echo "$ac_cv_search_clock_gettime" >&6; }
9108+
ac_res=$ac_cv_search_clock_gettime
9109+
if test "$ac_res" != no; then :
9110+
test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
9111+
9112+
fi
9113+
90589114
# Solaris:
90599115
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing fdatasync" >&5
90609116
$as_echo_n "checking for library containing fdatasync... " >&6; }
@@ -12520,7 +12576,7 @@ fi
1252012576
LIBS_including_readline="$LIBS"
1252112577
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1252212578

12523-
for ac_func in cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat pthread_is_threaded_np readlink setproctitle setsid shm_open symlink sync_file_range towlower utime utimes wcstombs wcstombs_l
12579+
for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat pthread_is_threaded_np readlink setproctitle setsid shm_open symlink sync_file_range towlower utime utimes wcstombs wcstombs_l
1252412580
do :
1252512581
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1252612582
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"

configure.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ AC_SEARCH_LIBS(getopt_long, [getopt gnugetopt])
10161016
AC_SEARCH_LIBS(crypt, crypt)
10171017
AC_SEARCH_LIBS(shm_open, rt)
10181018
AC_SEARCH_LIBS(shm_unlink, rt)
1019+
AC_SEARCH_LIBS(clock_gettime, [rt posix4])
10191020
# Solaris:
10201021
AC_SEARCH_LIBS(fdatasync, [rt posix4])
10211022
# Required for thread_test.c on Solaris
@@ -1415,7 +1416,7 @@ PGAC_FUNC_WCSTOMBS_L
14151416
LIBS_including_readline="$LIBS"
14161417
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
14171418

1418-
AC_CHECK_FUNCS([cbrt dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat pthread_is_threaded_np readlink setproctitle setsid shm_open symlink sync_file_range towlower utime utimes wcstombs wcstombs_l])
1419+
AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll pstat pthread_is_threaded_np readlink setproctitle setsid shm_open symlink sync_file_range towlower utime utimes wcstombs wcstombs_l])
14191420

14201421
AC_REPLACE_FUNCS(fseeko)
14211422
case $host_os in

src/include/pg_config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
/* Define to 1 if you have the `class' function. */
106106
#undef HAVE_CLASS
107107

108+
/* Define to 1 if you have the `clock_gettime' function. */
109+
#undef HAVE_CLOCK_GETTIME
110+
108111
/* Define to 1 if you have the <crtdefs.h> header file. */
109112
#undef HAVE_CRTDEFS_H
110113

src/include/pg_config.h.win32

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
/* Define to 1 if you have the `class' function. */
7676
/* #undef HAVE_CLASS */
7777

78+
/* Define to 1 if you have the `clock_gettime' function. */
79+
/* #undef HAVE_CLOCK_GETTIME */
80+
7881
/* Define to 1 if you have the `crypt' function. */
7982
/* #undef HAVE_CRYPT */
8083

src/include/portability/instr_time.h

+98-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* portable high-precision interval timing
55
*
66
* 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.
1111
*
1212
* The basic data type is instr_time, which all callers should treat as an
1313
* opaque typedef. instr_time can store either an absolute time (of
@@ -54,6 +54,94 @@
5454

5555
#ifndef WIN32
5656

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+
57145
#include <sys/time.h>
58146

59147
typedef struct timeval instr_time;
@@ -113,8 +201,13 @@ typedef struct timeval instr_time;
113201

114202
#define INSTR_TIME_GET_MICROSEC(t) \
115203
(((uint64) (t).tv_sec * (uint64) 1000000) + (uint64) (t).tv_usec)
204+
205+
#endif /* HAVE_CLOCK_GETTIME */
206+
116207
#else /* WIN32 */
117208

209+
/* Use QueryPerformanceCounter() */
210+
118211
typedef LARGE_INTEGER instr_time;
119212

120213
#define INSTR_TIME_IS_ZERO(t) ((t).QuadPart == 0)
@@ -149,6 +242,7 @@ GetTimerFrequency(void)
149242
QueryPerformanceFrequency(&f);
150243
return (double) f.QuadPart;
151244
}
245+
152246
#endif /* WIN32 */
153247

154248
#endif /* INSTR_TIME_H */

0 commit comments

Comments
 (0)