/root/doris/be/src/pipeline/task_queue.h
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 | | #pragma once |
18 | | |
19 | | #include <glog/logging.h> |
20 | | #include <stddef.h> |
21 | | #include <stdint.h> |
22 | | |
23 | | #include <atomic> |
24 | | #include <condition_variable> |
25 | | #include <memory> |
26 | | #include <mutex> |
27 | | #include <ostream> |
28 | | #include <queue> |
29 | | #include <set> |
30 | | |
31 | | #include "common/status.h" |
32 | | #include "pipeline_task.h" |
33 | | #include "runtime/workload_group/workload_group.h" |
34 | | |
35 | | namespace doris { |
36 | | namespace pipeline { |
37 | | |
38 | | class TaskQueue { |
39 | | public: |
40 | 0 | TaskQueue(int core_size) : _core_size(core_size) {} |
41 | | virtual ~TaskQueue(); |
42 | | virtual void close() = 0; |
43 | | // Get the task by core id. |
44 | | // TODO: To think the logic is useful? |
45 | | virtual PipelineTask* take(int core_id) = 0; |
46 | | |
47 | | // push from scheduler |
48 | | virtual Status push_back(PipelineTask* task) = 0; |
49 | | |
50 | | // push from worker |
51 | | virtual Status push_back(PipelineTask* task, int core_id) = 0; |
52 | | |
53 | 0 | virtual void update_statistics(PipelineTask* task, int64_t time_spent) {} |
54 | | |
55 | 0 | int cores() const { return _core_size; } |
56 | | |
57 | | protected: |
58 | | int _core_size; |
59 | | static constexpr auto WAIT_CORE_TASK_TIMEOUT_MS = 100; |
60 | | }; |
61 | | |
62 | | class SubTaskQueue { |
63 | | friend class PriorityTaskQueue; |
64 | | |
65 | | public: |
66 | 0 | void push_back(PipelineTask* task) { _queue.emplace(task); } |
67 | | |
68 | | PipelineTask* try_take(bool is_steal); |
69 | | |
70 | 0 | void set_level_factor(double level_factor) { _level_factor = level_factor; } |
71 | | |
72 | | // note: |
73 | | // runtime is the time consumed by the actual execution of the task |
74 | | // vruntime(means virtual runtime) = runtime / _level_factor |
75 | 0 | double get_vruntime() { return _runtime / _level_factor; } |
76 | | |
77 | 0 | void inc_runtime(uint64_t delta_time) { _runtime += delta_time; } |
78 | | |
79 | 0 | void adjust_runtime(uint64_t vruntime) { this->_runtime = uint64_t(vruntime * _level_factor); } |
80 | | |
81 | 0 | bool empty() { return _queue.empty(); } |
82 | | |
83 | | private: |
84 | | std::queue<PipelineTask*> _queue; |
85 | | // depends on LEVEL_QUEUE_TIME_FACTOR |
86 | | double _level_factor = 1; |
87 | | |
88 | | std::atomic<uint64_t> _runtime = 0; |
89 | | }; |
90 | | |
91 | | // A Multilevel Feedback Queue |
92 | | class PriorityTaskQueue { |
93 | | public: |
94 | | PriorityTaskQueue(); |
95 | | |
96 | | void close(); |
97 | | |
98 | | PipelineTask* try_take(bool is_steal); |
99 | | |
100 | | PipelineTask* take(uint32_t timeout_ms = 0); |
101 | | |
102 | | Status push(PipelineTask* task); |
103 | | |
104 | 0 | void inc_sub_queue_runtime(int level, uint64_t runtime) { |
105 | 0 | _sub_queues[level].inc_runtime(runtime); |
106 | 0 | } |
107 | | |
108 | | private: |
109 | | PipelineTask* _try_take_unprotected(bool is_steal); |
110 | | static constexpr auto LEVEL_QUEUE_TIME_FACTOR = 2; |
111 | | static constexpr size_t SUB_QUEUE_LEVEL = 6; |
112 | | SubTaskQueue _sub_queues[SUB_QUEUE_LEVEL]; |
113 | | // 1s, 3s, 10s, 60s, 300s |
114 | | uint64_t _queue_level_limit[SUB_QUEUE_LEVEL - 1] = {1000000000, 3000000000, 10000000000, |
115 | | 60000000000, 300000000000}; |
116 | | std::mutex _work_size_mutex; |
117 | | std::condition_variable _wait_task; |
118 | | std::atomic<size_t> _total_task_size = 0; |
119 | | bool _closed; |
120 | | |
121 | | // used to adjust vruntime of a queue when it's not empty |
122 | | // protected by lock _work_size_mutex |
123 | | uint64_t _queue_level_min_vruntime = 0; |
124 | | |
125 | | int _compute_level(uint64_t real_runtime); |
126 | | }; |
127 | | |
128 | | // Need consider NUMA architecture |
129 | | class MultiCoreTaskQueue : public TaskQueue { |
130 | | public: |
131 | | explicit MultiCoreTaskQueue(int core_size); |
132 | | |
133 | | ~MultiCoreTaskQueue() override; |
134 | | |
135 | | void close() override; |
136 | | |
137 | | // Get the task by core id. |
138 | | PipelineTask* take(int core_id) override; |
139 | | |
140 | | // TODO combine these methods to `push_back(task, core_id = -1)` |
141 | | Status push_back(PipelineTask* task) override; |
142 | | |
143 | | Status push_back(PipelineTask* task, int core_id) override; |
144 | | |
145 | 0 | void update_statistics(PipelineTask* task, int64_t time_spent) override { |
146 | 0 | task->inc_runtime_ns(time_spent); |
147 | 0 | auto prio_task_queue_list = |
148 | 0 | std::atomic_load_explicit(&_prio_task_queue_list, std::memory_order_relaxed); |
149 | 0 | (*prio_task_queue_list)[task->get_core_id()]->inc_sub_queue_runtime(task->get_queue_level(), |
150 | 0 | time_spent); |
151 | 0 | } |
152 | | |
153 | | private: |
154 | | PipelineTask* _steal_take( |
155 | | int core_id, std::vector<std::unique_ptr<PriorityTaskQueue>>& prio_task_queue_list); |
156 | | |
157 | | std::shared_ptr<std::vector<std::unique_ptr<PriorityTaskQueue>>> _prio_task_queue_list; |
158 | | std::atomic<uint32_t> _next_core = 0; |
159 | | std::atomic<bool> _closed; |
160 | | }; |
161 | | |
162 | | } // namespace pipeline |
163 | | } // namespace doris |