]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/stop_watch.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / util / stop_watch.h
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
10 namespace rocksdb {
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.
15 class StopWatch {
16 public:
17 StopWatch(Env* const env, Statistics* statistics, const uint32_t hist_type,
18 uint64_t* elapsed = nullptr, bool overwrite = true,
19 bool delay_enabled = false)
20 : env_(env),
21 statistics_(statistics),
22 hist_type_(hist_type),
23 elapsed_(elapsed),
24 overwrite_(overwrite),
25 stats_enabled_(statistics && statistics->HistEnabledForType(hist_type)),
26 delay_enabled_(delay_enabled),
27 total_delay_(0),
28 delay_start_time_(0),
29 start_time_((stats_enabled_ || elapsed != nullptr) ? env->NowMicros()
30 : 0) {}
31
32 ~StopWatch() {
33 if (elapsed_) {
34 if (overwrite_) {
35 *elapsed_ = env_->NowMicros() - start_time_;
36 } else {
37 *elapsed_ += env_->NowMicros() - start_time_;
38 }
39 }
40 if (elapsed_ && delay_enabled_) {
41 *elapsed_ -= total_delay_;
42 }
43 if (stats_enabled_) {
44 statistics_->measureTime(hist_type_,
45 (elapsed_ != nullptr) ? *elapsed_ :
46 (env_->NowMicros() - start_time_));
47 }
48 }
49
50 void DelayStart() {
51 // if delay_start_time_ is not 0, it means we are already tracking delay,
52 // so delay_start_time_ should not be overwritten
53 if (elapsed_ && delay_enabled_ && delay_start_time_ == 0) {
54 delay_start_time_ = env_->NowMicros();
55 }
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
67 uint64_t GetDelay() const { return delay_enabled_ ? total_delay_ : 0; }
68
69 uint64_t start_time() const { return start_time_; }
70
71 private:
72 Env* const env_;
73 Statistics* statistics_;
74 const uint32_t hist_type_;
75 uint64_t* elapsed_;
76 bool overwrite_;
77 bool stats_enabled_;
78 bool delay_enabled_;
79 uint64_t total_delay_;
80 uint64_t delay_start_time_;
81 const uint64_t start_time_;
82 };
83
84 // a nano second precision stopwatch
85 class StopWatchNano {
86 public:
87 explicit StopWatchNano(Env* const env, bool auto_start = false)
88 : env_(env), start_(0) {
89 if (auto_start) {
90 Start();
91 }
92 }
93
94 void Start() { start_ = env_->NowNanos(); }
95
96 uint64_t ElapsedNanos(bool reset = false) {
97 auto now = env_->NowNanos();
98 auto elapsed = now - start_;
99 if (reset) {
100 start_ = now;
101 }
102 return elapsed;
103 }
104
105 uint64_t ElapsedNanosSafe(bool reset = false) {
106 return (env_ != nullptr) ? ElapsedNanos(reset) : 0U;
107 }
108
109 private:
110 Env* const env_;
111 uint64_t start_;
112 };
113
114 } // namespace rocksdb