Coverage Report

Created: 2026-07-04 04:47

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
185
#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
102
5
    SUBTRACT_FIELD(segment_footer_index_num_local_io_total);
103
5
    SUBTRACT_FIELD(segment_footer_index_num_remote_io_total);
104
5
    SUBTRACT_FIELD(segment_footer_index_num_peer_io_total);
105
5
    SUBTRACT_FIELD(segment_footer_index_bytes_read_from_local);
106
5
    SUBTRACT_FIELD(segment_footer_index_bytes_read_from_remote);
107
5
    SUBTRACT_FIELD(segment_footer_index_bytes_read_from_peer);
108
5
    SUBTRACT_FIELD(segment_footer_index_local_io_timer);
109
5
    SUBTRACT_FIELD(segment_footer_index_remote_io_timer);
110
5
    SUBTRACT_FIELD(segment_footer_index_peer_io_timer);
111
5
#undef SUBTRACT_FIELD
112
5
    return diff;
113
5
}
114
115
7
FileCacheProfileReporter::FileCacheProfileReporter(RuntimeProfile* profile) : _profile(profile) {
116
7
    static const char* cache_profile = "FileCache";
117
7
    ADD_TIMER_WITH_LEVEL(profile, cache_profile, 2);
118
7
    num_local_io_total =
119
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
120
7
    num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumRemoteIOTotal", TUnit::UNIT,
121
7
                                                       cache_profile, 1);
122
7
    num_peer_io_total =
123
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
124
7
    local_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "LocalIOUseTimer", cache_profile, 1);
125
7
    remote_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "RemoteIOUseTimer", cache_profile, 1);
126
7
    peer_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "PeerIOUseTimer", cache_profile, 1);
127
7
    remote_wait_timer =
128
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "WaitOtherDownloaderTimer", cache_profile, 1);
129
7
    write_cache_io_timer =
130
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "WriteCacheIOUseTimer", cache_profile, 1);
131
7
    bytes_write_into_cache = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesWriteIntoCache",
132
7
                                                          TUnit::BYTES, cache_profile, 1);
133
7
    num_skip_cache_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumSkipCacheIOTotal",
134
7
                                                           TUnit::UNIT, cache_profile, 1);
135
7
    bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromCache",
136
7
                                                            TUnit::BYTES, cache_profile, 1);
137
7
    bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromRemote",
138
7
                                                             TUnit::BYTES, cache_profile, 1);
139
7
    bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromPeer",
140
7
                                                           TUnit::BYTES, cache_profile, 1);
141
7
    read_cache_file_directly_timer =
142
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "ReadCacheFileDirectlyTimer", cache_profile, 1);
143
7
    cache_get_or_set_timer =
144
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "CacheGetOrSetTimer", cache_profile, 1);
145
7
    lock_wait_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "LockWaitTimer", cache_profile, 1);
146
7
    get_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "GetTimer", cache_profile, 1);
147
7
    set_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "SetTimer", cache_profile, 1);
148
149
7
    inverted_index_num_local_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
150
7
            profile, "InvertedIndexNumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
151
7
    inverted_index_num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
152
7
            profile, "InvertedIndexNumRemoteIOTotal", TUnit::UNIT, cache_profile, 1);
153
7
    inverted_index_num_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
154
7
            profile, "InvertedIndexNumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
155
7
    inverted_index_bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(
156
7
            profile, "InvertedIndexBytesScannedFromCache", TUnit::BYTES, cache_profile, 1);
157
7
    inverted_index_bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(
158
7
            profile, "InvertedIndexBytesScannedFromRemote", TUnit::BYTES, cache_profile, 1);
159
7
    inverted_index_bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(
160
7
            profile, "InvertedIndexBytesScannedFromPeer", TUnit::BYTES, cache_profile, 1);
161
7
    inverted_index_local_io_timer =
162
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexLocalIOUseTimer", cache_profile, 1);
163
7
    inverted_index_remote_io_timer =
164
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexRemoteIOUseTimer", cache_profile, 1);
165
7
    inverted_index_peer_io_timer =
166
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexPeerIOUseTimer", cache_profile, 1);
167
7
    inverted_index_io_timer =
168
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexIOTimer", cache_profile, 1);
169
170
7
    segment_footer_index_num_local_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
171
7
            profile, "SegmentFooterIndexNumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
172
7
    segment_footer_index_num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
