Coverage Report

Created: 2026-07-13 02:25

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
16
std::shared_ptr<AtomicStatistics> FileCacheMetrics::report() {
29
16
    std::shared_ptr<AtomicStatistics> output_stats = std::make_shared<AtomicStatistics>();
30
16
    std::lock_guard lock(_mtx);
31
16
    output_stats->num_io_bytes_read_from_cache += _statistics->num_io_bytes_read_from_cache;
32
16
    output_stats->num_io_bytes_read_from_remote += _statistics->num_io_bytes_read_from_remote;
33
16
    output_stats->num_io_bytes_read_from_peer += _statistics->num_io_bytes_read_from_peer;
34
16
    output_stats->inverted_index_bytes_read_from_remote +=
35
16
            _statistics->inverted_index_bytes_read_from_remote;
36
16
    output_stats->segment_footer_index_bytes_read_from_remote +=
37
16
            _statistics->segment_footer_index_bytes_read_from_remote;
38
16
    return output_stats;
39
16
}
40
41
718k
void FileCacheMetrics::update(FileCacheStatistics* input_stats) {
42
718k
    if (_statistics == nullptr) {
43
1
        std::lock_guard<std::mutex> lock(_mtx);
44
1
        if (_statistics == nullptr) {
45
1
            _statistics = std::make_shared<AtomicStatistics>();
46
1
            register_entity();
47
1
        }
48
1
    }
49
718k
    _statistics->num_io_bytes_read_from_cache += input_stats->bytes_read_from_local;
50
718k
    _statistics->num_io_bytes_read_from_remote += input_stats->bytes_read_from_remote;
51
718k
    _statistics->num_io_bytes_read_from_peer += input_stats->bytes_read_from_peer;
52
718k
    _statistics->inverted_index_bytes_read_from_remote +=
53
718k
            input_stats->inverted_index_bytes_read_from_remote;
54
718k
    _statistics->segment_footer_index_bytes_read_from_remote +=
55
718k
            input_stats->segment_footer_index_bytes_read_from_remote;
56
718k
}
57
58
1
void FileCacheMetrics::register_entity() {
59
1
    DorisMetrics::instance()->server_entity()->register_hook(
60
1
            "block_file_cache", [this]() { update_metrics_callback(); });
61
1
}
62
63
2
void FileCacheMetrics::update_metrics_callback() {
64
2
    std::shared_ptr<AtomicStatistics> stats = report();
65
2
    DorisMetrics::instance()->num_io_bytes_read_from_cache->set_value(
66
2
            stats->num_io_bytes_read_from_cache);
67
2
    DorisMetrics::instance()->num_io_bytes_read_from_remote->set_value(
68
2
            stats->num_io_bytes_read_from_remote);
69
2
    DorisMetrics::instance()->num_io_bytes_read_from_peer->set_value(
70
2
            stats->num_io_bytes_read_from_peer);
71
2
    DorisMetrics::instance()->num_io_bytes_read_total->set_value(
72
2
            stats->num_io_bytes_read_from_cache + stats->num_io_bytes_read_from_remote +
73
2
            stats->num_io_bytes_read_from_peer);
74
2
    DorisMetrics::instance()->inverted_index_bytes_read_from_remote->set_value(
75
2
            stats->inverted_index_bytes_read_from_remote);
76
2
    DorisMetrics::instance()->segment_footer_index_bytes_read_from_remote->set_value(
77
2
            stats->segment_footer_index_bytes_read_from_remote);
78
2
}
79
80
FileCacheStatistics diff_file_cache_statistics(const FileCacheStatistics& current,
81
5
                                               const FileCacheStatistics& previous) {
82
5
    FileCacheStatistics diff;
83
185
#define SUBTRACT_FIELD(field) diff.field = current.field - previous.field
84
5
    SUBTRACT_FIELD(num_local_io_total);
85
5
    SUBTRACT_FIELD(num_remote_io_total);
86
5
    SUBTRACT_FIELD(num_peer_io_total);
87
5
    SUBTRACT_FIELD(local_io_timer);
88
5
    SUBTRACT_FIELD(bytes_read_from_local);
89
5
    SUBTRACT_FIELD(bytes_read_from_remote);
90
5
    SUBTRACT_FIELD(bytes_read_from_peer);
91
5
    SUBTRACT_FIELD(remote_io_timer);
92
5
    SUBTRACT_FIELD(peer_io_timer);
93
5
    SUBTRACT_FIELD(remote_wait_timer);
94
5
    SUBTRACT_FIELD(write_cache_io_timer);
95
5
    SUBTRACT_FIELD(bytes_write_into_cache);
96
5
    SUBTRACT_FIELD(num_skip_cache_io_total);
97
5
    SUBTRACT_FIELD(read_cache_file_directly_timer);
98
5
    SUBTRACT_FIELD(cache_get_or_set_timer);
99
5
    SUBTRACT_FIELD(lock_wait_timer);
100
5
    SUBTRACT_FIELD(get_timer);
101
5
    SUBTRACT_FIELD(set_timer);
102
103
5
    SUBTRACT_FIELD(inverted_index_num_local_io_total);
104
5
    SUBTRACT_FIELD(inverted_index_num_remote_io_total);
105
5
    SUBTRACT_FIELD(inverted_index_num_peer_io_total);
106
5
    SUBTRACT_FIELD(inverted_index_bytes_read_from_local);
107
5
    SUBTRACT_FIELD(inverted_index_bytes_read_from_remote);
108
5
    SUBTRACT_FIELD(inverted_index_bytes_read_from_peer);
109
5
    SUBTRACT_FIELD(inverted_index_local_io_timer);
110
5
    SUBTRACT_FIELD(inverted_index_remote_io_timer);
111
5
    SUBTRACT_FIELD(inverted_index_peer_io_timer);
112
5
    SUBTRACT_FIELD(inverted_index_io_timer);
113
114
5
    SUBTRACT_FIELD(segment_footer_index_num_local_io_total);
115
5
    SUBTRACT_FIELD(segment_footer_index_num_remote_io_total);
116
5
    SUBTRACT_FIELD(segment_footer_index_num_peer_io_total);
117
5
    SUBTRACT_FIELD(segment_footer_index_bytes_read_from_local);
118
5
    SUBTRACT_FIELD(segment_footer_index_bytes_read_from_remote);
119
5
    SUBTRACT_FIELD(segment_footer_index_bytes_read_from_peer);
120
5
    SUBTRACT_FIELD(segment_footer_index_local_io_timer);
121
5
    SUBTRACT_FIELD(segment_footer_index_remote_io_timer);
122
5
    SUBTRACT_FIELD(segment_footer_index_peer_io_timer);
123
5
#undef SUBTRACT_FIELD
124
5
    return diff;
125
5
}
126
127
8
FileCacheProfileReporter::FileCacheProfileReporter(RuntimeProfile* profile) : _profile(profile) {
128
8
    static const char* cache_profile = "FileCache";
129
8
    ADD_TIMER_WITH_LEVEL(profile, cache_profile, 2);
130
8
    num_local_io_total =
131
8
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
132
8
    num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumRemoteIOTotal", TUnit::UNIT,
133
8
                                                       cache_profile, 1);
