]>
git.proxmox.com Git - ceph.git/blob - 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).
7 #include "monitoring/statistics.h"
8 #include "rocksdb/env.h"
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.
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)
21 statistics_(statistics
),
22 hist_type_(hist_type
),
24 overwrite_(overwrite
),
25 stats_enabled_(statistics
&&
26 statistics
->get_stats_level() >=
27 StatsLevel::kExceptTimers
&&
28 statistics
->HistEnabledForType(hist_type
)),
29 delay_enabled_(delay_enabled
),
32 start_time_((stats_enabled_
|| elapsed
!= nullptr) ? env
->NowMicros()
38 *elapsed_
= env_
->NowMicros() - start_time_
;
40 *elapsed_
+= env_
->NowMicros() - start_time_
;
43 if (elapsed_
&& delay_enabled_
) {
44 *elapsed_
-= total_delay_
;
47 statistics_
->reportTimeToHistogram(
48 hist_type_
, (elapsed_
!= nullptr)
50 : (env_
->NowMicros() - start_time_
));
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) {
58 delay_start_time_
= env_
->NowMicros();
63 if (elapsed_
&& delay_enabled_
&& delay_start_time_
!= 0) {
64 total_delay_
+= env_
->NowMicros() - delay_start_time_
;
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;
71 uint64_t GetDelay() const { return delay_enabled_
? total_delay_
: 0; }
73 uint64_t start_time() const { return start_time_
; }
77 Statistics
* statistics_
;
78 const uint32_t hist_type_
;
83 uint64_t total_delay_
;
84 uint64_t delay_start_time_
;
85 const uint64_t start_time_
;
88 // a nano second precision stopwatch
91 explicit StopWatchNano(Env
* const env
, bool auto_start
= false)
92 : env_(env
), start_(0) {
98 void Start() { start_
= env_
->NowNanos(); }
100 uint64_t ElapsedNanos(bool reset
= false) {
101 auto now
= env_
->NowNanos();
102 auto elapsed
= now
- start_
;
109 uint64_t ElapsedNanosSafe(bool reset
= false) {
110 return (env_
!= nullptr) ? ElapsedNanos(reset
) : 0U;
118 } // namespace rocksdb