[feature-wip] (memory tracker) (step5) Fix track bthread, fix track vectorized query (#9145)
1. fix track bthread
- Bthread, a high performance M:N thread library used by brpc. In Doris, a brpc server response runs on one bthread, possibly on multiple pthreads. Currently, MemTracker consumption relies on pthread local variables (TLS).
- This caused pthread TLS MemTracker confusion when switching pthread TLS MemTracker in brpc server response. So replacing pthread TLS with bthread TLS in the brpc server response saves the MemTracker.
Ref: 731730da85/docs/en/server.md (bthread-local)
2. fix track vectorized query
- Added track mmap. Currently, mmap allocates memory in many places of the vectorized execution engine.
- Refactored ThreadContext to avoid dependency conflicts and make it easier to debug.
- Fix some bugs.
This commit is contained in:
@ -17,6 +17,8 @@
|
||||
|
||||
#include "runtime/memory/chunk_allocator.h"
|
||||
|
||||
#include <sanitizer/asan_interface.h>
|
||||
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
|
||||
@ -134,8 +136,7 @@ ChunkAllocator::ChunkAllocator(size_t reserve_limit)
|
||||
|
||||
Status ChunkAllocator::allocate(size_t size, Chunk* chunk, MemTracker* tracker, bool check_limits) {
|
||||
MemTracker* reset_tracker =
|
||||
tracker ? tracker
|
||||
: thread_local_ctx.get()->_thread_mem_tracker_mgr->mem_tracker().get();
|
||||
tracker ? tracker : tls_ctx()->_thread_mem_tracker_mgr->mem_tracker().get();
|
||||
// In advance, transfer the memory ownership of allocate from ChunkAllocator::tracker to the parameter tracker.
|
||||
// Next, if the allocate is successful, it will exit normally;
|
||||
// if the allocate fails, return this part of the memory to the parameter tracker.
|
||||
@ -178,7 +179,7 @@ Status ChunkAllocator::allocate(size_t size, Chunk* chunk, MemTracker* tracker,
|
||||
chunk->data = SystemAllocator::allocate(size);
|
||||
// The allocated chunk is consumed in the tls mem tracker, we want to consume in the ChunkAllocator tracker,
|
||||
// transfer memory ownership. TODO(zxy) replace with switch tls tracker
|
||||
thread_local_ctx.get()->_thread_mem_tracker_mgr->mem_tracker()->transfer_to(_mem_tracker.get(), size);
|
||||
tls_ctx()->_thread_mem_tracker_mgr->mem_tracker()->transfer_to(_mem_tracker.get(), size);
|
||||
}
|
||||
chunk_pool_system_alloc_count->increment(1);
|
||||
chunk_pool_system_alloc_cost_ns->increment(cost_ns);
|
||||
@ -208,9 +209,8 @@ void ChunkAllocator::free(const Chunk& chunk, MemTracker* tracker) {
|
||||
// it was consumed in the parameter tracker, so if the tls mem tracker and the parameter
|
||||
// tracker are different, transfer memory ownership.
|
||||
if (tracker)
|
||||
tracker->transfer_to(
|
||||
thread_local_ctx.get()->_thread_mem_tracker_mgr->mem_tracker().get(),
|
||||
chunk.size);
|
||||
tracker->transfer_to(tls_ctx()->_thread_mem_tracker_mgr->mem_tracker().get(),
|
||||
chunk.size);
|
||||
}
|
||||
chunk_pool_system_free_count->increment(1);
|
||||
chunk_pool_system_free_cost_ns->increment(cost_ns);
|
||||
@ -223,8 +223,8 @@ void ChunkAllocator::free(const Chunk& chunk, MemTracker* tracker) {
|
||||
if (tracker) {
|
||||
tracker->transfer_to(_mem_tracker.get(), chunk.size);
|
||||
} else {
|
||||
thread_local_ctx.get()->_thread_mem_tracker_mgr->mem_tracker()->transfer_to(
|
||||
_mem_tracker.get(), chunk.size);
|
||||
tls_ctx()->_thread_mem_tracker_mgr->mem_tracker()->transfer_to(_mem_tracker.get(),
|
||||
chunk.size);
|
||||
}
|
||||
_arenas[chunk.core_id]->push_free_chunk(chunk.data, chunk.size);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user