mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-10 05:10:06 +00:00
6e29cb3f61
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)
53 lines
1.4 KiB
C
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 */
|