diff options
Diffstat (limited to 'src/mongo/util/tcmalloc_server_status_section.cpp')
-rw-r--r-- | src/mongo/util/tcmalloc_server_status_section.cpp | 168 |
1 files changed, 83 insertions, 85 deletions
diff --git a/src/mongo/util/tcmalloc_server_status_section.cpp b/src/mongo/util/tcmalloc_server_status_section.cpp index 4a16b56030e..1da1c5a96c1 100644 --- a/src/mongo/util/tcmalloc_server_status_section.cpp +++ b/src/mongo/util/tcmalloc_server_status_section.cpp @@ -40,95 +40,93 @@ namespace mongo { namespace { - // If many clients are used, the per-thread caches become smaller and chances of - // rebalancing of free space during critical sections increases. In such situations, - // it is better to release memory when it is likely the thread will be blocked for - // a long time. - const int kManyClients = 40; - size_t tcmallocPoolSize = 0; - - // Callback to allow TCMalloc to release freed memory to the central list at favorable times. - void threadStateChange() { - int thread_count = Listener::globalTicketHolder.used(); - if (thread_count <= kManyClients) - return; - - #if MONGO_HAVE_GPERFTOOLS_SHRINK_CACHE_SIZE - MallocExtension::instance()->ShrinkCacheIfAboveSize(tcmallocPoolSize/thread_count); - #else - MallocExtension::instance()->MarkThreadIdle(); - MallocExtension::instance()->MarkThreadBusy(); - #endif - } +// If many clients are used, the per-thread caches become smaller and chances of +// rebalancing of free space during critical sections increases. In such situations, +// it is better to release memory when it is likely the thread will be blocked for +// a long time. +const int kManyClients = 40; +size_t tcmallocPoolSize = 0; + +// Callback to allow TCMalloc to release freed memory to the central list at favorable times. +void threadStateChange() { + int thread_count = Listener::globalTicketHolder.used(); + if (thread_count <= kManyClients) + return; + +#if MONGO_HAVE_GPERFTOOLS_SHRINK_CACHE_SIZE + MallocExtension::instance()->ShrinkCacheIfAboveSize(tcmallocPoolSize / thread_count); +#else + MallocExtension::instance()->MarkThreadIdle(); + MallocExtension::instance()->MarkThreadBusy(); +#endif +} + +// Register threadStateChange callback +MONGO_INITIALIZER(TCMallocThreadIdleListener)(InitializerContext*) { + registerThreadIdleCallback(&threadStateChange); + MallocExtension::instance()->GetNumericProperty("tcmalloc.max_total_thread_cache_bytes", + &tcmallocPoolSize); + LOG(1) << "tcmallocPoolSize: " << tcmallocPoolSize << "\n"; + return Status::OK(); +} - // Register threadStateChange callback - MONGO_INITIALIZER(TCMallocThreadIdleListener)(InitializerContext*) { - registerThreadIdleCallback(&threadStateChange); - MallocExtension::instance()->GetNumericProperty("tcmalloc.max_total_thread_cache_bytes", - &tcmallocPoolSize); - LOG(1) << "tcmallocPoolSize: " << tcmallocPoolSize << "\n"; - return Status::OK(); +class TCMallocServerStatusSection : public ServerStatusSection { +public: + TCMallocServerStatusSection() : ServerStatusSection("tcmalloc") {} + virtual bool includeByDefault() const { + return false; } - class TCMallocServerStatusSection : public ServerStatusSection { - public: - - TCMallocServerStatusSection() : ServerStatusSection("tcmalloc") {} - virtual bool includeByDefault() const { return false; } - - virtual BSONObj generateSection(OperationContext* txn, - const BSONElement& configElement) const { - - BSONObjBuilder builder; - - // For a list of properties see the "Generic Tcmalloc Status" section of - // http://google-perftools.googlecode.com/svn/trunk/doc/tcmalloc.html and - // http://code.google.com/p/gperftools/source/browse/src/gperftools/malloc_extension.h - { - BSONObjBuilder sub(builder.subobjStart("generic")); - appendNumericPropertyIfAvailable(sub, "current_allocated_bytes", - "generic.current_allocated_bytes"); - appendNumericPropertyIfAvailable(sub, "heap_size", - "generic.heap_size"); - } - { - BSONObjBuilder sub(builder.subobjStart("tcmalloc")); - appendNumericPropertyIfAvailable(sub, "pageheap_free_bytes", - "tcmalloc.pageheap_free_bytes"); - appendNumericPropertyIfAvailable(sub, "pageheap_unmapped_bytes", - "tcmalloc.pageheap_unmapped_bytes"); - appendNumericPropertyIfAvailable(sub, "max_total_thread_cache_bytes", - "tcmalloc.max_total_thread_cache_bytes"); - appendNumericPropertyIfAvailable(sub, "current_total_thread_cache_bytes", - "tcmalloc.current_total_thread_cache_bytes"); - // Not including tcmalloc.slack_bytes since it is deprecated. - - appendNumericPropertyIfAvailable(sub, "central_cache_free_bytes", - "tcmalloc.central_cache_free_bytes"); - appendNumericPropertyIfAvailable(sub, "transfer_cache_free_bytes", - "tcmalloc.transfer_cache_free_bytes"); - appendNumericPropertyIfAvailable(sub, "thread_cache_free_bytes", - "tcmalloc.thread_cache_free_bytes"); - appendNumericPropertyIfAvailable(sub, "aggressive_memory_decommit", - "tcmalloc.aggressive_memory_decommit"); - } - - char buffer[4096]; - MallocExtension::instance()->GetStats(buffer, sizeof(buffer)); - builder.append("formattedString", buffer); - - return builder.obj(); + virtual BSONObj generateSection(OperationContext* txn, const BSONElement& configElement) const { + BSONObjBuilder builder; + + // For a list of properties see the "Generic Tcmalloc Status" section of + // http://google-perftools.googlecode.com/svn/trunk/doc/tcmalloc.html and + // http://code.google.com/p/gperftools/source/browse/src/gperftools/malloc_extension.h + { + BSONObjBuilder sub(builder.subobjStart("generic")); + appendNumericPropertyIfAvailable( + sub, "current_allocated_bytes", "generic.current_allocated_bytes"); + appendNumericPropertyIfAvailable(sub, "heap_size", "generic.heap_size"); } - - private: - static void appendNumericPropertyIfAvailable(BSONObjBuilder& builder, - StringData bsonName, - const char* property) { - size_t value; - if (MallocExtension::instance()->GetNumericProperty(property, &value)) - builder.appendNumber(bsonName, value); + { + BSONObjBuilder sub(builder.subobjStart("tcmalloc")); + appendNumericPropertyIfAvailable( + sub, "pageheap_free_bytes", "tcmalloc.pageheap_free_bytes"); + appendNumericPropertyIfAvailable( + sub, "pageheap_unmapped_bytes", "tcmalloc.pageheap_unmapped_bytes"); + appendNumericPropertyIfAvailable( + sub, "max_total_thread_cache_bytes", "tcmalloc.max_total_thread_cache_bytes"); + appendNumericPropertyIfAvailable(sub, + "current_total_thread_cache_bytes", + "tcmalloc.current_total_thread_cache_bytes"); + // Not including tcmalloc.slack_bytes since it is deprecated. + + appendNumericPropertyIfAvailable( + sub, "central_cache_free_bytes", "tcmalloc.central_cache_free_bytes"); + appendNumericPropertyIfAvailable( + sub, "transfer_cache_free_bytes", "tcmalloc.transfer_cache_free_bytes"); + appendNumericPropertyIfAvailable( + sub, "thread_cache_free_bytes", "tcmalloc.thread_cache_free_bytes"); + appendNumericPropertyIfAvailable( + sub, "aggressive_memory_decommit", "tcmalloc.aggressive_memory_decommit"); } - } tcmallocServerStatusSection; + + char buffer[4096]; + MallocExtension::instance()->GetStats(buffer, sizeof(buffer)); + builder.append("formattedString", buffer); + + return builder.obj(); + } + +private: + static void appendNumericPropertyIfAvailable(BSONObjBuilder& builder, + StringData bsonName, + const char* property) { + size_t value; + if (MallocExtension::instance()->GetNumericProperty(property, &value)) + builder.appendNumber(bsonName, value); + } +} tcmallocServerStatusSection; } } - |