Coverage Report

Created: 2026-06-03 09:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/cache/block_file_cache_profile.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 "io/cache/block_file_cache_profile.h"
19
20
#include <functional>
21
#include <memory>
22
#include <string>
23
24
#include "common/metrics/doris_metrics.h"
25
26
namespace doris::io {
27
28
14
std::shared_ptr<AtomicStatistics> FileCacheMetrics::report() {
29
14
    std::shared_ptr<AtomicStatistics> output_stats = std::make_shared<AtomicStatistics>();
30
14
    std::lock_guard lock(_mtx);
31
14
    output_stats->num_io_bytes_read_from_cache += _statistics->num_io_bytes_read_from_cache;
32
14
    output_stats->num_io_bytes_read_from_remote += _statistics->num_io_bytes_read_from_remote;
33
14
    output_stats->num_io_bytes_read_from_peer += _statistics->num_io_bytes_read_from_peer;
34
14
    return output_stats;
35
14
}
36
37
718k
void FileCacheMetrics::update(FileCacheStatistics* input_stats) {
38
718k
    if (_statistics == nullptr) {
39
1
        std::lock_guard<std::mutex> lock(_mtx);
40
1
        if (_statistics == nullptr) {
41
1
            _statistics = std::make_shared<AtomicStatistics>();
42
1
            register_entity();
43
1
        }
44
1
    }
45
718k
    _statistics->num_io_bytes_read_from_cache += input_stats->bytes_read_from_local;
46
718k
    _statistics->num_io_bytes_read_from_remote += input_stats->bytes_read_from_remote;
47
718k
    _statistics->num_io_bytes_read_from_peer += input_stats->bytes_read_from_peer;
48
718k
}
49
50
1
void FileCacheMetrics::register_entity() {
51
1
    DorisMetrics::instance()->server_entity()->register_hook(
52
1
            "block_file_cache", [this]() { update_metrics_callback(); });
53
1
}
54
55
0
void FileCacheMetrics::update_metrics_callback() {
56
0
    std::shared_ptr<AtomicStatistics> stats = report();
57
0
    DorisMetrics::instance()->num_io_bytes_read_from_cache->set_value(
58
0
            stats->num_io_bytes_read_from_cache);
59
0
    DorisMetrics::instance()->num_io_bytes_read_from_remote->set_value(
60
0
            stats->num_io_bytes_read_from_remote);
61
0
    DorisMetrics::instance()->num_io_bytes_read_from_peer->set_value(
62
0
            stats->num_io_bytes_read_from_peer);
63
0
    DorisMetrics::instance()->num_io_bytes_read_total->set_value(
64
0
            stats->num_io_bytes_read_from_cache + stats->num_io_bytes_read_from_remote +
65
0
            stats->num_io_bytes_read_from_peer);
66
0
}
67
68
FileCacheStatistics diff_file_cache_statistics(const FileCacheStatistics& current,
69
5
                                               const FileCacheStatistics& previous) {
70
5
    FileCacheStatistics diff;
71
140
#define SUBTRACT_FIELD(field) diff.field = current.field - previous.field
72
5
    SUBTRACT_FIELD(num_local_io_total);
73
5
    SUBTRACT_FIELD(num_remote_io_total);
74
5
    SUBTRACT_FIELD(num_peer_io_total);
75
5
    SUBTRACT_FIELD(local_io_timer);
76
5
    SUBTRACT_FIELD(bytes_read_from_local);
77
5
    SUBTRACT_FIELD(bytes_read_from_remote);
78
5
    SUBTRACT_FIELD(bytes_read_from_peer);
79
5
    SUBTRACT_FIELD(remote_io_timer);
80
5
    SUBTRACT_FIELD(peer_io_timer);
81
5
    SUBTRACT_FIELD(remote_wait_timer);
82
5
    SUBTRACT_FIELD(write_cache_io_timer);
83
5
    SUBTRACT_FIELD(bytes_write_into_cache);
84
5
    SUBTRACT_FIELD(num_skip_cache_io_total);
85
5
    SUBTRACT_FIELD(read_cache_file_directly_timer);
86
5
    SUBTRACT_FIELD(cache_get_or_set_timer);
87
5
    SUBTRACT_FIELD(lock_wait_timer);
88
5
    SUBTRACT_FIELD(get_timer);
89
5
    SUBTRACT_FIELD(set_timer);
90
91
5
    SUBTRACT_FIELD(inverted_index_num_local_io_total);
92
5
    SUBTRACT_FIELD(inverted_index_num_remote_io_total);
93
5
    SUBTRACT_FIELD(inverted_index_num_peer_io_total);
94
5
    SUBTRACT_FIELD(inverted_index_bytes_read_from_local);
95
5
    SUBTRACT_FIELD(inverted_index_bytes_read_from_remote);
96
5
    SUBTRACT_FIELD(inverted_index_bytes_read_from_peer);
97
5
    SUBTRACT_FIELD(inverted_index_local_io_timer);
98
5
    SUBTRACT_FIELD(inverted_index_remote_io_timer);
99
5
    SUBTRACT_FIELD(inverted_index_peer_io_timer);
100
5
    SUBTRACT_FIELD(inverted_index_io_timer);
101
5
#undef SUBTRACT_FIELD
102
5
    return diff;
103
5
}
104
105
7
FileCacheProfileReporter::FileCacheProfileReporter(RuntimeProfile* profile) {
106
7
    static const char* cache_profile = "FileCache";
107
7
    ADD_TIMER_WITH_LEVEL(profile, cache_profile, 2);
108
7
    num_local_io_total =
109
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
110
7
    num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumRemoteIOTotal", TUnit::UNIT,
111
7
                                                       cache_profile, 1);
112
7
    num_peer_io_total =
113
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
114
7
    local_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "LocalIOUseTimer", cache_profile, 1);
