summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAliaksey Kandratsenka <alk@tut.by>2015-09-12 16:20:53 -0700
committerAliaksey Kandratsenka <alkondratenko@gmail.com>2015-10-05 20:56:28 -0700
commit4194e485cbb5d8c59f65aba49da63c08ecc573da (patch)
tree47ed15fbbb1f94ea5862902db87fb62ee37b86db
parent121038308d8c5b34707614c44de265816a322563 (diff)
downloadgperftools-4194e485cbb5d8c59f65aba49da63c08ecc573da.tar.gz
Don't link libtcmalloc_minimal.so to libpthread.so
So that LD_PRELOAD-ing doesn't force loading libpthread.so which may slow down some single-threaded apps. tcmalloc already has maybe_threads facility that can detect if libpthread.so is loaded (via weak symbols) and provide 'simulations' of some pthread functions that tcmalloc needs.
-rwxr-xr-xMakefile.am10
-rw-r--r--src/maybe_threads.cc14
-rw-r--r--src/maybe_threads.h7
-rw-r--r--src/static_vars.cc12
4 files changed, 32 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am
index bae605a..6fed874 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -482,10 +482,10 @@ libtcmalloc_minimal_internal_la_SOURCES = src/common.cc \
# We #define NO_TCMALLOC_SAMPLES, since sampling is turned off for _minimal.
libtcmalloc_minimal_internal_la_CXXFLAGS = -DNO_TCMALLOC_SAMPLES \
-DNO_HEAP_CHECK \
- $(PTHREAD_CFLAGS) -DNDEBUG \
+ -DNDEBUG \
$(AM_CXXFLAGS) $(NO_EXCEPTIONS)
-libtcmalloc_minimal_internal_la_LDFLAGS = $(PTHREAD_CFLAGS) $(AM_LDFLAGS)
-libtcmalloc_minimal_internal_la_LIBADD = $(PTHREAD_LIBS) $(LIBSPINLOCK) libmaybe_threads.la
+libtcmalloc_minimal_internal_la_LDFLAGS = $(AM_LDFLAGS)
+libtcmalloc_minimal_internal_la_LIBADD = $(LIBSPINLOCK) libmaybe_threads.la
lib_LTLIBRARIES += libtcmalloc_minimal.la
WINDOWS_PROJECTS += vsprojects/libtcmalloc_minimal/libtcmalloc_minimal.vcproj
@@ -493,8 +493,8 @@ libtcmalloc_minimal_la_SOURCES = $(TCMALLOC_CC) $(TCMALLOC_MINIMAL_INCLUDES)
libtcmalloc_minimal_la_CXXFLAGS = -DNO_TCMALLOC_SAMPLES \
$(PTHREAD_CFLAGS) -DNDEBUG $(AM_CXXFLAGS)
# -version-info gets passed to libtool
-libtcmalloc_minimal_la_LDFLAGS = $(PTHREAD_CFLAGS) -version-info @TCMALLOC_SO_VERSION@ $(AM_LDFLAGS)
-libtcmalloc_minimal_la_LIBADD = libtcmalloc_minimal_internal.la $(PTHREAD_LIBS)
+libtcmalloc_minimal_la_LDFLAGS = -version-info @TCMALLOC_SO_VERSION@ $(AM_LDFLAGS)
+libtcmalloc_minimal_la_LIBADD = libtcmalloc_minimal_internal.la
# For windows, we're playing around with trying to do some stacktrace
# support even with libtcmalloc_minimal. For everyone else, though,
diff --git a/src/maybe_threads.cc b/src/maybe_threads.cc
index 6dd0d8d..acfc99a 100644
--- a/src/maybe_threads.cc
+++ b/src/maybe_threads.cc
@@ -48,6 +48,7 @@
#include <string>
#include "maybe_threads.h"
#include "base/basictypes.h"
+#include "base/logging.h"
// __THROW is defined in glibc systems. It means, counter-intuitively,
// "This function will never throw an exception." It's an optional
@@ -68,6 +69,10 @@ extern "C" {
__THROW ATTRIBUTE_WEAK;
int pthread_once(pthread_once_t *, void (*)(void))
ATTRIBUTE_WEAK;
+ int pthread_atfork(void (*__prepare) (void),
+ void (*__parent) (void),
+ void (*__child) (void))
+ __THROW ATTRIBUTE_WEAK;
}
#define MAX_PERTHREAD_VALS 16
@@ -155,3 +160,12 @@ int perftools_pthread_once(pthread_once_t *ctl,
return 0;
}
}
+
+void perftools_pthread_atfork(void (*before)(),
+ void (*parent_after)(),
+ void (*child_after)()) {
+ if (pthread_atfork) {
+ int rv = pthread_atfork(before, parent_after, child_after);
+ CHECK(rv == 0);
+ }
+}
diff --git a/src/maybe_threads.h b/src/maybe_threads.h
index b60f4ef..c6cfdf7 100644
--- a/src/maybe_threads.h
+++ b/src/maybe_threads.h
@@ -51,4 +51,11 @@ int perftools_pthread_setspecific(pthread_key_t key, void *val);
int perftools_pthread_once(pthread_once_t *ctl,
void (*init_routine) (void));
+// Our wrapper for pthread_atfork. Does _nothing_ when there are no
+// threads. See static_vars.cc:SetupAtForkLocksHandler for only user
+// of this.
+void perftools_pthread_atfork(void (*before)(),
+ void (*parent_after)(),
+ void (*child_after)());
+
#endif /* GOOGLE_MAYBE_THREADS_H_ */
diff --git a/src/static_vars.cc b/src/static_vars.cc
index 09d2b59..79de97e 100644
--- a/src/static_vars.cc
+++ b/src/static_vars.cc
@@ -43,6 +43,7 @@
#include "sampler.h" // for Sampler
#include "getenv_safe.h" // TCMallocGetenvSafe
#include "base/googleinit.h"
+#include "maybe_threads.h"
namespace tcmalloc {
@@ -107,16 +108,15 @@ void Static::InitStaticVars() {
}
-#if defined(HAVE_FORK) && defined(HAVE_PTHREAD)
+#if defined(HAVE_FORK) && defined(HAVE_PTHREAD) && !defined(__APPLE__)
static inline
void SetupAtForkLocksHandler()
{
-#if !defined(__APPLE__)
- pthread_atfork(CentralCacheLockAll, // parent calls before fork
- CentralCacheUnlockAll, // parent calls after fork
- CentralCacheUnlockAll); // child calls after fork
-#endif
+ perftools_pthread_atfork(
+ CentralCacheLockAll, // parent calls before fork
+ CentralCacheUnlockAll, // parent calls after fork
+ CentralCacheUnlockAll); // child calls after fork
}
REGISTER_MODULE_INITIALIZER(tcmalloc_fork_handler, SetupAtForkLocksHandler());