134
8
    num_peer_io_total =
135
8
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
136
8
    local_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "LocalIOUseTimer", cache_profile, 1);
137
8
    remote_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "RemoteIOUseTimer", cache_profile, 1);
138
8
    peer_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "PeerIOUseTimer", cache_profile, 1);
139
8
    remote_wait_timer =
140
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "WaitOtherDownloaderTimer", cache_profile, 1);
141
8
    write_cache_io_timer =
142
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "WriteCacheIOUseTimer", cache_profile, 1);
143
8
    bytes_write_into_cache = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesWriteIntoCache",
144
8
                                                          TUnit::BYTES, cache_profile, 1);
145
8
    num_skip_cache_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "NumSkipCacheIOTotal",
146
8
                                                           TUnit::UNIT, cache_profile, 1);
147
8
    bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromCache",
148
8
                                                            TUnit::BYTES, cache_profile, 1);
149
8
    bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromRemote",
150
8
                                                             TUnit::BYTES, cache_profile, 1);
151
8
    bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "BytesScannedFromPeer",
152
8
                                                           TUnit::BYTES, cache_profile, 1);
153
8
    read_cache_file_directly_timer =
154
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "ReadCacheFileDirectlyTimer", cache_profile, 1);
155
8
    cache_get_or_set_timer =