115
7
    remote_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "RemoteIOUseTimer", cache_profile, 1);
116
7
    peer_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "PeerIOUseTimer", cache_profile, 1);
117
7
    remote_wait_timer =
118
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "WaitOtherDownloaderTimer", cache_profile, 1);
119
7
    write_cache_io_timer =
120
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "WriteCacheIOUseTimer", cache_profile, 1);
121
7
    bytes_write_into_cache = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesWriteIntoCache",
122
7
                                                          TUnit::BYTES, cache_profile, 1);
123
7
    num_skip_cache_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumSkipCacheIOTotal",
124
7
                                                           TUnit::UNIT, cache_profile, 1);
125
7
    bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromCache",
126
7
                                                            TUnit::BYTES, cache_profile, 1);
127
7
    bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromRemote",
128
7
                                                             TUnit::BYTES, cache_profile, 1);
129
7
    bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromPeer",
130
7
                                                           TUnit::BYTES, cache_profile, 1);
131
7
    read_cache_file_directly_timer =
132
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "ReadCacheFileDirectlyTimer", cache_profile, 1);
133
7
    cache_get_or_set_timer =
134
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "CacheGetOrSetTimer", cache_profile, 1);
135
7
    lock_wait_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "LockWaitTimer", cache_profile, 1);
136
7
    get_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "GetTimer", cache_profile, 1);
137
7
    set_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "SetTimer", cache_profile, 1);
138
139
7
    inverted_index_num_local_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
140
7
            profile, "InvertedIndexNumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
141
7
    inverted_index_num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
142
7
            profile, "InvertedIndexNumRemoteIOTotal", TUnit::UNIT, cache_profile, 1);
143
7
    inverted_index_num_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
144
7
            profile, "InvertedIndexNumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
145
7
    inverted_index_bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(
146
7
            profile, "InvertedIndexBytesScannedFromCache", TUnit::BYTES, cache_profile, 1);
