2525 * then also delete it in the license file.
2626 */
2727
28+ #define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kDefault
29+
2830#include " mongo/platform/basic.h"
2931
3032#include < third_party/gperftools-2.2/src/gperftools/malloc_extension.h>
3133
3234#include " mongo/base/init.h"
3335#include " mongo/db/commands/server_status.h"
3436#include " mongo/util/concurrency/synchronization.h"
37+ #include " mongo/util/log.h"
3538#include " mongo/util/net/listen.h"
3639
3740namespace mongo {
@@ -43,16 +46,35 @@ namespace {
4346 // a long time.
4447 const int kManyClients = 40 ;
4548
49+ boost::mutex tcmallocCleanupLock;
50+
4651 /* *
4752 * Callback to allow TCMalloc to release freed memory to the central list at
4853 * favorable times. Ideally would do some milder cleanup or scavenge...
4954 */
5055 void threadStateChange () {
51- if (Listener::globalTicketHolder.used () > kManyClients ) {
52- // Mark thread busy while we're idle, so that overhead is incurred now.
53- MallocExtension::instance ()->MarkThreadIdle ();
54- MallocExtension::instance ()->MarkThreadBusy ();
56+ if (Listener::globalTicketHolder.used () <= kManyClients ) {
57+ return ;
5558 }
59+
60+ size_t threadCacheSizeBytes = MallocExtension::instance ()->GetThreadCacheSize ();
61+
62+ static const size_t kMaxThreadCacheSizeBytes = 0x10000 ;
63+ if (threadCacheSizeBytes < kMaxThreadCacheSizeBytes ) {
64+ // This number was chosen a bit magically.
65+ // At 1000 threads and the current (64mb) thread local cache size, we're "full".
66+ // So we may want this number to scale with the number of current clients.
67+ return ;
68+ }
69+
70+ LOG (1 ) << " thread over memory limit, cleaning up, current: "
71+ << (threadCacheSizeBytes/1024 ) << " k" ;
72+
73+ // We synchronize as the tcmalloc central list uses a spinlock, and we can cause a really
74+ // terrible runaway if we're not careful.
75+ boost::mutex::scoped_lock lk (tcmallocCleanupLock);
76+ MallocExtension::instance ()->MarkThreadIdle ();
77+ MallocExtension::instance ()->MarkThreadBusy ();
5678 }
5779
5880 // Register threadStateChange callback
0 commit comments