173
7
            profile, "SegmentFooterIndexNumRemoteIOTotal", TUnit::UNIT, cache_profile, 1);
174
7
    segment_footer_index_num_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
175
7
            profile, "SegmentFooterIndexNumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
176
7
    segment_footer_index_bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(
177
7
            profile, "SegmentFooterIndexBytesScannedFromCache", TUnit::BYTES, cache_profile, 1);
178
7
    segment_footer_index_bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(
179
7
            profile, "SegmentFooterIndexBytesScannedFromRemote", TUnit::BYTES, cache_profile, 1);
180
7
    segment_footer_index_bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(
181
7
            profile, "SegmentFooterIndexBytesScannedFromPeer", TUnit::BYTES, cache_profile, 1);
182
7
    segment_footer_index_local_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(
183
7
            profile, "SegmentFooterIndexLocalIOUseTimer", cache_profile, 1);
184
7
    segment_footer_index_remote_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(
185
7
            profile, "SegmentFooterIndexRemoteIOUseTimer", cache_profile, 1);
186
7
    segment_footer_index_peer_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(
187
7
            profile, "SegmentFooterIndexPeerIOUseTimer", cache_profile, 1);
188
189
7
    num_cross_cg_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "CrossCGPeerIOTotal",
190
7
                                                              TUnit::UNIT, cache_profile, 1);
191
7
    bytes_scanned_from_cross_cg_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "CrossCGPeerBytesRead",
192
7
                                                                    TUnit::BYTES, cache_profile, 1);
193
7
    cross_cg_peer_io_timer =
194
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "CrossCGPeerIOTime", cache_profile, 1);
195
7
    num_same_cg_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "SameCGPeerIOTotal",
196
7
                                                             TUnit::UNIT, cache_profile, 1);
197
7
    bytes_scanned_from_same_cg_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "SameCGPeerBytesRead",
198
7
                                                                   TUnit::BYTES, cache_profile, 1);
199
7
    same_cg_peer_io_timer =
200
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "SameCGPeerIOTime", cache_profile, 1);
201
7
    num_peer_race_peer_win =
202
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "PeerRaceWin", TUnit::UNIT, cache_profile, 1);
203
7
    num_peer_race_s3_win =
204
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "S3RaceWin", TUnit::UNIT, cache_profile, 1);
205
7
    num_peer_lazy_fetch =
206
7
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "PeerLazyFetch", TUnit::UNIT, cache_profile, 1);
207
7
    peer_lazy_fetch_timer =
208
7
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "PeerLazyFetchTime", cache_profile, 1);
209
7
}
210
211
8
void FileCacheProfileReporter::update(const FileCacheStatistics* statistics) const {
212
8
    COUNTER_UPDATE(num_local_io_total, statistics->num_local_io_total);
213
8
    COUNTER_UPDATE(num_remote_io_total, statistics->num_remote_io_total);
214
8
    COUNTER_UPDATE(num_peer_io_total, statistics->num_peer_io_total);
215
8
    COUNTER_UPDATE(local_io_timer, statistics->local_io_timer);
216
8
    COUNTER_UPDATE(remote_io_timer, statistics->remote_io_timer);
217
8
    COUNTER_UPDATE(peer_io_timer, statistics->peer_io_timer);
218
8
    COUNTER_UPDATE(remote_wait_timer, statistics->remote_wait_timer);
219
8
    COUNTER_UPDATE(write_cache_io_timer, statistics->write_cache_io_timer);
220
8
    COUNTER_UPDATE(bytes_write_into_cache, statistics->bytes_write_into_cache);
221
8
    COUNTER_UPDATE(num_skip_cache_io_total, statistics->num_skip_cache_io_total);
222
8
    COUNTER_UPDATE(bytes_scanned_from_cache, statistics->bytes_read_from_local);
223
8
    COUNTER_UPDATE(bytes_scanned_from_remote, statistics->bytes_read_from_remote);
224
8
    COUNTER_UPDATE(bytes_scanned_from_peer, statistics->bytes_read_from_peer);
225
8
    COUNTER_UPDATE(read_cache_file_directly_timer, statistics->read_cache_file_directly_timer);
226
8
    COUNTER_UPDATE(cache_get_or_set_timer, statistics->cache_get_or_set_timer);
227
8
    COUNTER_UPDATE(lock_wait_timer, statistics->lock_wait_timer);
228
8
    COUNTER_UPDATE(get_timer, statistics->get_timer);
229
8
    COUNTER_UPDATE(set_timer, statistics->set_timer);
230
231
8
    COUNTER_UPDATE(inverted_index_num_local_io_total,
232
8
                   statistics->inverted_index_num_local_io_total);
233
8
    COUNTER_UPDATE(inverted_index_num_remote_io_total,
234
8
                   statistics->inverted_index_num_remote_io_total);
235
8
    COUNTER_UPDATE(inverted_index_num_peer_io_total, statistics->inverted_index_num_peer_io_total);
236
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_cache,
237
8
                   statistics->inverted_index_bytes_read_from_local);
