Skip to content

Commit

Permalink
PerfFlag implementation aside the PerfLevel
Browse files Browse the repository at this point in the history
design spec in on: https://docs.google.com/document/d/1JYmWMIZwYV0AZW6rNv_oFVZLBCAkxLLYslt39eEZstM/edit?usp=sharing

Signed-off-by: lemonhx <lemonhx@lemonhx.tech>
  • Loading branch information
LemonHX committed Jul 2, 2021
1 parent 58c0ecb commit f955144
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 24 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ set(SOURCES
monitoring/iostats_context.cc
monitoring/perf_context.cc
monitoring/perf_level.cc
monitoring/perf_flag.cc
monitoring/persistent_stats_history.cc
monitoring/statistics.cc
monitoring/thread_status_impl.cc
Expand Down
16 changes: 16 additions & 0 deletions db/c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "rocksdb/memtablerep.h"
#include "rocksdb/merge_operator.h"
#include "rocksdb/options.h"
#include "rocksdb/perf_flag.h"
#include "rocksdb/perf_context.h"
#include "rocksdb/rate_limiter.h"
#include "rocksdb/slice_transform.h"
Expand Down Expand Up @@ -114,6 +115,9 @@ using rocksdb::Checkpoint;
using rocksdb::TransactionLogIterator;
using rocksdb::BatchResult;
using rocksdb::PerfLevel;
using rocksdb::EnablePerfFlag;
using rocksdb::DisablePerfFlag;
using rocksdb::CheckPerfFlag;
using rocksdb::PerfContext;
using rocksdb::MemoryUtil;

Expand Down Expand Up @@ -2749,6 +2753,18 @@ void rocksdb_set_perf_level(int v) {
SetPerfLevel(level);
}

void rocksdb_enable_perf_flag(uint64_t flag){
EnablePerfFlag(flag);
}

void rocksdb_disable_perf_flag(uint64_t flag){
DisablePerfFlag(flag);
}

int rocksdb_check_perf_flag(uint64_t flag){
return (int)CheckPerfFlag(flag);
}

rocksdb_perfcontext_t* rocksdb_perfcontext_create() {
rocksdb_perfcontext_t* context = new rocksdb_perfcontext_t;
context->rep = rocksdb::get_perf_context();
Expand Down
4 changes: 4 additions & 0 deletions include/rocksdb/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,11 @@ enum {
rocksdb_total_metric_count = 68
};

#include "perf_flag_defs.h"
extern ROCKSDB_LIBRARY_API void rocksdb_set_perf_level(int);
extern ROCKSDB_LIBRARY_API void rocksdb_enable_perf_flag(uint64_t);
extern ROCKSDB_LIBRARY_API void rocksdb_disable_perf_flag(uint64_t);
extern ROCKSDB_LIBRARY_API int rocksdb_check_perf_flag(uint64_t);
extern ROCKSDB_LIBRARY_API rocksdb_perfcontext_t* rocksdb_perfcontext_create();
extern ROCKSDB_LIBRARY_API void rocksdb_perfcontext_reset(
rocksdb_perfcontext_t* context);
Expand Down
15 changes: 15 additions & 0 deletions include/rocksdb/perf_flag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// complements to perf_level
#include <cstdint>
#pragma once
#define FLAGS_LEN \
(((uint64_t)FLAG_END & (uint64_t)0b111) == 0 \
? ((uint64_t)FLAG_END >> (uint64_t)0b11) \
: ((uint64_t)FLAG_END >> (uint64_t)0b11) + (uint64_t)1)
namespace rocksdb{
void EnablePerfFlag(uint64_t flag);
void DisablePerfFlag(uint64_t flag);
bool CheckPerfFlag(uint64_t flag);
}

#define GET_FLAG(flag) perf_flags[(uint64_t)(flag) >> (uint64_t)0b11]
#include "perf_flag_defs.h"
107 changes: 107 additions & 0 deletions include/rocksdb/perf_flag_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#pragma once
#include <stdint.h>

enum {
flag_user_key_comparison_count = 0,
flag_block_cache_hit_count,
flag_block_read_count,
flag_block_read_byte,
flag_block_read_time,
flag_block_cache_index_hit_count,
flag_index_block_read_count,
flag_block_cache_filter_hit_count,
flag_filter_block_read_count,
flag_compression_dict_block_read_count,
flag_block_checksum_time,
flag_block_decompress_time,
flag_get_read_bytes,
flag_multiget_read_bytes,
flag_iter_read_bytes,
flag_internal_key_skipped_count,
flag_internal_delete_skipped_count,
flag_internal_recent_skipped_count,
flag_internal_merge_count,
flag_get_snapshot_time,
flag_get_from_memtable_time,
flag_get_from_memtable_count,
flag_get_post_process_time,
flag_get_from_output_files_time,
flag_seek_on_memtable_time,
flag_seek_on_memtable_count,
flag_next_on_memtable_count,
flag_prev_on_memtable_count,
flag_seek_child_seek_time,
flag_seek_child_seek_count,
flag_seek_min_heap_time,
flag_seek_max_heap_time,
flag_seek_internal_seek_time,
flag_find_next_user_entry_time,
flag_write_wal_time,
flag_write_memtable_time,
flag_write_delay_time,
flag_write_scheduling_flushes_compactions_time,
flag_write_pre_and_post_process_time,
flag_write_thread_wait_nanos,
flag_db_mutex_lock_nanos,
flag_db_condition_wait_nanos,
flag_merge_operator_time_nanos,
flag_read_index_block_nanos,
flag_read_filter_block_nanos,
flag_new_table_block_iter_nanos,
flag_new_table_iterator_nanos,
flag_block_seek_nanos,
flag_find_table_nanos,
flag_bloom_memtable_hit_count,
flag_bloom_memtable_miss_count,
flag_bloom_sst_hit_count,
flag_bloom_sst_miss_count,
flag_key_lock_wait_time,
flag_key_lock_wait_count,
flag_env_new_sequential_file_nanos,
flag_env_new_random_access_file_nanos,
flag_env_new_writable_file_nanos,
flag_env_reuse_writable_file_nanos,
flag_env_new_random_rw_file_nanos,
flag_env_new_directory_nanos,
flag_env_file_exists_nanos,
flag_env_get_children_nanos,
flag_env_get_children_file_attributes_nanos,
flag_env_delete_file_nanos,
flag_env_create_dir_nanos,
flag_env_create_dir_if_missing_nanos,
flag_env_delete_dir_nanos,
flag_env_get_file_size_nanos,
flag_env_get_file_modification_time_nanos,
flag_env_rename_file_nanos,
flag_env_link_file_nanos,
flag_env_lock_file_nanos,
flag_env_unlock_file_nanos,
flag_env_new_logger_nanos,
flag_get_cpu_nanos,
flag_iter_next_cpu_nanos,
flag_iter_prev_cpu_nanos,
flag_iter_seek_cpu_nanos,
flag_encrypt_data_nanos,
flag_decrypt_data_nanos,

flag_get_from_table_nanos,
flag_user_key_return_count,
flag_block_cache_miss_count,
flag_bloom_filter_full_positive,
flag_bloom_filter_useful,
flag_bloom_filter_full_true_positive,

flag_bytes_read,
flag_bytes_written,
flag_open_nanos,
flag_allocate_nanos,
flag_write_nanos,
flag_read_nanos,
flag_range_sync_nanos,
flag_prepare_write_nanos,
flag_fsync_nanos,
flag_logger_nanos,
flag_cpu_read_nanos,
flag_cpu_write_nanos,
FLAG_END
};
4 changes: 2 additions & 2 deletions monitoring/iostats_context_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,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), CheckPerfFlag(flag_##metric)); \
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, \
&(iostats_context.metric), CheckPerfFlag(flag_##metric), env, true, \
PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \
iostats_step_timer_##metric.Start();

Expand Down
39 changes: 19 additions & 20 deletions monitoring/perf_context_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,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),CheckPerfFlag(flag_##metric)); \
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),CheckPerfFlag(flag_##metric),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, \
&(perf_context.metric),CheckPerfFlag(flag_##metric), env, true, \
PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \
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, \
PerfStepTimer perf_step_timer_##metric(&(perf_context.metric),CheckPerfFlag(flag_##metric), nullptr, \
false, PerfLevel::kEnableTime, stats, \
ticker_type); \
if (condition) { \
Expand All @@ -68,26 +68,25 @@ extern thread_local PerfContext perf_context;

// Increase metric value
#define PERF_COUNTER_ADD(metric, value) \
if (perf_level >= PerfLevel::kEnableCount) { \
if (perf_level >= PerfLevel::kEnableCount || CheckPerfFlag(flag_##metric)) { \
perf_context.metric += value; \
}

// Increase metric value
#define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level) \
if (perf_level >= PerfLevel::kEnableCount && \
perf_context.per_level_perf_context_enabled && \
perf_context.level_to_perf_context) { \
if ((*(perf_context.level_to_perf_context)).find(level) != \
(*(perf_context.level_to_perf_context)).end()) { \
(*(perf_context.level_to_perf_context))[level].metric += value; \
} \
else { \
PerfContextByLevel empty_context; \
(*(perf_context.level_to_perf_context))[level] = empty_context; \
(*(perf_context.level_to_perf_context))[level].metric += value; \
} \
} \
#define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level) \
if ((perf_level >= PerfLevel::kEnableCount || CheckPerfFlag(flag_##metric)) && \
perf_context.per_level_perf_context_enabled && \
perf_context.level_to_perf_context) { \
if ((*(perf_context.level_to_perf_context)).find(level) != \
(*(perf_context.level_to_perf_context)).end()) { \
(*(perf_context.level_to_perf_context))[level].metric += value; \
} else { \
PerfContextByLevel empty_context; \
(*(perf_context.level_to_perf_context))[level] = empty_context; \
(*(perf_context.level_to_perf_context))[level].metric += value; \
} \
}

#endif

}
} // namespace rocksdb
10 changes: 10 additions & 0 deletions monitoring/perf_flag_imp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <cstdint>
#include "rocksdb/perf_flag.h"

namespace rocksdb {
#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL
extern __thread uint8_t perf_flags[FLAGS_LEN];
#else
extern uint8_t perf_flags[FLAGS_LEN];
#endif
}
5 changes: 3 additions & 2 deletions monitoring/perf_step_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//
#pragma once
#include "monitoring/perf_level_imp.h"
#include "monitoring/perf_flag_imp.h"
#include "rocksdb/env.h"
#include "util/stop_watch.h"

Expand All @@ -13,10 +14,10 @@ namespace rocksdb {
class PerfStepTimer {
public:
explicit PerfStepTimer(
uint64_t* metric, Env* env = nullptr, bool use_cpu_time = false,
uint64_t* metric, bool enable_flag = false, 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),
: perf_counter_enabled_(perf_level >= enable_level || enable_flag),
use_cpu_time_(use_cpu_time),
env_((perf_counter_enabled_ || statistics != nullptr)
? ((env != nullptr) ? env : Env::Default())
Expand Down

0 comments on commit f955144

Please sign in to comment.