Coverage Report

Created: 2025-03-10 18:45

/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
PipelineTask* 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
3
PriorityTaskQueue::PriorityTaskQueue() : _closed(false) {
44
3
    double factor = 1;
45
21
    for (int i = SUB_QUEUE_LEVEL - 1; i >= 0; i--) {
46
18
        _sub_queues[i].set_level_factor(factor);
47
18
        factor *= LEVEL_QUEUE_TIME_FACTOR;
48
18
    }
49
3
}
50
51
0
void PriorityTaskQueue::close() {
52
0
    std::unique_lock<std::mutex> lock(_work_size_mutex);
53
0
    _closed = true;
54
0
    _wait_task.notify_all();
55
0
}
56
57
13
PipelineTask* PriorityTaskQueue::_try_take_unprotected(bool is_steal) {
58
13
    if (_total_task_size == 0 || _closed) {
59
3
        return nullptr;
60
3
    }
61
62
10
    double min_vruntime = 0;
63
10
    int level = -1;
64
70
    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
10
    DCHECK(level != -1);
74
10
    _queue_level_min_vruntime = uint64_t(min_vruntime);
75
76
10
    auto task = _sub_queues[level].try_take(is_steal);
77
10
    if (task) {
78
10
        task->update_queue_level(level);
79
10
        _total_task_size--;
80
10
    }
81
10
    return task;
82
13
}
83
84
17
int PriorityTaskQueue::_compute_level(uint64_t runtime) {
85
17
    for (int i = 0; i < SUB_QUEUE_LEVEL - 1; ++i) {
86
17
        if (runtime <= _queue_level_limit[i]) {
87
17
            return i;
88
17
        }
89
17
    }
90
0
    return SUB_QUEUE_LEVEL - 1;
91
17
}
92
93
11
PipelineTask* PriorityTaskQueue::try_take(bool is_steal) {
94
    // TODO other efficient lock? e.g. if get lock fail, return null_ptr
95
11
    std::unique_lock<std::mutex> lock(_work_size_mutex);
96
11
    return _try_take_unprotected(is_steal);
97
11
}
98
99
1
PipelineTask* PriorityTaskQueue::take(uint32_t timeout_ms) {
100
1
    std::unique_lock<std::mutex> lock(_work_size_mutex);
101
1
    auto task = _try_take_unprotected(false);
102
1
    if (task) {
103
0
        return task;
104
1
    } else {
105
1
        if (timeout_ms > 0) {
106
1
            _wait_task.wait_for(lock, std::chrono::milliseconds(timeout_ms));
107
1
        } else {
108
0
            _wait_task.wait(lock);
109
0
        }
110
1
        return _try_take_unprotected(false);
111
1
    }
112
1
}
113
114
17
Status PriorityTaskQueue::push(PipelineTask* task) {
115
17
    if (_closed) {
116
0
        return Status::InternalError("WorkTaskQueue closed");
117
0
    }
118
17
    auto level = _compute_level(task->get_runtime_ns());
119
17
    std::unique_lock<std::mutex> lock(_work_size_mutex);
120
121
    // update empty queue's  runtime, to avoid too high priority
122
17
    if (_sub_queues[level].empty() &&
123
17
        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
17
    _sub_queues[level].push_back(task);
128
17
    _total_task_size++;
129
17
    _wait_task.notify_one();
130
17
    return Status::OK();
131
17
}
132
133
3
MultiCoreTaskQueue::~MultiCoreTaskQueue() = default;
134
135
MultiCoreTaskQueue::MultiCoreTaskQueue(int core_size)
136
3
        : _prio_task_queues(core_size), _closed(false), _core_size(core_size) {}
137
138
0
void MultiCoreTaskQueue::close() {
139
0
    if (_closed) {
140
0
        return;
141
0
    }
142
0
    _closed = true;
143
    // close all priority task queue
144
0
    std::ranges::for_each(_prio_task_queues,
145
0
                          [](auto& prio_task_queue) { prio_task_queue.close(); });
146
0
}
147
148
0
PipelineTask* MultiCoreTaskQueue::take(int core_id) {
149
0
    PipelineTask* task = nullptr;
150
0
    while (!_closed) {
151
0
        DCHECK(_prio_task_queues.size() > core_id)
152
0
                << " list size: " << _prio_task_queues.size() << " core_id: " << core_id
153
0
                << " _core_size: " << _core_size << " _next_core: " << _next_core.load();
154
0
        task = _prio_task_queues[core_id].try_take(false);
155
0
        if (task) {
156
0
            break;
157
0
        }
158
0
        task = _steal_take(core_id);
159
0
        if (task) {
160
0
            break;
161
0
        }
162
0
        task = _prio_task_queues[core_id].take(WAIT_CORE_TASK_TIMEOUT_MS /* timeout_ms */);
163
0
        if (task) {
164
0
            break;
165
0
        }
166
0
    }
167
0
    if (task) {
168
0
        task->pop_out_runnable_queue();
169
0
    }
170
0
    return task;
171
0
}
172
173
1
PipelineTask* MultiCoreTaskQueue::_steal_take(int core_id) {
174
1
    DCHECK(core_id < _core_size);
175
1
    int next_id = core_id;
176
1
    for (int i = 1; i < _core_size; ++i) {
177
0
        ++next_id;
178
0
        if (next_id == _core_size) {
179
0
            next_id = 0;
180
0
        }
181
0
        DCHECK(next_id < _core_size);
182
0
        auto task = _prio_task_queues[next_id].try_take(true);
183
0
        if (task) {
184
0
            return task;
185
0
        }
186
0
    }
187
1
    return nullptr;
188
1
}
189
190
17
Status MultiCoreTaskQueue::push_back(PipelineTask* task) {
191
17
    int core_id = task->get_core_id();
192
17
    if (core_id < 0) {
193
17
        core_id = _next_core.fetch_add(1) % _core_size;
194
17
    }
195
17
    return push_back(task, core_id);
196
17
}
197
198
17
Status MultiCoreTaskQueue::push_back(PipelineTask* task, int core_id) {
199
17
    DCHECK(core_id < _core_size);
200
17
    task->put_in_runnable_queue();
201
17
    return _prio_task_queues[core_id].push(task);
202
17
}
203
204
27
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
27
    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
27
}
212
213
} // namespace doris::pipeline