/root/doris/be/src/runtime/thread_context.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "runtime/thread_context.h" |
19 | | |
20 | | #include "common/signal_handler.h" |
21 | | #include "runtime/runtime_state.h" |
22 | | |
23 | | namespace doris { |
24 | | class MemTracker; |
25 | | |
26 | | DEFINE_STATIC_THREAD_LOCAL(ThreadContext, ThreadContextPtr, _ptr); |
27 | | |
28 | 55 | ThreadContextPtr::ThreadContextPtr() { |
29 | 55 | INIT_STATIC_THREAD_LOCAL(ThreadContext, _ptr); |
30 | 55 | init = true; |
31 | 55 | } |
32 | | |
33 | | AttachTask::AttachTask(const std::shared_ptr<MemTrackerLimiter>& mem_tracker, |
34 | 1 | const TUniqueId& task_id, const TUniqueId& fragment_instance_id) { |
35 | 1 | SwitchBthreadLocal::switch_to_bthread_local(); |
36 | 1 | signal::set_signal_task_id(task_id); |
37 | 1 | thread_context()->attach_task(task_id, fragment_instance_id, mem_tracker); |
38 | 1 | } |
39 | | |
40 | 25 | AttachTask::AttachTask(RuntimeState* runtime_state) { |
41 | 25 | SwitchBthreadLocal::switch_to_bthread_local(); |
42 | 25 | signal::set_signal_task_id(runtime_state->query_id()); |
43 | 25 | signal::set_signal_is_nereids(runtime_state->is_nereids()); |
44 | 25 | thread_context()->attach_task(runtime_state->query_id(), runtime_state->fragment_instance_id(), |
45 | 25 | runtime_state->query_mem_tracker()); |
46 | 25 | } |
47 | | |
48 | 26 | AttachTask::~AttachTask() { |
49 | 26 | thread_context()->detach_task(); |
50 | 26 | SwitchBthreadLocal::switch_back_pthread_local(); |
51 | 26 | } |
52 | | |
53 | 128 | AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer(MemTracker* mem_tracker) { |
54 | 128 | SwitchBthreadLocal::switch_to_bthread_local(); |
55 | 128 | if (mem_tracker) { |
56 | 128 | _need_pop = thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(mem_tracker); |
57 | 128 | } |
58 | 128 | } |
59 | | |
60 | | AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer( |
61 | | const std::shared_ptr<MemTracker>& mem_tracker) |
62 | 96 | : _mem_tracker(mem_tracker) { |
63 | 96 | SwitchBthreadLocal::switch_to_bthread_local(); |
64 | 96 | if (_mem_tracker) { |
65 | 96 | _need_pop = |
66 | 96 | thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(_mem_tracker.get()); |
67 | 96 | } |
68 | 96 | } |
69 | | |
70 | 224 | AddThreadMemTrackerConsumer::~AddThreadMemTrackerConsumer() { |
71 | 224 | if (_need_pop) { |
72 | 222 | thread_context()->thread_mem_tracker_mgr->pop_consumer_tracker(); |
73 | 222 | } |
74 | 224 | SwitchBthreadLocal::switch_back_pthread_local(); |
75 | 224 | } |
76 | | |
77 | | } // namespace doris |