Make clock_gettime() faster on FreeBSD

This commit is contained in:
Justine Tunney 2022-06-11 00:05:06 -07:00
parent 5deda43766
commit 5ea618f0af
15 changed files with 67 additions and 18 deletions

View file

@ -32,7 +32,9 @@
.underrun
kClockNames:
.e CLOCK_REALTIME,"REALTIME"
.e CLOCK_REALTIME_FAST,"REALTIME_FAST"
.e CLOCK_MONOTONIC,"MONOTONIC"
.e CLOCK_MONOTONIC_FAST,"MONOTONIC_FAST"
.e CLOCK_MONOTONIC_RAW,"MONOTONIC_RAW"
.e CLOCK_REALTIME_COARSE,"REALTIME_COARSE"
.e CLOCK_MONOTONIC_COARSE,"MONOTONIC_COARSE"

View file

@ -44,10 +44,10 @@ static long double GetTimeSample(void) {
uint64_t tick1, tick2;
long double time1, time2;
sched_yield();
time1 = dtime(CLOCK_REALTIME);
time1 = dtime(CLOCK_MONOTONIC_FAST);
tick1 = rdtsc();
nanosleep(&(struct timespec){0, 1000000}, NULL);
time2 = dtime(CLOCK_REALTIME);
time2 = dtime(CLOCK_MONOTONIC_FAST);
tick2 = rdtsc();
return (time2 - time1) * 1e9 / MAX(1, tick2 - tick1);
}

View file

@ -40,10 +40,10 @@ static long double GetTimeSample(void) {
uint64_t tick1, tick2;
long double time1, time2;
sched_yield();
time1 = dtime(CLOCK_REALTIME);
time1 = dtime(CLOCK_MONOTONIC);
tick1 = rdtsc();
nanosleep(&(struct timespec){0, 1000000}, NULL);
time2 = dtime(CLOCK_REALTIME);
time2 = dtime(CLOCK_MONOTONIC);
tick2 = rdtsc();
return (time2 - time1) * 1e9 / MAX(1, tick2 - tick1);
}

View file

@ -587,7 +587,9 @@ syscon ss SS_DISABLE 2 4 4 4 4 2 # bsd consensus
#
# group name GNU/Systemd XNU's Not UNIX! FreeBSD OpenBSD NetBSD The New Technology Commentary
syscon clock CLOCK_REALTIME 0 0 0 0 0 0 # consensus
syscon clock CLOCK_REALTIME_FAST 0 0 10 0 0 0 #
syscon clock CLOCK_MONOTONIC 1 1 4 3 3 1 # XNU/NT faked; could move backwards if NTP introduces negative leap second
syscon clock CLOCK_MONOTONIC_FAST 1 1 12 3 3 1 #
syscon clock CLOCK_PROCESS_CPUTIME_ID 2 -1 15 2 0x40000000 -1 #
syscon clock CLOCK_THREAD_CPUTIME_ID 3 -1 14 4 0x20000000 -1 #
syscon clock CLOCK_MONOTONIC_RAW 4 4 0x4000 0x4000 0x4000 4 # actually monotonic; not subject to NTP adjustments; Linux 2.6.28+; XNU/NT/FreeBSD/OpenBSD faked; not available on RHEL5
@ -598,6 +600,12 @@ syscon clock CLOCK_BOOTTIME 7 -1 -1 6 -1 -1 #
syscon clock CLOCK_REALTIME_ALARM 8 -1 -1 -1 -1 -1 #
syscon clock CLOCK_BOOTTIME_ALARM 9 -1 -1 -1 -1 -1 #
syscon clock CLOCK_TAI 11 -1 -1 -1 -1 -1 #
syscon clock CLOCK_UPTIME -1 -1 5 5 -1 -1 #
syscon clock CLOCK_UPTIME_PRECISE -1 -1 7 -1 -1 -1 #
syscon clock CLOCK_UPTIME_FAST -1 -1 8 -1 -1 -1 #
syscon clock CLOCK_REALTIME_PRECISE -1 -1 9 -1 -1 -1 #
syscon clock CLOCK_MONOTONIC_PRECISE -1 -1 11 -1 -1 -1 #
syscon clock CLOCK_SECOND -1 -1 13 -1 -1 -1 #
# poll()
#

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_MONOTONIC_FAST,1,1,12,3,3,1

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_MONOTONIC_PRECISE,-1,-1,11,-1,-1,-1

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_REALTIME_FAST,0,0,10,0,0,0

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_REALTIME_PRECISE,-1,-1,9,-1,-1,-1

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_SECOND,-1,-1,13,-1,-1,-1

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_UPTIME,-1,-1,5,5,-1,-1

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_UPTIME_FAST,-1,-1,8,-1,-1,-1

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon clock,CLOCK_UPTIME_PRECISE,-1,-1,7,-1,-1,-1

View file

