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).
7 #include "monitoring/perf_step_timer.h"
8 #include "rocksdb/perf_context.h"
9 #include "util/stop_watch.h"
11 namespace ROCKSDB_NAMESPACE
{
12 #if defined(NPERF_CONTEXT)
13 extern PerfContext perf_context
;
15 #if defined(OS_SOLARIS)
16 extern thread_local PerfContext perf_context_
;
17 #define perf_context (*get_perf_context())
19 extern thread_local PerfContext perf_context
;
23 #if defined(NPERF_CONTEXT)
25 #define PERF_TIMER_STOP(metric)
26 #define PERF_TIMER_START(metric)
27 #define PERF_TIMER_GUARD(metric)
28 #define PERF_TIMER_GUARD_WITH_CLOCK(metric, clock)
29 #define PERF_CPU_TIMER_GUARD(metric, clock)
30 #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \
32 #define PERF_TIMER_MEASURE(metric)
33 #define PERF_COUNTER_ADD(metric, value)
34 #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level)
38 // Stop the timer and update the metric
39 #define PERF_TIMER_STOP(metric) perf_step_timer_##metric.Stop();
41 #define PERF_TIMER_START(metric) perf_step_timer_##metric.Start();
43 // Declare and set start time of the timer
44 #define PERF_TIMER_GUARD(metric) \
45 PerfStepTimer perf_step_timer_##metric(&(perf_context.metric)); \
46 perf_step_timer_##metric.Start();
48 // Declare and set start time of the timer
49 #define PERF_TIMER_GUARD_WITH_CLOCK(metric, clock) \
50 PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), clock); \
51 perf_step_timer_##metric.Start();
53 // Declare and set start time of the timer
54 #define PERF_CPU_TIMER_GUARD(metric, clock) \
55 PerfStepTimer perf_step_timer_##metric( \
56 &(perf_context.metric), clock, true, \
57 PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \
58 perf_step_timer_##metric.Start();
60 #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \
62 PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), nullptr, \
63 false, PerfLevel::kEnableTime, stats, \
66 perf_step_timer_##metric.Start(); \
69 // Update metric with time elapsed since last START. start time is reset
70 // to current timestamp.
71 #define PERF_TIMER_MEASURE(metric) perf_step_timer_##metric.Measure();
73 // Increase metric value
74 #define PERF_COUNTER_ADD(metric, value) \
75 if (perf_level >= PerfLevel::kEnableCount) { \
76 perf_context.metric += value; \
79 // Increase metric value
80 #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level) \
81 if (perf_level >= PerfLevel::kEnableCount && \
82 perf_context.per_level_perf_context_enabled && \
83 perf_context.level_to_perf_context) { \
84 if ((*(perf_context.level_to_perf_context)).find(level) != \
85 (*(perf_context.level_to_perf_context)).end()) { \
86 (*(perf_context.level_to_perf_context))[level].metric += value; \
88 PerfContextByLevel empty_context; \
89 (*(perf_context.level_to_perf_context))[level] = empty_context; \
90 (*(perf_context.level_to_perf_context))[level].metric += value; \
96 } // namespace ROCKSDB_NAMESPACE