Coverage Report

Created: 2025-10-26 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/monitoring/perf_context.cc
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
//
6
7
#include <sstream>
8
9
#include "monitoring/perf_context_imp.h"
10
11
namespace ROCKSDB_NAMESPACE {
12
13
/*
14
 * Please add new metrics to this macro and appropriate fields will be copied,
15
 * and/or emitted when converted to string.
16
 * When people need to add new metrics please add the metric to the macro below
17
 * and enclose the name of the specific metric within defCmd().
18
 * The position of the field will be dictated by the
19
 * order in which the macros are enumerated and the offsets of the fields will
20
 * be matched against ''PerfContextByLevelBase'' declared in perf_context.h.
21
 */
22
// clang-format off
23
#define DEF_PERF_CONTEXT_LEVEL_METRICS(defCmd) \
24
0
  defCmd(bloom_filter_useful)                  \
25
0
  defCmd(bloom_filter_full_positive)           \
26
0
  defCmd(bloom_filter_full_true_positive)      \
27
0
  defCmd(user_key_return_count)                \
28
0
  defCmd(get_from_table_nanos)                 \
29
0
  defCmd(block_cache_hit_count)                \
30
0
  defCmd(block_cache_miss_count)
31
// clang-format on
32
33
// Break down performance counters by level and store per-level perf context in
34
// PerfContextByLevel
35
struct PerfContextByLevelInt {
36
#define EMIT_FIELDS(x) uint64_t x = 0;
37
  DEF_PERF_CONTEXT_LEVEL_METRICS(EMIT_FIELDS)
38
#undef EMIT_FIELDS
39
};
40
41
/*
42
 * Please add new metrics to this macro and appropriate fields will be copied,
43
 * and/or emitted when converted to string.
44
 * When people need to add new metrics please enclose the name of the specific
45
 * metric within defCmd(). The position of the field will be dictated by the
46
 * order in which the macros are enumerated and the offsets of the fields will
47
 * be matched against ''PerfContextBase'' declared in perf_context.h.
48
 */
49
50
// clang-format off
51
#define DEF_PERF_CONTEXT_METRICS(defCmd)           \
52
121k
  defCmd(user_key_comparison_count)                \
53
121k
  defCmd(block_cache_hit_count)                    \
54
121k
  defCmd(block_read_count)                         \
55
121k
  defCmd(block_read_byte)                          \
56
121k
  defCmd(block_read_time)                          \
57
121k
  defCmd(block_read_cpu_time)                      \
58
121k
  defCmd(block_cache_index_hit_count)              \
59
121k
  defCmd(block_cache_standalone_handle_count)      \
60
121k
  defCmd(block_cache_real_handle_count)            \
61
121k
  defCmd(index_block_read_count)                   \
62
121k
  defCmd(block_cache_filter_hit_count)             \
63
121k
  defCmd(filter_block_read_count)                  \
64
121k
  defCmd(compression_dict_block_read_count)        \
65
121k
  defCmd(block_cache_index_read_byte)              \
66
121k
  defCmd(block_cache_filter_read_byte)             \
67
121k
  defCmd(block_cache_compression_dict_read_byte)   \
68
121k
  defCmd(block_cache_read_byte)                    \
69
121k
  defCmd(secondary_cache_hit_count)                \
70
121k
  defCmd(compressed_sec_cache_insert_real_count)   \
71
121k
  defCmd(compressed_sec_cache_insert_dummy_count)  \
72
121k
  defCmd(compressed_sec_cache_uncompressed_bytes)  \
73
121k
  defCmd(compressed_sec_cache_compressed_bytes)    \
74
121k
  defCmd(block_checksum_time)                      \
75
121k
  defCmd(block_decompress_time)                    \
76
121k
  defCmd(get_read_bytes)                           \
77
121k
  defCmd(multiget_read_bytes)                      \
78
121k
  defCmd(iter_read_bytes)                          \
79
121k
  defCmd(blob_cache_hit_count)                     \
80
121k
  defCmd(blob_read_count)                          \
81
121k
  defCmd(blob_read_byte)                           \
82
121k
  defCmd(blob_read_time)                           \
83
121k
  defCmd(blob_checksum_time)                       \
84
121k
  defCmd(blob_decompress_time)                     \
85
121k
  defCmd(internal_key_skipped_count)               \
86
121k
  defCmd(internal_delete_skipped_count)            \
87
121k
  defCmd(internal_recent_skipped_count)            \
88
121k
  defCmd(internal_merge_count)                     \
89
121k
  defCmd(internal_merge_point_lookup_count)        \
90
121k
  defCmd(internal_range_del_reseek_count)          \
91
121k
  defCmd(get_snapshot_time)                        \
92
121k
  defCmd(get_from_memtable_time)                   \
93
121k
  defCmd(get_from_memtable_count)                  \
94
121k
  defCmd(get_post_process_time)                    \
95
121k
  defCmd(get_from_output_files_time)               \
96
121k
  defCmd(seek_on_memtable_time)                    \
97
121k
  defCmd(seek_on_memtable_count)                   \
98
121k
  defCmd(next_on_memtable_count)                   \
99
121k
  defCmd(prev_on_memtable_count)                   \
100
121k
  defCmd(seek_child_seek_time)                     \
101
121k
  defCmd(seek_child_seek_count)                    \
102
121k
  defCmd(seek_min_heap_time)                       \
103
121k
  defCmd(seek_max_heap_time)                       \
104
121k
  defCmd(seek_internal_seek_time)                  \
105
121k
  defCmd(find_next_user_entry_time)                \
106
121k
  defCmd(write_wal_time)                           \
107
121k
  defCmd(write_memtable_time)                      \
108
121k
  defCmd(write_delay_time)                         \
109
121k
  defCmd(write_scheduling_flushes_compactions_time)\
110
121k
  defCmd(write_pre_and_post_process_time)          \
111
121k
  defCmd(write_thread_wait_nanos)                  \
112
121k
  defCmd(db_mutex_lock_nanos)                      \
113
121k
  defCmd(db_condition_wait_nanos)                  \
114
121k
  defCmd(merge_operator_time_nanos)                \
115
121k
  defCmd(read_index_block_nanos)                   \
116
121k
  defCmd(read_filter_block_nanos)                  \
117
121k
  defCmd(new_table_block_iter_nanos)               \
118
121k
  defCmd(new_table_iterator_nanos)                 \
119
121k
  defCmd(block_seek_nanos)                         \
120
121k
  defCmd(find_table_nanos)                         \
121
121k
  defCmd(bloom_memtable_hit_count)                 \
122
121k
  defCmd(bloom_memtable_miss_count)                \
123
121k
  defCmd(bloom_sst_hit_count)                      \
124
121k
  defCmd(bloom_sst_miss_count)                     \
125
121k
  defCmd(key_lock_wait_time)                       \
126
121k
  defCmd(key_lock_wait_count)                      \
127
121k
  defCmd(env_new_sequential_file_nanos)            \
128
121k
  defCmd(env_new_random_access_file_nanos)         \
129
121k
  defCmd(env_new_writable_file_nanos)              \
130
121k
  defCmd(env_reuse_writable_file_nanos)            \
131
121k
  defCmd(env_new_random_rw_file_nanos)             \
132
121k
  defCmd(env_new_directory_nanos)                  \
133
121k
  defCmd(env_file_exists_nanos)                    \
134
121k
  defCmd(env_get_children_nanos)                   \
135
121k
  defCmd(env_get_children_file_attributes_nanos)   \
136
121k
  defCmd(env_delete_file_nanos)                    \
137
121k
  defCmd(env_create_dir_nanos)                     \
138
121k
  defCmd(env_create_dir_if_missing_nanos)          \
139
121k
  defCmd(env_delete_dir_nanos)                     \
140
121k
  defCmd(env_get_file_size_nanos)                  \
141
121k
  defCmd(env_get_file_modification_time_nanos)     \
142
121k
  defCmd(env_rename_file_nanos)                    \
143
121k
  defCmd(env_link_file_nanos)                      \
144
121k
  defCmd(env_lock_file_nanos)                      \
145
121k
  defCmd(env_unlock_file_nanos)                    \
146
121k
  defCmd(env_new_logger_nanos)                     \
147
121k
  defCmd(get_cpu_nanos)                            \
148
121k
  defCmd(iter_next_cpu_nanos)                      \
149
121k
  defCmd(iter_prev_cpu_nanos)                      \
150
121k
  defCmd(iter_seek_cpu_nanos)                      \
151
121k
  defCmd(iter_next_count)                          \
152
121k
  defCmd(iter_prev_count)                          \
153
121k
  defCmd(iter_seek_count)                          \
154
121k
  defCmd(encrypt_data_nanos)                       \
155
121k
  defCmd(decrypt_data_nanos)                       \
156
121k
  defCmd(number_async_seek)                        \
157
121k
  defCmd(file_ingestion_nanos)                     \
158
121k
  defCmd(file_ingestion_blocking_live_writes_nanos)
159
// clang-format on
160
161
struct PerfContextInt {
162
#define EMIT_FIELDS(x) uint64_t x;
163
  DEF_PERF_CONTEXT_METRICS(EMIT_FIELDS)
164
#undef EMIT_FIELDS
165
};
166
167
#if defined(NPERF_CONTEXT)
168
// Should not be used because the counters are not thread-safe.
169
// Put here just to make get_perf_context() simple without ifdef.
170
PerfContext perf_context;
171
#else
172
thread_local PerfContext perf_context;
173
#endif
174
175
0
PerfContext* get_perf_context() {
176
0
  static_assert(sizeof(PerfContextBase) == sizeof(PerfContextInt));
177
0
  static_assert(sizeof(PerfContextByLevelBase) ==
178
0
                sizeof(PerfContextByLevelInt));
179
  /*
180
   * Validate that we have the same fields and offsets between the external user
181
   * facing
182
   * ''PerfContextBase'' and ''PerfContextByLevelBase' structures with the
183
   * internal structures that we generate from the DEF_* macros above. This way
184
   * if people add metrics to the user-facing header file, they will have a
185
   * build failure and need to add similar fields to the macros in this file.
186
   * These are compile-time validations and don't impose any run-time penalties.
187
   */
188
0
#define EMIT_OFFSET_ASSERTION(x) \
189
0
  static_assert(offsetof(PerfContextBase, x) == offsetof(PerfContextInt, x));
190
0
  DEF_PERF_CONTEXT_METRICS(EMIT_OFFSET_ASSERTION)
191
0
#undef EMIT_OFFSET_ASSERTION
192
0
#define EMIT_OFFSET_ASSERTION(x)                       \
193
0
  static_assert(offsetof(PerfContextByLevelBase, x) == \
194
0
                offsetof(PerfContextByLevelInt, x));
195
0
  DEF_PERF_CONTEXT_LEVEL_METRICS(EMIT_OFFSET_ASSERTION)
196
0
#undef EMIT_OFFSET_ASSERTION
197
0
  return &perf_context;
198
0
}
199
200
121k
PerfContext::~PerfContext() {
201
121k
#if !defined(NPERF_CONTEXT) && !defined(OS_SOLARIS)
202
121k
  ClearPerLevelPerfContext();
203
121k
#endif
204
121k
}
205
206
0
PerfContext::PerfContext(const PerfContext& other) {
207
#ifdef NPERF_CONTEXT
208
  (void)other;
209
#else
210
0
  copyMetrics(&other);
211
0
#endif
212
0
}
213
214
0
PerfContext::PerfContext(PerfContext&& other) noexcept {
215
#ifdef NPERF_CONTEXT
216
  (void)other;
217
#else
218
0
  copyMetrics(&other);
219
0
#endif
220
0
}
221
222
0
PerfContext& PerfContext::operator=(const PerfContext& other) {
223
#ifdef NPERF_CONTEXT
224
  (void)other;
225
#else
226
0
  copyMetrics(&other);
227
0
#endif
228
0
  return *this;
229
0
}
230
231
0
void PerfContext::copyMetrics(const PerfContext* other) noexcept {
232
#ifdef NPERF_CONTEXT
233
  (void)other;
234
#else
235
0
#define EMIT_COPY_FIELDS(x) x = other->x;
236
0
  DEF_PERF_CONTEXT_METRICS(EMIT_COPY_FIELDS)
237
0
#undef EMIT_COPY_FIELDS
238
0
  if (per_level_perf_context_enabled && level_to_perf_context != nullptr) {
239
0
    ClearPerLevelPerfContext();
240
0
  }
241
0
  if (other->level_to_perf_context != nullptr) {
242
0
    level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
243
0
    *level_to_perf_context = *other->level_to_perf_context;
244
0
  }
245
0
  per_level_perf_context_enabled = other->per_level_perf_context_enabled;
246
0
#endif
247
0
}
248
249
121k
void PerfContext::Reset() {
250
121k
#ifndef NPERF_CONTEXT
251
13.0M
#define EMIT_FIELDS(x) x = 0;
252
13.0M
  DEF_PERF_CONTEXT_METRICS(EMIT_FIELDS)
253
121k
#undef EMIT_FIELDS
254
121k
  if (per_level_perf_context_enabled && level_to_perf_context) {
255
0
    for (auto& kv : *level_to_perf_context) {
256
0
      kv.second.Reset();
257
0
    }
258
0
  }
259
121k
#endif
260
121k
}
261
262
0
void PerfContextByLevel::Reset(){
263
0
#ifndef NPERF_CONTEXT
264
0
#define EMIT_FIELDS(x) x = 0;
265
0
    DEF_PERF_CONTEXT_LEVEL_METRICS(EMIT_FIELDS)
266
0
#undef EMIT_FIELDS
267
0
#endif
268
0
}
269
270
0
std::string PerfContext::ToString(bool exclude_zero_counters) const {
271
#ifdef NPERF_CONTEXT
272
  (void)exclude_zero_counters;
273
  return "";
274
#else
275
0
  std::ostringstream ss;
276
0
#define PERF_CONTEXT_OUTPUT(counter)             \
277
0
  if (!exclude_zero_counters || (counter > 0)) { \
278
0
    ss << #counter << " = " << counter << ", ";  \
279
0
  }
280
0
  DEF_PERF_CONTEXT_METRICS(PERF_CONTEXT_OUTPUT)
281
0
#undef PERF_CONTEXT_OUTPUT
282
0
  if (per_level_perf_context_enabled && level_to_perf_context) {
283
0
#define PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(counter)      \
284
0
  ss << #counter << " = ";                                     \
285
0
  for (auto& kv : *level_to_perf_context) {                    \
286
0
    if (!exclude_zero_counters || (kv.second.counter > 0)) {   \
287
0
      ss << kv.second.counter << "@level" << kv.first << ", "; \
288
0
    }                                                          \
289
0
  }
290
0
    DEF_PERF_CONTEXT_LEVEL_METRICS(PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER)
291
0
#undef PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER
292
0
  }
293
0
  std::string str = ss.str();
294
0
  str.erase(str.find_last_not_of(", ") + 1);
295
0
  return str;
296
0
#endif
297
0
}
298
299
0
void PerfContext::EnablePerLevelPerfContext() {
300
0
  if (level_to_perf_context == nullptr) {
301
0
    level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
302
0
  }
303
0
  per_level_perf_context_enabled = true;
304
0
}
305
306
0
void PerfContext::DisablePerLevelPerfContext() {
307
0
  per_level_perf_context_enabled = false;
308
0
}
309
310
121k
void PerfContext::ClearPerLevelPerfContext() {
311
121k
  if (level_to_perf_context != nullptr) {
312
0
    level_to_perf_context->clear();
313
0
    delete level_to_perf_context;
314
0
    level_to_perf_context = nullptr;
315
0
  }
316
121k
  per_level_perf_context_enabled = false;
317
121k
}
318
319
}  // namespace ROCKSDB_NAMESPACE