From: John Coyle Date: Fri, 24 Jun 2016 17:49:42 +0000 (-0400) Subject: compat: add abstractions for non portable pthread name funcs X-Git-Tag: ses5-milestone5~456^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3ba630636de437f7c5b69e620b7e3275eb5ab0a2;p=ceph.git compat: add abstractions for non portable pthread name funcs Supports GNU, FreeBSD, OSX, and Alpine. Signed-off-by: John Coyle --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ea937f10cd5..5f549195854 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,9 @@ CHECK_FUNCTION_EXISTS(name_to_handle_at HAVE_NAME_TO_HANDLE_AT) CHECK_FUNCTION_EXISTS(pipe2 HAVE_PIPE2) set(CMAKE_REQUIRED_LIBRARIES pthread) CHECK_FUNCTION_EXISTS(pthread_spin_init HAVE_PTHREAD_SPINLOCK) +CHECK_FUNCTION_EXISTS(pthread_set_name_np HAVE_PTHREAD_SET_NAME_NP) +CHECK_FUNCTION_EXISTS(pthread_setname_np HAVE_PTHREAD_SETNAME_NP) +CHECK_FUNCTION_EXISTS(pthread_getname_np HAVE_PTHREAD_GETNAME_NP) CHECK_FUNCTION_EXISTS(eventfd HAVE_EVENTFD) CHECK_INCLUDE_FILE_CXX("boost/asio/coroutine.hpp" HAVE_BOOST_ASIO_COROUTINE) diff --git a/configure.ac b/configure.ac index b83cdf9e4e9..0845fcb6b07 100644 --- a/configure.ac +++ b/configure.ac @@ -1145,6 +1145,7 @@ LIBS="$PTHREAD_LIBS $LIBS" CFLAGS="$PTHREAD_CFLAGS $CFLAGS" AC_CHECK_FUNC([pthread_spin_init], [AC_DEFINE(HAVE_PTHREAD_SPINLOCK, 1, [Define if you have pthread_spin_init])]) +AC_CHECK_FUNCS([pthread_getname_np pthread_setname_np pthread_set_name_np]) LIBS="$saved_LIBS" CFLAGS="$saved_CFLAGS" diff --git a/src/client/SyntheticClient.cc b/src/client/SyntheticClient.cc index b2ef93ae30b..7e324fdb704 100644 --- a/src/client/SyntheticClient.cc +++ b/src/client/SyntheticClient.cc @@ -12,6 +12,8 @@ * */ +#include "include/compat.h" + #include #include using namespace std; diff --git a/src/common/WorkQueue.cc b/src/common/WorkQueue.cc index 6fc468ab5c1..34304915887 100644 --- a/src/common/WorkQueue.cc +++ b/src/common/WorkQueue.cc @@ -12,6 +12,8 @@ * */ +#include "include/compat.h" + #include #include "include/types.h" @@ -95,9 +97,7 @@ void ThreadPool::worker(WorkThread *wt) std::stringstream ss; char name[16] = {0}; -#if !defined(__FreeBSD__) pthread_getname_np(pthread_self(), name, sizeof(name)); -#endif ss << name << " thread " << name; heartbeat_handle_d *hb = cct->get_heartbeat_map()->add_worker(ss.str(), pthread_self()); @@ -302,9 +302,7 @@ void ShardedThreadPool::shardedthreadpool_worker(uint32_t thread_index) std::stringstream ss; char name[16] = {0}; -#if !defined(__FreeBSD__) pthread_getname_np(pthread_self(), name, sizeof(name)); -#endif ss << name << " thread " << name; heartbeat_handle_d *hb = cct->get_heartbeat_map()->add_worker(ss.str(), pthread_self()); diff --git a/src/global/signal_handler.cc b/src/global/signal_handler.cc index a01a78a868a..5b65065cdc6 100644 --- a/src/global/signal_handler.cc +++ b/src/global/signal_handler.cc @@ -12,6 +12,8 @@ * */ +#include "include/compat.h" + #include "common/BackTrace.h" #include "common/debug.h" #include "global/pidfile.h" @@ -90,10 +92,8 @@ static void handle_fatal_signal(int signum) // presumably dump core-- will handle it. char buf[1024]; char pthread_name[16] = {0}; //limited by 16B include terminating null byte. -#if !defined(__FreeBSD__) int r = pthread_getname_np(pthread_self(), pthread_name, sizeof(pthread_name)); (void)r; -#endif #if defined(__sun) char message[SIG2STR_MAX]; sig2str(signum,message); diff --git a/src/include/compat.h b/src/include/compat.h index 1df098bc42f..7dc2833ad6f 100644 --- a/src/include/compat.h +++ b/src/include/compat.h @@ -12,6 +12,8 @@ #ifndef CEPH_COMPAT_H #define CEPH_COMPAT_H +#include "acconfig.h" + #if defined(__FreeBSD__) /* Make sure that ENODATA is defined in the correct way */ @@ -58,8 +60,6 @@ #endif #endif -/* Fix a small name diff */ -#define pthread_setname_np pthread_set_name_np /* And include the extra required include file */ #include @@ -112,4 +112,30 @@ #define MSG_DONTWAIT MSG_NONBLOCK #endif +#if defined(HAVE_PTHREAD_SETNAME_NP) + #if defined(__APPLE__) + #define pthread_setname_np(thread, name) ({ \ + int __result = 0; \ + if (thread == pthread_self()) \ + __result = pthread_setname_np(name) \ + __result; }) + #endif +#elif defined(HAVE_PTHREAD_SET_NAME_NP) + /* Fix a small name diff */ + #define pthread_setname_np pthread_set_name_np +#else + /* compiler warning free success noop */ + #define pthread_setname_np(thread, name) ({ \ + int __i = 0; \ + __i; }) +#endif + +#if !defined(HAVE_PTHREAD_GETNAME_NP) + /* compiler warning free success noop */ + #define pthread_getname_np(thread, name, len) ({ \ + if (name != NULL) \ + *name = '\0'; \ + 0; }) +#endif + #endif /* !CEPH_COMPAT_H */ diff --git a/src/include/config-h.in.cmake b/src/include/config-h.in.cmake index 0292f7e8532..1d6bc706f68 100644 --- a/src/include/config-h.in.cmake +++ b/src/include/config-h.in.cmake @@ -267,4 +267,13 @@ /* Version number of package */ #cmakedefine VERSION "@VERSION@" +/* Defined if pthread_setname_np() is available */ +#cmakedefine HAVE_PTHREAD_SETNAME_NP 1 + +/* Defined if pthread_set_name_np() is available */ +#cmakedefine HAVE_PTHREAD_SET_NAME_NP + +/* Defined if pthread_getname_np() is available */ +#cmakedefine HAVE_PTHREAD_GETNAME_NP 1 + #endif /* CONFIG_H */