bionic: add vdso clock_getres
clock_getres() should not be a hot call, nevertheless it is
~6-7 times faster for supported clock ids if it uses
__vdso_clock_getres if available. There is a 3% performance
penalty for unsupported clock ids via __vdso_clock_getres with
respect to a direct syscall.
[TL;DR]
w/vdso32 kernel patches, locked cores to MAX, little cores only.
BEFORE:
hikey960 vdso (aarch64):
----------------------------------------------------------------------
Benchmark Time CPU Iterations
----------------------------------------------------------------------
BM_time_clock_getres 126 ns 126 ns 5577874
BM_time_clock_getres_syscall 127 ns 127 ns 5505016
BM_time_clock_getres_REALTIME 126 ns 126 ns 5574682
BM_time_clock_getres_BOOTTIME 126 ns 126 ns 5575237
BM_time_clock_getres_TAI 126 ns 126 ns 5576810
BM_time_clock_getres_unsupported 128 ns 128 ns 5480189
hikey960 vdso32 (aarch32):
----------------------------------------------------------------------
Benchmark Time CPU Iterations
----------------------------------------------------------------------
BM_time_clock_getres 199 ns 199 ns 3508708
BM_time_clock_getres_syscall 220 ns 220 ns 3184676
BM_time_clock_getres_REALTIME 199 ns 199 ns 3509697
BM_time_clock_getres_BOOTTIME 199 ns 199 ns 3513551
BM_time_clock_getres_TAI 200 ns 199 ns 3512412
BM_time_clock_getres_unsupported 196 ns 196 ns 3575609
x86_64 (glibc):
---------------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------------
BM_time_clock_getres 252 ns 252 ns 2370263
BM_time_clock_getres_syscall 215 ns 215 ns 3287497
BM_time_clock_getres_REALTIME 214 ns 214 ns 3294228
BM_time_clock_getres_BOOTTIME 213 ns 213 ns 3277519
BM_time_clock_getres_TAI 213 ns 213 ns 3294991
BM_time_clock_getres_unsupported 206 ns 206 ns 3450654
imx7d_pico IOT nyc (w/arm,cpu-registers-not-fw-configured) (armv7a):
(Virtual Timers)
Benchmark Time(ns) CPU(ns) Iterations
------------------------------------------------------------------
BM_time_clock_getres 16 345 2000000
BM_time_clock_getres_syscall 16 339 2121212
BM_time_clock_getres_REALTIME 17 350 2058824
BM_time_clock_getres_BOOTTIME 17 345 2000000
BM_time_clock_getres_TAI 16 350 2000000
BM_time_clock_getres_unsupported 13 284 2500000
AFTER:
hikey960 vdso (aarch64):
---------------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------------
BM_time_clock_getres 18 ns 18 ns 37880389
BM_time_clock_getres_syscall 127 ns 127 ns 5520029
BM_time_clock_getres_REALTIME 18 ns 18 ns 37879962
BM_time_clock_getres_BOOTTIME 19 ns 18 ns 37878361
BM_time_clock_getres_TAI 131 ns 131 ns 5368484
BM_time_clock_getres_unsupported 97 ns 97 ns 7182864
hikey960 vdso32 (aarch32):
---------------------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------------------
BM_time_clock_getres 36 ns 36 ns 19205240
BM_time_clock_getres_syscall 212 ns 212 ns 3297100
BM_time_clock_getres_REALTIME 36 ns 36 ns 19219109
BM_time_clock_getres_BOOTTIME 36 ns 36 ns 19222490
BM_time_clock_getres_TAI 206 ns 206 ns 3402868
BM_time_clock_getres_unsupported 159 ns 159 ns 4409492
imx7d_pico IOT nyc (wo/arm,cpu-registers-not-fw-configured) (armv7a):
(Physical Timers)
Benchmark Time(ns) CPU(ns) Iterations
------------------------------------------------------------------
BM_time_clock_getres 2 48 14000000
BM_time_clock_getres_syscall 14 335 2058824
BM_time_clock_getres_REALTIME 2 49 14583333
BM_time_clock_getres_BOOTTIME 2 48 14000000
BM_time_clock_getres_TAI 14 350 2058824
BM_time_clock_getres_unsupported 8 203 3500000
Test: taskset F \
/data/benchmarktest{64}/bionic-benchmarks/bionic-benchmarks \
--bionic_xml=vdso.xml --benchmark_filter=BM_time_clock_getres*
Bug: 63737556
Change-Id: I80c0a5106625d76720287f715fcf145d2aad1705
diff --git a/libc/arch-arm/syscalls/clock_getres.S b/libc/arch-arm/syscalls/__clock_getres.S
similarity index 82%
rename from libc/arch-arm/syscalls/clock_getres.S
rename to libc/arch-arm/syscalls/__clock_getres.S
index e101127..a81cf00 100644
--- a/libc/arch-arm/syscalls/clock_getres.S
+++ b/libc/arch-arm/syscalls/__clock_getres.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(clock_getres)
+ENTRY(__clock_getres)
mov ip, r7
.cfi_register r7, ip
ldr r7, =__NR_clock_getres
@@ -13,4 +13,5 @@
bxls lr
neg r0, r0
b __set_errno_internal
-END(clock_getres)
+END(__clock_getres)
+.hidden __clock_getres
diff --git a/libc/arch-arm64/syscalls/clock_getres.S b/libc/arch-arm64/syscalls/__clock_getres.S
similarity index 78%
rename from libc/arch-arm64/syscalls/clock_getres.S
rename to libc/arch-arm64/syscalls/__clock_getres.S
index 33fda8f..9d0deaa 100644
--- a/libc/arch-arm64/syscalls/clock_getres.S
+++ b/libc/arch-arm64/syscalls/__clock_getres.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(clock_getres)
+ENTRY(__clock_getres)
mov x8, __NR_clock_getres
svc #0
@@ -11,4 +11,5 @@
b.hi __set_errno_internal
ret
-END(clock_getres)
+END(__clock_getres)
+.hidden __clock_getres
diff --git a/libc/arch-mips/syscalls/clock_getres.S b/libc/arch-mips/syscalls/__clock_getres.S
similarity index 81%
rename from libc/arch-mips/syscalls/clock_getres.S
rename to libc/arch-mips/syscalls/__clock_getres.S
index 153f629..c5eed97 100644
--- a/libc/arch-mips/syscalls/clock_getres.S
+++ b/libc/arch-mips/syscalls/__clock_getres.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(clock_getres)
+ENTRY(__clock_getres)
.set noreorder
.cpload $t9
li $v0, __NR_clock_getres
@@ -16,4 +16,5 @@
j $t9
nop
.set reorder
-END(clock_getres)
+END(__clock_getres)
+.hidden __clock_getres
diff --git a/libc/arch-mips64/syscalls/clock_getres.S b/libc/arch-mips64/syscalls/__clock_getres.S
similarity index 85%
rename from libc/arch-mips64/syscalls/clock_getres.S
rename to libc/arch-mips64/syscalls/__clock_getres.S
index 32fac1a..fb871c9 100644
--- a/libc/arch-mips64/syscalls/clock_getres.S
+++ b/libc/arch-mips64/syscalls/__clock_getres.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(clock_getres)
+ENTRY(__clock_getres)
.set push
.set noreorder
li $v0, __NR_clock_getres
@@ -22,4 +22,5 @@
j $t9
move $ra, $t0
.set pop
-END(clock_getres)
+END(__clock_getres)
+.hidden __clock_getres
diff --git a/libc/arch-x86/syscalls/clock_getres.S b/libc/arch-x86/syscalls/__clock_getres.S
similarity index 90%
rename from libc/arch-x86/syscalls/clock_getres.S
rename to libc/arch-x86/syscalls/__clock_getres.S
index 9466e08..b8c5374 100644
--- a/libc/arch-x86/syscalls/clock_getres.S
+++ b/libc/arch-x86/syscalls/__clock_getres.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(clock_getres)
+ENTRY(__clock_getres)
pushl %ebx
.cfi_def_cfa_offset 8
.cfi_rel_offset ebx, 0
@@ -31,4 +31,5 @@
popl %ecx
popl %ebx
ret
-END(clock_getres)
+END(__clock_getres)
+.hidden __clock_getres
diff --git a/libc/arch-x86_64/syscalls/clock_getres.S b/libc/arch-x86_64/syscalls/__clock_getres.S
similarity index 80%
rename from libc/arch-x86_64/syscalls/clock_getres.S
rename to libc/arch-x86_64/syscalls/__clock_getres.S
index f65d127..aff974e 100644
--- a/libc/arch-x86_64/syscalls/clock_getres.S
+++ b/libc/arch-x86_64/syscalls/__clock_getres.S
@@ -2,7 +2,7 @@
#include <private/bionic_asm.h>
-ENTRY(clock_getres)
+ENTRY(__clock_getres)
movl $__NR_clock_getres, %eax
syscall
cmpq $-MAX_ERRNO, %rax
@@ -12,4 +12,5 @@
call __set_errno_internal
1:
ret
-END(clock_getres)
+END(__clock_getres)
+.hidden __clock_getres
diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp
index 969c39f..00d6490 100644
--- a/libc/bionic/vdso.cpp
+++ b/libc/bionic/vdso.cpp
@@ -42,6 +42,15 @@
return __clock_gettime(clock_id, tp);
}
+int clock_getres(int clock_id, timespec* tp) {
+ auto vdso_clock_getres = reinterpret_cast<decltype(&clock_getres)>(
+ __libc_globals->vdso[VDSO_CLOCK_GETRES].fn);
+ if (__predict_true(vdso_clock_getres)) {
+ return vdso_return(vdso_clock_getres(clock_id, tp));
+ }
+ return __clock_getres(clock_id, tp);
+}
+
int gettimeofday(timeval* tv, struct timezone* tz) {
auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>(
__libc_globals->vdso[VDSO_GETTIMEOFDAY].fn);
@@ -54,6 +63,7 @@
void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) {
auto&& vdso = globals->vdso;
vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, nullptr };
+ vdso[VDSO_CLOCK_GETRES] = { VDSO_CLOCK_GETRES_SYMBOL, nullptr };
vdso[VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, nullptr };
// Do we have a vdso?
diff --git a/libc/private/bionic_vdso.h b/libc/private/bionic_vdso.h
index 2d11cd6..ed11501 100644
--- a/libc/private/bionic_vdso.h
+++ b/libc/private/bionic_vdso.h
@@ -33,13 +33,16 @@
#if defined(__aarch64__)
#define VDSO_CLOCK_GETTIME_SYMBOL "__kernel_clock_gettime"
+#define VDSO_CLOCK_GETRES_SYMBOL "__kernel_clock_getres"
#define VDSO_GETTIMEOFDAY_SYMBOL "__kernel_gettimeofday"
#else
#define VDSO_CLOCK_GETTIME_SYMBOL "__vdso_clock_gettime"
+#define VDSO_CLOCK_GETRES_SYMBOL "__vdso_clock_getres"
#define VDSO_GETTIMEOFDAY_SYMBOL "__vdso_gettimeofday"
#endif
extern "C" int __clock_gettime(int, timespec*);
+extern "C" int __clock_getres(int, timespec*);
extern "C" int __gettimeofday(timeval*, struct timezone*);
struct vdso_entry {
@@ -49,6 +52,7 @@
enum {
VDSO_CLOCK_GETTIME = 0,
+ VDSO_CLOCK_GETRES,
VDSO_GETTIMEOFDAY,
VDSO_END
};