From b0a5bd8940c1ee3d2779d1bdb50f106bbe9a3219 Mon Sep 17 00:00:00 2001 From: Matthew Barr Date: Fri, 12 May 2017 11:29:58 +1000 Subject: [PATCH] test for pthread_setaffinity_np Only enable setting threads per core when available --- cmake/config.h.in | 2 ++ tools/hsbench/CMakeLists.txt | 12 ++++++++++++ tools/hsbench/main.cpp | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cmake/config.h.in b/cmake/config.h.in index 5434668e..62029cb9 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -46,6 +46,8 @@ 0 if you don't. */ #cmakedefine HAVE_DECL_PTHREAD_SETAFFINITY_NP +#cmakedefine HAVE_PTHREAD_NP_H + /* Define to 1 if you have the `malloc_info' function. */ #cmakedefine HAVE_MALLOC_INFO diff --git a/tools/hsbench/CMakeLists.txt b/tools/hsbench/CMakeLists.txt index 3b9a73f7..8f718ee3 100644 --- a/tools/hsbench/CMakeLists.txt +++ b/tools/hsbench/CMakeLists.txt @@ -11,6 +11,18 @@ else() set(EXTRA_CXX_FLAGS "${EXTRA_CXX_FLAGS} -isystem ${SQLITE3_INCLUDE_DIRS}") endif() +# BSD has the _np funcs in a _np header +CHECK_INCLUDE_FILE(pthread_np.h HAVE_PTHREAD_NP_H) +if (HAVE_PTHREAD_NP_H) + set (PTHREAD_NP_INC pthread_np.h) +else () + set (PTHREAD_NP_INC pthread.h) +endif () + +set (CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -D_GNU_SOURCE") +set (CMAKE_REQUIRED_LIBRARIES pthread) +CHECK_CXX_SYMBOL_EXISTS(pthread_setaffinity_np ${PTHREAD_NP_INC} HAVE_DECL_PTHREAD_SETAFFINITY_NP) + CHECK_FUNCTION_EXISTS(malloc_info HAVE_MALLOC_INFO) CHECK_FUNCTION_EXISTS(shmget HAVE_SHMGET) set(HAVE_SHMGET ${HAVE_SHMGET} CACHE BOOL "shmget()") diff --git a/tools/hsbench/main.cpp b/tools/hsbench/main.cpp index b5506af3..3153737e 100644 --- a/tools/hsbench/main.cpp +++ b/tools/hsbench/main.cpp @@ -56,6 +56,9 @@ #include #ifndef _WIN32 #include +#if defined(HAVE_PTHREAD_NP_H) +#include +#endif #include #endif @@ -122,7 +125,11 @@ public: // Apply processor affinity (if available) to this thread. bool affine(UNUSED int cpu) { #ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP +#if defined(__linux__) cpu_set_t cpuset; +#else // BSD + cpuset_t cpuset; +#endif CPU_ZERO(&cpuset); assert(cpu >= 0 && cpu < CPU_SETSIZE); @@ -166,7 +173,9 @@ void usage(const char *error) { " (default: streaming).\n"); printf(" -V Benchmark in vectored mode" " (default: streaming).\n"); +#ifdef HAVE_DECL_PTHREAD_SETAFFINITY_NP printf(" -T CPU,CPU,... Benchmark with threads on these CPUs.\n"); +#endif printf(" -i DIR Don't compile, load from files in DIR" " instead.\n"); printf(" -w DIR After compiling, save to files in DIR.\n"); @@ -195,7 +204,11 @@ struct BenchmarkSigs { static void processArgs(int argc, char *argv[], vector &sigSets, UNUSED unique_ptr &grey) { - const char options[] = "-b:c:Cd:e:E:G:hi:n:No:p:sT:Vw:z:"; + const char options[] = "-b:c:Cd:e:E:G:hi:n:No:p:sVw:z:" +#if HAVE_DECL_PTHREAD_SETAFFINITY_N + "T:" // add the thread flag +#endif + ; int in_sigfile = 0; int do_per_scan = 0; int do_echo_matches = 0;