Coverage Report

Created: 2024-11-21 11:46

/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
34
namespace doris::pipeline {
35
#include "common/compile_check_begin.h"
36
37
class SubTaskQueue {
38
    friend class PriorityTaskQueue;
39
40
public:
41
0
    void push_back(PipelineTask* task) { _queue.emplace(task); }
42
43
    PipelineTask* try_take(bool is_steal);
44
45
0
    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
0
    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
0
    bool empty() { return _queue.empty(); }
59
60
private:
61
    std::queue<PipelineTask*> _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
    PipelineTask* try_take(bool is_steal);
76
77
    PipelineTask* take(uint32_t timeout_ms = 0);
78
79
    Status push(PipelineTask* 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
    PipelineTask* _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
    ~MultiCoreTaskQueue();
111
112
    void close();
113
114
    // Get the task by core id.
115
    PipelineTask* take(int core_id);
116
117
    // TODO combine these methods to `push_back(task, core_id = -1)`
118
    Status push_back(PipelineTask* task);
119
120
    Status push_back(PipelineTask* task, int core_id);
121
122
    void update_statistics(PipelineTask* task, int64_t time_spent);
123
124
0
    int cores() const { return _core_size; }
125
126
private:
127
    PipelineTask* _steal_take(int core_id);
128
129
    std::vector<PriorityTaskQueue> _prio_task_queues;
130
    std::atomic<uint32_t> _next_core = 0;
131
    std::atomic<bool> _closed;
132
133
    int _core_size;
134
    static constexpr auto WAIT_CORE_TASK_TIMEOUT_MS = 100;
135
};
136
#include "common/compile_check_end.h"
137
} // namespace doris::pipeline