be/src/exec/pipeline/task_queue.h
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 | | #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 "exec/pipeline/pipeline_task.h" |
33 | | |
34 | | namespace doris { |
35 | | |
36 | | class SubTaskQueue { |
37 | | friend class PriorityTaskQueue; |
38 | | |
39 | | public: |
40 | 7.78M | void push_back(PipelineTaskSPtr task) { _queue.emplace(task); } |
41 | | |
42 | | PipelineTaskSPtr try_take(bool is_steal); |
43 | | |
44 | 20.3k | void set_level_factor(double level_factor) { _level_factor = level_factor; } |
45 | | |
46 | | // note: |
47 | | // runtime is the time consumed by the actual execution of the task |
48 | | // vruntime(means virtual runtime) = runtime / _level_factor |
49 | 53.2M | double get_vruntime() { return double(_runtime) / _level_factor; } |
50 | | |
51 | 7.18M | void inc_runtime(uint64_t delta_time) { _runtime += delta_time; } |
52 | | |
53 | 0 | void adjust_runtime(uint64_t vruntime) { |
54 | 0 | this->_runtime = uint64_t(double(vruntime) * _level_factor); |
55 | 0 | } |
56 | | |
57 | 54.4M | bool empty() { return _queue.empty(); } |
58 | | |
59 | | private: |
60 | | std::queue<PipelineTaskSPtr> _queue; |
61 | | // depends on LEVEL_QUEUE_TIME_FACTOR |
62 | | double _level_factor = 1; |
63 | | |
64 | | std::atomic<uint64_t> _runtime = 0; |
65 | | }; |
66 | | |
67 | | // A Multilevel Feedback Queue |
68 | | class PriorityTaskQueue { |
69 | | public: |
70 | | PriorityTaskQueue(); |
71 | | |
72 | | void close(); |
73 | | |
74 | | PipelineTaskSPtr try_take(bool is_steal); |
75 | | |
76 | | PipelineTaskSPtr take(uint32_t timeout_ms = 0); |
77 | | |
78 | | Status push(PipelineTaskSPtr task); |
79 | | |
80 | 7.18M | void inc_sub_queue_runtime(int level, uint64_t runtime) { |
81 | 7.18M | _sub_queues[level].inc_runtime(runtime); |
82 | 7.18M | } |
83 | | |
84 | | private: |
85 | | PipelineTaskSPtr _try_take_unprotected(bool is_steal); |
86 | | static constexpr auto LEVEL_QUEUE_TIME_FACTOR = 2; |
87 | | static constexpr size_t SUB_QUEUE_LEVEL = 6; |
88 | | SubTaskQueue _sub_queues[SUB_QUEUE_LEVEL]; |
89 | | // 1s, 3s, 10s, 60s, 300s |
90 | | uint64_t _queue_level_limit[SUB_QUEUE_LEVEL - 1] = {1000000000, 3000000000, 10000000000, |
91 | | 60000000000, 300000000000}; |
92 | | std::mutex _work_size_mutex; |
93 | | std::condition_variable _wait_task; |
94 | | std::atomic<size_t> _total_task_size = 0; |
95 | | bool _closed; |
96 | | |
97 | | // used to adjust vruntime of a queue when it's not empty |
98 | | // protected by lock _work_size_mutex |
99 | | uint64_t _queue_level_min_vruntime = 0; |
100 | | |
101 | | int _compute_level(uint64_t real_runtime); |
102 | | }; |
103 | | |
104 | | // Need consider NUMA architecture |
105 | | class MultiCoreTaskQueue { |
106 | | public: |
107 | | explicit MultiCoreTaskQueue(int core_size); |
108 | | |
109 | | #ifndef BE_TEST |
110 | | ~MultiCoreTaskQueue(); |
111 | | // Get the task by core id. |
112 | | PipelineTaskSPtr take(int core_id); |
113 | | #else |
114 | | virtual ~MultiCoreTaskQueue(); |
115 | | virtual PipelineTaskSPtr take(int core_id); |
116 | | #endif |
117 | | void close(); |
118 | | |
119 | | // TODO combine these methods to `push_back(task, core_id = -1)` |
120 | | Status push_back(PipelineTaskSPtr task); |
121 | | |
122 | | Status push_back(PipelineTaskSPtr task, int core_id); |
123 | | |
124 | | void update_statistics(PipelineTask* task, int64_t time_spent); |
125 | | |
126 | 0 | int cores() const { return _core_size; } |
127 | | |
128 | | private: |
129 | | PipelineTaskSPtr _steal_take(int core_id); |
130 | | |
131 | | std::vector<PriorityTaskQueue> _prio_task_queues; |
132 | | std::atomic<uint32_t> _next_core = 0; |
133 | | std::atomic<bool> _closed; |
134 | | |
135 | | int _core_size; |
136 | | static constexpr auto WAIT_CORE_TASK_TIMEOUT_MS = 100; |
137 | | }; |
138 | | } // namespace doris |