Coverage Report

Created: 2026-04-28 15:58

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
8.01M
PipelineTaskSPtr SubTaskQueue::try_take(bool is_steal) {
32
8.01M
    if (_queue.empty()) {
33
0
        return nullptr;
34
0
    }
35
8.01M
    auto task = _queue.front();
36
8.01M
    _queue.pop();
37
8.01M
    return task;
38
8.01M
}
39
40
////////////////////  PriorityTaskQueue ////////////////////
41
42
3.24k
PriorityTaskQueue::PriorityTaskQueue() : _closed(false) {
43
3.24k
    double factor = 1;
44
22.6k
    for (int i = SUB_QUEUE_LEVEL - 1; i >= 0; i--) {
45
19.4k
        _sub_queues[i].set_level_factor(factor);
46
19.4k
        factor *= LEVEL_QUEUE_TIME_FACTOR;
47
19.4k
    }
48
3.24k
}
49
50
1.22k
void PriorityTaskQueue::close() {
51
1.22k
    std::unique_lock<std::mutex> lock(_work_size_mutex);
52
1.22k
    _closed = true;
53
1.22k
    _wait_task.notify_all();
54
1.22k
    DorisMetrics::instance()->pipeline_task_queue_size->increment(-_total_task_size);
55
1.22k
}
56
57
1.67G
PipelineTaskSPtr PriorityTaskQueue::_try_take_unprotected(bool is_steal) {
58
1.67G
    if (_total_task_size == 0 || _closed) {
59
1.67G
        return nullptr;
60
1.67G
    }
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
48.0M
        double cur_queue_vruntime = _sub_queues[i].get_vruntime();
66
48.0M
        if (!_sub_queues[i].empty()) {
67
8.02M
            if (level == -1 || cur_queue_vruntime < min_vruntime) {
68
8.02M
                level = i;
69
8.02M
                min_vruntime = cur_queue_vruntime;
70
8.02M
            }
71
8.02M
        }
72
48.0M
    }
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
8.02M
        task->update_queue_level(level);
79
8.02M
        _total_task_size--;
80
8.02M
        DorisMetrics::instance()->pipeline_task_queue_size->increment(-1);
81
8.02M
    }
82
18.4E
    return task;
