Coverage Report

Created: 2026-04-15 15:52

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