/root/doris/be/src/runtime/thread_context.cpp
Line | Count | Source (jump to first uncovered line) |
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/query_context.h" |
22 | | #include "runtime/runtime_state.h" |
23 | | |
24 | | namespace doris { |
25 | | class MemTracker; |
26 | | |
27 | 317 | void AttachTask::init(const std::shared_ptr<ResourceContext>& rc) { |
28 | 317 | ThreadLocalHandle::create_thread_local_if_not_exits(); |
29 | 317 | signal::set_signal_task_id(rc->task_controller()->task_id()); |
30 | 317 | thread_context()->attach_task(rc); |
31 | 317 | } |
32 | | |
33 | 137 | AttachTask::AttachTask(const std::shared_ptr<ResourceContext>& rc) { |
34 | 137 | init(rc); |
35 | 137 | } |
36 | | |
37 | 47 | AttachTask::AttachTask(const std::shared_ptr<MemTrackerLimiter>& mem_tracker) { |
38 | | // if parameter is `orphan_mem_tracker`, if you do not switch thraed mem tracker afterwards, |
39 | | // alloc or free memory from Allocator will fail DCHECK. unless you know for sure that |
40 | | // the thread will not alloc or free memory from Allocator later. |
41 | 47 | std::shared_ptr<ResourceContext> rc = ResourceContext::create_shared(); |
42 | 47 | rc->memory_context()->set_mem_tracker(mem_tracker); |
43 | 47 | init(rc); |
44 | 47 | } |
45 | | |
46 | 133 | AttachTask::AttachTask(RuntimeState* runtime_state) { |
47 | 133 | signal::set_signal_is_nereids(runtime_state->is_nereids()); |
48 | 133 | init(runtime_state->get_query_ctx()->resource_ctx()); |
49 | 133 | } |
50 | | |
51 | 0 | AttachTask::AttachTask(QueryContext* query_ctx) { |
52 | 0 | init(query_ctx->resource_ctx()); |
53 | 0 | } |
54 | | |
55 | 317 | AttachTask::~AttachTask() { |
56 | 317 | signal::set_signal_task_id(TUniqueId()); |
57 | 317 | thread_context()->detach_task(); |
58 | 317 | ThreadLocalHandle::del_thread_local_if_count_is_zero(); |
59 | 317 | } |
60 | | |
61 | 5 | SwitchResourceContext::SwitchResourceContext(const std::shared_ptr<ResourceContext>& rc) { |
62 | 5 | DCHECK(rc != nullptr); |
63 | 5 | doris::ThreadLocalHandle::create_thread_local_if_not_exits(); |
64 | 5 | DCHECK(thread_context()->is_attach_task()); |
65 | 5 | old_resource_ctx_ = thread_context()->resource_ctx(); |
66 | 5 | if (rc != old_resource_ctx_) { |
67 | 4 | signal::set_signal_task_id(rc->task_controller()->task_id()); |
68 | 4 | thread_context()->resource_ctx_ = rc; |
69 | 4 | thread_context()->thread_mem_tracker_mgr->attach_limiter_tracker( |
70 | 4 | rc->memory_context()->mem_tracker(), rc->workload_group()); |
71 | 4 | } |
72 | 5 | } |
73 | | |
74 | 5 | SwitchResourceContext::~SwitchResourceContext() { |
75 | 5 | if (old_resource_ctx_ != thread_context()->resource_ctx()) { |
76 | 4 | DCHECK(old_resource_ctx_ != nullptr); |
77 | 4 | signal::set_signal_task_id(old_resource_ctx_->task_controller()->task_id()); |
78 | 4 | thread_context()->resource_ctx_ = old_resource_ctx_; |
79 | 4 | thread_context()->thread_mem_tracker_mgr->detach_limiter_tracker(); |
80 | 4 | } |
81 | 5 | doris::ThreadLocalHandle::del_thread_local_if_count_is_zero(); |
82 | 5 | } |
83 | | |
84 | | SwitchThreadMemTrackerLimiter::SwitchThreadMemTrackerLimiter( |
85 | 1.40M | const std::shared_ptr<doris::MemTrackerLimiter>& mem_tracker) { |
86 | 1.40M | DCHECK(mem_tracker); |
87 | 1.40M | doris::ThreadLocalHandle::create_thread_local_if_not_exits(); |
88 | 1.40M | if (mem_tracker != thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker_sptr()) { |
89 | 1.33M | thread_context()->thread_mem_tracker_mgr->attach_limiter_tracker(mem_tracker); |
90 | 1.33M | is_switched_ = true; |
91 | 1.33M | } |
92 | 1.40M | } |
93 | | |
94 | 1.40M | SwitchThreadMemTrackerLimiter::~SwitchThreadMemTrackerLimiter() { |
95 | 1.40M | if (is_switched_) { |
96 | 1.33M | thread_context()->thread_mem_tracker_mgr->detach_limiter_tracker(); |
97 | 1.33M | } |
98 | 1.40M | doris::ThreadLocalHandle::del_thread_local_if_count_is_zero(); |
99 | 1.40M | } |
100 | | |
101 | 90 | AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer(MemTracker* mem_tracker) { |
102 | 90 | ThreadLocalHandle::create_thread_local_if_not_exits(); |
103 | 90 | if (mem_tracker) { |
104 | 90 | _need_pop = thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(mem_tracker); |
105 | 90 | } |
106 | 90 | } |
107 | | |
108 | | AddThreadMemTrackerConsumer::AddThreadMemTrackerConsumer( |
109 | | const std::shared_ptr<MemTracker>& mem_tracker) |
110 | 69 | : _mem_tracker(mem_tracker) { |
111 | 69 | ThreadLocalHandle::create_thread_local_if_not_exits(); |
112 | 69 | if (_mem_tracker) { |
113 | 69 | _need_pop = |
114 | 69 | thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(_mem_tracker.get()); |
115 | 69 | } |
116 | 69 | } |
117 | | |
118 | 159 | AddThreadMemTrackerConsumer::~AddThreadMemTrackerConsumer() { |
119 | 159 | if (_need_pop) { |
120 | 158 | thread_context()->thread_mem_tracker_mgr->pop_consumer_tracker(); |
121 | 158 | } |
122 | 159 | ThreadLocalHandle::del_thread_local_if_count_is_zero(); |
123 | 159 | } |
124 | | |
125 | | AddThreadMemTrackerConsumerByHook::AddThreadMemTrackerConsumerByHook( |
126 | | const std::shared_ptr<MemTracker>& mem_tracker) |
127 | 0 | : _mem_tracker(mem_tracker) { |
128 | 0 | ThreadLocalHandle::create_thread_local_if_not_exits(); |
129 | 0 | DCHECK(mem_tracker != nullptr); |
130 | 0 | use_mem_hook = true; |
131 | 0 | thread_context()->thread_mem_tracker_mgr->push_consumer_tracker(_mem_tracker.get()); |
132 | 0 | } |
133 | | |
134 | 0 | AddThreadMemTrackerConsumerByHook::~AddThreadMemTrackerConsumerByHook() { |
135 | 0 | thread_context()->thread_mem_tracker_mgr->pop_consumer_tracker(); |
136 | 0 | use_mem_hook = false; |
137 | 0 | ThreadLocalHandle::del_thread_local_if_count_is_zero(); |
138 | 0 | } |
139 | | |
140 | | } // namespace doris |