Skip to content

Commit 4eec6b9

Browse files
grooverdansysprg
authored andcommitted
MDEV-23175: my_timer_milliseconds clock_gettime for multiple platfomrs
Small postfix to MDEV-23175 to ensure faster option on FreeBSD and compatibility to Solaris that isn't high resolution. ftime is left as a backup in case an implementation doesn't contain any of these clocks. FreeBSD $ ./unittest/mysys/my_rdtsc-t 1..11 # ----- Routine --------------- # myt.cycles.routine : 5 # myt.nanoseconds.routine : 11 # myt.microseconds.routine : 13 # myt.milliseconds.routine : 11 # myt.ticks.routine : 17 # ----- Frequency ------------- # myt.cycles.frequency : 3610295566 # myt.nanoseconds.frequency : 1000000000 # myt.microseconds.frequency : 1000000 # myt.milliseconds.frequency : 899 # myt.ticks.frequency : 136 # ----- Resolution ------------ # myt.cycles.resolution : 1 # myt.nanoseconds.resolution : 1 # myt.microseconds.resolution : 1 # myt.milliseconds.resolution : 7 # myt.ticks.resolution : 1 # ----- Overhead -------------- # myt.cycles.overhead : 26 # myt.nanoseconds.overhead : 19140 # myt.microseconds.overhead : 19036 # myt.milliseconds.overhead : 578 # myt.ticks.overhead : 21544 ok 1 - my_timer_init() did not crash ok 2 - The cycle timer is strictly increasing ok 3 - The cycle timer is implemented ok 4 - The nanosecond timer is increasing ok 5 - The nanosecond timer is implemented ok 6 - The microsecond timer is increasing ok 7 - The microsecond timer is implemented ok 8 - The millisecond timer is increasing ok 9 - The millisecond timer is implemented ok 10 - The tick timer is increasing ok 11 - The tick timer is implemented
1 parent 61a66d8 commit 4eec6b9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

mysys/my_rdtsc.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,29 @@ ulonglong my_timer_microseconds(void)
173173
milliseconds.
174174
*/
175175

176+
#if defined(HAVE_CLOCK_GETTIME)
177+
#if defined(CLOCK_MONOTONIC_FAST)
178+
/* FreeBSD */
179+
#define MY_CLOCK_ID CLOCK_MONOTONIC_FAST
180+
#elif defined(CLOCK_MONOTONIC_COARSE)
181+
/* Linux */
182+
#define MY_CLOCK_ID CLOCK_MONOTONIC_COARSE
183+
#elif defined(CLOCK_MONOTONIC)
184+
/* POSIX (includes OSX) */
185+
#define MY_CLOCK_ID CLOCK_MONOTONIC
186+
#elif defined(CLOCK_REALTIME)
187+
/* Solaris (which doesn't seem to have MONOTONIC) */
188+
#define MY_CLOCK_ID CLOCK_REALTIME
189+
#endif
190+
#endif
191+
176192
ulonglong my_timer_milliseconds(void)
177193
{
178-
#if defined(HAVE_SYS_TIMEB_H) && defined(HAVE_FTIME)
194+
#if defined(MY_CLOCK_ID)
195+
struct timespec tp;
196+
clock_gettime(MY_CLOCK_ID, &tp);
197+
return (ulonglong)tp.tv_sec * 1000 + (ulonglong)tp.tv_nsec / 1000000;
198+
#elif defined(HAVE_SYS_TIMEB_H) && defined(HAVE_FTIME)
179199
/* ftime() is obsolete but maybe the platform is old */
180200
struct timeb ft;
181201
ftime(&ft);
@@ -428,7 +448,9 @@ void my_timer_init(MY_TIMER_INFO *mti)
428448

429449
/* milliseconds */
430450
mti->milliseconds.frequency= 1000; /* initial assumption */
431-
#if defined(HAVE_SYS_TIMEB_H) && defined(HAVE_FTIME)
451+
#ifdef MY_CLOCK_ID
452+
mti->milliseconds.routine= MY_TIMER_ROUTINE_CLOCK_GETTIME;
453+
#elif defined(HAVE_SYS_TIMEB_H) && defined(HAVE_FTIME)
432454
mti->milliseconds.routine= MY_TIMER_ROUTINE_FTIME;
433455
#elif defined(_WIN32)
434456
mti->milliseconds.routine= MY_TIMER_ROUTINE_GETSYSTEMTIMEASFILETIME;

0 commit comments

Comments
 (0)