-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[feature-wip] (memory tracker) (step3) Switch TLS mem tracker to separate more detailed memory usage #8605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature-wip] (memory tracker) (step3) Switch TLS mem tracker to separate more detailed memory usage #8605
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,13 +364,7 @@ void LRUCache::erase(const CacheKey& key, uint32_t hash, MemTracker* tracker) { | |
| } | ||
| // free handle out of mutex, when last_ref is true, e must not be nullptr | ||
| if (last_ref) { | ||
| size_t charge = e->charge; | ||
| e->free(); | ||
| // The parameter tracker is ShardedLRUCache::_mem_tracker, | ||
| // because the memory released by LRUHandle is recorded in the tls mem tracker, | ||
| // so this part of the memory is subsidized from ShardedLRUCache::_mem_tracker to the tls mem tracker | ||
| tracker->transfer_to(thread_local_ctx.get()->_thread_mem_tracker_mgr->mem_tracker().get(), | ||
| charge); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -449,11 +443,15 @@ ShardedLRUCache::ShardedLRUCache(const std::string& name, size_t total_capacity, | |
| : _name(name), | ||
| _last_id(1), | ||
| _mem_tracker(MemTracker::create_tracker(-1, name, nullptr, MemTrackerLevel::OVERVIEW)) { | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
| const size_t per_shard = (total_capacity + (kNumShards - 1)) / kNumShards; | ||
| for (int s = 0; s < kNumShards; s++) { | ||
| _shards[s] = new LRUCache(type); | ||
| _shards[s]->set_capacity(per_shard); | ||
| } | ||
| // After the lru cache is created in the main thread, the main thread will not switch to the | ||
| // lru cache mem tracker again, so manually clear the untracked mem in tls. | ||
| thread_local_ctx.get()->_thread_mem_tracker_mgr->clear_untracked_mems(); | ||
|
|
||
| _entity = DorisMetrics::instance()->metric_registry()->register_entity( | ||
| std::string("lru_cache:") + name, {{"name", name}}); | ||
|
|
@@ -467,6 +465,7 @@ ShardedLRUCache::ShardedLRUCache(const std::string& name, size_t total_capacity, | |
| } | ||
|
|
||
| ShardedLRUCache::~ShardedLRUCache() { | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
| for (int s = 0; s < kNumShards; s++) { | ||
| delete _shards[s]; | ||
| } | ||
|
|
@@ -481,6 +480,7 @@ Cache::Handle* ShardedLRUCache::insert(const CacheKey& key, void* value, size_t | |
| // transfer the memory ownership of the value to ShardedLRUCache::_mem_tracker. | ||
| thread_local_ctx.get()->_thread_mem_tracker_mgr->mem_tracker()->transfer_to(_mem_tracker.get(), | ||
| charge); | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
| const uint32_t hash = _hash_slice(key); | ||
| return _shards[_shard(hash)]->insert(key, hash, value, charge, deleter, priority); | ||
| } | ||
|
|
@@ -491,11 +491,13 @@ Cache::Handle* ShardedLRUCache::lookup(const CacheKey& key) { | |
| } | ||
|
|
||
| void ShardedLRUCache::release(Handle* handle) { | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a metric counter for this SWITCH operation?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I try to add.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix |
||
| LRUHandle* h = reinterpret_cast<LRUHandle*>(handle); | ||
| _shards[_shard(h->hash)]->release(handle); | ||
| } | ||
|
|
||
| void ShardedLRUCache::erase(const CacheKey& key) { | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
| const uint32_t hash = _hash_slice(key); | ||
| _shards[_shard(hash)]->erase(key, hash, _mem_tracker.get()); | ||
| } | ||
|
|
@@ -514,6 +516,7 @@ uint64_t ShardedLRUCache::new_id() { | |
| } | ||
|
|
||
| int64_t ShardedLRUCache::prune() { | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
| int64_t num_prune = 0; | ||
| for (int s = 0; s < kNumShards; s++) { | ||
| num_prune += _shards[s]->prune(); | ||
|
|
@@ -522,6 +525,7 @@ int64_t ShardedLRUCache::prune() { | |
| } | ||
|
|
||
| int64_t ShardedLRUCache::prune_if(CacheValuePredicate pred) { | ||
| SCOPED_SWITCH_THREAD_LOCAL_MEM_TRACKER(_mem_tracker); | ||
| int64_t num_prune = 0; | ||
| for (int s = 0; s < kNumShards; s++) { | ||
| num_prune += _shards[s]->prune_if(pred); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why clear untracked mem here?
you can add some comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the lru cache is created in the main thread, the main thread will not switch the lru cache mem tracker again.
After the non-query thread switches the mem tracker, if the thread will not switch the mem tracker again in the short term, can consider manually clear_untracked_mems.
The query thread will automatically clear_untracked_mems when detach_task.