@ -8,29 +8,45 @@ extern const long CLOCK_BOOTTIME;
extern const long CLOCK_BOOTTIME_ALARM;
extern const long CLOCK_MONOTONIC;
extern const long CLOCK_MONOTONIC_COARSE;
extern const long CLOCK_MONOTONIC_FAST;
extern const long CLOCK_MONOTONIC_PRECISE;
extern const long CLOCK_MONOTONIC_RAW;
extern const long CLOCK_PROCESS_CPUTIME_ID;
extern const long CLOCK_PROF;
extern const long CLOCK_REALTIME;
extern const long CLOCK_REALTIME_ALARM;
extern const long CLOCK_REALTIME_COARSE;
extern const long CLOCK_REALTIME_FAST;
extern const long CLOCK_REALTIME_PRECISE;
extern const long CLOCK_SECOND;
extern const long CLOCK_TAI;
extern const long CLOCK_THREAD_CPUTIME_ID;
extern const long CLOCK_UPTIME;
extern const long CLOCK_UPTIME_FAST;
extern const long CLOCK_UPTIME_PRECISE;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#define CLOCK_REALTIME LITERALLY(0)
#define CLOCK_BOOTTIME SYMBOLIC(CLOCK_BOOTTIME)
#define CLOCK_BOOTTIME_ALARM SYMBOLIC(CLOCK_BOOTTIME_ALARM)
#define CLOCK_MONOTONIC SYMBOLIC(CLOCK_MONOTONIC)
#define CLOCK_MONOTONIC_COARSE SYMBOLIC(CLOCK_MONOTONIC_COARSE)
#define CLOCK_MONOTONIC_FAST SYMBOLIC(CLOCK_MONOTONIC_FAST)
#define CLOCK_MONOTONIC_PRECISE SYMBOLIC(CLOCK_MONOTONIC_PRECISE)
#define CLOCK_MONOTONIC_RAW SYMBOLIC(CLOCK_MONOTONIC_RAW)
#define CLOCK_PROCESS_CPUTIME_ID SYMBOLIC(CLOCK_PROCESS_CPUTIME_ID)
#define CLOCK_PROF SYMBOLIC(CLOCK_PROF)
#define CLOCK_REALTIME SYMBOLIC(CLOCK_REALTIME)
#define CLOCK_REALTIME_ALARM SYMBOLIC(CLOCK_REALTIME_ALARM)
#define CLOCK_REALTIME_COARSE SYMBOLIC(CLOCK_REALTIME_COARSE)
#define CLOCK_REALTIME_FAST SYMBOLIC(CLOCK_REALTIME_FAST)
#define CLOCK_REALTIME_PRECISE SYMBOLIC(CLOCK_REALTIME_PRECISE)
#define CLOCK_SECOND SYMBOLIC(CLOCK_SECOND)
#define CLOCK_TAI SYMBOLIC(CLOCK_TAI)
#define CLOCK_THREAD_CPUTIME_ID SYMBOLIC(CLOCK_THREAD_CPUTIME_ID)
#define CLOCK_UPTIME SYMBOLIC(CLOCK_UPTIME)
#define CLOCK_UPTIME_FAST SYMBOLIC(CLOCK_UPTIME_FAST)
#define CLOCK_UPTIME_PRECISE SYMBOLIC(CLOCK_UPTIME_PRECISE)
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_CLOCK_H_ */

View file

@ -25,6 +25,7 @@
#include "libc/nexgen32e/rdtsc.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/clock.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/time/time.h"
@ -50,19 +51,23 @@ BENCH(clock_gettime, bench) {
EZBENCH2("nowl", donothing, nowl());
EZBENCH2("rdtsc", donothing, rdtsc());
EZBENCH2("gettimeofday", donothing, gettimeofday(&tv, 0));
EZBENCH2("clock_gettime 0", donothing, clock_gettime(0, &ts));
EZBENCH2("clock_gettime 1", donothing, clock_gettime(1, &ts));
EZBENCH2("clock_gettime 4", donothing, clock_gettime(4, &ts));
EZBENCH2("__clock_gettime 0", donothing, __clock_gettime(0, &ts));
EZBENCH2("__clock_gettime 1", donothing, __clock_gettime(1, &ts));
EZBENCH2("__clock_gettime 4", donothing, __clock_gettime(4, &ts));
EZBENCH2("clock_gettime 0", donothing,
clock_gettime(CLOCK_REALTIME_FAST, &ts));
EZBENCH2("clock_gettime 1", donothing,
clock_gettime(CLOCK_MONOTONIC_FAST, &ts));
EZBENCH2("__clock_gettime 0", donothing,
__clock_gettime(CLOCK_REALTIME_FAST, &ts));
EZBENCH2("__clock_gettime 1", donothing,
__clock_gettime(CLOCK_MONOTONIC_FAST, &ts));
if (IsWindows()) {
EZBENCH2("sys_clock_gettime 0", donothing, sys_clock_gettime_nt(0, &ts));
EZBENCH2("sys_clock_gettime 1", donothing, sys_clock_gettime_nt(1, &ts));
EZBENCH2("sys_clock_gettime 4", donothing, sys_clock_gettime_nt(4, &ts));
EZBENCH2("sys_clock_gettime 0", donothing,
sys_clock_gettime_nt(CLOCK_REALTIME_FAST, &ts));
EZBENCH2("sys_clock_gettime 1", donothing,
sys_clock_gettime_nt(CLOCK_MONOTONIC_FAST, &ts));
} else {
EZBENCH2("sys_clock_gettime 0", donothing, sys_clock_gettime(0, &ts));
EZBENCH2("sys_clock_gettime 1", donothing, sys_clock_gettime(1, &ts));
EZBENCH2("sys_clock_gettime 4", donothing, sys_clock_gettime(4, &ts));
EZBENCH2("sys_clock_gettime 0", donothing,
sys_clock_gettime(CLOCK_REALTIME_FAST, &ts));
EZBENCH2("sys_clock_gettime 1", donothing,
sys_clock_gettime(CLOCK_MONOTONIC_FAST, &ts));
}
}

View file

@ -2407,7 +2407,9 @@ UNIX MODULE
`clock` can be any one of of:
- `CLOCK_REALTIME`: universally supported
- `CLOCK_REALTIME_FAST`: ditto but faster on freebsd
- `CLOCK_MONOTONIC`: universally supported
- `CLOCK_MONOTONIC_FAST`: ditto but faster on freebsd
- `CLOCK_MONOTONIC_RAW`: nearly universally supported
- `CLOCK_PROCESS_CPUTIME_ID`: linux and bsd
- `CLOCK_THREAD_CPUTIME_ID`: linux and bsd