Coverage Report

Created: 2024-09-08 07:17

/src/rocksdb/monitoring/perf_context.cc
Line
Count
Source (jump to first uncovered line)
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
12.9k
  defCmd(user_key_comparison_count)                \
53
12.9k
  defCmd(block_cache_hit_count)                    \
54
12.9k
  defCmd(block_read_count)                         \
55
12.9k
  defCmd(block_read_byte)                          \
56
12.9k
  defCmd(block_read_time)                          \
57
12.9k
  defCmd(block_read_cpu_time)                      \
58
12.9k
  defCmd(block_cache_index_hit_count)              \
59
12.9k
  defCmd(block_cache_standalone_handle_count)      \
60
12.9k
  defCmd(block_cache_real_handle_count)            \
61
12.9k
  defCmd(index_block_read_count)                   \
62
12.9k
  defCmd(block_cache_filter_hit_count)             \
63
12.9k
  defCmd(filter_block_read_count)                  \
64
12.9k
  defCmd(compression_dict_block_read_count)        \
65
12.9k
  defCmd(block_cache_index_read_byte)              \
66
12.9k
  defCmd(block_cache_filter_read_byte)             \
67
12.9k
  defCmd(block_cache_compression_dict_read_byte)   \
68
12.9k
  defCmd(block_cache_read_byte)                    \
69
12.9k
  defCmd(secondary_cache_hit_count)                \
70
12.9k
  defCmd(compressed_sec_cache_insert_real_count)   \
71
12.9k
  defCmd(compressed_sec_cache_insert_dummy_count)  \
72
12.9k
  defCmd(compressed_sec_cache_uncompressed_bytes)  \
73
12.9k
  defCmd(compressed_sec_cache_compressed_bytes)    \
74
12.9k
  defCmd(block_checksum_time)                      \
75
12.9k
  defCmd(block_decompress_time)                    \
76
12.9k
  defCmd(get_read_bytes)                           \
77
12.9k
  defCmd(multiget_read_bytes)                      \
78
12.9k
  defCmd(iter_read_bytes)                          \
79
12.9k
  defCmd(blob_cache_hit_count)                     \
80
12.9k
  defCmd(blob_read_count)                          \
81
12.9k
  defCmd(blob_read_byte)                           \
82
12.9k
  defCmd(blob_read_time)                           \
83
12.9k
  defCmd(blob_checksum_time)                       \
84
12.9k
  defCmd(blob_decompress_time)                     \
85
12.9k
  defCmd(internal_key_skipped_count)               \
86
12.9k
  defCmd(internal_delete_skipped_count)            \
87
12.9k
  defCmd(internal_recent_skipped_count)            \
88
12.9k
  defCmd(internal_merge_count)                     \
89
12.9k
  defCmd(internal_merge_point_lookup_count)        \
90
12.9k
  defCmd(internal_range_del_reseek_count)          \
91
12.9k
  defCmd(get_snapshot_time)                        \
92
12.9k
  defCmd(get_from_memtable_time)                   \
93
12.9k
  defCmd(get_from_memtable_count)                  \
94
12.9k
  defCmd(get_post_process_time)                    \
95
12.9k
  defCmd(get_from_output_files_time)               \
96
12.9k
  defCmd(seek_on_memtable_time)                    \
97
12.9k
  defCmd(seek_on_memtable_count)                   \
98
12.9k
  defCmd(next_on_memtable_count)                   \
99
12.9k
  defCmd(prev_on_memtable_count)                   \
100
12.9k
  defCmd(seek_child_seek_time)                     \
101
12.9k
  defCmd(seek_child_seek_count)                    \
102
12.9k
  defCmd(seek_min_heap_time)                       \
103
12.9k
  defCmd(seek_max_heap_time)                       \
104
12.9k
  defCmd(seek_internal_seek_time)                  \
105
12.9k
  defCmd(find_next_user_entry_time)                \
106
12.9k
  defCmd(write_wal_time)                           \
107
12.9k
  defCmd(write_memtable_time)                      \
108
12.9k
  defCmd(write_delay_time)                         \
109
12.9k
  defCmd(write_scheduling_flushes_compactions_time)\
110
12.9k
  defCmd(write_pre_and_post_process_time)          \
111
12.9k
  defCmd(write_thread_wait_nanos)                  \
112
12.9k
  defCmd(db_mutex_lock_nanos)                      \
113
12.9k
  defCmd(db_condition_wait_nanos)                  \
114
12.9k
  defCmd(merge_operator_time_nanos)                \
115
12.9k
  defCmd(read_index_block_nanos)                   \
116
12.9k
  defCmd(read_filter_block_nanos)                  \
117
12.9k
  defCmd(new_table_block_iter_nanos)               \
118
12.9k
  defCmd(new_table_iterator_nanos)                 \
119
12.9k
  defCmd(block_seek_nanos)                         \
