Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Aug 6, 2024
1 parent 2ae2a13 commit fa6dd8f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions be/src/runtime/thread_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ class ThreadContext {
ss << std::this_thread::get_id();
return ss.str();
}
// After thread_mem_tracker_mgr is initialized, the current thread Hook starts to
// consume/release mem_tracker.
// Note that the use of shared_ptr will cause a crash. The guess is that there is an
// Note that if set global Memory Hook, After thread_mem_tracker_mgr is initialized,
// the current thread Hook starts to consume/release mem_tracker.
// the use of shared_ptr will cause a crash. The guess is that there is an
// intermediate state during the copy construction of shared_ptr. Shared_ptr is not equal
// to nullptr, but the object it points to is not initialized. At this time, when the memory
// is released somewhere, the hook is triggered to cause the crash.
Expand Down
13 changes: 11 additions & 2 deletions be/src/vec/common/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,23 @@ void Allocator<clear_memory_, mmap_populate, use_mmap, MemoryAllocator>::memory_

template <bool clear_memory_, bool mmap_populate, bool use_mmap, typename MemoryAllocator>
void Allocator<clear_memory_, mmap_populate, use_mmap, MemoryAllocator>::consume_memory(
size_t size) const {
size_t size) {
if (_tracker == nullptr) { // first time called
_tracker = doris::thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker();
}
CONSUME_THREAD_MEM_TRACKER(size);
}

template <bool clear_memory_, bool mmap_populate, bool use_mmap, typename MemoryAllocator>
void Allocator<clear_memory_, mmap_populate, use_mmap, MemoryAllocator>::release_memory(
size_t size) const {
RELEASE_THREAD_MEM_TRACKER(size);
doris::ThreadContext* thread_context = doris::thread_context(true);
DCHECK(_tracker);
if (thread_context && thread_context->thread_mem_tracker()->label() == _tracker->label()) {
RELEASE_THREAD_MEM_TRACKER(size);
} else {
_tracker->release(size);
}
}

template <bool clear_memory_, bool mmap_populate, bool use_mmap, typename MemoryAllocator>
Expand Down
8 changes: 7 additions & 1 deletion be/src/vec/common/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ class RecordSizeMemoryAllocator {
static std::mutex _mutex;
};

namespace doris {
class MemTrackerLimiter;
}

/** Responsible for allocating / freeing memory. Used, for example, in PODArray, Arena.
* Also used in hash tables.
* The interface is different from std::allocator
Expand All @@ -228,7 +232,7 @@ class Allocator {
// alloc will continue to execute, so the consume memtracker is forced.
void memory_check(size_t size) const;
// Increases consumption of this tracker by 'bytes'.
void consume_memory(size_t size) const;
void consume_memory(size_t size);
void release_memory(size_t size) const;
void throw_bad_alloc(const std::string& err) const;
#ifndef NDEBUG
Expand Down Expand Up @@ -412,6 +416,8 @@ class Allocator {
| (mmap_populate ? MAP_POPULATE : 0)
#endif
;

std::shared_ptr<doris::MemTrackerLimiter> _tracker {nullptr};
};

/** Allocator with optimization to place small memory ranges in automatic memory.
Expand Down

0 comments on commit fa6dd8f

Please sign in to comment.