diff --git a/CMakeLists.txt b/CMakeLists.txt index 441173a29a1..9b06ea9eb6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,7 +189,7 @@ if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /FC /d2Zi+ /W4 /wd4127 /wd4800 /wd4996 /wd4351 /wd4100 /wd4204 /wd4324") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wextra -Wall") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare -Wshadow -Wno-unused-parameter -Wno-unused-variable -Woverloaded-virtual -Wnon-virtual-dtor -Wno-missing-field-initializers -Wno-strict-aliasing") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-compare -Wshadow -Wno-unused-parameter -Wno-unused-variable -Woverloaded-virtual -Wnon-virtual-dtor -Wno-missing-field-initializers -Wno-strict-aliasing -w") if(MINGW) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format") add_definitions(-D_POSIX_C_SOURCE=1) @@ -584,6 +584,7 @@ set(SOURCES monitoring/instrumented_mutex.cc monitoring/iostats_context.cc monitoring/perf_context.cc + monitoring/perf_flags.cc monitoring/perf_level.cc monitoring/persistent_stats_history.cc monitoring/statistics.cc diff --git a/db/c.cc b/db/c.cc index 4fe556c3063..37d11e1a01f 100644 --- a/db/c.cc +++ b/db/c.cc @@ -47,6 +47,9 @@ #include #include +using rocksdb::PerfContext; +using rocksdb::PerfLevel; +using rocksdb::PerfFlags; using rocksdb::BytewiseComparator; using rocksdb::Cache; using rocksdb::ColumnFamilyDescriptor; @@ -113,8 +116,9 @@ using rocksdb::Transaction; using rocksdb::Checkpoint; using rocksdb::TransactionLogIterator; using rocksdb::BatchResult; -using rocksdb::PerfLevel; -using rocksdb::PerfContext; +using rocksdb::WritableFile; +using rocksdb::WriteBatch; +using rocksdb::WriteBatchWithIndex; using rocksdb::MemoryUtil; using std::shared_ptr; @@ -2740,7 +2744,7 @@ rocksdb_ratelimiter_t* rocksdb_writeampbasedratelimiter_create( return rate_limiter; } -void rocksdb_ratelimiter_destroy(rocksdb_ratelimiter_t *limiter) { +void rocksdb_ratelimiter_destroy(rocksdb_ratelimiter_t* limiter) { delete limiter; } @@ -2749,8 +2753,14 @@ void rocksdb_set_perf_level(int v) { SetPerfLevel(level); } +void rocksdb_set_perf_flags_by_mask(uint8_t* flags_bytes) { + PerfFlags flags; + memmove(&flags,flags_bytes,sizeof(PerfFlags)); + SetPerfFlags(flags); +} + rocksdb_perfcontext_t* rocksdb_perfcontext_create() { - rocksdb_perfcontext_t* context = new rocksdb_perfcontext_t; + auto* context = new rocksdb_perfcontext_t; context->rep = rocksdb::get_perf_context(); return context; } diff --git a/db/compaction/compaction_job.cc b/db/compaction/compaction_job.cc index 452621cf7f9..8f4ac48fa6b 100644 --- a/db/compaction/compaction_job.cc +++ b/db/compaction/compaction_job.cc @@ -7,8 +7,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. -#include +#include "db/compaction/compaction_job.h" + #include +#include #include #include #include @@ -19,7 +21,6 @@ #include #include "db/builder.h" -#include "db/compaction/compaction_job.h" #include "db/db_impl/db_impl.h" #include "db/db_iter.h" #include "db/dbformat.h" diff --git a/db/db_impl/db_impl.cc b/db/db_impl/db_impl.cc index 7caa3a3e4a4..24fa3b0daeb 100644 --- a/db/db_impl/db_impl.cc +++ b/db/db_impl/db_impl.cc @@ -33,9 +33,9 @@ #include "db/error_handler.h" #include "db/event_helpers.h" #include "db/external_sst_file_ingestion_job.h" -#include "db/import_column_family_job.h" #include "db/flush_job.h" #include "db/forward_iterator.h" +#include "db/import_column_family_job.h" #include "db/job_context.h" #include "db/log_reader.h" #include "db/log_writer.h" diff --git a/db/db_impl/db_impl_compaction_flush.cc b/db/db_impl/db_impl_compaction_flush.cc index ea5f9b15070..f7a009a0dd1 100644 --- a/db/db_impl/db_impl_compaction_flush.cc +++ b/db/db_impl/db_impl_compaction_flush.cc @@ -6,11 +6,10 @@ // Copyright (c) 2011 The LevelDB Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. -#include "db/db_impl/db_impl.h" - #include #include "db/builder.h" +#include "db/db_impl/db_impl.h" #include "db/error_handler.h" #include "db/event_helpers.h" #include "file/sst_file_manager_impl.h" diff --git a/db/db_iter.cc b/db/db_iter.cc index 10ce051515e..52db743682f 100644 --- a/db/db_iter.cc +++ b/db/db_iter.cc @@ -8,9 +8,10 @@ // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "db/db_iter.h" -#include + #include #include +#include #include "db/dbformat.h" #include "db/merge_context.h" diff --git a/db/db_iter_test.cc b/db/db_iter_test.cc index 1503886443b..d435f487f3e 100644 --- a/db/db_iter_test.cc +++ b/db/db_iter_test.cc @@ -420,7 +420,8 @@ TEST_F(DBIteratorTest, DBIteratorPrevNext) { db_iter->SeekToLast(); ASSERT_TRUE(db_iter->Valid()); - ASSERT_EQ(static_cast(get_perf_context()->internal_key_skipped_count), 1); + ASSERT_EQ(static_cast(get_perf_context()->internal_key_skipped_count), + 1); ASSERT_EQ(db_iter->key().ToString(), "b"); SetPerfLevel(kDisable); @@ -543,7 +544,8 @@ TEST_F(DBIteratorTest, DBIteratorPrevNext) { db_iter->SeekToLast(); ASSERT_TRUE(db_iter->Valid()); - ASSERT_EQ(static_cast(get_perf_context()->internal_delete_skipped_count), 0); + ASSERT_EQ( + static_cast(get_perf_context()->internal_delete_skipped_count), 0); ASSERT_EQ(db_iter->key().ToString(), "b"); SetPerfLevel(kDisable); diff --git a/db/dbformat.cc b/db/dbformat.cc index 130ba4e8adf..b7d776786d2 100644 --- a/db/dbformat.cc +++ b/db/dbformat.cc @@ -8,8 +8,10 @@ // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "db/dbformat.h" -#include #include + +#include + #include "monitoring/perf_context_imp.h" #include "port/port.h" #include "util/coding.h" diff --git a/db/dbformat.h b/db/dbformat.h index 1d9b7ef7e3f..8370313e3ad 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -9,9 +9,11 @@ #pragma once #include + #include #include #include + #include "db/lookup_key.h" #include "db/merge_context.h" #include "logging/logging.h" diff --git a/db/flush_job.cc b/db/flush_job.cc index 716e21e9749..16800a83413 100644 --- a/db/flush_job.cc +++ b/db/flush_job.cc @@ -9,9 +9,8 @@ #include "db/flush_job.h" -#include - #include +#include #include #include "db/builder.h" diff --git a/db/perf_context_test.cc b/db/perf_context_test.cc index 94eabff7ff5..6456d3b985f 100644 --- a/db/perf_context_test.cc +++ b/db/perf_context_test.cc @@ -3,6 +3,8 @@ // COPYING file in the root directory) and Apache 2.0 License // (found in the LICENSE.Apache file in the root directory). // +#include "rocksdb/perf_context.h" + #include #include #include @@ -15,7 +17,6 @@ #include "port/port.h" #include "rocksdb/db.h" #include "rocksdb/memtablerep.h" -#include "rocksdb/perf_context.h" #include "rocksdb/slice_transform.h" #include "test_util/testharness.h" #include "util/stop_watch.h" diff --git a/db/table_cache.cc b/db/table_cache.cc index bc2cce0e43b..94989373862 100644 --- a/db/table_cache.cc +++ b/db/table_cache.cc @@ -14,7 +14,6 @@ #include "db/snapshot_impl.h" #include "db/version_edit.h" #include "file/filename.h" - #include "monitoring/perf_context_imp.h" #include "rocksdb/statistics.h" #include "table/block_based/block_based_table_reader.h" diff --git a/db/version_set.cc b/db/version_set.cc index 736240278d7..c8408d0b53d 100644 --- a/db/version_set.cc +++ b/db/version_set.cc @@ -10,6 +10,7 @@ #include "db/version_set.h" #include + #include #include #include @@ -19,6 +20,7 @@ #include #include #include + #include "compaction/compaction.h" #include "db/internal_stats.h" #include "db/log_reader.h" diff --git a/include/rocksdb/c.h b/include/rocksdb/c.h index f21dbcf7d4a..15ed853389a 100644 --- a/include/rocksdb/c.h +++ b/include/rocksdb/c.h @@ -1120,6 +1120,7 @@ enum { }; extern ROCKSDB_LIBRARY_API void rocksdb_set_perf_level(int); +extern ROCKSDB_LIBRARY_API void rocksdb_set_perf_flags_by_mask(uint8_t* flags_bytes); extern ROCKSDB_LIBRARY_API rocksdb_perfcontext_t* rocksdb_perfcontext_create(); extern ROCKSDB_LIBRARY_API void rocksdb_perfcontext_reset( rocksdb_perfcontext_t* context); diff --git a/include/rocksdb/perf_context.h b/include/rocksdb/perf_context.h index 755fdcd6d0c..f11727fea2f 100644 --- a/include/rocksdb/perf_context.h +++ b/include/rocksdb/perf_context.h @@ -6,11 +6,12 @@ #pragma once #include + #include #include +#include "rocksdb/perf_flags.h" #include "rocksdb/perf_level.h" - namespace rocksdb { // A thread local context for gathering performance counter efficiently diff --git a/include/rocksdb/perf_flags.h b/include/rocksdb/perf_flags.h new file mode 100644 index 00000000000..ae7cac66645 --- /dev/null +++ b/include/rocksdb/perf_flags.h @@ -0,0 +1,111 @@ +#pragma once +#include + +namespace rocksdb { +struct PerfFlags { + // represent original Level 2 + // TODO: think a better design for context_by_level structure + uint8_t enable_perf_context_by_level_count_bit : 1; // 2 + uint8_t enable_user_key_comparison_count_bit : 1; // 2 + uint8_t enable_block_cache_hit_count_bit : 1; // 2 + uint8_t enable_block_read_count_bit : 1; // 2 + uint8_t enable_block_read_byte_bit : 1; // 2 + uint8_t enable_block_cache_index_hit_count_bit : 1; // 2 + uint8_t enable_index_block_read_count_bit : 1; // 2 + uint8_t enable_block_cache_filter_hit_count_bit : 1; // 2 + uint8_t enable_filter_block_read_count_bit : 1; // 2 + uint8_t enable_compression_dict_block_read_count_bit : 1; // 2 + uint8_t enable_get_read_bytes_bit : 1; // 2 + uint8_t enable_multiget_read_bytes_bit : 1; // 2 + uint8_t enable_iter_read_bytes_bit : 1; // 2 + uint8_t enable_internal_key_skipped_count_bit : 1; // 2 + uint8_t enable_internal_delete_skipped_count_bit : 1; // 2 + uint8_t enable_internal_recent_skipped_count_bit : 1; // 2 + uint8_t enable_internal_merge_count_bit : 1; // 2 + uint8_t enable_get_from_memtable_count_bit : 1; // 2 + uint8_t enable_seek_on_memtable_count_bit : 1; // 2 + uint8_t enable_next_on_memtable_count_bit : 1; // 2 + uint8_t enable_prev_on_memtable_count_bit : 1; // 2 + uint8_t enable_seek_child_seek_count_bit : 1; // 2 + uint8_t enable_bloom_memtable_hit_count_bit : 1; // 2 + uint8_t enable_bloom_memtable_miss_count_bit : 1; // 2 + uint8_t enable_bloom_sst_hit_count_bit : 1; // 2 + uint8_t enable_bloom_sst_miss_count_bit : 1; // 2 + uint8_t enable_key_lock_wait_count_bit : 1; // 2 + + // represent original Level 3 + uint8_t enable_measure_cpu_time_bit : 1; // 3 + uint8_t enable_block_read_time_bit : 1; // 3 + uint8_t enable_block_checksum_time_bit : 1; // 3 + uint8_t enable_block_decompress_time_bit : 1; // 3 + uint8_t enable_get_snapshot_time_bit : 1; // 3 + uint8_t enable_get_from_memtable_time_bit : 1; // 3 + uint8_t enable_get_post_process_time_bit : 1; // 3 + uint8_t enable_get_from_output_files_time_bit : 1; // 3 + uint8_t enable_seek_on_memtable_time_bit : 1; // 3 + uint8_t enable_seek_child_seek_time_bit : 1; // 3 + uint8_t enable_seek_min_heap_time_bit : 1; // 3 + uint8_t enable_seek_max_heap_time_bit : 1; // 3 + uint8_t enable_seek_internal_seek_time_bit : 1; // 3 + uint8_t enable_find_next_user_entry_time_bit : 1; // 3 + uint8_t enable_write_wal_time_bit : 1; // 3 + uint8_t enable_write_memtable_time_bit : 1; // 3 + uint8_t enable_write_delay_time_bit : 1; // 3 + uint8_t enable_write_scheduling_flushes_compactions_time_bit : 1; // 3 + uint8_t enable_write_pre_and_post_process_time_bit : 1; // 3 + uint8_t enable_write_thread_wait_nanos_bit : 1; // 3 + uint8_t enable_merge_operator_time_nanos_bit : 1; // 3 + uint8_t enable_read_index_block_nanos_bit : 1; // 3 + uint8_t enable_read_filter_block_nanos_bit : 1; // 3 + uint8_t enable_new_table_block_iter_nanos_bit : 1; // 3 + uint8_t enable_new_table_iterator_nanos_bit : 1; // 3 + uint8_t enable_block_seek_nanos_bit : 1; // 3 + uint8_t enable_find_table_nanos_bit : 1; // 3 + uint8_t enable_key_lock_wait_time_bit : 1; // 3 + uint8_t enable_env_new_sequential_file_nanos_bit : 1; // 3 + uint8_t enable_env_new_random_access_file_nanos_bit : 1; // 3 + uint8_t enable_env_new_writable_file_nanos_bit : 1; // 3 + uint8_t enable_env_reuse_writable_file_nanos_bit : 1; // 3 + uint8_t enable_env_new_random_rw_file_nanos_bit : 1; // 3 + uint8_t enable_env_new_directory_nanos_bit : 1; // 3 + uint8_t enable_env_file_exists_nanos_bit : 1; // 3 + uint8_t enable_env_get_children_nanos_bit : 1; // 3 + uint8_t enable_env_get_children_file_attributes_nanos_bit : 1; // 3 + uint8_t enable_env_delete_file_nanos_bit : 1; // 3 + uint8_t enable_env_create_dir_nanos_bit : 1; // 3 + uint8_t enable_env_create_dir_if_missing_nanos_bit : 1; // 3 + uint8_t enable_env_delete_dir_nanos_bit : 1; // 3 + uint8_t enable_env_get_file_size_nanos_bit : 1; // 3 + uint8_t enable_env_get_file_modification_time_nanos_bit : 1; // 3 + uint8_t enable_env_rename_file_nanos_bit : 1; // 3 + uint8_t enable_env_link_file_nanos_bit : 1; // 3 + uint8_t enable_env_lock_file_nanos_bit : 1; // 3 + uint8_t enable_env_unlock_file_nanos_bit : 1; // 3 + uint8_t enable_env_new_logger_nanos_bit : 1; // 3 + uint8_t enable_encrypt_data_nanos_bit : 1; // 3 + uint8_t enable_decrypt_data_nanos_bit : 1; // 3 + + // represent original Level 4 + // TODO: think a better design for iostats + uint8_t enable_iostats_cpu_timer_bit : 1; // 4 + uint8_t enable_get_cpu_nanos_bit : 1; // 4 + uint8_t enable_iter_next_cpu_nanos_bit : 1; // 4 + uint8_t enable_iter_prev_cpu_nanos_bit : 1; // 4 + uint8_t enable_iter_seek_cpu_nanos_bit : 1; // 4 + + // represent original Level 5 + uint8_t enable_db_mutex_lock_nanos_bit : 1; // 5 + uint8_t enable_db_condition_wait_nanos_bit : 1; // 5 +}; +extern const PerfFlags PERF_LEVEL1; +extern const PerfFlags PERF_LEVEL2; +extern const PerfFlags PERF_LEVEL3; +extern const PerfFlags PERF_LEVEL4; +extern const PerfFlags PERF_LEVEL5; +// set the perf flags for current thread +void SetPerfFlags(PerfFlags pbf); + +// get current perf flags for current thread +PerfFlags GetPerfFlags(); + +} // namespace rocksdb diff --git a/include/rocksdb/perf_level.h b/include/rocksdb/perf_level.h index de0a214d6ae..9f5f28f57ee 100644 --- a/include/rocksdb/perf_level.h +++ b/include/rocksdb/perf_level.h @@ -21,13 +21,14 @@ enum PerfLevel : unsigned char { // time (neither wall time nor CPU time) for mutexes. kEnableTimeAndCPUTimeExceptForMutex = 4, kEnableTime = 5, // enable count and time stats - kOutOfBounds = 6 // N.B. Must always be the last value! + kCustomFlags = 6, // could not measured by level because is a custom bit flags configuration + kOutOfBounds = 7, // N.B. Must always be the last value! }; // set the perf stats level for current thread void SetPerfLevel(PerfLevel level); -// get current perf stats level for current thread +// get current estimated perf stats level for current thread PerfLevel GetPerfLevel(); } // namespace rocksdb diff --git a/monitoring/iostats_context_imp.h b/monitoring/iostats_context_imp.h index 19e34209b1b..634f320ca00 100644 --- a/monitoring/iostats_context_imp.h +++ b/monitoring/iostats_context_imp.h @@ -4,6 +4,7 @@ // (found in the LICENSE.Apache file in the root directory). // #pragma once +#include "monitoring/perf_flags_imp.h" #include "monitoring/perf_step_timer.h" #include "rocksdb/iostats_context.h" @@ -34,14 +35,13 @@ extern __thread IOStatsContext iostats_context; // Declare and set start time of the timer #define IOSTATS_TIMER_GUARD(metric) \ - PerfStepTimer iostats_step_timer_##metric(&(iostats_context.metric)); \ + PerfStepTimer iostats_step_timer_##metric(&(iostats_context.metric),(bool)perf_flags.enable_iostats_cpu_timer_bit); \ iostats_step_timer_##metric.Start(); // Declare and set start time of the timer #define IOSTATS_CPU_TIMER_GUARD(metric, env) \ PerfStepTimer iostats_step_timer_##metric( \ - &(iostats_context.metric), env, true, \ - PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \ + &(iostats_context.metric),(bool)perf_flags.enable_iostats_cpu_timer_bit, env, true); \ iostats_step_timer_##metric.Start(); #else // ROCKSDB_SUPPORT_THREAD_LOCAL diff --git a/monitoring/perf_context.cc b/monitoring/perf_context.cc index 40b0b215c47..2e2e1b31bc4 100644 --- a/monitoring/perf_context.cc +++ b/monitoring/perf_context.cc @@ -5,6 +5,7 @@ // #include + #include "monitoring/perf_context_imp.h" namespace rocksdb { @@ -30,8 +31,9 @@ PerfContext* get_perf_context() { #endif #endif } - PerfContext::~PerfContext() { + auto str = this->ToString(true); + printf("%s\n",str.c_str()); #if !defined(NPERF_CONTEXT) && defined(ROCKSDB_SUPPORT_THREAD_LOCAL) && !defined(OS_SOLARIS) ClearPerLevelPerfContext(); #endif diff --git a/monitoring/perf_context_imp.h b/monitoring/perf_context_imp.h index e0ff8afc58e..573ea4bcffc 100644 --- a/monitoring/perf_context_imp.h +++ b/monitoring/perf_context_imp.h @@ -4,6 +4,7 @@ // (found in the LICENSE.Apache file in the root directory). // #pragma once +#include "monitoring/perf_flags_imp.h" #include "monitoring/perf_step_timer.h" #include "rocksdb/perf_context.h" #include "util/stop_watch.h" @@ -38,25 +39,24 @@ extern thread_local PerfContext perf_context; // Declare and set start time of the timer #define PERF_TIMER_GUARD(metric) \ - PerfStepTimer perf_step_timer_##metric(&(perf_context.metric)); \ + PerfStepTimer perf_step_timer_##metric(&(perf_context.metric),(bool)perf_flags.enable_##metric##_bit); \ perf_step_timer_##metric.Start(); // Declare and set start time of the timer #define PERF_TIMER_GUARD_WITH_ENV(metric, env) \ - PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), env); \ + PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), (bool)perf_flags.enable_##metric##_bit, env); \ perf_step_timer_##metric.Start(); // Declare and set start time of the timer #define PERF_CPU_TIMER_GUARD(metric, env) \ PerfStepTimer perf_step_timer_##metric( \ - &(perf_context.metric), env, true, \ - PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \ + &(perf_context.metric), (bool)perf_flags.enable_##metric##_bit, env, true); \ perf_step_timer_##metric.Start(); #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \ ticker_type) \ - PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), nullptr, \ - false, PerfLevel::kEnableTime, stats, \ + PerfStepTimer perf_step_timer_##metric(&(perf_context.metric),(bool)perf_flags.enable_##metric##_bit , nullptr, \ + false, stats, \ ticker_type); \ if (condition) { \ perf_step_timer_##metric.Start(); \ @@ -68,13 +68,13 @@ extern thread_local PerfContext perf_context; // Increase metric value #define PERF_COUNTER_ADD(metric, value) \ - if (perf_level >= PerfLevel::kEnableCount) { \ + if ((bool)perf_flags.enable_##metric##_bit) { \ perf_context.metric += value; \ } // Increase metric value #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level) \ - if (perf_level >= PerfLevel::kEnableCount && \ + if ((bool)perf_flags.enable_perf_context_by_level_count_bit && \ perf_context.per_level_perf_context_enabled && \ perf_context.level_to_perf_context) { \ if ((*(perf_context.level_to_perf_context)).find(level) != \ @@ -90,4 +90,4 @@ extern thread_local PerfContext perf_context; #endif -} +} \ No newline at end of file diff --git a/monitoring/perf_flags.cc b/monitoring/perf_flags.cc new file mode 100644 index 00000000000..16c6309b233 --- /dev/null +++ b/monitoring/perf_flags.cc @@ -0,0 +1,113 @@ +#include "monitoring/perf_flags_imp.h" + +#define PERF_FLAGS_INIT_LEVEL2 \ +.enable_perf_context_by_level_count_bit = 1, \ +.enable_user_key_comparison_count_bit = 1, \ +.enable_block_cache_hit_count_bit = 1, \ +.enable_block_read_count_bit = 1, \ +.enable_block_read_byte_bit = 1, \ +.enable_block_cache_index_hit_count_bit = 1, \ +.enable_index_block_read_count_bit = 1, \ +.enable_block_cache_filter_hit_count_bit = 1, \ +.enable_filter_block_read_count_bit = 1, \ +.enable_compression_dict_block_read_count_bit = 1, \ +.enable_get_read_bytes_bit = 1, \ +.enable_multiget_read_bytes_bit = 1, \ +.enable_iter_read_bytes_bit = 1, \ +.enable_internal_key_skipped_count_bit = 1, \ +.enable_internal_delete_skipped_count_bit = 1, \ +.enable_internal_recent_skipped_count_bit = 1, \ + .enable_internal_merge_count_bit = 1, \ + .enable_get_from_memtable_count_bit = 1, \ + .enable_seek_on_memtable_count_bit = 1, \ + .enable_next_on_memtable_count_bit = 1, \ + .enable_prev_on_memtable_count_bit = 1, \ + .enable_seek_child_seek_count_bit = 1, \ + .enable_bloom_memtable_hit_count_bit = 1, \ + .enable_bloom_memtable_miss_count_bit = 1, \ + .enable_bloom_sst_hit_count_bit = 1, .enable_bloom_sst_miss_count_bit = 1, \ + .enable_key_lock_wait_count_bit = 1 + +#define PERF_FLAGS_INIT_LEVEL3 \ + PERF_FLAGS_INIT_LEVEL2, \ + .enable_measure_cpu_time_bit = 1, .enable_block_read_time_bit = 1, \ + .enable_block_checksum_time_bit = 1, \ + .enable_block_decompress_time_bit = 1, \ + .enable_get_snapshot_time_bit = 1, \ + .enable_get_from_memtable_time_bit = 1, \ + .enable_get_post_process_time_bit = 1, \ + .enable_get_from_output_files_time_bit = 1, \ + .enable_seek_on_memtable_time_bit = 1, \ + .enable_seek_child_seek_time_bit = 1, \ + .enable_seek_min_heap_time_bit = 1, .enable_seek_max_heap_time_bit = 1, \ + .enable_seek_internal_seek_time_bit = 1, \ + .enable_find_next_user_entry_time_bit = 1, \ + .enable_write_wal_time_bit = 1, .enable_write_memtable_time_bit = 1, \ + .enable_write_delay_time_bit = 1, \ + .enable_write_scheduling_flushes_compactions_time_bit = 1, \ + .enable_write_pre_and_post_process_time_bit = 1, \ + .enable_write_thread_wait_nanos_bit = 1, \ + .enable_merge_operator_time_nanos_bit = 1, \ + .enable_read_index_block_nanos_bit = 1, \ +.enable_read_filter_block_nanos_bit = 1, \ +.enable_new_table_block_iter_nanos_bit = 1, \ +.enable_new_table_iterator_nanos_bit = 1, \ +.enable_block_seek_nanos_bit = 1, \ +.enable_find_table_nanos_bit = 1, \ +.enable_key_lock_wait_time_bit = 1, \ +.enable_env_new_sequential_file_nanos_bit = 1, \ +.enable_env_new_random_access_file_nanos_bit = 1, \ +.enable_env_new_writable_file_nanos_bit = 1, \ +.enable_env_reuse_writable_file_nanos_bit = 1, \ +.enable_env_new_random_rw_file_nanos_bit = 1, \ +.enable_env_new_directory_nanos_bit = 1, \ +.enable_env_file_exists_nanos_bit = 1, \ +.enable_env_get_children_nanos_bit = 1, \ +.enable_env_get_children_file_attributes_nanos_bit = 1, \ +.enable_env_delete_file_nanos_bit = 1, \ +.enable_env_create_dir_nanos_bit = 1, \ +.enable_env_create_dir_if_missing_nanos_bit = 1, \ +.enable_env_delete_dir_nanos_bit = 1, \ +.enable_env_get_file_size_nanos_bit = 1, \ +.enable_env_get_file_modification_time_nanos_bit = 1, \ +.enable_env_rename_file_nanos_bit = 1, \ +.enable_env_link_file_nanos_bit = 1, \ +.enable_env_lock_file_nanos_bit = 1, \ +.enable_env_unlock_file_nanos_bit = 1, \ +.enable_env_new_logger_nanos_bit = 1, \ +.enable_encrypt_data_nanos_bit = 1, \ +.enable_decrypt_data_nanos_bit = 1 + +#define PERF_FLAGS_INIT_LEVEL4 \ + PERF_FLAGS_INIT_LEVEL3, \ + .enable_iostats_cpu_timer_bit = 1, .enable_get_cpu_nanos_bit = 1, \ + .enable_iter_next_cpu_nanos_bit = 1, \ + .enable_iter_prev_cpu_nanos_bit = 1, .enable_iter_seek_cpu_nanos_bit = 1 + +#define PERF_FLAGS_INIT_LEVEL5 \ + PERF_FLAGS_INIT_LEVEL4, .enable_db_mutex_lock_nanos_bit = 1, \ + .enable_db_condition_wait_nanos_bit = 1 + +namespace rocksdb { +const PerfFlags PERF_LEVEL1 = {}; +const PerfFlags PERF_LEVEL2 = {PERF_FLAGS_INIT_LEVEL2}; +const PerfFlags PERF_LEVEL3 = {PERF_FLAGS_INIT_LEVEL3}; +const PerfFlags PERF_LEVEL4 = {PERF_FLAGS_INIT_LEVEL4}; +const PerfFlags PERF_LEVEL5 = {PERF_FLAGS_INIT_LEVEL5}; + +// set default value of perf_flags +#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL +__thread PerfFlags perf_flags = { + PERF_FLAGS_INIT_LEVEL2 +}; +#else +PerfFlags perf_flags = { + PERF_FLAGS_INIT_LEVEL2 +}; +#endif + +void SetPerfFlags(PerfFlags pbf) { perf_flags = pbf; } + +PerfFlags GetPerfFlags() { return perf_flags; } + +} // namespace rocksdb diff --git a/monitoring/perf_flags_imp.h b/monitoring/perf_flags_imp.h new file mode 100644 index 00000000000..52ec389ab2d --- /dev/null +++ b/monitoring/perf_flags_imp.h @@ -0,0 +1,12 @@ +#pragma once +#include "rocksdb/perf_flags.h" + +namespace rocksdb { + +#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL +extern __thread PerfFlags perf_flags; +#else +extern PerfLevelByPerfFlags perf_flags; +#endif + +} // namespace rocksdb diff --git a/monitoring/perf_level.cc b/monitoring/perf_level.cc index 79c718cce76..bb44bd11d8b 100644 --- a/monitoring/perf_level.cc +++ b/monitoring/perf_level.cc @@ -4,25 +4,49 @@ // (found in the LICENSE.Apache file in the root directory). // -#include -#include "monitoring/perf_level_imp.h" +#include "rocksdb/perf_level.h" -namespace rocksdb { +#include +#include + +#include "monitoring/perf_flags_imp.h" -#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL -__thread PerfLevel perf_level = kEnableCount; -#else -PerfLevel perf_level = kEnableCount; -#endif +namespace rocksdb { void SetPerfLevel(PerfLevel level) { + // TODO: discuss keep assertions or not assert(level > kUninitialized); assert(level < kOutOfBounds); - perf_level = level; + switch (level) { + case kEnableTime: + perf_flags = PERF_LEVEL5; + break; + case kEnableTimeAndCPUTimeExceptForMutex: + perf_flags = PERF_LEVEL4; + break; + case kEnableTimeExceptForMutex: + perf_flags = PERF_LEVEL3; + break; + case kEnableCount: + perf_flags = PERF_LEVEL2; + return; + default: + perf_flags = {}; + break; + } + // perf_flags.perf_level = level; } - PerfLevel GetPerfLevel() { - return perf_level; + void* levels[5] = {(void*)&PERF_LEVEL1, (void*)&PERF_LEVEL2, + (void*)&PERF_LEVEL3, (void*)&PERF_LEVEL4, + (void*)&PERF_LEVEL5}; + for (int i = 0; i < 5; ++i) { + int cmp = memcmp(&perf_flags, levels[i], sizeof(PerfFlags)); + if (cmp == 0) return (PerfLevel)(i + 1); + if (cmp < 0 && i == 0) return kOutOfBounds; + if (cmp < 0) return kCustomFlags; + if (cmp > 0 && i == 4) return kOutOfBounds; + } } } // namespace rocksdb diff --git a/monitoring/perf_step_timer.h b/monitoring/perf_step_timer.h index 6501bd54aba..4c43ce62227 100644 --- a/monitoring/perf_step_timer.h +++ b/monitoring/perf_step_timer.h @@ -12,11 +12,13 @@ namespace rocksdb { class PerfStepTimer { public: - explicit PerfStepTimer( - uint64_t* metric, Env* env = nullptr, bool use_cpu_time = false, - PerfLevel enable_level = PerfLevel::kEnableTimeExceptForMutex, - Statistics* statistics = nullptr, uint32_t ticker_type = 0) - : perf_counter_enabled_(perf_level >= enable_level), + explicit PerfStepTimer(uint64_t* metric, + bool perf_counter_enabled_ = false, + Env* env = nullptr, + bool use_cpu_time = false, + Statistics* statistics = nullptr, + uint32_t ticker_type = 0) + : perf_counter_enabled_(perf_counter_enabled_), use_cpu_time_(use_cpu_time), env_((perf_counter_enabled_ || statistics != nullptr) ? ((env != nullptr) ? env : Env::Default()) diff --git a/table/block_based/block.cc b/table/block_based/block.cc index b0accaa7508..fcba797a901 100644 --- a/table/block_based/block.cc +++ b/table/block_based/block.cc @@ -10,6 +10,7 @@ // Decodes the blocks generated by block_builder.cc. #include "table/block_based/block.h" + #include #include #include diff --git a/table/block_based/block_based_filter_block.cc b/table/block_based/block_based_filter_block.cc index 319c5bf6d87..8bac865081b 100644 --- a/table/block_based/block_based_filter_block.cc +++ b/table/block_based/block_based_filter_block.cc @@ -8,6 +8,7 @@ // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "table/block_based/block_based_filter_block.h" + #include #include "db/dbformat.h" diff --git a/table/block_based/block_based_table_reader.cc b/table/block_based/block_based_table_reader.cc index a7593a9a99f..4b9257aac1d 100644 --- a/table/block_based/block_based_table_reader.cc +++ b/table/block_based/block_based_table_reader.cc @@ -17,7 +17,7 @@ #include "db/dbformat.h" #include "db/pinned_iterators_manager.h" - +#include "monitoring/perf_context_imp.h" #include "rocksdb/cache.h" #include "rocksdb/comparator.h" #include "rocksdb/env.h" @@ -27,7 +27,6 @@ #include "rocksdb/statistics.h" #include "rocksdb/table.h" #include "rocksdb/table_properties.h" - #include "table/block_based/block.h" #include "table/block_based/block_based_filter_block.h" #include "table/block_based/block_based_table_factory.h" @@ -44,8 +43,6 @@ #include "table/persistent_cache_helper.h" #include "table/sst_file_writer_collectors.h" #include "table/two_level_iterator.h" - -#include "monitoring/perf_context_imp.h" #include "test_util/sync_point.h" #include "util/coding.h" #include "util/crc32c.h" diff --git a/table/block_based/filter_block_reader_common.cc b/table/block_based/filter_block_reader_common.cc index b6a33498678..257ca0f5b12 100644 --- a/table/block_based/filter_block_reader_common.cc +++ b/table/block_based/filter_block_reader_common.cc @@ -5,6 +5,7 @@ // #include "table/block_based/filter_block_reader_common.h" + #include "monitoring/perf_context_imp.h" #include "table/block_based/block_based_table_reader.h" diff --git a/table/block_based/uncompression_dict_reader.cc b/table/block_based/uncompression_dict_reader.cc index 559e8af5cbd..beaca59c23e 100644 --- a/table/block_based/uncompression_dict_reader.cc +++ b/table/block_based/uncompression_dict_reader.cc @@ -5,6 +5,7 @@ // #include "table/block_based/uncompression_dict_reader.h" + #include "monitoring/perf_context_imp.h" #include "table/block_based/block_based_table_reader.h" #include "util/compression.h" diff --git a/table/get_context.cc b/table/get_context.cc index 70b7e6ae23a..35b9ef627fe 100644 --- a/table/get_context.cc +++ b/table/get_context.cc @@ -4,6 +4,7 @@ // (found in the LICENSE.Apache file in the root directory). #include "table/get_context.h" + #include "db/merge_helper.h" #include "db/pinned_iterators_manager.h" #include "db/read_callback.h" diff --git a/table/merging_iterator.cc b/table/merging_iterator.cc index 592eddfb41a..61ebf64982f 100644 --- a/table/merging_iterator.cc +++ b/table/merging_iterator.cc @@ -8,8 +8,10 @@ // found in the LICENSE file. See the AUTHORS file for names of contributors. #include "table/merging_iterator.h" + #include #include + #include "db/dbformat.h" #include "db/pinned_iterators_manager.h" #include "memory/arena.h" diff --git a/table/plain/plain_table_reader.cc b/table/plain/plain_table_reader.cc index f6c348fdbf9..754a4806994 100644 --- a/table/plain/plain_table_reader.cc +++ b/table/plain/plain_table_reader.cc @@ -11,14 +11,15 @@ #include #include "db/dbformat.h" - +#include "memory/arena.h" +#include "monitoring/histogram.h" +#include "monitoring/perf_context_imp.h" #include "rocksdb/cache.h" #include "rocksdb/comparator.h" #include "rocksdb/env.h" #include "rocksdb/filter_policy.h" #include "rocksdb/options.h" #include "rocksdb/statistics.h" - #include "table/block_based/block.h" #include "table/block_based/filter_block.h" #include "table/bloom_block.h" @@ -29,10 +30,6 @@ #include "table/plain/plain_table_factory.h" #include "table/plain/plain_table_key_coding.h" #include "table/two_level_iterator.h" - -#include "memory/arena.h" -#include "monitoring/histogram.h" -#include "monitoring/perf_context_imp.h" #include "util/coding.h" #include "util/dynamic_bloom.h" #include "util/hash.h" diff --git a/tools/db_bench_tool.cc b/tools/db_bench_tool.cc index eb741c9e62d..05621ad0234 100644 --- a/tools/db_bench_tool.cc +++ b/tools/db_bench_tool.cc @@ -892,7 +892,7 @@ DEFINE_int32(thread_status_per_interval, 0, "Takes and report a snapshot of the current status of each thread" " when this is greater than 0."); -DEFINE_int32(perf_level, rocksdb::PerfLevel::kDisable, "Level of perf collection"); +DEFINE_int32(perf_level, rocksdb::PerfLevel::kEnableTime, "Level of perf collection"); static bool ValidateRateLimit(const char* flagname, double value) { const double EPSILON = 1e-10; diff --git a/util/file_reader_writer.cc b/util/file_reader_writer.cc index adbe111e875..3c67760799b 100644 --- a/util/file_reader_writer.cc +++ b/util/file_reader_writer.cc @@ -14,6 +14,7 @@ #include "monitoring/histogram.h" #include "monitoring/iostats_context_imp.h" +#include "monitoring/perf_flags_imp.h" #include "port/port.h" #include "test_util/sync_point.h" #include "util/random.h" @@ -76,8 +77,8 @@ Status RandomAccessFileReader::Read(uint64_t offset, size_t n, Slice* result, { StopWatch sw(env_, stats_, hist_type_, (stats_ != nullptr) ? &elapsed : nullptr, true /*overwrite*/, - true /*delay_enabled*/); - auto prev_perf_level = GetPerfLevel(); + true /*delay_enabled*/); + auto prev_perf_flags = GetPerfFlags(); IOSTATS_TIMER_GUARD(read_nanos); if (use_direct_io()) { #ifndef ROCKSDB_LITE @@ -183,7 +184,7 @@ Status RandomAccessFileReader::Read(uint64_t offset, size_t n, Slice* result, *result = Slice(res_scratch, s.ok() ? pos : 0); } IOSTATS_ADD_IF_POSITIVE(bytes_read, result->size()); - SetPerfLevel(prev_perf_level); + SetPerfFlags(prev_perf_flags); } if (stats_ != nullptr && file_read_hist_ != nullptr) { file_read_hist_->Add(elapsed); @@ -200,19 +201,19 @@ Status RandomAccessFileReader::MultiRead(ReadRequest* read_reqs, { StopWatch sw(env_, stats_, hist_type_, (stats_ != nullptr) ? &elapsed : nullptr, true /*overwrite*/, - true /*delay_enabled*/); - auto prev_perf_level = GetPerfLevel(); + true /*delay_enabled*/); + auto prev_perf_flags = GetPerfFlags(); IOSTATS_TIMER_GUARD(read_nanos); #ifndef ROCKSDB_LITE - FileOperationInfo::TimePoint start_ts; - if (ShouldNotifyListeners()) { - start_ts = std::chrono::system_clock::now(); - } -#endif // ROCKSDB_LITE - { - IOSTATS_CPU_TIMER_GUARD(cpu_read_nanos, env_); - s = file_->MultiRead(read_reqs, num_reqs); + FileOperationInfo::TimePoint start_ts; + if (ShouldNotifyListeners()) { + start_ts = std::chrono::system_clock::now(); + } +#endif // ROCKSDB_LITE + { + IOSTATS_CPU_TIMER_GUARD(cpu_read_nanos, env_); + s = file_->MultiRead(read_reqs, num_reqs); } for (size_t i = 0; i < num_reqs; ++i) { #ifndef ROCKSDB_LITE @@ -225,7 +226,7 @@ Status RandomAccessFileReader::MultiRead(ReadRequest* read_reqs, #endif // ROCKSDB_LITE IOSTATS_ADD_IF_POSITIVE(bytes_read, read_reqs[i].result.size()); } - SetPerfLevel(prev_perf_level); + SetPerfFlags(prev_perf_flags); } if (stats_ != nullptr && file_read_hist_ != nullptr) { file_read_hist_->Add(elapsed); @@ -460,14 +461,15 @@ Status WritableFileWriter::SyncInternal(bool use_fsync) { Status s; IOSTATS_TIMER_GUARD(fsync_nanos); TEST_SYNC_POINT("WritableFileWriter::SyncInternal:0"); - auto prev_perf_level = GetPerfLevel(); + auto prev_perf_flags = GetPerfFlags(); IOSTATS_CPU_TIMER_GUARD(cpu_write_nanos, env_); if (use_fsync) { s = writable_file_->Fsync(); } else { s = writable_file_->Sync(); } - SetPerfLevel(prev_perf_level); + // SetPerfLevel(prev_perf_level); + SetPerfFlags(prev_perf_flags); return s; } @@ -508,10 +510,10 @@ Status WritableFileWriter::WriteBuffered(const char* data, size_t size) { } #endif { - auto prev_perf_level = GetPerfLevel(); + auto prev_perf_flags = GetPerfFlags(); IOSTATS_CPU_TIMER_GUARD(cpu_write_nanos, env_); s = writable_file_->Append(Slice(src, allowed)); - SetPerfLevel(prev_perf_level); + SetPerfFlags(prev_perf_flags); } #ifndef ROCKSDB_LITE if (ShouldNotifyListeners()) { diff --git a/utilities/transactions/transaction_lock_mgr.cc b/utilities/transactions/transaction_lock_mgr.cc index 084d817ea08..d883415adbd 100644 --- a/utilities/transactions/transaction_lock_mgr.cc +++ b/utilities/transactions/transaction_lock_mgr.cc @@ -7,9 +7,8 @@ #include "utilities/transactions/transaction_lock_mgr.h" -#include - #include +#include #include #include #include