perf_context.cc 12 KB

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