238
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_remote,
239
8
                   statistics->inverted_index_bytes_read_from_remote);
240
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_peer,
241
8
                   statistics->inverted_index_bytes_read_from_peer);
242
8
    COUNTER_UPDATE(inverted_index_local_io_timer, statistics->inverted_index_local_io_timer);
243
8
    COUNTER_UPDATE(inverted_index_remote_io_timer, statistics->inverted_index_remote_io_timer);
244
8
    COUNTER_UPDATE(inverted_index_peer_io_timer, statistics->inverted_index_peer_io_timer);
245
8
    COUNTER_UPDATE(inverted_index_io_timer, statistics->inverted_index_io_timer);
246
247
8
    COUNTER_UPDATE(segment_footer_index_num_local_io_total,
248
8
                   statistics->segment_footer_index_num_local_io_total);
249
8
    COUNTER_UPDATE(segment_footer_index_num_remote_io_total,
250
8
                   statistics->segment_footer_index_num_remote_io_total);
251
8
    COUNTER_UPDATE(segment_footer_index_num_peer_io_total,
252
8
                   statistics->segment_footer_index_num_peer_io_total);
253
8
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_cache,
254
8
                   statistics->segment_footer_index_bytes_read_from_local);
255
8
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_remote,
256
8
                   statistics->segment_footer_index_bytes_read_from_remote);
257
8
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_peer,
258
8
                   statistics->segment_footer_index_bytes_read_from_peer);
259
8
    COUNTER_UPDATE(segment_footer_index_local_io_timer,
260
8
                   statistics->segment_footer_index_local_io_timer);
261
8
    COUNTER_UPDATE(segment_footer_index_remote_io_timer,
262
8
                   statistics->segment_footer_index_remote_io_timer);
263
8
    COUNTER_UPDATE(segment_footer_index_peer_io_timer,
264
8
                   statistics->segment_footer_index_peer_io_timer);
265
266
8
    COUNTER_UPDATE(num_cross_cg_peer_io_total, statistics->num_cross_cg_peer_io_total);
267
8
    COUNTER_UPDATE(bytes_scanned_from_cross_cg_peer, statistics->bytes_read_from_cross_cg_peer);
268
8
    COUNTER_UPDATE(cross_cg_peer_io_timer, statistics->cross_cg_peer_io_timer);
269
8
    COUNTER_UPDATE(num_same_cg_peer_io_total, statistics->num_same_cg_peer_io_total);
270
8
    COUNTER_UPDATE(bytes_scanned_from_same_cg_peer, statistics->bytes_read_from_same_cg_peer);
271
8
    COUNTER_UPDATE(same_cg_peer_io_timer, statistics->same_cg_peer_io_timer);
272
8
    COUNTER_UPDATE(num_peer_race_peer_win, statistics->num_peer_race_peer_win);
273
8
    COUNTER_UPDATE(num_peer_race_s3_win, statistics->num_peer_race_s3_win);
274
8
    COUNTER_UPDATE(num_peer_lazy_fetch, statistics->num_peer_lazy_fetch);
275
8
    COUNTER_UPDATE(peer_lazy_fetch_timer, statistics->peer_lazy_fetch_timer);
276
277
8
    if (!statistics->peer_hosts.empty() && _profile != nullptr) {
278
0
        std::string peer_nodes;
279
0
        for (const auto& host : statistics->peer_hosts) {
280
0
            if (!peer_nodes.empty()) {
281
0
                peer_nodes += ", ";
282
0
            }
283
0
            peer_nodes += host;
284
0
        }
285
0
        _profile->add_info_string("PeerCacheNodes", peer_nodes);
286
0
    }
287
8
}
288
289
} // namespace doris::io