/root/doris/be/src/pipeline/task_queue.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 "task_queue.h" |
19 | | |
20 | | // IWYU pragma: no_include <bits/chrono.h> |
21 | | #include <chrono> // IWYU pragma: keep |
22 | | #include <string> |
23 | | |
24 | | #include "common/logging.h" |
25 | | #include "pipeline/pipeline_task.h" |
26 | | |
27 | | namespace doris { |
28 | | namespace pipeline { |
29 | | |
30 | 0 | TaskQueue::~TaskQueue() = default; |
31 | | |
32 | 0 | PipelineTask* SubTaskQueue::try_take(bool is_steal) { |
33 | 0 | if (_queue.empty()) { |
34 | 0 | return nullptr; |
35 | 0 | } |
36 | 0 | auto task = _queue.front(); |
37 | 0 | _queue.pop(); |
38 | 0 | return task; |
39 | 0 | } |
40 | | |
41 | | //////////////////// PriorityTaskQueue //////////////////// |
42 | | |
43 | 0 | PriorityTaskQueue::PriorityTaskQueue() : _closed(false) { |
44 | 0 | double factor = 1; |
45 | 0 | for (int i = SUB_QUEUE_LEVEL - 1; i >= 0; i--) { |
46 | 0 | _sub_queues[i].set_level_factor(factor); |
47 | 0 | factor *= LEVEL_QUEUE_TIME_FACTOR; |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 0 | void PriorityTaskQueue::close() { |
52 | 0 | std::unique_lock<std::mutex> lock(_work_size_mutex); |
53 | 0 | _closed = true; |
54 | 0 | _wait_task.notify_all(); |
55 | 0 | } |
56 | | |
57 | 0 | PipelineTask* PriorityTaskQueue::_try_take_unprotected(bool is_steal) { |
58 | 0 | if (_total_task_size == 0 || _closed) { |
59 | 0 | return nullptr; |
60 | 0 | } |
61 | | |
62 | 0 | double min_vruntime = 0; |
63 | 0 | int level = -1; |
64 | 0 | for (int i = 0; i < SUB_QUEUE_LEVEL; ++i) { |
65 | 0 | double cur_queue_vruntime = _sub_queues[i].get_vruntime(); |
66 | 0 | if (!_sub_queues[i].empty()) { |
67 | 0 | if (level == -1 || cur_queue_vruntime < min_vruntime) { |
68 | 0 | level = i; |
69 | 0 | min_vruntime = cur_queue_vruntime; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | 0 | DCHECK(level != -1); |
74 | 0 | _queue_level_min_vruntime = uint64_t(min_vruntime); |
75 | |
|
76 | 0 | auto task = _sub_queues[level].try_take(is_steal); |
77 | 0 | if (task) { |
78 | 0 | task->update_queue_level(level); |
79 | 0 | _total_task_size--; |
80 | 0 | } |
81 | 0 | return task; |
82 | 0 | } |
83 | | |
84 | 0 | int PriorityTaskQueue::_compute_level(uint64_t runtime) { |
85 | 0 | for (int i = 0; i < SUB_QUEUE_LEVEL - 1; ++i) { |
86 | 0 | if (runtime <= _queue_level_limit[i]) { |
87 | 0 | return i; |
88 | 0 | } |
89 | 0 | } |
90 | 0 | return SUB_QUEUE_LEVEL - 1; |
91 | 0 | } |
92 | | |
93 | 0 | PipelineTask* PriorityTaskQueue::try_take(bool is_steal) { |
94 | | // TODO other efficient lock? e.g. if get lock fail, return null_ptr |
95 | 0 | std::unique_lock<std::mutex> lock(_work_size_mutex); |
96 | 0 | return _try_take_unprotected(is_steal); |
97 | 0 | } |
98 | | |
99 | 0 | PipelineTask* PriorityTaskQueue::take(uint32_t timeout_ms) { |
100 | 0 | std::unique_lock<std::mutex> lock(_work_size_mutex); |
101 | 0 | auto task = _try_take_unprotected(false); |
102 | 0 | if (task) { |
103 | 0 | return task; |
104 | 0 | } else { |
105 | 0 | if (timeout_ms > 0) { |
106 | 0 | _wait_task.wait_for(lock, std::chrono::milliseconds(timeout_ms)); |
107 | 0 | } else { |
108 | 0 | _wait_task.wait(lock); |
109 | 0 | } |
110 | 0 | return _try_take_unprotected(false); |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | 0 | Status PriorityTaskQueue::push(PipelineTask* task) { |
115 | 0 | if (_closed) { |
116 | 0 | return Status::InternalError("WorkTaskQueue closed"); |
117 | 0 | } |
118 | 0 | auto level = _compute_level(task->get_runtime_ns()); |
119 | 0 | std::unique_lock<std::mutex> lock(_work_size_mutex); |
120 | | |
121 | | // update empty queue's runtime, to avoid too high priority |
122 | 0 | if (_sub_queues[level].empty() && |
123 | 0 | _queue_level_min_vruntime > _sub_queues[level].get_vruntime()) { |
124 | 0 | _sub_queues[level].adjust_runtime(_queue_level_min_vruntime); |
125 | 0 | } |
126 | |
|
127 | 0 | _sub_queues[level].push_back(task); |
128 | 0 | _total_task_size++; |
129 | 0 | _wait_task.notify_one(); |
130 | 0 | return Status::OK(); |
131 | 0 | } |
132 | | |
133 | 0 | MultiCoreTaskQueue::~MultiCoreTaskQueue() = default; |
134 | | |
135 | 0 | MultiCoreTaskQueue::MultiCoreTaskQueue(int core_size) : TaskQueue(core_size), _closed(false) { |
136 | 0 | _prio_task_queue_list = |
137 | 0 | std::make_shared<std::vector<std::unique_ptr<PriorityTaskQueue>>>(core_size); |
138 | 0 | for (int i = 0; i < core_size; i++) { |
139 | 0 | (*_prio_task_queue_list)[i] = std::make_unique<PriorityTaskQueue>(); |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | 0 | void MultiCoreTaskQueue::close() { |
144 | 0 | if (_closed) { |
145 | 0 | return; |
146 | 0 | } |
147 | 0 | _closed = true; |
148 | 0 | for (int i = 0; i < _core_size; ++i) { |
149 | 0 | (*_prio_task_queue_list)[i]->close(); |
150 | 0 | } |
151 | 0 | std::atomic_store(&_prio_task_queue_list, |
152 | 0 | std::shared_ptr<std::vector<std::unique_ptr<PriorityTaskQueue>>>(nullptr)); |
153 | 0 | } |
154 | | |
155 | 0 | PipelineTask* MultiCoreTaskQueue::take(int core_id) { |
156 | 0 | PipelineTask* task = nullptr; |
157 | 0 | auto prio_task_queue_list = |
158 | 0 | std::atomic_load_explicit(&_prio_task_queue_list, std::memory_order_relaxed); |
159 | 0 | while (!_closed) { |
160 | 0 | DCHECK(prio_task_queue_list->size() > core_id) |
161 | 0 | << " list size: " << prio_task_queue_list->size() << " core_id: " << core_id |
162 | 0 | << " _core_size: " << _core_size << " _next_core: " << _next_core.load(); |
163 | 0 | task = (*prio_task_queue_list)[core_id]->try_take(false); |
164 | 0 | if (task) { |
165 | 0 | task->set_core_id(core_id); |
166 | 0 | break; |
167 | 0 | } |
168 | 0 | task = _steal_take(core_id, *prio_task_queue_list); |
169 | 0 | if (task) { |
170 | 0 | break; |
171 | 0 | } |
172 | 0 | task = (*prio_task_queue_list)[core_id]->take(WAIT_CORE_TASK_TIMEOUT_MS /* timeout_ms */); |
173 | 0 | if (task) { |
174 | 0 | task->set_core_id(core_id); |
175 | 0 | break; |
176 | 0 | } |
177 | 0 | } |
178 | 0 | if (task) { |
179 | 0 | task->pop_out_runnable_queue(); |
180 | 0 | } |
181 | 0 | return task; |
182 | 0 | } |
183 | | |
184 | | PipelineTask* MultiCoreTaskQueue::_steal_take( |
185 | 0 | int core_id, std::vector<std::unique_ptr<PriorityTaskQueue>>& prio_task_queue_list) { |
186 | 0 | DCHECK(core_id < _core_size); |
187 | 0 | int next_id = core_id; |
188 | 0 | for (int i = 1; i < _core_size; ++i) { |
189 | 0 | ++next_id; |
190 | 0 | if (next_id == _core_size) { |
191 | 0 | next_id = 0; |
192 | 0 | } |
193 | 0 | DCHECK(next_id < _core_size); |
194 | 0 | auto task = prio_task_queue_list[next_id]->try_take(true); |
195 | 0 | if (task) { |
196 | 0 | task->set_core_id(next_id); |
197 | 0 | return task; |
198 | 0 | } |
199 | 0 | } |
200 | 0 | return nullptr; |
201 | 0 | } |
202 | | |
203 | 0 | Status MultiCoreTaskQueue::push_back(PipelineTask* task) { |
204 | 0 | int core_id = task->get_previous_core_id(); |
205 | 0 | if (core_id < 0) { |
206 | 0 | core_id = _next_core.fetch_add(1) % _core_size; |
207 | 0 | } |
208 | 0 | return push_back(task, core_id); |
209 | 0 | } |
210 | | |
211 | 0 | Status MultiCoreTaskQueue::push_back(PipelineTask* task, int core_id) { |
212 | 0 | DCHECK(core_id < _core_size); |
213 | 0 | task->put_in_runnable_queue(); |
214 | 0 | auto prio_task_queue_list = |
215 | 0 | std::atomic_load_explicit(&_prio_task_queue_list, std::memory_order_relaxed); |
216 | 0 | return (*prio_task_queue_list)[core_id]->push(task); |
217 | 0 | } |
218 | | |
219 | | } // namespace pipeline |
220 | | } // namespace doris |