perf_context_imp.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/perf_context.h"
  9. #include "util/stop_watch.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. #if defined(NPERF_CONTEXT)
  12. extern PerfContext perf_context;
  13. #else
  14. #if defined(OS_SOLARIS)
  15. extern thread_local PerfContext perf_context_;
  16. #define perf_context (*get_perf_context())
  17. #else
  18. extern thread_local PerfContext perf_context;
  19. #endif
  20. #endif
  21. #if defined(NPERF_CONTEXT)
  22. #define PERF_TIMER_STOP(metric)
  23. #define PERF_TIMER_START(metric)
  24. #define PERF_TIMER_GUARD(metric)
  25. #define PERF_TIMER_GUARD_WITH_CLOCK(metric, clock)
  26. #define PERF_CPU_TIMER_GUARD(metric, clock)
  27. #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \
  28. ticker_type)
  29. #define PERF_TIMER_FOR_WAIT_GUARD(metric)
  30. #define PERF_TIMER_MEASURE(metric)
  31. #define PERF_COUNTER_ADD(metric, value)
  32. #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level)
  33. #else
  34. // Stop the timer and update the metric
  35. #define PERF_TIMER_STOP(metric) perf_step_timer_##metric.Stop();
  36. #define PERF_TIMER_START(metric) perf_step_timer_##metric.Start();
  37. // Declare and set start time of the timer
  38. #define PERF_TIMER_GUARD(metric) \
  39. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric)); \
  40. perf_step_timer_##metric.Start();
  41. // Declare and set start time of the timer
  42. #define PERF_TIMER_GUARD_WITH_CLOCK(metric, clock) \
  43. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), clock); \
  44. perf_step_timer_##metric.Start();
  45. // Declare and set start time of the timer
  46. #define PERF_CPU_TIMER_GUARD(metric, clock) \
  47. PerfStepTimer perf_step_timer_##metric( \
  48. &(perf_context.metric), clock, true, \
  49. PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \
  50. perf_step_timer_##metric.Start();
  51. #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \
  52. ticker_type) \
  53. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), nullptr, \
  54. false, PerfLevel::kEnableTime, stats, \
  55. ticker_type); \
  56. if (condition) { \
  57. perf_step_timer_##metric.Start(); \
  58. }
  59. #define PERF_TIMER_FOR_WAIT_GUARD(metric) \
  60. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), nullptr, \
  61. false, PerfLevel::kEnableWait); \
  62. perf_step_timer_##metric.Start();
  63. // Update metric with time elapsed since last START. start time is reset
  64. // to current timestamp.
  65. #define PERF_TIMER_MEASURE(metric) perf_step_timer_##metric.Measure();
  66. // Increase metric value
  67. #define PERF_COUNTER_ADD(metric, value) \
  68. if (perf_level >= PerfLevel::kEnableCount) { \
  69. perf_context.metric += value; \
  70. } \
  71. static_assert(true, "semicolon required")
  72. // Increase metric value
  73. #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level) \
  74. if (perf_level >= PerfLevel::kEnableCount && \
  75. perf_context.per_level_perf_context_enabled && \
  76. perf_context.level_to_perf_context) { \
  77. if ((*(perf_context.level_to_perf_context)).find(level) != \
  78. (*(perf_context.level_to_perf_context)).end()) { \
  79. (*(perf_context.level_to_perf_context))[level].metric += value; \
  80. } else { \
  81. PerfContextByLevel empty_context; \
  82. (*(perf_context.level_to_perf_context))[level] = empty_context; \
  83. (*(perf_context.level_to_perf_context))[level].metric += value; \
  84. } \
  85. }
  86. #endif
  87. } // namespace ROCKSDB_NAMESPACE