perf_context_imp.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) || !defined(ROCKSDB_SUPPORT_THREAD_LOCAL)
  12. extern PerfContext perf_context;
  13. #else
  14. #if defined(OS_SOLARIS)
  15. extern __thread 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_ENV(metric, env)
  26. #define PERF_CPU_TIMER_GUARD(metric, env)
  27. #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \
  28. ticker_type)
  29. #define PERF_TIMER_MEASURE(metric)
  30. #define PERF_COUNTER_ADD(metric, value)
  31. #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level)
  32. #else
  33. // Stop the timer and update the metric
  34. #define PERF_TIMER_STOP(metric) perf_step_timer_##metric.Stop();
  35. #define PERF_TIMER_START(metric) perf_step_timer_##metric.Start();
  36. // Declare and set start time of the timer
  37. #define PERF_TIMER_GUARD(metric) \
  38. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric)); \
  39. perf_step_timer_##metric.Start();
  40. // Declare and set start time of the timer
  41. #define PERF_TIMER_GUARD_WITH_ENV(metric, env) \
  42. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), env); \
  43. perf_step_timer_##metric.Start();
  44. // Declare and set start time of the timer
  45. #define PERF_CPU_TIMER_GUARD(metric, env) \
  46. PerfStepTimer perf_step_timer_##metric( \
  47. &(perf_context.metric), env, true, \
  48. PerfLevel::kEnableTimeAndCPUTimeExceptForMutex); \
  49. perf_step_timer_##metric.Start();
  50. #define PERF_CONDITIONAL_TIMER_FOR_MUTEX_GUARD(metric, condition, stats, \
  51. ticker_type) \
  52. PerfStepTimer perf_step_timer_##metric(&(perf_context.metric), nullptr, \
  53. false, PerfLevel::kEnableTime, stats, \
  54. ticker_type); \
  55. if (condition) { \
  56. perf_step_timer_##metric.Start(); \
  57. }
  58. // Update metric with time elapsed since last START. start time is reset
  59. // to current timestamp.
  60. #define PERF_TIMER_MEASURE(metric) perf_step_timer_##metric.Measure();
  61. // Increase metric value
  62. #define PERF_COUNTER_ADD(metric, value) \
  63. if (perf_level >= PerfLevel::kEnableCount) { \
  64. perf_context.metric += value; \
  65. }
  66. // Increase metric value
  67. #define PERF_COUNTER_BY_LEVEL_ADD(metric, value, level) \
  68. if (perf_level >= PerfLevel::kEnableCount && \
  69. perf_context.per_level_perf_context_enabled && \
  70. perf_context.level_to_perf_context) { \
  71. if ((*(perf_context.level_to_perf_context)).find(level) != \
  72. (*(perf_context.level_to_perf_context)).end()) { \
  73. (*(perf_context.level_to_perf_context))[level].metric += value; \
  74. } \
  75. else { \
  76. PerfContextByLevel empty_context; \
  77. (*(perf_context.level_to_perf_context))[level] = empty_context; \
  78. (*(perf_context.level_to_perf_context))[level].metric += value; \
  79. } \
  80. } \
  81. #endif
  82. } // namespace ROCKSDB_NAMESPACE