120
12.9k
  defCmd(find_table_nanos)                         \
121
12.9k
  defCmd(bloom_memtable_hit_count)                 \
122
12.9k
  defCmd(bloom_memtable_miss_count)                \
123
12.9k
  defCmd(bloom_sst_hit_count)                      \
124
12.9k
  defCmd(bloom_sst_miss_count)                     \
125
12.9k
  defCmd(key_lock_wait_time)                       \
126
12.9k
  defCmd(key_lock_wait_count)                      \
127
12.9k
  defCmd(env_new_sequential_file_nanos)            \
128
12.9k
  defCmd(env_new_random_access_file_nanos)         \
129
12.9k
  defCmd(env_new_writable_file_nanos)              \
130
12.9k
  defCmd(env_reuse_writable_file_nanos)            \
131
12.9k
  defCmd(env_new_random_rw_file_nanos)             \
132
12.9k
  defCmd(env_new_directory_nanos)                  \
133
12.9k
  defCmd(env_file_exists_nanos)                    \
134
12.9k
  defCmd(env_get_children_nanos)                   \
135
12.9k
  defCmd(env_get_children_file_attributes_nanos)   \
136
12.9k
  defCmd(env_delete_file_nanos)                    \
137
12.9k
  defCmd(env_create_dir_nanos)                     \
138
12.9k
  defCmd(env_create_dir_if_missing_nanos)          \
139
12.9k
  defCmd(env_delete_dir_nanos)                     \
140
12.9k
  defCmd(env_get_file_size_nanos)                  \
141
12.9k
  defCmd(env_get_file_modification_time_nanos)     \
142
12.9k
  defCmd(env_rename_file_nanos)                    \
143
12.9k
  defCmd(env_link_file_nanos)                      \
144
12.9k
  defCmd(env_lock_file_nanos)                      \
145
12.9k
  defCmd(env_unlock_file_nanos)                    \
146
12.9k
  defCmd(env_new_logger_nanos)                     \
147
12.9k
  defCmd(get_cpu_nanos)                            \
148
12.9k
  defCmd(iter_next_cpu_nanos)                      \
149
12.9k
  defCmd(iter_prev_cpu_nanos)                      \
150
12.9k
  defCmd(iter_seek_cpu_nanos)                      \
151
12.9k
  defCmd(iter_next_count)                          \
152
12.9k
  defCmd(iter_prev_count)                          \
153
12.9k
  defCmd(iter_seek_count)                          \
154
12.9k
  defCmd(encrypt_data_nanos)                       \
155
12.9k
  defCmd(decrypt_data_nanos)                       \
156
12.9k
  defCmd(number_async_seek)
