]>
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
&& statistics
->HistEnabledForType(hist_type
)),
26 delay_enabled_(delay_enabled
),
29 start_time_((stats_enabled_
|| elapsed
!= nullptr) ? env
->NowMicros()
35 *elapsed_
= env_
->NowMicros() - start_time_
;
37 *elapsed_
+= env_
->NowMicros() - start_time_
;
40 if (elapsed_
&& delay_enabled_
) {
41 *elapsed_
-= total_delay_
;
44 statistics_
->measureTime(hist_type_
,
45 (elapsed_
!= nullptr) ? *elapsed_
:
46 (env_
->NowMicros() - start_time_
));
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();
59 if (elapsed_
&& delay_enabled_
&& delay_start_time_
!= 0) {
60 total_delay_
+= env_
->NowMicros() - delay_start_time_
;
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;
67 uint64_t GetDelay() const { return delay_enabled_
? total_delay_
: 0; }
69 uint64_t start_time() const { return start_time_
; }
73 Statistics
* statistics_
;
74 const uint32_t hist_type_
;
79 uint64_t total_delay_
;
80 uint64_t delay_start_time_
;
81 const uint64_t start_time_
;
84 // a nano second precision stopwatch
87 explicit StopWatchNano(Env
* const env
, bool auto_start
= false)
88 : env_(env
), start_(0) {
94 void Start() { start_
= env_
->NowNanos(); }
96 uint64_t ElapsedNanos(bool reset
= false) {
97 auto now
= env_
->NowNanos();
98 auto elapsed
= now
- start_
;
105 uint64_t ElapsedNanosSafe(bool reset
= false) {
106 return (env_
!= nullptr) ? ElapsedNanos(reset
) : 0U;
114 } // namespace rocksdb