Skip to content

Commit

Permalink
fix thread local2
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Oct 24, 2023
1 parent 3c52039 commit c083917
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion be/src/runtime/memory/mem_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void MemTracker::bind_parent(MemTrackerLimiter* parent) {
if (parent) {
_parent_label = parent->label();
_parent_group_num = parent->group_num();
} else if (thread_context_ptr.init) {
} else if (doris::thread_context_ptr_init) {
_parent_label = thread_context()->thread_mem_tracker()->label();
_parent_group_num = thread_context()->thread_mem_tracker()->group_num();
}
Expand Down
13 changes: 9 additions & 4 deletions be/src/runtime/thread_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ namespace doris {
class MemTracker;

ThreadContextPtr::ThreadContextPtr() {
_ptr = new ThreadContext;
init = true;
if (thread_context_raw == nullptr) {
thread_context_raw = new ThreadContext;
_own_thread_context = true;
}
doris::thread_context_ptr_init = true;
}

ThreadContextPtr::~ThreadContextPtr() {
init = false;
delete _ptr;
doris::thread_context_ptr_init = false;
if (_own_thread_context) {
delete thread_context_raw;
}
}

AttachTask::AttachTask(const std::shared_ptr<MemTrackerLimiter>& mem_tracker,
Expand Down
24 changes: 13 additions & 11 deletions be/src/runtime/thread_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class RuntimeState;
extern bool k_doris_exit;
extern bthread_key_t btls_key;

// TCMalloc hook is triggered during ThreadContext construction, which may lead to deadlock.
inline thread_local bool thread_context_ptr_init = false;
inline thread_local ThreadContext* thread_context_raw = nullptr;

// Using gcc11 compiles thread_local variable on lower versions of GLIBC will report an error,
// see https://github.com/apache/doris/pull/7911
//
Expand Down Expand Up @@ -130,10 +134,8 @@ class ThreadContextPtr {
// Cannot add destructor `~ThreadContextPtr`, otherwise it will no longer be of type POD, the reason is as above.
~ThreadContextPtr();

// TCMalloc hook is triggered during ThreadContext construction, which may lead to deadlock.
bool init = false;

ThreadContext* _ptr;
ThreadContext* ptr() { return thread_context_raw; }
bool _own_thread_context = false;
};

inline thread_local ThreadContextPtr thread_context_ptr;
Expand Down Expand Up @@ -231,13 +233,13 @@ class SwitchBthreadLocal {
// 2. A pthread switch occurs. Because the pthread switch cannot be accurately identified at the moment.
// So tracker call reset 0 like reuses btls.
// during this period, stop the use of thread_context.
thread_context_ptr.init = false;
doris::thread_context_ptr_init = false;
bthread_context = new ThreadContext;
// The brpc server should respond as quickly as possible.
bthread_context->thread_mem_tracker_mgr->disable_wait_gc();
// set the data so that next time bthread_getspecific in the thread returns the data.
CHECK((0 == bthread_setspecific(btls_key, bthread_context)) || doris::k_doris_exit);
thread_context_ptr.init = true;
doris::thread_context_ptr_init = true;
}
bthread_id = bthread_self();
bthread_context->switch_bthread_local_count++;
Expand All @@ -255,7 +257,7 @@ class SwitchBthreadLocal {
}
bthread_context->switch_bthread_local_count--;
if (bthread_context->switch_bthread_local_count == 0) {
bthread_context = thread_context_ptr._ptr;
bthread_context = thread_context_ptr.ptr();
}
}
}
Expand All @@ -273,13 +275,13 @@ static ThreadContext* thread_context() {
// or bthread switch pthread but not call switch_to_bthread_local, use pthread local context
// else, bthread switch pthread and called switch_to_bthread_local, use bthread local context.
if (bthread_context == nullptr) {
bthread_context = thread_context_ptr._ptr;
bthread_context = thread_context_ptr.ptr();
}
}
return bthread_context;
} else {
// in pthread
return thread_context_ptr._ptr;
return thread_context_ptr.ptr();
}
}

Expand Down Expand Up @@ -387,15 +389,15 @@ class AddThreadMemTrackerConsumer {
// which is different from the previous behavior.
#define CONSUME_MEM_TRACKER(size) \
do { \
if (doris::thread_context_ptr.init) { \
if (doris::thread_context_ptr_init) { \
doris::thread_context()->consume_memory(size); \
} else if (doris::ExecEnv::GetInstance()->initialized()) { \
doris::ExecEnv::GetInstance()->orphan_mem_tracker_raw()->consume_no_update_peak(size); \
} \
} while (0)
#define RELEASE_MEM_TRACKER(size) \
do { \
if (doris::thread_context_ptr.init) { \
if (doris::thread_context_ptr_init) { \
doris::thread_context()->consume_memory(-size); \
} else if (doris::ExecEnv::GetInstance()->initialized()) { \
doris::ExecEnv::GetInstance()->orphan_mem_tracker_raw()->consume_no_update_peak( \
Expand Down
6 changes: 6 additions & 0 deletions be/src/util/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "gutil/stringprintf.h"
#include "gutil/strings/substitute.h"
#include "http/web_page_handler.h"
#include "runtime/thread_context.h"
#include "util/debug/sanitizer_scopes.h"
#include "util/easy_json.h"
#include "util/os_util.h"
Expand Down Expand Up @@ -454,6 +455,8 @@ void* Thread::supervise_thread(void* arg) {
// already incremented the reference count in StartThread.
Thread::_tls = t;

doris::thread_context_raw = new ThreadContext();

// Publish our tid to '_tid', which unblocks any callers waiting in
// WaitForTid().
Release_Store(&t->_tid, system_tid);
Expand Down Expand Up @@ -489,6 +492,9 @@ void Thread::finish_thread(void* arg) {
// NOTE: the above 'Release' call could be the last reference to 'this',
// so 'this' could be destructed at this point. Do not add any code
// following here!

doris::thread_context_ptr_init = false;
delete doris::thread_context_raw;
}

void Thread::init_threadmgr() {
Expand Down

0 comments on commit c083917

Please sign in to comment.