]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/util/stop_watch.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / util / stop_watch.h
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5//
6#pragma once
7#include "monitoring/statistics.h"
1e59de90 8#include "rocksdb/system_clock.h"
7c673cae 9
f67539c2 10namespace ROCKSDB_NAMESPACE {
7c673cae
FG
11// Auto-scoped.
12// Records the measure time into the corresponding histogram if statistics
13// is not nullptr. It is also saved into *elapsed if the pointer is not nullptr
14// and overwrite is true, it will be added to *elapsed if overwrite is false.
15class StopWatch {
16 public:
1e59de90
TL
17 StopWatch(SystemClock* clock, Statistics* statistics,
18 const uint32_t hist_type, uint64_t* elapsed = nullptr,
19 bool overwrite = true, bool delay_enabled = false)
20 : clock_(clock),
7c673cae
FG
21 statistics_(statistics),
22 hist_type_(hist_type),
23 elapsed_(elapsed),
24 overwrite_(overwrite),
494da23a
TL
25 stats_enabled_(statistics &&
26 statistics->get_stats_level() >=
27 StatsLevel::kExceptTimers &&
28 statistics->HistEnabledForType(hist_type)),
11fdf7f2
TL
29 delay_enabled_(delay_enabled),
30 total_delay_(0),
31 delay_start_time_(0),
1e59de90 32 start_time_((stats_enabled_ || elapsed != nullptr) ? clock->NowMicros()
7c673cae
FG
33 : 0) {}
34
35 ~StopWatch() {
36 if (elapsed_) {
37 if (overwrite_) {
1e59de90 38 *elapsed_ = clock_->NowMicros() - start_time_;
7c673cae 39 } else {
1e59de90 40 *elapsed_ += clock_->NowMicros() - start_time_;
7c673cae
FG
41 }
42 }
11fdf7f2
TL
43 if (elapsed_ && delay_enabled_) {
44 *elapsed_ -= total_delay_;
45 }
7c673cae 46 if (stats_enabled_) {
494da23a
TL
47 statistics_->reportTimeToHistogram(
48 hist_type_, (elapsed_ != nullptr)
49 ? *elapsed_
1e59de90 50 : (clock_->NowMicros() - start_time_));
7c673cae
FG
51 }
52 }
53
11fdf7f2
TL
54 void DelayStart() {
55 // if delay_start_time_ is not 0, it means we are already tracking delay,
56 // so delay_start_time_ should not be overwritten
57 if (elapsed_ && delay_enabled_ && delay_start_time_ == 0) {
1e59de90 58 delay_start_time_ = clock_->NowMicros();
11fdf7f2
TL
59 }
60 }
61
62 void DelayStop() {
63 if (elapsed_ && delay_enabled_ && delay_start_time_ != 0) {
1e59de90 64 total_delay_ += clock_->NowMicros() - delay_start_time_;
11fdf7f2
TL
65 }
66 // reset to 0 means currently no delay is being tracked, so two consecutive
67 // calls to DelayStop will not increase total_delay_
68 delay_start_time_ = 0;
69 }
70
71 uint64_t GetDelay() const { return delay_enabled_ ? total_delay_ : 0; }
72
7c673cae
FG
73 uint64_t start_time() const { return start_time_; }
74
75 private:
1e59de90 76 SystemClock* clock_;
7c673cae
FG
77 Statistics* statistics_;
78 const uint32_t hist_type_;
79 uint64_t* elapsed_;
80 bool overwrite_;
81 bool stats_enabled_;
11fdf7f2
TL
82 bool delay_enabled_;
83 uint64_t total_delay_;
84 uint64_t delay_start_time_;
7c673cae
FG
85 const uint64_t start_time_;
86};
87
88// a nano second precision stopwatch
89class StopWatchNano {
90 public:
1e59de90
TL
91 explicit StopWatchNano(SystemClock* clock, bool auto_start = false)
92 : clock_(clock), start_(0) {
7c673cae
FG
93 if (auto_start) {
94 Start();
95 }
96 }
97
1e59de90 98 void Start() { start_ = clock_->NowNanos(); }
7c673cae
FG
99
100 uint64_t ElapsedNanos(bool reset = false) {
1e59de90 101 auto now = clock_->NowNanos();
7c673cae
FG
102 auto elapsed = now - start_;
103 if (reset) {
104 start_ = now;
105 }
106 return elapsed;
107 }
108
109 uint64_t ElapsedNanosSafe(bool reset = false) {
1e59de90 110 return (clock_ != nullptr) ? ElapsedNanos(reset) : 0U;
7c673cae
FG
111 }
112
113 private:
1e59de90 114 SystemClock* clock_;
7c673cae
FG
115 uint64_t start_;
116};
117
f67539c2 118} // namespace ROCKSDB_NAMESPACE