Coverage Report

Created: 2026-03-15 17:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/runtime/workload_management/io_throttle.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 "runtime/workload_management/io_throttle.h"
19
20
#include "util/defer_op.h"
21
#include "util/time.h"
22
23
namespace doris {
24
25
0
bool IOThrottle::acquire(int64_t block_timeout_ms) {
26
0
    if (_io_bytes_per_second_limit < 0) {
27
0
        return true;
28
0
    }
29
30
0
    std::unique_lock<std::mutex> w_lock(_mutex);
31
0
    int64_t current_time = GetCurrentTimeMicros();
32
0
    int64_t block_finish_time = block_timeout_ms <= 0 ? 0 : current_time + block_timeout_ms * 1000;
33
34
0
    while (current_time <= _next_io_time_micros) {
35
0
        if (block_finish_time > 0 && current_time >= block_finish_time) {
36
0
            return false;
37
0
        }
38
0
        wait_condition.wait_for(w_lock,
39
0
                                std::chrono::microseconds(_next_io_time_micros - current_time));
40
0
        current_time = GetCurrentTimeMicros();
41
0
    }
42
0
    return true;
43
0
}
44
45
0
bool IOThrottle::try_acquire() {
46
0
    if (_io_bytes_per_second_limit < 0) {
47
0
        return true;
48
0
    }
49
0
    std::unique_lock<std::mutex> w_lock(_mutex);
50
0
    return GetCurrentTimeMicros() > _next_io_time_micros;
51
0
}
52
53
0
void IOThrottle::update_next_io_time(int64_t io_bytes) {
54
0
    if (_io_bytes_per_second_limit <= 0 || io_bytes <= 0) {
55
0
        return;
56
0
    }
57
0
    int64_t read_bytes_per_second = _io_bytes_per_second_limit;
58
0
    {
59
0
        std::unique_lock<std::mutex> w_lock(_mutex);
60
0
        double io_bytes_float = static_cast<double>(io_bytes);
61
0
        double ret = (io_bytes_float / static_cast<double>(read_bytes_per_second)) *
62
0
                     static_cast<double>(MICROS_PER_SEC);
63
0
        int64_t current_time = GetCurrentTimeMicros();
64
65
0
        if (current_time > _next_io_time_micros) {
66
0
            _next_io_time_micros = current_time;
67
0
        }
68
0
        _next_io_time_micros += ret < 1 ? static_cast<int64_t>(1) : static_cast<int64_t>(ret);
69
0
    }
70
0
}
71
72
0
void IOThrottle::set_io_bytes_per_second(int64_t io_bytes_per_second) {
73
0
    _io_bytes_per_second_limit = io_bytes_per_second;
74
0
}
75
76
}; // namespace doris