Coverage Report

Created: 2026-07-02 10:51

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) {
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
7
}
189
190
8
void FileCacheProfileReporter::update(const FileCacheStatistics* statistics) const {
191
8
    COUNTER_UPDATE(num_local_io_total, statistics->num_local_io_total);
192
8
    COUNTER_UPDATE(num_remote_io_total, statistics->num_remote_io_total);
193
8
    COUNTER_UPDATE(num_peer_io_total, statistics->num_peer_io_total);
194
8
    COUNTER_UPDATE(local_io_timer, statistics->local_io_timer);
195
8
    COUNTER_UPDATE(remote_io_timer, statistics->remote_io_timer);
196
8
    COUNTER_UPDATE(peer_io_timer, statistics->peer_io_timer);
197
8
    COUNTER_UPDATE(remote_wait_timer, statistics->remote_wait_timer);
198
8
    COUNTER_UPDATE(write_cache_io_timer, statistics->write_cache_io_timer);
199
8
    COUNTER_UPDATE(bytes_write_into_cache, statistics->bytes_write_into_cache);
200
8
    COUNTER_UPDATE(num_skip_cache_io_total, statistics->num_skip_cache_io_total);
201
8
    COUNTER_UPDATE(bytes_scanned_from_cache, statistics->bytes_read_from_local);
202
8
    COUNTER_UPDATE(bytes_scanned_from_remote, statistics->bytes_read_from_remote);
203
8
    COUNTER_UPDATE(bytes_scanned_from_peer, statistics->bytes_read_from_peer);
204
8
    COUNTER_UPDATE(read_cache_file_directly_timer, statistics->read_cache_file_directly_timer);
205
8
    COUNTER_UPDATE(cache_get_or_set_timer, statistics->cache_get_or_set_timer);
206
8
    COUNTER_UPDATE(lock_wait_timer, statistics->lock_wait_timer);
207
8
    COUNTER_UPDATE(get_timer, statistics->get_timer);
208
8
    COUNTER_UPDATE(set_timer, statistics->set_timer);
209
210
8
    COUNTER_UPDATE(inverted_index_num_local_io_total,
211
8
                   statistics->inverted_index_num_local_io_total);
212
8
    COUNTER_UPDATE(inverted_index_num_remote_io_total,
213
8
                   statistics->inverted_index_num_remote_io_total);
214
8
    COUNTER_UPDATE(inverted_index_num_peer_io_total, statistics->inverted_index_num_peer_io_total);
215
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_cache,
216
8
                   statistics->inverted_index_bytes_read_from_local);
217
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_remote,
218
8
                   statistics->inverted_index_bytes_read_from_remote);
219
8
    COUNTER_UPDATE(inverted_index_bytes_scanned_from_peer,
220
8
                   statistics->inverted_index_bytes_read_from_peer);
221
8
    COUNTER_UPDATE(inverted_index_local_io_timer, statistics->inverted_index_local_io_timer);
222
8
    COUNTER_UPDATE(inverted_index_remote_io_timer, statistics->inverted_index_remote_io_timer);
223
8
    COUNTER_UPDATE(inverted_index_peer_io_timer, statistics->inverted_index_peer_io_timer);
224
8
    COUNTER_UPDATE(inverted_index_io_timer, statistics->inverted_index_io_timer);
225
226
8
    COUNTER_UPDATE(segment_footer_index_num_local_io_total,
227
8
                   statistics->segment_footer_index_num_local_io_total);
228
8
    COUNTER_UPDATE(segment_footer_index_num_remote_io_total,
229
8
                   statistics->segment_footer_index_num_remote_io_total);
230
8
    COUNTER_UPDATE(segment_footer_index_num_peer_io_total,
231
8
                   statistics->segment_footer_index_num_peer_io_total);
232
8
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_cache,
233
8
                   statistics->segment_footer_index_bytes_read_from_local);
234
8
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_remote,
235
8
                   statistics->segment_footer_index_bytes_read_from_remote);
236
8
    COUNTER_UPDATE(segment_footer_index_bytes_scanned_from_peer,
237
8
                   statistics->segment_footer_index_bytes_read_from_peer);
238
8
    COUNTER_UPDATE(segment_footer_index_local_io_timer,
239
8
                   statistics->segment_footer_index_local_io_timer);
240
8
    COUNTER_UPDATE(segment_footer_index_remote_io_timer,
241
8
                   statistics->segment_footer_index_remote_io_timer);
242
8
    COUNTER_UPDATE(segment_footer_index_peer_io_timer,
243
8
                   statistics->segment_footer_index_peer_io_timer);
244
8
}
245
246
} // namespace doris::io