Skip to content

Commit

Permalink
port: updated PhysicalCoreID()
Browse files Browse the repository at this point in the history
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: #2230

Signed-off-by: Jos Collin <jcollin@redhat.com>
Closes #2260

Differential Revision: D5025734

Pulled By: ajkr

fbshipit-source-id: f4cca68c12573cafcf8531e7411a1e733bbf8eef
  • Loading branch information
joscollin authored and facebook-github-bot committed May 10, 2017
1 parent df035b6 commit a620966
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions port/port_posix.cc
Expand Up @@ -137,23 +137,28 @@ void RWMutex::ReadUnlock() { PthreadCall("read unlock", pthread_rwlock_unlock(&m

void RWMutex::WriteUnlock() { PthreadCall("write unlock", pthread_rwlock_unlock(&mu_)); }

int GetCPUID() {
int cpuno = sched_getcpu();
if (cpuno < 0) {
return -1;
}
else {
return cpuno;
}
}

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
#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
int cpuno = sched_getcpu();
if (cpuno < 0) {
return -1;
}
else {
return cpuno;
}
return GetCPUID();
#endif
}

Expand Down

0 comments on commit a620966

Please sign in to comment.