perf_context.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. #if defined(NPERF_CONTEXT) || !defined(ROCKSDB_SUPPORT_THREAD_LOCAL)
  10. PerfContext perf_context;
  11. #else
  12. #if defined(OS_SOLARIS)
  13. __thread PerfContext perf_context_;
  14. #else
  15. thread_local PerfContext perf_context;
  16. #endif
  17. #endif
  18. PerfContext* get_perf_context() {
  19. #if defined(NPERF_CONTEXT) || !defined(ROCKSDB_SUPPORT_THREAD_LOCAL)
  20. return &perf_context;
  21. #else
  22. #if defined(OS_SOLARIS)
  23. return &perf_context_;
  24. #else
  25. return &perf_context;
  26. #endif
  27. #endif
  28. }
  29. PerfContext::~PerfContext() {
  30. #if !defined(NPERF_CONTEXT) && defined(ROCKSDB_SUPPORT_THREAD_LOCAL) && !defined(OS_SOLARIS)
  31. ClearPerLevelPerfContext();
  32. #endif
  33. }
  34. PerfContext::PerfContext(const PerfContext& other) {
  35. #ifndef NPERF_CONTEXT
  36. user_key_comparison_count = other.user_key_comparison_count;
  37. block_cache_hit_count = other.block_cache_hit_count;
  38. block_read_count = other.block_read_count;
  39. block_read_byte = other.block_read_byte;
  40. block_read_time = other.block_read_time;
  41. block_cache_index_hit_count = other.block_cache_index_hit_count;
  42. index_block_read_count = other.index_block_read_count;
  43. block_cache_filter_hit_count = other.block_cache_filter_hit_count;
  44. filter_block_read_count = other.filter_block_read_count;
  45. compression_dict_block_read_count = other.compression_dict_block_read_count;
  46. block_checksum_time = other.block_checksum_time;
  47. block_decompress_time = other.block_decompress_time;
  48. get_read_bytes = other.get_read_bytes;
  49. multiget_read_bytes = other.multiget_read_bytes;
  50. iter_read_bytes = other.iter_read_bytes;
  51. internal_key_skipped_count = other.internal_key_skipped_count;
  52. internal_delete_skipped_count = other.internal_delete_skipped_count;
  53. internal_recent_skipped_count = other.internal_recent_skipped_count;
  54. internal_merge_count = other.internal_merge_count;
  55. write_wal_time = other.write_wal_time;
  56. get_snapshot_time = other.get_snapshot_time;
  57. get_from_memtable_time = other.get_from_memtable_time;
  58. get_from_memtable_count = other.get_from_memtable_count;
  59. get_post_process_time = other.get_post_process_time;
  60. get_from_output_files_time = other.get_from_output_files_time;
  61. seek_on_memtable_time = other.seek_on_memtable_time;
  62. seek_on_memtable_count = other.seek_on_memtable_count;
  63. next_on_memtable_count = other.next_on_memtable_count;
  64. prev_on_memtable_count = other.prev_on_memtable_count;
  65. seek_child_seek_time = other.seek_child_seek_time;
  66. seek_child_seek_count = other.seek_child_seek_count;
  67. seek_min_heap_time = other.seek_min_heap_time;
  68. seek_internal_seek_time = other.seek_internal_seek_time;
  69. find_next_user_entry_time = other.find_next_user_entry_time;
  70. write_pre_and_post_process_time = other.write_pre_and_post_process_time;
  71. write_memtable_time = other.write_memtable_time;
  72. write_delay_time = other.write_delay_time;
  73. write_thread_wait_nanos = other.write_thread_wait_nanos;
  74. write_scheduling_flushes_compactions_time =
  75. other.write_scheduling_flushes_compactions_time;
  76. db_mutex_lock_nanos = other.db_mutex_lock_nanos;
  77. db_condition_wait_nanos = other.db_condition_wait_nanos;
  78. merge_operator_time_nanos = other.merge_operator_time_nanos;
  79. read_index_block_nanos = other.read_index_block_nanos;
  80. read_filter_block_nanos = other.read_filter_block_nanos;
  81. new_table_block_iter_nanos = other.new_table_block_iter_nanos;
  82. new_table_iterator_nanos = other.new_table_iterator_nanos;
  83. block_seek_nanos = other.block_seek_nanos;
  84. find_table_nanos = other.find_table_nanos;
  85. bloom_memtable_hit_count = other.bloom_memtable_hit_count;
  86. bloom_memtable_miss_count = other.bloom_memtable_miss_count;
  87. bloom_sst_hit_count = other.bloom_sst_hit_count;
  88. bloom_sst_miss_count = other.bloom_sst_miss_count;
  89. key_lock_wait_time = other.key_lock_wait_time;
  90. key_lock_wait_count = other.key_lock_wait_count;
  91. env_new_sequential_file_nanos = other.env_new_sequential_file_nanos;
  92. env_new_random_access_file_nanos = other.env_new_random_access_file_nanos;
  93. env_new_writable_file_nanos = other.env_new_writable_file_nanos;
  94. env_reuse_writable_file_nanos = other.env_reuse_writable_file_nanos;
  95. env_new_random_rw_file_nanos = other.env_new_random_rw_file_nanos;
  96. env_new_directory_nanos = other.env_new_directory_nanos;
  97. env_file_exists_nanos = other.env_file_exists_nanos;
  98. env_get_children_nanos = other.env_get_children_nanos;
  99. env_get_children_file_attributes_nanos =
  100. other.env_get_children_file_attributes_nanos;
  101. env_delete_file_nanos = other.env_delete_file_nanos;
  102. env_create_dir_nanos = other.env_create_dir_nanos;
  103. env_create_dir_if_missing_nanos = other.env_create_dir_if_missing_nanos;
  104. env_delete_dir_nanos = other.env_delete_dir_nanos;
  105. env_get_file_size_nanos = other.env_get_file_size_nanos;
  106. env_get_file_modification_time_nanos =
  107. other.env_get_file_modification_time_nanos;
  108. env_rename_file_nanos = other.env_rename_file_nanos;
  109. env_link_file_nanos = other.env_link_file_nanos;
  110. env_lock_file_nanos = other.env_lock_file_nanos;
  111. env_unlock_file_nanos = other.env_unlock_file_nanos;
  112. env_new_logger_nanos = other.env_new_logger_nanos;
  113. get_cpu_nanos = other.get_cpu_nanos;
  114. iter_next_cpu_nanos = other.iter_next_cpu_nanos;
  115. iter_prev_cpu_nanos = other.iter_prev_cpu_nanos;
  116. iter_seek_cpu_nanos = other.iter_seek_cpu_nanos;
  117. if (per_level_perf_context_enabled && level_to_perf_context != nullptr) {
  118. ClearPerLevelPerfContext();
  119. }
  120. if (other.level_to_perf_context != nullptr) {
  121. level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
  122. *level_to_perf_context = *other.level_to_perf_context;
  123. }
  124. per_level_perf_context_enabled = other.per_level_perf_context_enabled;
  125. #endif
  126. }
  127. PerfContext::PerfContext(PerfContext&& other) noexcept {
  128. #ifndef NPERF_CONTEXT
  129. user_key_comparison_count = other.user_key_comparison_count;
  130. block_cache_hit_count = other.block_cache_hit_count;
  131. block_read_count = other.block_read_count;
  132. block_read_byte = other.block_read_byte;
  133. block_read_time = other.block_read_time;
  134. block_cache_index_hit_count = other.block_cache_index_hit_count;
  135. index_block_read_count = other.index_block_read_count;
  136. block_cache_filter_hit_count = other.block_cache_filter_hit_count;
  137. filter_block_read_count = other.filter_block_read_count;
  138. compression_dict_block_read_count = other.compression_dict_block_read_count;
  139. block_checksum_time = other.block_checksum_time;
  140. block_decompress_time = other.block_decompress_time;
  141. get_read_bytes = other.get_read_bytes;
  142. multiget_read_bytes = other.multiget_read_bytes;
  143. iter_read_bytes = other.iter_read_bytes;
  144. internal_key_skipped_count = other.internal_key_skipped_count;
  145. internal_delete_skipped_count = other.internal_delete_skipped_count;
  146. internal_recent_skipped_count = other.internal_recent_skipped_count;
  147. internal_merge_count = other.internal_merge_count;
  148. write_wal_time = other.write_wal_time;
  149. get_snapshot_time = other.get_snapshot_time;
  150. get_from_memtable_time = other.get_from_memtable_time;
  151. get_from_memtable_count = other.get_from_memtable_count;
  152. get_post_process_time = other.get_post_process_time;
  153. get_from_output_files_time = other.get_from_output_files_time;
  154. seek_on_memtable_time = other.seek_on_memtable_time;
  155. seek_on_memtable_count = other.seek_on_memtable_count;
  156. next_on_memtable_count = other.next_on_memtable_count;
  157. prev_on_memtable_count = other.prev_on_memtable_count;
  158. seek_child_seek_time = other.seek_child_seek_time;
  159. seek_child_seek_count = other.seek_child_seek_count;
  160. seek_min_heap_time = other.seek_min_heap_time;
  161. seek_internal_seek_time = other.seek_internal_seek_time;
  162. find_next_user_entry_time = other.find_next_user_entry_time;
  163. write_pre_and_post_process_time = other.write_pre_and_post_process_time;
  164. write_memtable_time = other.write_memtable_time;
  165. write_delay_time = other.write_delay_time;
  166. write_thread_wait_nanos = other.write_thread_wait_nanos;
  167. write_scheduling_flushes_compactions_time =
  168. other.write_scheduling_flushes_compactions_time;
  169. db_mutex_lock_nanos = other.db_mutex_lock_nanos;
  170. db_condition_wait_nanos = other.db_condition_wait_nanos;
  171. merge_operator_time_nanos = other.merge_operator_time_nanos;
  172. read_index_block_nanos = other.read_index_block_nanos;
  173. read_filter_block_nanos = other.read_filter_block_nanos;
  174. new_table_block_iter_nanos = other.new_table_block_iter_nanos;
  175. new_table_iterator_nanos = other.new_table_iterator_nanos;
  176. block_seek_nanos = other.block_seek_nanos;
  177. find_table_nanos = other.find_table_nanos;
  178. bloom_memtable_hit_count = other.bloom_memtable_hit_count;
  179. bloom_memtable_miss_count = other.bloom_memtable_miss_count;
  180. bloom_sst_hit_count = other.bloom_sst_hit_count;
  181. bloom_sst_miss_count = other.bloom_sst_miss_count;
  182. key_lock_wait_time = other.key_lock_wait_time;
  183. key_lock_wait_count = other.key_lock_wait_count;
  184. env_new_sequential_file_nanos = other.env_new_sequential_file_nanos;
  185. env_new_random_access_file_nanos = other.env_new_random_access_file_nanos;
  186. env_new_writable_file_nanos = other.env_new_writable_file_nanos;
  187. env_reuse_writable_file_nanos = other.env_reuse_writable_file_nanos;
  188. env_new_random_rw_file_nanos = other.env_new_random_rw_file_nanos;
  189. env_new_directory_nanos = other.env_new_directory_nanos;
  190. env_file_exists_nanos = other.env_file_exists_nanos;
  191. env_get_children_nanos = other.env_get_children_nanos;
  192. env_get_children_file_attributes_nanos =
  193. other.env_get_children_file_attributes_nanos;
  194. env_delete_file_nanos = other.env_delete_file_nanos;
  195. env_create_dir_nanos = other.env_create_dir_nanos;
  196. env_create_dir_if_missing_nanos = other.env_create_dir_if_missing_nanos;
  197. env_delete_dir_nanos = other.env_delete_dir_nanos;
  198. env_get_file_size_nanos = other.env_get_file_size_nanos;
  199. env_get_file_modification_time_nanos =
  200. other.env_get_file_modification_time_nanos;
  201. env_rename_file_nanos = other.env_rename_file_nanos;
  202. env_link_file_nanos = other.env_link_file_nanos;
  203. env_lock_file_nanos = other.env_lock_file_nanos;
  204. env_unlock_file_nanos = other.env_unlock_file_nanos;
  205. env_new_logger_nanos = other.env_new_logger_nanos;
  206. get_cpu_nanos = other.get_cpu_nanos;
  207. iter_next_cpu_nanos = other.iter_next_cpu_nanos;
  208. iter_prev_cpu_nanos = other.iter_prev_cpu_nanos;
  209. iter_seek_cpu_nanos = other.iter_seek_cpu_nanos;
  210. if (per_level_perf_context_enabled && level_to_perf_context != nullptr) {
  211. ClearPerLevelPerfContext();
  212. }
  213. if (other.level_to_perf_context != nullptr) {
  214. level_to_perf_context = other.level_to_perf_context;
  215. other.level_to_perf_context = nullptr;
  216. }
  217. per_level_perf_context_enabled = other.per_level_perf_context_enabled;
  218. #endif
  219. }
  220. // TODO(Zhongyi): reduce code duplication between copy constructor and
  221. // assignment operator
  222. PerfContext& PerfContext::operator=(const PerfContext& other) {
  223. #ifndef NPERF_CONTEXT
  224. user_key_comparison_count = other.user_key_comparison_count;
  225. block_cache_hit_count = other.block_cache_hit_count;
  226. block_read_count = other.block_read_count;
  227. block_read_byte = other.block_read_byte;
  228. block_read_time = other.block_read_time;
  229. block_cache_index_hit_count = other.block_cache_index_hit_count;
  230. index_block_read_count = other.index_block_read_count;
  231. block_cache_filter_hit_count = other.block_cache_filter_hit_count;
  232. filter_block_read_count = other.filter_block_read_count;
  233. compression_dict_block_read_count = other.compression_dict_block_read_count;
  234. block_checksum_time = other.block_checksum_time;
  235. block_decompress_time = other.block_decompress_time;
  236. get_read_bytes = other.get_read_bytes;
  237. multiget_read_bytes = other.multiget_read_bytes;
  238. iter_read_bytes = other.iter_read_bytes;
  239. internal_key_skipped_count = other.internal_key_skipped_count;
  240. internal_delete_skipped_count = other.internal_delete_skipped_count;
  241. internal_recent_skipped_count = other.internal_recent_skipped_count;
  242. internal_merge_count = other.internal_merge_count;
  243. write_wal_time = other.write_wal_time;
  244. get_snapshot_time = other.get_snapshot_time;
  245. get_from_memtable_time = other.get_from_memtable_time;
  246. get_from_memtable_count = other.get_from_memtable_count;
  247. get_post_process_time = other.get_post_process_time;
  248. get_from_output_files_time = other.get_from_output_files_time;
  249. seek_on_memtable_time = other.seek_on_memtable_time;
  250. seek_on_memtable_count = other.seek_on_memtable_count;
  251. next_on_memtable_count = other.next_on_memtable_count;
  252. prev_on_memtable_count = other.prev_on_memtable_count;
  253. seek_child_seek_time = other.seek_child_seek_time;
  254. seek_child_seek_count = other.seek_child_seek_count;
  255. seek_min_heap_time = other.seek_min_heap_time;
  256. seek_internal_seek_time = other.seek_internal_seek_time;
  257. find_next_user_entry_time = other.find_next_user_entry_time;
  258. write_pre_and_post_process_time = other.write_pre_and_post_process_time;
  259. write_memtable_time = other.write_memtable_time;
  260. write_delay_time = other.write_delay_time;
  261. write_thread_wait_nanos = other.write_thread_wait_nanos;
  262. write_scheduling_flushes_compactions_time =
  263. other.write_scheduling_flushes_compactions_time;
  264. db_mutex_lock_nanos = other.db_mutex_lock_nanos;
  265. db_condition_wait_nanos = other.db_condition_wait_nanos;
  266. merge_operator_time_nanos = other.merge_operator_time_nanos;
  267. read_index_block_nanos = other.read_index_block_nanos;
  268. read_filter_block_nanos = other.read_filter_block_nanos;
  269. new_table_block_iter_nanos = other.new_table_block_iter_nanos;
  270. new_table_iterator_nanos = other.new_table_iterator_nanos;
  271. block_seek_nanos = other.block_seek_nanos;
  272. find_table_nanos = other.find_table_nanos;
  273. bloom_memtable_hit_count = other.bloom_memtable_hit_count;
  274. bloom_memtable_miss_count = other.bloom_memtable_miss_count;
  275. bloom_sst_hit_count = other.bloom_sst_hit_count;
  276. bloom_sst_miss_count = other.bloom_sst_miss_count;
  277. key_lock_wait_time = other.key_lock_wait_time;
  278. key_lock_wait_count = other.key_lock_wait_count;
  279. env_new_sequential_file_nanos = other.env_new_sequential_file_nanos;
  280. env_new_random_access_file_nanos = other.env_new_random_access_file_nanos;
  281. env_new_writable_file_nanos = other.env_new_writable_file_nanos;
  282. env_reuse_writable_file_nanos = other.env_reuse_writable_file_nanos;
  283. env_new_random_rw_file_nanos = other.env_new_random_rw_file_nanos;
  284. env_new_directory_nanos = other.env_new_directory_nanos;
  285. env_file_exists_nanos = other.env_file_exists_nanos;
  286. env_get_children_nanos = other.env_get_children_nanos;
  287. env_get_children_file_attributes_nanos =
  288. other.env_get_children_file_attributes_nanos;
  289. env_delete_file_nanos = other.env_delete_file_nanos;
  290. env_create_dir_nanos = other.env_create_dir_nanos;
  291. env_create_dir_if_missing_nanos = other.env_create_dir_if_missing_nanos;
  292. env_delete_dir_nanos = other.env_delete_dir_nanos;
  293. env_get_file_size_nanos = other.env_get_file_size_nanos;
  294. env_get_file_modification_time_nanos =
  295. other.env_get_file_modification_time_nanos;
  296. env_rename_file_nanos = other.env_rename_file_nanos;
  297. env_link_file_nanos = other.env_link_file_nanos;
  298. env_lock_file_nanos = other.env_lock_file_nanos;
  299. env_unlock_file_nanos = other.env_unlock_file_nanos;
  300. env_new_logger_nanos = other.env_new_logger_nanos;
  301. get_cpu_nanos = other.get_cpu_nanos;
  302. iter_next_cpu_nanos = other.iter_next_cpu_nanos;
  303. iter_prev_cpu_nanos = other.iter_prev_cpu_nanos;
  304. iter_seek_cpu_nanos = other.iter_seek_cpu_nanos;
  305. if (per_level_perf_context_enabled && level_to_perf_context != nullptr) {
  306. ClearPerLevelPerfContext();
  307. }
  308. if (other.level_to_perf_context != nullptr) {
  309. level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
  310. *level_to_perf_context = *other.level_to_perf_context;
  311. }
  312. per_level_perf_context_enabled = other.per_level_perf_context_enabled;
  313. #endif
  314. return *this;
  315. }
  316. void PerfContext::Reset() {
  317. #ifndef NPERF_CONTEXT
  318. user_key_comparison_count = 0;
  319. block_cache_hit_count = 0;
  320. block_read_count = 0;
  321. block_read_byte = 0;
  322. block_read_time = 0;
  323. block_cache_index_hit_count = 0;
  324. index_block_read_count = 0;
  325. block_cache_filter_hit_count = 0;
  326. filter_block_read_count = 0;
  327. compression_dict_block_read_count = 0;
  328. block_checksum_time = 0;
  329. block_decompress_time = 0;
  330. get_read_bytes = 0;
  331. multiget_read_bytes = 0;
  332. iter_read_bytes = 0;
  333. internal_key_skipped_count = 0;
  334. internal_delete_skipped_count = 0;
  335. internal_recent_skipped_count = 0;
  336. internal_merge_count = 0;
  337. write_wal_time = 0;
  338. get_snapshot_time = 0;
  339. get_from_memtable_time = 0;
  340. get_from_memtable_count = 0;
  341. get_post_process_time = 0;
  342. get_from_output_files_time = 0;
  343. seek_on_memtable_time = 0;
  344. seek_on_memtable_count = 0;
  345. next_on_memtable_count = 0;
  346. prev_on_memtable_count = 0;
  347. seek_child_seek_time = 0;
  348. seek_child_seek_count = 0;
  349. seek_min_heap_time = 0;
  350. seek_internal_seek_time = 0;
  351. find_next_user_entry_time = 0;
  352. write_pre_and_post_process_time = 0;
  353. write_memtable_time = 0;
  354. write_delay_time = 0;
  355. write_thread_wait_nanos = 0;
  356. write_scheduling_flushes_compactions_time = 0;
  357. db_mutex_lock_nanos = 0;
  358. db_condition_wait_nanos = 0;
  359. merge_operator_time_nanos = 0;
  360. read_index_block_nanos = 0;
  361. read_filter_block_nanos = 0;
  362. new_table_block_iter_nanos = 0;
  363. new_table_iterator_nanos = 0;
  364. block_seek_nanos = 0;
  365. find_table_nanos = 0;
  366. bloom_memtable_hit_count = 0;
  367. bloom_memtable_miss_count = 0;
  368. bloom_sst_hit_count = 0;
  369. bloom_sst_miss_count = 0;
  370. key_lock_wait_time = 0;
  371. key_lock_wait_count = 0;
  372. env_new_sequential_file_nanos = 0;
  373. env_new_random_access_file_nanos = 0;
  374. env_new_writable_file_nanos = 0;
  375. env_reuse_writable_file_nanos = 0;
  376. env_new_random_rw_file_nanos = 0;
  377. env_new_directory_nanos = 0;
  378. env_file_exists_nanos = 0;
  379. env_get_children_nanos = 0;
  380. env_get_children_file_attributes_nanos = 0;
  381. env_delete_file_nanos = 0;
  382. env_create_dir_nanos = 0;
  383. env_create_dir_if_missing_nanos = 0;
  384. env_delete_dir_nanos = 0;
  385. env_get_file_size_nanos = 0;
  386. env_get_file_modification_time_nanos = 0;
  387. env_rename_file_nanos = 0;
  388. env_link_file_nanos = 0;
  389. env_lock_file_nanos = 0;
  390. env_unlock_file_nanos = 0;
  391. env_new_logger_nanos = 0;
  392. get_cpu_nanos = 0;
  393. iter_next_cpu_nanos = 0;
  394. iter_prev_cpu_nanos = 0;
  395. iter_seek_cpu_nanos = 0;
  396. if (per_level_perf_context_enabled && level_to_perf_context) {
  397. for (auto& kv : *level_to_perf_context) {
  398. kv.second.Reset();
  399. }
  400. }
  401. #endif
  402. }
  403. #define PERF_CONTEXT_OUTPUT(counter) \
  404. if (!exclude_zero_counters || (counter > 0)) { \
  405. ss << #counter << " = " << counter << ", "; \
  406. }
  407. #define PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(counter) \
  408. if (per_level_perf_context_enabled && \
  409. level_to_perf_context) { \
  410. ss << #counter << " = "; \
  411. for (auto& kv : *level_to_perf_context) { \
  412. if (!exclude_zero_counters || (kv.second.counter > 0)) { \
  413. ss << kv.second.counter << "@level" << kv.first << ", "; \
  414. } \
  415. } \
  416. }
  417. void PerfContextByLevel::Reset() {
  418. #ifndef NPERF_CONTEXT
  419. bloom_filter_useful = 0;
  420. bloom_filter_full_positive = 0;
  421. bloom_filter_full_true_positive = 0;
  422. block_cache_hit_count = 0;
  423. block_cache_miss_count = 0;
  424. #endif
  425. }
  426. std::string PerfContext::ToString(bool exclude_zero_counters) const {
  427. #ifdef NPERF_CONTEXT
  428. return "";
  429. #else
  430. std::ostringstream ss;
  431. PERF_CONTEXT_OUTPUT(user_key_comparison_count);
  432. PERF_CONTEXT_OUTPUT(block_cache_hit_count);
  433. PERF_CONTEXT_OUTPUT(block_read_count);
  434. PERF_CONTEXT_OUTPUT(block_read_byte);
  435. PERF_CONTEXT_OUTPUT(block_read_time);
  436. PERF_CONTEXT_OUTPUT(block_cache_index_hit_count);
  437. PERF_CONTEXT_OUTPUT(index_block_read_count);
  438. PERF_CONTEXT_OUTPUT(block_cache_filter_hit_count);
  439. PERF_CONTEXT_OUTPUT(filter_block_read_count);
  440. PERF_CONTEXT_OUTPUT(compression_dict_block_read_count);
  441. PERF_CONTEXT_OUTPUT(block_checksum_time);
  442. PERF_CONTEXT_OUTPUT(block_decompress_time);
  443. PERF_CONTEXT_OUTPUT(get_read_bytes);
  444. PERF_CONTEXT_OUTPUT(multiget_read_bytes);
  445. PERF_CONTEXT_OUTPUT(iter_read_bytes);
  446. PERF_CONTEXT_OUTPUT(internal_key_skipped_count);
  447. PERF_CONTEXT_OUTPUT(internal_delete_skipped_count);
  448. PERF_CONTEXT_OUTPUT(internal_recent_skipped_count);
  449. PERF_CONTEXT_OUTPUT(internal_merge_count);
  450. PERF_CONTEXT_OUTPUT(write_wal_time);
  451. PERF_CONTEXT_OUTPUT(get_snapshot_time);
  452. PERF_CONTEXT_OUTPUT(get_from_memtable_time);
  453. PERF_CONTEXT_OUTPUT(get_from_memtable_count);
  454. PERF_CONTEXT_OUTPUT(get_post_process_time);
  455. PERF_CONTEXT_OUTPUT(get_from_output_files_time);
  456. PERF_CONTEXT_OUTPUT(seek_on_memtable_time);
  457. PERF_CONTEXT_OUTPUT(seek_on_memtable_count);
  458. PERF_CONTEXT_OUTPUT(next_on_memtable_count);
  459. PERF_CONTEXT_OUTPUT(prev_on_memtable_count);
  460. PERF_CONTEXT_OUTPUT(seek_child_seek_time);
  461. PERF_CONTEXT_OUTPUT(seek_child_seek_count);
  462. PERF_CONTEXT_OUTPUT(seek_min_heap_time);
  463. PERF_CONTEXT_OUTPUT(seek_internal_seek_time);
  464. PERF_CONTEXT_OUTPUT(find_next_user_entry_time);
  465. PERF_CONTEXT_OUTPUT(write_pre_and_post_process_time);
  466. PERF_CONTEXT_OUTPUT(write_memtable_time);
  467. PERF_CONTEXT_OUTPUT(write_thread_wait_nanos);
  468. PERF_CONTEXT_OUTPUT(write_scheduling_flushes_compactions_time);
  469. PERF_CONTEXT_OUTPUT(db_mutex_lock_nanos);
  470. PERF_CONTEXT_OUTPUT(db_condition_wait_nanos);
  471. PERF_CONTEXT_OUTPUT(merge_operator_time_nanos);
  472. PERF_CONTEXT_OUTPUT(write_delay_time);
  473. PERF_CONTEXT_OUTPUT(read_index_block_nanos);
  474. PERF_CONTEXT_OUTPUT(read_filter_block_nanos);
  475. PERF_CONTEXT_OUTPUT(new_table_block_iter_nanos);
  476. PERF_CONTEXT_OUTPUT(new_table_iterator_nanos);
  477. PERF_CONTEXT_OUTPUT(block_seek_nanos);
  478. PERF_CONTEXT_OUTPUT(find_table_nanos);
  479. PERF_CONTEXT_OUTPUT(bloom_memtable_hit_count);
  480. PERF_CONTEXT_OUTPUT(bloom_memtable_miss_count);
  481. PERF_CONTEXT_OUTPUT(bloom_sst_hit_count);
  482. PERF_CONTEXT_OUTPUT(bloom_sst_miss_count);
  483. PERF_CONTEXT_OUTPUT(key_lock_wait_time);
  484. PERF_CONTEXT_OUTPUT(key_lock_wait_count);
  485. PERF_CONTEXT_OUTPUT(env_new_sequential_file_nanos);
  486. PERF_CONTEXT_OUTPUT(env_new_random_access_file_nanos);
  487. PERF_CONTEXT_OUTPUT(env_new_writable_file_nanos);
  488. PERF_CONTEXT_OUTPUT(env_reuse_writable_file_nanos);
  489. PERF_CONTEXT_OUTPUT(env_new_random_rw_file_nanos);
  490. PERF_CONTEXT_OUTPUT(env_new_directory_nanos);
  491. PERF_CONTEXT_OUTPUT(env_file_exists_nanos);
  492. PERF_CONTEXT_OUTPUT(env_get_children_nanos);
  493. PERF_CONTEXT_OUTPUT(env_get_children_file_attributes_nanos);
  494. PERF_CONTEXT_OUTPUT(env_delete_file_nanos);
  495. PERF_CONTEXT_OUTPUT(env_create_dir_nanos);
  496. PERF_CONTEXT_OUTPUT(env_create_dir_if_missing_nanos);
  497. PERF_CONTEXT_OUTPUT(env_delete_dir_nanos);
  498. PERF_CONTEXT_OUTPUT(env_get_file_size_nanos);
  499. PERF_CONTEXT_OUTPUT(env_get_file_modification_time_nanos);
  500. PERF_CONTEXT_OUTPUT(env_rename_file_nanos);
  501. PERF_CONTEXT_OUTPUT(env_link_file_nanos);
  502. PERF_CONTEXT_OUTPUT(env_lock_file_nanos);
  503. PERF_CONTEXT_OUTPUT(env_unlock_file_nanos);
  504. PERF_CONTEXT_OUTPUT(env_new_logger_nanos);
  505. PERF_CONTEXT_OUTPUT(get_cpu_nanos);
  506. PERF_CONTEXT_OUTPUT(iter_next_cpu_nanos);
  507. PERF_CONTEXT_OUTPUT(iter_prev_cpu_nanos);
  508. PERF_CONTEXT_OUTPUT(iter_seek_cpu_nanos);
  509. PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(bloom_filter_useful);
  510. PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(bloom_filter_full_positive);
  511. PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(bloom_filter_full_true_positive);
  512. PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(block_cache_hit_count);
  513. PERF_CONTEXT_BY_LEVEL_OUTPUT_ONE_COUNTER(block_cache_miss_count);
  514. std::string str = ss.str();
  515. str.erase(str.find_last_not_of(", ") + 1);
  516. return str;
  517. #endif
  518. }
  519. void PerfContext::EnablePerLevelPerfContext() {
  520. if (level_to_perf_context == nullptr) {
  521. level_to_perf_context = new std::map<uint32_t, PerfContextByLevel>();
  522. }
  523. per_level_perf_context_enabled = true;
  524. }
  525. void PerfContext::DisablePerLevelPerfContext(){
  526. per_level_perf_context_enabled = false;
  527. }
  528. void PerfContext::ClearPerLevelPerfContext(){
  529. if (level_to_perf_context != nullptr) {
  530. level_to_perf_context->clear();
  531. delete level_to_perf_context;
  532. level_to_perf_context = nullptr;
  533. }
  534. per_level_perf_context_enabled = false;
  535. }
  536. } // namespace ROCKSDB_NAMESPACE