156
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "CacheGetOrSetTimer", cache_profile, 1);
157
8
    lock_wait_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "LockWaitTimer", cache_profile, 1);
158
8
    get_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "GetTimer", cache_profile, 1);
159
8
    set_timer = ADD_CHILD_TIMER_WITH_LEVEL(profile, "SetTimer", cache_profile, 1);
160
161
8
    inverted_index_num_local_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
162
8
            profile, "InvertedIndexNumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
163
8
    inverted_index_num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
164
8
            profile, "InvertedIndexNumRemoteIOTotal", TUnit::UNIT, cache_profile, 1);
165
8
    inverted_index_num_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
166
8
            profile, "InvertedIndexNumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
167
8
    inverted_index_bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(
168
8
            profile, "InvertedIndexBytesScannedFromCache", TUnit::BYTES, cache_profile, 1);
169
8
    inverted_index_bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(
170
8
            profile, "InvertedIndexBytesScannedFromRemote", TUnit::BYTES, cache_profile, 1);
171
8
    inverted_index_bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(
172
8
            profile, "InvertedIndexBytesScannedFromPeer", TUnit::BYTES, cache_profile, 1);
173
8
    inverted_index_local_io_timer =
174
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexLocalIOUseTimer", cache_profile, 1);
175
8
    inverted_index_remote_io_timer =
176
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexRemoteIOUseTimer", cache_profile, 1);
177
8
    inverted_index_peer_io_timer =
178
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexPeerIOUseTimer", cache_profile, 1);
179
8
    inverted_index_io_timer =
180
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "InvertedIndexIOTimer", cache_profile, 1);
181
182
8
    segment_footer_index_num_local_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
183
8
            profile, "SegmentFooterIndexNumLocalIOTotal", TUnit::UNIT, cache_profile, 1);
184
8
    segment_footer_index_num_remote_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
185
8
            profile, "SegmentFooterIndexNumRemoteIOTotal", TUnit::UNIT, cache_profile, 1);
186
8
    segment_footer_index_num_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(
187
8
            profile, "SegmentFooterIndexNumPeerIOTotal", TUnit::UNIT, cache_profile, 1);
188
8
    segment_footer_index_bytes_scanned_from_cache = ADD_CHILD_COUNTER_WITH_LEVEL(
189
8
            profile, "SegmentFooterIndexBytesScannedFromCache", TUnit::BYTES, cache_profile, 1);
190
8
    segment_footer_index_bytes_scanned_from_remote = ADD_CHILD_COUNTER_WITH_LEVEL(
191
8
            profile, "SegmentFooterIndexBytesScannedFromRemote", TUnit::BYTES, cache_profile, 1);
192
8
    segment_footer_index_bytes_scanned_from_peer = ADD_CHILD_COUNTER_WITH_LEVEL(
193
8
            profile, "SegmentFooterIndexBytesScannedFromPeer", TUnit::BYTES, cache_profile, 1);
194
8
    segment_footer_index_local_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(
195
8
            profile, "SegmentFooterIndexLocalIOUseTimer", cache_profile, 1);
196
8
    segment_footer_index_remote_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(
197
8
            profile, "SegmentFooterIndexRemoteIOUseTimer", cache_profile, 1);
