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