83
1.67G
}
84
85
8.02M
int PriorityTaskQueue::_compute_level(uint64_t runtime) {
86
18.4E
    for (int i = 0; i < SUB_QUEUE_LEVEL - 1; ++i) {
87
8.02M
        if (runtime <= _queue_level_limit[i]) {
88
8.02M
            return i;
89
8.02M
        }
90
8.02M
    }
91
18.4E
    return SUB_QUEUE_LEVEL - 1;
92
8.02M
}
93
94
1.64G
PipelineTaskSPtr PriorityTaskQueue::try_take(bool is_steal) {
95
    // TODO other efficient lock? e.g. if get lock fail, return null_ptr
96
1.64G
    std::unique_lock<std::mutex> lock(_work_size_mutex);
97
1.64G
    return _try_take_unprotected(is_steal);
98
1.64G
}
99
100
34.0M
PipelineTaskSPtr PriorityTaskQueue::take(uint32_t timeout_ms) {
101
34.0M
    std::unique_lock<std::mutex> lock(_work_size_mutex);
102
34.0M
    auto task = _try_take_unprotected(false);
103
34.0M
    if (task) {
104
8.70k
        return task;
105
34.0M
    } else {
106
34.0M
        if (timeout_ms > 0) {
107
34.0M
            _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
34.0M
        return _try_take_unprotected(false);
112
34.0M
    }
113
34.0M
}
114
115
8.03M
Status PriorityTaskQueue::push(PipelineTaskSPtr task) {
116
8.03M
    if (_closed) {
117
0
        return Status::InternalError("WorkTaskQueue closed");
118
0
    }
119
8.03M
    auto level = _compute_level(task->get_runtime_ns());
120
8.03M
    std::unique_lock<std::mutex> lock(_work_size_mutex);
121
122
    // update empty queue's  runtime, to avoid too high priority
123
8.03M
    if (_sub_queues[level].empty() &&
124
8.03M
        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
8.03M
    _sub_queues[level].push_back(task);
129
8.03M
    _total_task_size++;
130
8.03M
    DorisMetrics::instance()->pipeline_task_queue_size->increment(1);
131
8.03M
    _wait_task.notify_one();
132
8.03M
    return Status::OK();
133
8.03M
}
134
135
94
MultiCoreTaskQueue::~MultiCoreTaskQueue() = default;
136
137
MultiCoreTaskQueue::MultiCoreTaskQueue(int core_size)
138
160
        : _prio_task_queues(core_size), _closed(false), _core_size(core_size) {}
139
140
68
void MultiCoreTaskQueue::close() {
141
68
    if (_closed) {
142
0
        return;
143
0
    }
144
68
    _closed = true;
145
    // close all priority task queue
146
68
    std::ranges::for_each(_prio_task_queues,
147
1.22k
                          [](auto& prio_task_queue) { prio_task_queue.close(); });
148
68
}
149
150
9.65M
PipelineTaskSPtr MultiCoreTaskQueue::take(int core_id) {
151
9.65M
    PipelineTaskSPtr task = nullptr;
152
40.4M
    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
38.8M
        task = _prio_task_queues[core_id].try_take(false);
157
38.8M
        if (task) {
158
1.48M
            break;
159
1.48M
        }
160
37.3M
        task = _steal_take(core_id);
161
37.3M
        if (task) {
162
3.29M
            break;
163
3.29M
        }
164
34.0M
        task = _prio_task_queues[core_id].take(WAIT_CORE_TASK_TIMEOUT_MS /* timeout_ms */);
165
34.0M
        if (task) {
166
3.24M
            break;
167
3.24M
        }
168
34.0M
    }
169
9.65M
    if (task) {
170
8.03M
        task->pop_out_runnable_queue();
171
8.03M
    }
172
9.65M
    return task;
173
9.65M
}
174
175
37.3M
PipelineTaskSPtr MultiCoreTaskQueue::_steal_take(int core_id) {
176
37.3M
    DCHECK(core_id < _core_size);
177
37.3M
    int next_id = core_id;
178
1.65G
    for (int i = 1; i < _core_size; ++i) {
179
1.62G
        ++next_id;
180
1.62G
        if (next_id == _core_size) {
181
34.2M
            next_id = 0;
182
34.2M
        }
183
1.62G
        DCHECK(next_id < _core_size);
184
1.62G
        auto task = _prio_task_queues[next_id].try_take(true);
185
1.62G
        if (task) {
186
3.29M
            return task;
187
3.29M
        }
188
1.62G
    }
189
34.0M
    return nullptr;
190
37.3M
}
191
192
7.48M
Status MultiCoreTaskQueue::push_back(PipelineTaskSPtr task) {
193
7.48M
    int thread_id = task->get_thread_id(_core_size);
194
7.48M
    if (thread_id < 0) {
195
2.06M
        thread_id = _next_core.fetch_add(1) % _core_size;
196
2.06M
    }
197
7.48M
    return push_back(task, thread_id);
198
7.48M
}
199
200
8.01M
Status MultiCoreTaskQueue::push_back(PipelineTaskSPtr task, int core_id) {
201
8.01M
    DCHECK(core_id < _core_size);
202
8.01M
    task->put_in_runnable_queue();
203
8.01M
    return _prio_task_queues[core_id].push(task);
204
8.01M
}
205
206
7.48M
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
7.48M
    if (auto core_id = task->get_thread_id(_core_size); core_id >= 0) {
210
7.48M
        task->inc_runtime_ns(time_spent);
211
7.48M
        _prio_task_queues[core_id].inc_sub_queue_runtime(task->get_queue_level(), time_spent);
212
7.48M
    }
213
7.48M
}
214
215
} // namespace doris