portable sched_getcpu calls

Summary:
- added a feature test in build_detect_platform to check whether sched_getcpu() is available. glibc offers it only on some platforms (e.g., linux but not mac); this way should be easier than maintaining a list of platforms on which it's available.
- refactored PhysicalCoreID() to be simpler / less repetitive. ordered the conditional compilation clauses from most-to-least preferred
Closes https://github.com/facebook/rocksdb/pull/2272

Differential Revision: D5038093

Pulled By: ajkr

fbshipit-source-id: 81d7db3cc620250de220bdeb3194b2b3d7673de7
main
Andrew Kryczka 8 years ago committed by Facebook Github Bot
parent 0f559abdb7
commit be421b0b16
  1. 1
      TARGETS
  2. 11
      build_tools/build_detect_platform
  3. 2
      build_tools/fbcode_config.sh
  4. 2
      build_tools/fbcode_config4.8.1.sh
  5. 2
      db/db_test2.cc
  6. 21
      port/port_posix.cc

@ -9,6 +9,7 @@ rocksdb_compiler_flags = [
"-DROCKSDB_LIB_IO_POSIX", "-DROCKSDB_LIB_IO_POSIX",
"-DROCKSDB_FALLOCATE_PRESENT", "-DROCKSDB_FALLOCATE_PRESENT",
"-DROCKSDB_MALLOC_USABLE_SIZE", "-DROCKSDB_MALLOC_USABLE_SIZE",
"-DROCKSDB_SCHED_GETCPU_PRESENT",
"-DOS_LINUX", "-DOS_LINUX",
# Flags to enable libs we include # Flags to enable libs we include
"-DSNAPPY", "-DSNAPPY",

@ -401,6 +401,17 @@ EOF
if [ "$?" = 0 ]; then if [ "$?" = 0 ]; then
COMMON_FLAGS="$COMMON_FLAGS -DROCKSDB_RANGESYNC_PRESENT" COMMON_FLAGS="$COMMON_FLAGS -DROCKSDB_RANGESYNC_PRESENT"
fi fi
# Test whether sched_getcpu is supported
$CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null <<EOF
#include <sched.h>
int main() {
int cpuid = sched_getcpu();
}
EOF
if [ "$?" = 0 ]; then
COMMON_FLAGS="$COMMON_FLAGS -DROCKSDB_SCHED_GETCPU_PRESENT"
fi
fi fi
# TODO(tec): Fix -Wshorten-64-to-32 errors on FreeBSD and enable the warning. # TODO(tec): Fix -Wshorten-64-to-32 errors on FreeBSD and enable the warning.

@ -128,7 +128,7 @@ else
fi fi
CFLAGS+=" $DEPS_INCLUDE" CFLAGS+=" $DEPS_INCLUDE"
CFLAGS+=" -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DROCKSDB_FALLOCATE_PRESENT -DROCKSDB_MALLOC_USABLE_SIZE" CFLAGS+=" -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DROCKSDB_FALLOCATE_PRESENT -DROCKSDB_MALLOC_USABLE_SIZE -DROCKSDB_SCHED_GETCPU_PRESENT"
CXXFLAGS+=" $CFLAGS" CXXFLAGS+=" $CFLAGS"
EXEC_LDFLAGS=" $SNAPPY_LIBS $ZLIB_LIBS $BZIP_LIBS $LZ4_LIBS $ZSTD_LIBS $GFLAGS_LIBS $NUMA_LIB $TBB_LIBS" EXEC_LDFLAGS=" $SNAPPY_LIBS $ZLIB_LIBS $BZIP_LIBS $LZ4_LIBS $ZSTD_LIBS $GFLAGS_LIBS $NUMA_LIB $TBB_LIBS"

@ -93,7 +93,7 @@ else
fi fi
CFLAGS+=" $DEPS_INCLUDE" CFLAGS+=" $DEPS_INCLUDE"
CFLAGS+=" -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DROCKSDB_FALLOCATE_PRESENT -DROCKSDB_MALLOC_USABLE_SIZE" CFLAGS+=" -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX -DROCKSDB_FALLOCATE_PRESENT -DROCKSDB_MALLOC_USABLE_SIZE -DROCKSDB_SCHED_GETCPU_PRESENT"
CFLAGS+=" -DSNAPPY -DGFLAGS=google -DZLIB -DBZIP2 -DLZ4 -DZSTD -DNUMA -DTBB" CFLAGS+=" -DSNAPPY -DGFLAGS=google -DZLIB -DBZIP2 -DLZ4 -DZSTD -DNUMA -DTBB"
CXXFLAGS+=" $CFLAGS" CXXFLAGS+=" $CFLAGS"

@ -1644,7 +1644,7 @@ size_t GetEncodedEntrySize(size_t key_size, size_t value_size) {
TEST_F(DBTest2, ReadAmpBitmap) { TEST_F(DBTest2, ReadAmpBitmap) {
Options options = CurrentOptions(); Options options = CurrentOptions();
BlockBasedTableOptions bbto; BlockBasedTableOptions bbto;
size_t bytes_per_bit[2] = {1, 16}; uint32_t bytes_per_bit[2] = {1, 16};
for (size_t k = 0; k < 2; k++) { for (size_t k = 0; k < 2; k++) {
// Disable delta encoding to make it easier to calculate read amplification // Disable delta encoding to make it easier to calculate read amplification
bbto.use_delta_encoding = false; bbto.use_delta_encoding = false;

@ -16,6 +16,7 @@
#include <cpuid.h> #include <cpuid.h>
#endif #endif
#include <errno.h> #include <errno.h>
#include <sched.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -137,28 +138,26 @@ void RWMutex::ReadUnlock() { PthreadCall("read unlock", pthread_rwlock_unlock(&m
void RWMutex::WriteUnlock() { PthreadCall("write unlock", pthread_rwlock_unlock(&mu_)); } void RWMutex::WriteUnlock() { PthreadCall("write unlock", pthread_rwlock_unlock(&mu_)); }
int GetCPUID() { int PhysicalCoreID() {
#if defined(ROCKSDB_SCHED_GETCPU_PRESENT) && defined(__x86_64__) && \
(__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 22))
// sched_getcpu uses VDSO getcpu() syscall since 2.22. I believe Linux offers VDSO
// support only on x86_64. This is the fastest/preferred method if available.
int cpuno = sched_getcpu(); int cpuno = sched_getcpu();
if (cpuno < 0) { if (cpuno < 0) {
return -1; return -1;
} }
else {
return cpuno; return cpuno;
} #elif defined(__x86_64__) || defined(__i386__)
} // clang/gcc both provide cpuid.h, which defines __get_cpuid(), for x86_64 and i386.
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; unsigned eax, ebx = 0, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) { if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return -1; return -1;
} }
return ebx >> 24; return ebx >> 24;
#else #else
return GetCPUID(); // give up, the caller can generate a random number or something.
return -1;
#endif #endif
} }

Loading…
Cancel
Save