[enhancement](memtracker) Fix bthread local consume mem tracker (#13368)
Previously, bthread_getspecific was called every time bthread local was used. In the test at #10823, it was found that frequent calls to bthread_getspecific had performance problems. So a cache is implemented on pthread local based on the btls key, but the btls key cannot correctly sense bthread switching. So, based on bthread_self to get the bthread id to implement the cache.
This commit is contained in:
@ -26,7 +26,7 @@ DEFINE_STATIC_THREAD_LOCAL(ThreadContext, ThreadContextPtr, _ptr);
|
||||
|
||||
ThreadContextPtr::ThreadContextPtr() {
|
||||
INIT_STATIC_THREAD_LOCAL(ThreadContext, _ptr);
|
||||
_init = true;
|
||||
init = true;
|
||||
}
|
||||
|
||||
AttachTask::AttachTask(const std::shared_ptr<MemTrackerLimiter>& mem_tracker,
|
||||
@ -70,34 +70,4 @@ AddThreadMemTrackerConsumer::~AddThreadMemTrackerConsumer() {
|
||||
}
|
||||
}
|
||||
|
||||
SwitchBthread::SwitchBthread() {
|
||||
_bthread_context = static_cast<ThreadContext*>(bthread_getspecific(btls_key));
|
||||
// First call to bthread_getspecific (and before any bthread_setspecific) returns NULL
|
||||
if (_bthread_context == nullptr) {
|
||||
// Create thread-local data on demand.
|
||||
_bthread_context = new ThreadContext;
|
||||
// set the data so that next time bthread_getspecific in the thread returns the data.
|
||||
CHECK_EQ(0, bthread_setspecific(btls_key, _bthread_context));
|
||||
} else {
|
||||
DCHECK(_bthread_context->type() == ThreadContext::TaskType::UNKNOWN);
|
||||
_bthread_context->_thread_mem_tracker_mgr->flush_untracked_mem<false>();
|
||||
}
|
||||
_bthread_context->_thread_mem_tracker_mgr->init();
|
||||
_bthread_context->set_type(ThreadContext::TaskType::BRPC);
|
||||
bthread_context_key = btls_key;
|
||||
bthread_context = _bthread_context;
|
||||
}
|
||||
|
||||
SwitchBthread::~SwitchBthread() {
|
||||
DCHECK(_bthread_context != nullptr);
|
||||
_bthread_context->_thread_mem_tracker_mgr->flush_untracked_mem<false>();
|
||||
_bthread_context->_thread_mem_tracker_mgr->init();
|
||||
_bthread_context->set_type(ThreadContext::TaskType::UNKNOWN);
|
||||
bthread_context = nullptr;
|
||||
bthread_context_key = EMPTY_BTLS_KEY;
|
||||
#ifndef NDEBUG
|
||||
DorisMetrics::instance()->switch_bthread_count->increment(1);
|
||||
#endif // NDEBUG
|
||||
}
|
||||
|
||||
} // namespace doris
|
||||
|
||||
Reference in New Issue
Block a user