Coverage Report

Created: 2025-04-22 18:57

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