198
8
    segment_footer_index_peer_io_timer = ADD_CHILD_TIMER_WITH_LEVEL(
199
8
            profile, "SegmentFooterIndexPeerIOUseTimer", cache_profile, 1);
200
201
8
    num_cross_cg_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "CrossCGPeerIOTotal",
202
8
                                                              TUnit::UNIT, cache_profile, 1);
203
8
    bytes_scanned_from_cross_cg_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "CrossCGPeerBytesRead",
204
8
                                                                    TUnit::BYTES, cache_profile, 1);
205
8
    cross_cg_peer_io_timer =
206
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "CrossCGPeerIOTime", cache_profile, 1);
207
8
    num_same_cg_peer_io_total = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "SameCGPeerIOTotal",
208
8
                                                             TUnit::UNIT, cache_profile, 1);
209
8
    bytes_scanned_from_same_cg_peer = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "SameCGPeerBytesRead",
210
8
                                                                   TUnit::BYTES, cache_profile, 1);
211
8
    same_cg_peer_io_timer =
212
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "SameCGPeerIOTime", cache_profile, 1);
213
8
    num_peer_race_peer_win =
214
8
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "PeerRaceWin", TUnit::UNIT, cache_profile, 1);
215
8
    num_peer_race_s3_win =
216
8
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "S3RaceWin", TUnit::UNIT, cache_profile, 1);
217
8
    num_peer_lazy_fetch =
218
8
            ADD_CHILD_COUNTER_WITH_LEVEL(profile, "PeerLazyFetch", TUnit::UNIT, cache_profile, 1);
219
8
    peer_lazy_fetch_timer =
220
8
            ADD_CHILD_TIMER_WITH_LEVEL(profile, "PeerLazyFetchTime", cache_profile, 1);
221
8
}
222
223
9
void FileCacheProfileReporter::update(const FileCacheStatistics* statistics) const {
224
9
    COUNTER_UPDATE(num_local_io_total, statistics->num_local_io_total);
225
9
    COUNTER_UPDATE(num_remote_io_total, statistics->num_remote_io_total);
226
9
    COUNTER_UPDATE(num_peer_io_total, statistics->num_peer_io_total);
227
9
    COUNTER_UPDATE(local_io_timer, statistics->local_io_timer);
228
9
    COUNTER_UPDATE(remote_io_timer, statistics->remote_io_timer);
229
9
    COUNTER_UPDATE(peer_io_timer, statistics->peer_io_timer);
230
9
    COUNTER_UPDATE(remote_wait_timer, statistics->remote_wait_timer);
231
9
    COUNTER_UPDATE(write_cache_io_timer, statistics->write_cache_io_timer);
232
9
    COUNTER_UPDATE(bytes_write_into_cache, statistics->bytes_write_into_cache);
233
9
    COUNTER_UPDATE(num_skip_cache_io_total, statistics->num_skip_cache_io_total);
234
9
    COUNTER_UPDATE(bytes_scanned_from_cache, statistics->bytes_read_from_local);
235
9
    COUNTER_UPDATE(bytes_scanned_from_remote, statistics->bytes_read_from_remote);
236
9
    COUNTER_UPDATE(bytes_scanned_from_peer, statistics->bytes_read_from_peer);
237
9
    COUNTER_UPDATE(read_cache_file_directly_timer, statistics->read_cache_file_directly_timer);
238
9
    COUNTER_UPDATE(cache_get_or_set_timer, statistics->cache_get_or_set_timer);
239
9
    COUNTER_UPDATE(lock_wait_timer, statistics->lock_wait_timer);
240
9
    COUNTER_UPDATE(get_timer, statistics->get_timer);
241
9
    COUNTER_UPDATE(set_timer, statistics->set_timer);
242
243
9
    COUNTER_UPDATE(inverted_index_num_local_io_total,
244
9
                   statistics->inverted_index_num_local_io_total);
245
9
    COUNTER_UPDATE(inverted_index_num_remote_io_total,
246
9
                   statistics->inverted_index_num_remote_io_total);
247
9
    COUNTER_UPDATE(inverted_index_num_peer_io_total, statistics->inverted_index_num_peer_io_total);
248
9
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_cache,
249
9
                   statistics->inverted_index_bytes_read_from_local);
