Coverage Report

Created: 2024-11-21 18:14

/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
0
PipelineTask* SubTaskQueue::try_take(bool is_steal) {
33
0
    if (_queue.empty()) {
34
0
        return nullptr;
35
0
    }
36
0
    auto task = _queue.front();
37
0
    _queue.pop();
38
0
    return task;
39
0
}
40
41
////////////////////  PriorityTaskQueue ////////////////////
42
43
0
PriorityTaskQueue::PriorityTaskQueue() : _closed(false) {
44
0
    double factor = 1;
45
0
    for (int i = SUB_QUEUE_LEVEL - 1; i >= 0; i--) {
46
0
        _sub_queues[i].set_level_factor(factor);
47
0
        factor *= LEVEL_QUEUE_TIME_FACTOR;
48
0
    }
49
0
}
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
0
PipelineTask* PriorityTaskQueue::_try_take_unprotected(bool is_steal) {
58
0
    if (_total_task_size == 0 || _closed) {
59
0
        return nullptr;
60
0
    }
61
62
0
    double min_vruntime = 0;
63
0
    int level = -1;
64
0
    for (int i = 0; i < SUB_QUEUE_LEVEL; ++i) {
65
0
        double cur_queue_vruntime = _sub_queues[i].get_vruntime();
66
0
        if (!_sub_queues[i].empty()) {
67
0
            if (level == -1 || cur_queue_vruntime < min_vruntime) {
68
0
                level = i;
69
0
                min_vruntime = cur_queue_vruntime;
70
0
            }
71
0
        }
72
0
    }
73
0
    DCHECK(level != -1);
74
0
    _queue_level_min_vruntime = uint64_t(min_vruntime);
75
76
0
    auto task = _sub_queues[level].try_take(is_steal);
77
0
    if (task) {
78
0
        task->update_queue_level(level);
79
0
        _total_task_size--;
80
0
    }
81
0
    return task;
82
0
}
83
84
0
int PriorityTaskQueue::_compute_level(uint64_t runtime) {
85
0
    for (int i = 0; i < SUB_QUEUE_LEVEL - 1; ++i) {
86
0
        if (runtime <= _queue_level_limit[i]) {
87
0
            return i;
88
0
        }
89
0
    }
90
0
    return SUB_QUEUE_LEVEL - 1;
91
0
}
92
93
0
PipelineTask* PriorityTaskQueue::try_take(bool is_steal) {
94
    // TODO other efficient lock? e.g. if get lock fail, return null_ptr
95
0
    std::unique_lock<std::mutex> lock(_work_size_mutex);
96
0
    return _try_take_unprotected(is_steal);
97
0
}
98
99
0
PipelineTask* PriorityTaskQueue::take(uint32_t timeout_ms) {
100
0
    std::unique_lock<std::mutex> lock(_work_size_mutex);
101
0
    auto task = _try_take_unprotected(false);
102
0
    if (task) {
103
0
        return task;
104
0
    } else {
105
0
        if (timeout_ms > 0) {
106
0
            _wait_task.wait_for(lock, std::chrono::milliseconds(timeout_ms));
107
0
        } else {
108
0
            _wait_task.wait(lock);
109
0
        }
110
0
        return _try_take_unprotected(false);
111
0
    }
112
0
}
113
114
0
Status PriorityTaskQueue::push(PipelineTask* task) {
115
0
    if (_closed) {
116
0
        return Status::InternalError("WorkTaskQueue closed");
117
0
    }
118
0
    auto level = _compute_level(task->get_runtime_ns());
119
0
    std::unique_lock<std::mutex> lock(_work_size_mutex);
120
121
    // update empty queue's  runtime, to avoid too high priority
122
0
    if (_sub_queues[level].empty() &&
123
0
        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
0
    _sub_queues[level].push_back(task);
128
0
    _total_task_size++;
129
0
    _wait_task.notify_one();
130
0
    return Status::OK();
131
0
}
132
133
0
MultiCoreTaskQueue::~MultiCoreTaskQueue() = default;
134
135
MultiCoreTaskQueue::MultiCoreTaskQueue(int core_size)
136
0
        : _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
            task->set_core_id(core_id);
157
0
            break;
158
0
        }
159
0
        task = _steal_take(core_id);
160
0
        if (task) {
161
0
            break;
162
0
        }
163
0
        task = _prio_task_queues[core_id].take(WAIT_CORE_TASK_TIMEOUT_MS /* timeout_ms */);
164
0
        if (task) {
165
0
            task->set_core_id(core_id);
166
0
            break;
167
0
        }
168
0
    }
169
0
    if (task) {
170
0
        task->pop_out_runnable_queue();
171
0
    }
172
0
    return task;
173
0
}
174
175
0
PipelineTask* MultiCoreTaskQueue::_steal_take(int core_id) {
176
0
    DCHECK(core_id < _core_size);
177
0
    int next_id = core_id;
178
0
    for (int i = 1; i < _core_size; ++i) {
179
0
        ++next_id;
180
0
        if (next_id == _core_size) {
181
0
            next_id = 0;
182
0
        }
183
0
        DCHECK(next_id < _core_size);
184
0
        auto task = _prio_task_queues[next_id].try_take(true);
185
0
        if (task) {
186
0
            task->set_core_id(next_id);
187
0
            return task;
188
0
        }
189
0
    }
190
0
    return nullptr;
191
0
}
192
193
0
Status MultiCoreTaskQueue::push_back(PipelineTask* task) {
194
0
    int core_id = task->get_previous_core_id();
195
0
    if (core_id < 0) {
196
0
        core_id = _next_core.fetch_add(1) % _core_size;
197
0
    }
198
0
    return push_back(task, core_id);
199
0
}
200
201
0
Status MultiCoreTaskQueue::push_back(PipelineTask* task, int core_id) {
202
0
    DCHECK(core_id < _core_size);
203
0
    task->put_in_runnable_queue();
204
0
    return _prio_task_queues[core_id].push(task);
205
0
}
206
207
0
void MultiCoreTaskQueue::update_statistics(PipelineTask* task, int64_t time_spent) {
208
0
    task->inc_runtime_ns(time_spent);
209
0
    _prio_task_queues[task->get_core_id()].inc_sub_queue_runtime(task->get_queue_level(),
210
0
                                                                 time_spent);
211
0
}
212
213
} // namespace doris::pipeline