Coverage Report

Created: 2026-04-16 16:35

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