157
// clang-format on
158
159
struct PerfContextInt {
160
#define EMIT_FIELDS(x) uint64_t x;
161
  DEF_PERF_CONTEXT_METRICS(EMIT_FIELDS)
162
#undef EMIT_FIELDS
163
};
164
165
#if defined(NPERF_CONTEXT)
166
// Should not be used because the counters are not thread-safe.
167
// Put here just to make get_perf_context() simple without ifdef.
168
PerfContext perf_context;
169
#else
170
thread_local PerfContext perf_context;
171
#endif
172
173
0
PerfContext* get_perf_context() {
174
0
  static_assert(sizeof(PerfContextBase) == sizeof(PerfContextInt));
175
0
  static_assert(sizeof(PerfContextByLevelBase) ==
176
0
                sizeof(PerfContextByLevelInt));
177
  /*
178
   * Validate that we have the same fields and offsets between the external user
179
   * facing
180
   * ''PerfContextBase'' and ''PerfContextByLevelBase' structures with the
181
   * internal structures that we generate from the DEF_* macros above. This way
182
   * if people add metrics to the user-facing header file, they will have a
183
   * build failure and need to add similar fields to the macros in this file.
184
   * These are compile-time validations and don't impose any run-time penalties.
185
   */
186
0
#define EMIT_OFFSET_ASSERTION(x) \
187
0
  static_assert(offsetof(PerfContextBase, x) == offsetof(PerfContextInt, x));
188
0
  DEF_PERF_CONTEXT_METRICS(EMIT_OFFSET_ASSERTION)
189
0
#undef EMIT_OFFSET_ASSERTION
190
0
#define EMIT_OFFSET_ASSERTION(x)                       \
191
0
  static_assert(offsetof(PerfContextByLevelBase, x) == \
192
0
                offsetof(PerfContextByLevelInt, x));
193
0
  DEF_PERF_CONTEXT_LEVEL_METRICS(EMIT_OFFSET_ASSERTION)
194
0
#undef EMIT_OFFSET_ASSERTION
195
0
  return &perf_context;
196
0
}
197
198
12.9k
PerfContext::~PerfContext() {
199
12.9k
#if !defined(NPERF_CONTEXT) && !defined(OS_SOLARIS)
200
12.9k
  ClearPerLevelPerfContext();
201
12.9k
#endif
202
12.9k
}
203
204
0
PerfContext::PerfContext(const PerfContext& other) {
205
#ifdef NPERF_CONTEXT
206
  (void)other;
207
#else
208
0
  copyMetrics(&other);
209
0
#endif
210
0
}
211
212
0
PerfContext::PerfContext(PerfContext&& other) noexcept {
213
#ifdef NPERF_CONTEXT
214
  (void)other;
215
#else
216
0
  copyMetrics(&other);
217
0
#endif
218
0
}
219
220
0
PerfContext& PerfContext::operator=(const PerfContext& other) {
221
#ifdef NPERF_CONTEXT
222
  (void)other;
223
#else
224
0
  copyMetrics(&other);
225
0
#endif
226
0
  return *this;
227
0
}
228
229
0
void PerfContext::copyMetrics(const PerfContext* other) noexcept {
230
#ifdef NPERF_CONTEXT
231
  (void)other;
232
#else
233
0
#define EMIT_COPY_FIELDS(x) x = other->x;
234
0
  DEF_PERF_CONTEXT_METRICS(EMIT_COPY_FIELDS)
235
0
#undef EMIT_COPY_FIELDS
236
0
  if (per_level_perf_context_enabled && level_to_perf_context != nullptr) {
237
0
    ClearPerLevelPerfContext();
238
0
  }
239
0
  if (other->level_to_perf_context != nullptr) {
240
0
    level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
241
0
    *level_to_perf_context = *other->level_to_perf_context;
242
0
  }
243
0
  per_level_perf_context_enabled = other->per_level_perf_context_enabled;
244
0
#endif
245
0
}
246
247
12.9k
void PerfContext::Reset() {
248
12.9k
#ifndef NPERF_CONTEXT
249
1.36M
#define EMIT_FIELDS(x) x = 0;
250
1.36M
  DEF_PERF_CONTEXT_METRICS(EMIT_FIELDS)
251
12.9k
#undef EMIT_FIELDS
252
12.9k
  if (per_level_perf_context_enabled && level_to_perf_context) {
253
0
    for (auto& kv : *level_to_perf_context) {
254
0
      kv.second.Reset();
255
0
    }
256
0
  }
257
12.9k
#endif
258
12.9k
}
259
260
0
void PerfContextByLevel::Reset() {
261
0
#ifndef NPERF_CONTEXT
262
0
#define EMIT_FIELDS(x) x = 0;
263
0
  DEF_PERF_CONTEXT_LEVEL_METRICS(EMIT_FIELDS)
264
0
#undef EMIT_FIELDS
265
0
#endif
266
0
}
267
268
0
std::string PerfContext::ToString(bool exclude_zero_counters) const {
269
#ifdef NPERF_CONTEXT
270
  (void)exclude_zero_counters;
271
  return "";
272
#else
273
0
  std::ostringstream ss;
274
0
#define PERF_CONTEXT_OUTPUT(counter)             \
275
0
  if (!exclude_zero_counters || (counter > 0)) { \
276
0
    ss << #counter << " = " << counter << ", ";  \
277
0
  }
278
0
  DEF_PERF_CONTEXT_METRICS(PERF_CONTEXT_OUTPUT)
279
0
#undef PERF_CONTEXT_OUTPUT
280
0
  if (per_level_perf_context_enabled && level_to_perf_context) {
281
0
#define PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(counter)      \
282
0
  ss << #counter << " = ";                                     \
283
0
  for (auto& kv : *level_to_perf_context) {                    \
284
0
    if (!exclude_zero_counters || (kv.second.counter > 0)) {   \
285
0
      ss << kv.second.counter << "@level" << kv.first << ", "; \
286
0
    }                                                          \
287
0
  }
288
0
    DEF_PERF_CONTEXT_LEVEL_METRICS(PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER)
289
0
#undef PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER
290
0
  }
291
0
  std::string str = ss.str();
292
0
  str.erase(str.find_last_not_of(", ") + 1);
293
0
  return str;
294
0
#endif
295
0
}
296
297
0
void PerfContext::EnablePerLevelPerfContext() {
298
0
  if (level_to_perf_context == nullptr) {
299
0
    level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
300
0
  }
301
0
  per_level_perf_context_enabled = true;
302
0
}
303
304
0
void PerfContext::DisablePerLevelPerfContext() {
305
0
  per_level_perf_context_enabled = false;
306
0
}
307
308
12.9k
void PerfContext::ClearPerLevelPerfContext() {
309
12.9k
  if (level_to_perf_context != nullptr) {
310
0
    level_to_perf_context->clear();
311
0
    delete level_to_perf_context;
312
0
    level_to_perf_context = nullptr;
313
0
  }
314
12.9k
  per_level_perf_context_enabled = false;
315
12.9k
}
316
317
}  // namespace ROCKSDB_NAMESPACE