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 #pragma once
7 #include "monitoring/perf_step_timer.h"
8 #include "rocksdb/iostats_context.h"
9 
10 #ifdef ROCKSDB_SUPPORT_THREAD_LOCAL
11 namespace ROCKSDB_NAMESPACE {
12 extern __thread IOStatsContext iostats_context;
13 }  // namespace ROCKSDB_NAMESPACE
14 
15 // increment a specific counter by the specified value
16 #define IOSTATS_ADD(metric, value) (iostats_context.metric += value)
17 
18 // Increase metric value only when it is positive
19 #define IOSTATS_ADD_IF_POSITIVE(metric, value)   \
20   if (value > 0) { IOSTATS_ADD(metric, value); }
21 
22 // reset a specific counter to zero
23 #define IOSTATS_RESET(metric) (iostats_context.metric = 0)
24 
25 // reset all counters to zero
26 #define IOSTATS_RESET_ALL() (iostats_context.Reset())
27 
28 #define IOSTATS_SET_THREAD_POOL_ID(value) \
29   (iostats_context.thread_pool_id = value)
30 
31 #define IOSTATS_THREAD_POOL_ID() (iostats_context.thread_pool_id)
32 
33 #define IOSTATS(metric) (iostats_context.metric)
34 
35 // Declare and set start time of the timer
36 #define IOSTATS_TIMER_GUARD(metric)                                     \
37   PerfStepTimer iostats_step_timer_##metric(&(iostats_context.metric)); \
38   iostats_step_timer_##metric.Start();
39 
40 // Declare and set start time of the timer
41 #define IOSTATS_CPU_TIMER_GUARD(metric, env)           \
42   PerfStepTimer iostats_step_timer_##metric(           \
43       &(iostats_context.metric), env, true,            \
44       PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \
45   iostats_step_timer_##metric.Start();
46 
47 #else  // ROCKSDB_SUPPORT_THREAD_LOCAL
48 
49 #define IOSTATS_ADD(metric, value)
50 #define IOSTATS_ADD_IF_POSITIVE(metric, value)
51 #define IOSTATS_RESET(metric)
52 #define IOSTATS_RESET_ALL()
53 #define IOSTATS_SET_THREAD_POOL_ID(value)
54 #define IOSTATS_THREAD_POOL_ID()
55 #define IOSTATS(metric) 0
56 
57 #define IOSTATS_TIMER_GUARD(metric)
58 #define IOSTATS_CPU_TIMER_GUARD(metric, env)   static_cast<void>(env)
59 
60 #endif  // ROCKSDB_SUPPORT_THREAD_LOCAL
61