Skip to content

Commit

Permalink
#3750: [docdb] Deregister callback for common mem trackers from root …
Browse files Browse the repository at this point in the history
…mem tracker in destructor of vector

Summary:
Tree of `MemTrackers` is organized so that a node maintains pointers to all nodes along the path to root. Hence (almost) all traversal is in the upward direction. But common `MemTracker`s are accessed from the root - i.e., in the         downward direction. In the destructor of a common `MemTracker`, an upward traversal to root is followed by a downward traversal back into vector of common `MemTracker`s. This vector should not be accessed by root mem tracker once any     common mem tracker in it is destroyed.

Prevent vector access from root mem tracker by setting the lambda for downward traversal to null in the destructor of vector.

Test Plan: Jenkins

Reviewers: amitanand, bogdan, sergei

Reviewed By: sergei

Subscribers: ybase

Differential Revision: https://phabricator.dev.yugabyte.com/D8087
  • Loading branch information
rajukumaryb committed Mar 14, 2020
1 parent 9f4448d commit d6fa3f9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/yb/server/server_base.cc
Expand Up @@ -117,7 +117,19 @@ AtomicInt<int32_t> mem_tracker_id_counter(-1);

std::string kServerMemTrackerName = "server";

std::vector<MemTrackerPtr> common_mem_trackers;
struct CommonMemTrackers {
std::vector<MemTrackerPtr> trackers;

~CommonMemTrackers() {
#if defined(TCMALLOC_ENABLED)
// Prevent root mem tracker from accessing common mem trackers.
auto root = MemTracker::GetRootTracker();
root->SetPollChildrenConsumptionFunctors(nullptr);
#endif
}
};

std::unique_ptr<CommonMemTrackers> common_mem_trackers;

} // anonymous namespace

Expand All @@ -132,7 +144,7 @@ std::shared_ptr<MemTracker> CreateMemTrackerForServer() {

#if defined(TCMALLOC_ENABLED)
void RegisterTCMallocTracker(const char* name, const char* prop) {
common_mem_trackers.push_back(MemTracker::CreateTracker(
common_mem_trackers->trackers.push_back(MemTracker::CreateTracker(
-1, "TCMalloc "s + name, std::bind(&MemTracker::GetTCMallocProperty, prop)));
}
#endif
Expand All @@ -155,14 +167,16 @@ RpcServerBase::RpcServerBase(string name, const ServerBaseOptions& options,
// When mem tracker for first server is created we register mem trackers that report tc malloc
// status.
if (mem_tracker_->id() == kServerMemTrackerName) {
common_mem_trackers = std::make_unique<CommonMemTrackers>();

RegisterTCMallocTracker("Thread Cache", "tcmalloc.thread_cache_free_bytes");
RegisterTCMallocTracker("Central Cache", "tcmalloc.central_cache_free_bytes");
RegisterTCMallocTracker("Transfer Cache", "tcmalloc.transfer_cache_free_bytes");
RegisterTCMallocTracker("PageHeap Free", "tcmalloc.pageheap_free_bytes");

auto root = MemTracker::GetRootTracker();
root->SetPollChildrenConsumptionFunctors([]() {
for (auto& tracker : common_mem_trackers) {
for (auto& tracker : common_mem_trackers->trackers) {
tracker->UpdateConsumption();
}
});
Expand Down

0 comments on commit d6fa3f9

Please sign in to comment.