250
9
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_remote,
251
9
                   statistics->inverted_index_bytes_read_from_remote);
252
9
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_peer,
253
9
                   statistics->inverted_index_bytes_read_from_peer);
254
9
    COUNTER_UPDATE(inverted_index_local_io_timer, statistics->inverted_index_local_io_timer);
255
9
    COUNTER_UPDATE(inverted_index_remote_io_timer, statistics->inverted_index_remote_io_timer);
256
9
    COUNTER_UPDATE(inverted_index_peer_io_timer, statistics->inverted_index_peer_io_timer);
257
9
    COUNTER_UPDATE(inverted_index_io_timer, statistics->inverted_index_io_timer);
258
259
9
    COUNTER_UPDATE(segment_footer_index_num_local_io_total,
260
9
                   statistics->segment_footer_index_num_local_io_total);
261
9
    COUNTER_UPDATE(segment_footer_index_num_remote_io_total,
262
9
                   statistics->segment_footer_index_num_remote_io_total);
263
9
    COUNTER_UPDATE(segment_footer_index_num_peer_io_total,
264
9
                   statistics->segment_footer_index_num_peer_io_total);
265
9
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_cache,
266
9
                   statistics->segment_footer_index_bytes_read_from_local);
267
9
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_remote,
268
9
                   statistics->segment_footer_index_bytes_read_from_remote);
269
9
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_peer,
270
9
                   statistics->segment_footer_index_bytes_read_from_peer);
271
9
    COUNTER_UPDATE(segment_footer_index_local_io_timer,
272
9
                   statistics->segment_footer_index_local_io_timer);
273
9
    COUNTER_UPDATE(segment_footer_index_remote_io_timer,
274
9
                   statistics->segment_footer_index_remote_io_timer);
275
9
    COUNTER_UPDATE(segment_footer_index_peer_io_timer,
276
9
                   statistics->segment_footer_index_peer_io_timer);
277
278
9
    COUNTER_UPDATE(num_cross_cg_peer_io_total, statistics->num_cross_cg_peer_io_total);
279
9
    COUNTER_UPDATE(bytes_scanned_from_cross_cg_peer, statistics->bytes_read_from_cross_cg_peer);
280
9
    COUNTER_UPDATE(cross_cg_peer_io_timer, statistics->cross_cg_peer_io_timer);
281
9
    COUNTER_UPDATE(num_same_cg_peer_io_total, statistics->num_same_cg_peer_io_total);
282
9
    COUNTER_UPDATE(bytes_scanned_from_same_cg_peer, statistics->bytes_read_from_same_cg_peer);
283
9
    COUNTER_UPDATE(same_cg_peer_io_timer, statistics->same_cg_peer_io_timer);
284
9
    COUNTER_UPDATE(num_peer_race_peer_win, statistics->num_peer_race_peer_win);
285
9
    COUNTER_UPDATE(num_peer_race_s3_win, statistics->num_peer_race_s3_win);
286
9
    COUNTER_UPDATE(num_peer_lazy_fetch, statistics->num_peer_lazy_fetch);
287
9
    COUNTER_UPDATE(peer_lazy_fetch_timer, statistics->peer_lazy_fetch_timer);
288
289
9
    if (!statistics->peer_hosts.empty() && _profile != nullptr) {
290
1
        std::string peer_nodes;
291
2
        for (const auto& host : statistics->peer_hosts) {
292
2
            if (!peer_nodes.empty()) {
293
1
                peer_nodes += ", ";
294
1
            }
295
2
            peer_nodes += host;
296
2
        }
297
1
        _profile->add_info_string("PeerCacheNodes", peer_nodes);
298
1
    }
299
9
}
300
301
} // namespace doris::io