port: updated PhysicalCoreID()

Summary:
Updated PhysicalCoreID() to use sched_getcpu() on x86_64 for glibc >= 2.22.  Added a new
function named GetCPUID() that calls sched_getcpu(), to avoid repeated code. This change is done as per the comments of PR: https://github.com/facebook/rocksdb/pull/2230

Signed-off-by: Jos Collin <jcollin@redhat.com>
Closes https://github.com/facebook/rocksdb/pull/2260

Differential Revision: D5025734

Pulled By: ajkr

fbshipit-source-id: f4cca68c12573cafcf8531e7411a1e733bbf8eef
This commit is contained in:
Jos Collin 2017-05-09 18:43:23 -07:00 committed by Facebook Github Bot
parent df035b6826
commit a620966969

View File

@ -137,16 +137,7 @@ void RWMutex::ReadUnlock() { PthreadCall("read unlock", pthread_rwlock_unlock(&m
void RWMutex::WriteUnlock() { PthreadCall("write unlock", pthread_rwlock_unlock(&mu_)); }
int PhysicalCoreID() {
#if defined(__i386__) || defined(__x86_64__)
// if you ever find that this function is hot on Linux, you can go from
// ~200 nanos to ~20 nanos by adding the machinery to use __vdso_getcpu
unsigned eax, ebx = 0, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return -1;
}
return ebx >> 24;
#else
int GetCPUID() {
int cpuno = sched_getcpu();
if (cpuno < 0) {
return -1;
@ -154,6 +145,20 @@ int PhysicalCoreID() {
else {
return cpuno;
}
}
int PhysicalCoreID() {
#if defined(__i386__) || defined(__x86_64__)
#if defined(__x86_64__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 22))
return GetCPUID();
#endif
unsigned eax, ebx = 0, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return -1;
}
return ebx >> 24;
#else
return GetCPUID();
#endif
}