stop_watch.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/statistics_impl.h"
  8. #include "rocksdb/system_clock.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. // Auto-scoped.
  11. // When statistics is not nullptr, records the measured time into any enabled
  12. // histograms supplied to the constructor. A histogram argument may be omitted
  13. // by setting it to Histograms::HISTOGRAM_ENUM_MAX. It is also saved into
  14. // *elapsed if the pointer is not nullptr and overwrite is true, it will be
  15. // added to *elapsed if overwrite is false.
  16. class StopWatch {
  17. public:
  18. StopWatch(SystemClock* clock, Statistics* statistics,
  19. const uint32_t hist_type_1,
  20. const uint32_t hist_type_2 = Histograms::HISTOGRAM_ENUM_MAX,
  21. uint64_t* elapsed = nullptr, bool overwrite = true,
  22. bool delay_enabled = false)
  23. : clock_(clock),
  24. statistics_(statistics),
  25. hist_type_1_(statistics && statistics->HistEnabledForType(hist_type_1)
  26. ? hist_type_1
  27. : Histograms::HISTOGRAM_ENUM_MAX),
  28. hist_type_2_(statistics && statistics->HistEnabledForType(hist_type_2)
  29. ? hist_type_2
  30. : Histograms::HISTOGRAM_ENUM_MAX),
  31. elapsed_(elapsed),
  32. overwrite_(overwrite),
  33. stats_enabled_(statistics &&
  34. statistics->get_stats_level() >
  35. StatsLevel::kExceptTimers &&
  36. (hist_type_1_ != Histograms::HISTOGRAM_ENUM_MAX ||
  37. hist_type_2_ != Histograms::HISTOGRAM_ENUM_MAX)),
  38. delay_enabled_(delay_enabled),
  39. total_delay_(0),
  40. delay_start_time_(0),
  41. start_time_((stats_enabled_ || elapsed != nullptr) ? clock->NowMicros()
  42. : 0) {}
  43. ~StopWatch() {
  44. if (elapsed_) {
  45. if (overwrite_) {
  46. *elapsed_ = clock_->NowMicros() - start_time_;
  47. } else {
  48. *elapsed_ += clock_->NowMicros() - start_time_;
  49. }
  50. }
  51. if (elapsed_ && delay_enabled_) {
  52. *elapsed_ -= total_delay_;
  53. }
  54. if (stats_enabled_) {
  55. const auto time = (elapsed_ != nullptr)
  56. ? *elapsed_
  57. : (clock_->NowMicros() - start_time_);
  58. if (hist_type_1_ != Histograms::HISTOGRAM_ENUM_MAX) {
  59. statistics_->reportTimeToHistogram(hist_type_1_, time);
  60. }
  61. if (hist_type_2_ != Histograms::HISTOGRAM_ENUM_MAX) {
  62. statistics_->reportTimeToHistogram(hist_type_2_, time);
  63. }
  64. }
  65. }
  66. void DelayStart() {
  67. // if delay_start_time_ is not 0, it means we are already tracking delay,
  68. // so delay_start_time_ should not be overwritten
  69. if (elapsed_ && delay_enabled_ && delay_start_time_ == 0) {
  70. delay_start_time_ = clock_->NowMicros();
  71. }
  72. }
  73. void DelayStop() {
  74. if (elapsed_ && delay_enabled_ && delay_start_time_ != 0) {
  75. total_delay_ += clock_->NowMicros() - delay_start_time_;
  76. }
  77. // reset to 0 means currently no delay is being tracked, so two consecutive
  78. // calls to DelayStop will not increase total_delay_
  79. delay_start_time_ = 0;
  80. }
  81. uint64_t GetDelay() const { return delay_enabled_ ? total_delay_ : 0; }
  82. uint64_t start_time() const { return start_time_; }
  83. private:
  84. SystemClock* clock_;
  85. Statistics* statistics_;
  86. const uint32_t hist_type_1_;
  87. const uint32_t hist_type_2_;
  88. uint64_t* elapsed_;
  89. bool overwrite_;
  90. bool stats_enabled_;
  91. bool delay_enabled_;
  92. uint64_t total_delay_;
  93. uint64_t delay_start_time_;
  94. const uint64_t start_time_;
  95. };
  96. // a nano second precision stopwatch
  97. template <bool use_cpu_time = false>
  98. class StopWatchNano {
  99. public:
  100. explicit StopWatchNano(SystemClock* clock, bool auto_start = false)
  101. : clock_(clock), start_(0) {
  102. if (auto_start) {
  103. Start();
  104. }
  105. }
  106. void Start() {
  107. if constexpr (use_cpu_time) {
  108. start_ = clock_->CPUNanos();
  109. } else {
  110. start_ = clock_->NowNanos();
  111. }
  112. }
  113. uint64_t ElapsedNanos(bool reset = false) {
  114. uint64_t now = 0;
  115. if constexpr (use_cpu_time) {
  116. now = clock_->CPUNanos();
  117. } else {
  118. now = clock_->NowNanos();
  119. }
  120. auto elapsed = now - start_;
  121. if (reset) {
  122. start_ = now;
  123. }
  124. return elapsed;
  125. }
  126. uint64_t ElapsedNanosSafe(bool reset = false) {
  127. return (clock_ != nullptr) ? ElapsedNanos(reset) : 0U;
  128. }
  129. bool IsStarted() { return start_ != 0; }
  130. uint64_t ElapsedMicros(bool reset = false) {
  131. return ElapsedNanos(reset) / 1000;
  132. }
  133. private:
  134. SystemClock* clock_;
  135. uint64_t start_;
  136. };
  137. } // namespace ROCKSDB_NAMESPACE