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.
This commit is contained in:
Aliaksey Kandratsenka 2015-09-12 16:20:53 -07:00 committed by Aliaksey Kandratsenka
parent 121038308d
commit 4194e485cb
4 changed files with 32 additions and 11 deletions

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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_ */

View File

@ -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());