glibc/sysdeps/unix/sysv/linux/sched_getcpu.c
Mathieu Desnoyers 6e29cb3f61 Linux: Use rseq in sched_getcpu if available
When available, use the cpu_id field from __rseq_abi on Linux to
implement sched_getcpu().  Fall-back on the vgetcpu vDSO if unavailable.

Benchmarks:

x86-64: Intel E5-2630 v3@2.40GHz, 16-core, hyperthreading

glibc sched_getcpu():                     13.7 ns (baseline)
glibc sched_getcpu() using rseq:           2.5 ns (speedup:  5.5x)
inline load cpuid from __rseq_abi TLS:     0.8 ns (speedup: 17.1x)
2020-07-06 10:21:32 +02:00

53 lines
1.4 KiB
C

/* Copyright (C) 2007-2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <sched.h>
#include <sysdep.h>
#include <atomic.h>
#include <sysdep-vdso.h>
#include <sys/rseq.h>
static int
vsyscall_sched_getcpu (void)
{
unsigned int cpu;
int r = -1;
#ifdef HAVE_GETCPU_VSYSCALL
r = INLINE_VSYSCALL (getcpu, 3, &cpu, NULL, NULL);
#else
r = INLINE_SYSCALL_CALL (getcpu, &cpu, NULL, NULL);
#endif
return r == -1 ? r : cpu;
}
#ifdef RSEQ_SIG
int
sched_getcpu (void)
{
int cpu_id = atomic_load_relaxed (&__rseq_abi.cpu_id);
return cpu_id >= 0 ? cpu_id : vsyscall_sched_getcpu ();
}
#else /* RSEQ_SIG */
int
sched_getcpu (void)
{
return vsyscall_sched_getcpu ();
}
#endif /* RSEQ_SIG */