147
7
    inverted_index_bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(
148
7
            profile, "InvertedIndexBytesScannedFromRemote", TUnit::BYTES, cache_profile, 1);
149
7
    inverted_index_bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(
150
7
            profile, "InvertedIndexBytesScannedFromPeer", TUnit::BYTES, cache_profile, 1);
151
7
    inverted_index_local_io_timer =
152
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexLocalIOUseTimer", cache_profile, 1);
153
7
    inverted_index_remote_io_timer =
154
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexRemoteIOUseTimer", cache_profile, 1);
155
7
    inverted_index_peer_io_timer =
156
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexPeerIOUseTimer", cache_profile, 1);
157
7
    inverted_index_io_timer =
158
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexIOTimer", cache_profile, 1);
159
7
}
160
161
8
void FileCacheProfileReporter::update(const FileCacheStatistics* statistics) const {
162
8
    COUNTER_UPDATE(num_local_io_total, statistics->num_local_io_total);
163
8
    COUNTER_UPDATE(num_remote_io_total, statistics->num_remote_io_total);
164
8
    COUNTER_UPDATE(num_peer_io_total, statistics->num_peer_io_total);
165
8
    COUNTER_UPDATE(local_io_timer, statistics->local_io_timer);
166
8
    COUNTER_UPDATE(remote_io_timer, statistics->remote_io_timer);
167
8
    COUNTER_UPDATE(peer_io_timer, statistics->peer_io_timer);
168
8
    COUNTER_UPDATE(remote_wait_timer, statistics->remote_wait_timer);
169
8
    COUNTER_UPDATE(write_cache_io_timer, statistics->write_cache_io_timer);
170
8
    COUNTER_UPDATE(bytes_write_into_cache, statistics->bytes_write_into_cache);
171
8
    COUNTER_UPDATE(num_skip_cache_io_total, statistics->num_skip_cache_io_total);
172
8
    COUNTER_UPDATE(bytes_scanned_from_cache, statistics->bytes_read_from_local);
173
8
    COUNTER_UPDATE(bytes_scanned_from_remote, statistics->bytes_read_from_remote);
174
8
    COUNTER_UPDATE(bytes_scanned_from_peer, statistics->bytes_read_from_peer);
175
8
    COUNTER_UPDATE(read_cache_file_directly_timer, statistics->read_cache_file_directly_timer);
176
8
    COUNTER_UPDATE(cache_get_or_set_timer, statistics->cache_get_or_set_timer);
177
8
    COUNTER_UPDATE(lock_wait_timer, statistics->lock_wait_timer);
178
8
    COUNTER_UPDATE(get_timer, statistics->get_timer);
179
8
    COUNTER_UPDATE(set_timer, statistics->set_timer);
180
181
8
    COUNTER_UPDATE(inverted_index_num_local_io_total,
182
8
                   statistics->inverted_index_num_local_io_total);
183
8
    COUNTER_UPDATE(inverted_index_num_remote_io_total,
184
8
                   statistics->inverted_index_num_remote_io_total);
185
8
    COUNTER_UPDATE(inverted_index_num_peer_io_total, statistics->inverted_index_num_peer_io_total);
186
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_cache,
187
8
                   statistics->inverted_index_bytes_read_from_local);
188
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_remote,
189
8
                   statistics->inverted_index_bytes_read_from_remote);
190
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_peer,
191
8
                   statistics->inverted_index_bytes_read_from_peer);
192
8
    COUNTER_UPDATE(inverted_index_local_io_timer, statistics->inverted_index_local_io_timer);
193
8
    COUNTER_UPDATE(inverted_index_remote_io_timer, statistics->inverted_index_remote_io_timer);
194
8
    COUNTER_UPDATE(inverted_index_peer_io_timer, statistics->inverted_index_peer_io_timer);
195
8
    COUNTER_UPDATE(inverted_index_io_timer, statistics->inverted_index_io_timer);
196
8
}
197
198
} // namespace doris::io