[feature] (memory) Switch TLS mem tracker to separate more detailed memory usage (#8605)
In pr #8476, all memory usage of a process is recorded in the process mem tracker, and all memory usage of a query is recorded in the query mem tracker, and it is still necessary to manually call `transfer to` to track the cached memory size. We hope to separate out more detailed memory usage based on Hook TCMalloc new/delete + TLS mem tracker. In this pr, the more detailed mem tracker is switched to TLS, which automatically and accurately counts more detailed memory usage than before.
This commit is contained in:
@ -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);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user