stop_watch.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.h"
  8. #include "rocksdb/env.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. // Auto-scoped.
  11. // Records the measure time into the corresponding histogram if statistics
  12. // is not nullptr. It is also saved into *elapsed if the pointer is not nullptr
  13. // and overwrite is true, it will be added to *elapsed if overwrite is false.
  14. class StopWatch {
  15. public:
  16. StopWatch(Env* const env, Statistics* statistics, const uint32_t hist_type,
  17. uint64_t* elapsed = nullptr, bool overwrite = true,
  18. bool delay_enabled = false)
  19. : env_(env),
  20. statistics_(statistics),
  21. hist_type_(hist_type),
  22. elapsed_(elapsed),
  23. overwrite_(overwrite),
  24. stats_enabled_(statistics &&
  25. statistics->get_stats_level() >=
  26. StatsLevel::kExceptTimers &&
  27. statistics->HistEnabledForType(hist_type)),
  28. delay_enabled_(delay_enabled),
  29. total_delay_(0),
  30. delay_start_time_(0),
  31. start_time_((stats_enabled_ || elapsed != nullptr) ? env->NowMicros()
  32. : 0) {}
  33. ~StopWatch() {
  34. if (elapsed_) {
  35. if (overwrite_) {
  36. *elapsed_ = env_->NowMicros() - start_time_;
  37. } else {
  38. *elapsed_ += env_->NowMicros() - start_time_;
  39. }
  40. }
  41. if (elapsed_ && delay_enabled_) {
  42. *elapsed_ -= total_delay_;
  43. }
  44. if (stats_enabled_) {
  45. statistics_->reportTimeToHistogram(
  46. hist_type_, (elapsed_ != nullptr)
  47. ? *elapsed_
  48. : (env_->NowMicros() - start_time_));
  49. }
  50. }
  51. void DelayStart() {
  52. // if delay_start_time_ is not 0, it means we are already tracking delay,
  53. // so delay_start_time_ should not be overwritten
  54. if (elapsed_ && delay_enabled_ && delay_start_time_ == 0) {
  55. delay_start_time_ = env_->NowMicros();
  56. }
  57. }
  58. void DelayStop() {
  59. if (elapsed_ && delay_enabled_ && delay_start_time_ != 0) {
  60. total_delay_ += env_->NowMicros() - delay_start_time_;
  61. }
  62. // reset to 0 means currently no delay is being tracked, so two consecutive
  63. // calls to DelayStop will not increase total_delay_
  64. delay_start_time_ = 0;
  65. }
  66. uint64_t GetDelay() const { return delay_enabled_ ? total_delay_ : 0; }
  67. uint64_t start_time() const { return start_time_; }
  68. private:
  69. Env* const env_;
  70. Statistics* statistics_;
  71. const uint32_t hist_type_;
  72. uint64_t* elapsed_;
  73. bool overwrite_;
  74. bool stats_enabled_;
  75. bool delay_enabled_;
  76. uint64_t total_delay_;
  77. uint64_t delay_start_time_;
  78. const uint64_t start_time_;
  79. };
  80. // a nano second precision stopwatch
  81. class StopWatchNano {
  82. public:
  83. explicit StopWatchNano(Env* const env, bool auto_start = false)
  84. : env_(env), start_(0) {
  85. if (auto_start) {
  86. Start();
  87. }
  88. }
  89. void Start() { start_ = env_->NowNanos(); }
  90. uint64_t ElapsedNanos(bool reset = false) {
  91. auto now = env_->NowNanos();
  92. auto elapsed = now - start_;
  93. if (reset) {
  94. start_ = now;
  95. }
  96. return elapsed;
  97. }
  98. uint64_t ElapsedNanosSafe(bool reset = false) {
  99. return (env_ != nullptr) ? ElapsedNanos(reset) : 0U;
  100. }
  101. private:
  102. Env* const env_;
  103. uint64_t start_;
  104. };
  105. } // namespace ROCKSDB_NAMESPACE