/src/rocksdb/monitoring/perf_step_timer.h
Line | Count | Source |
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/perf_level_imp.h" |
8 | | #include "monitoring/statistics_impl.h" |
9 | | #include "rocksdb/system_clock.h" |
10 | | |
11 | | namespace ROCKSDB_NAMESPACE { |
12 | | |
13 | | class PerfStepTimer { |
14 | | public: |
15 | | explicit PerfStepTimer( |
16 | | uint64_t* metric, SystemClock* clock = nullptr, bool use_cpu_time = false, |
17 | | PerfLevel enable_level = PerfLevel::kEnableTimeExceptForMutex, |
18 | | Statistics* statistics = nullptr, uint32_t ticker_type = 0) |
19 | 55.3M | : perf_counter_enabled_(perf_level >= enable_level), |
20 | 55.3M | use_cpu_time_(use_cpu_time), |
21 | 55.3M | ticker_type_(ticker_type), |
22 | 55.3M | clock_((perf_counter_enabled_ || statistics != nullptr) |
23 | 55.3M | ? (clock ? clock : SystemClock::Default().get()) |
24 | 55.3M | : nullptr), |
25 | 55.3M | start_(0), |
26 | 55.3M | metric_(metric), |
27 | 55.3M | statistics_(statistics) {} |
28 | | |
29 | 55.2M | ~PerfStepTimer() { Stop(); } |
30 | | |
31 | 54.1M | void Start() { |
32 | 54.1M | if (perf_counter_enabled_ || statistics_ != nullptr) { |
33 | 0 | start_ = time_now(); |
34 | 0 | } |
35 | 54.1M | } |
36 | | |
37 | 0 | void Measure() { |
38 | 0 | if (start_) { |
39 | 0 | uint64_t now = time_now(); |
40 | 0 | *metric_ += now - start_; |
41 | 0 | start_ = now; |
42 | 0 | } |
43 | 0 | } |
44 | | |
45 | 56.8M | void Stop() { |
46 | 56.8M | if (start_) { |
47 | 0 | uint64_t duration = time_now() - start_; |
48 | 0 | if (perf_counter_enabled_) { |
49 | 0 | *metric_ += duration; |
50 | 0 | } |
51 | |
|
52 | 0 | if (statistics_ != nullptr) { |
53 | 0 | RecordTick(statistics_, ticker_type_, duration); |
54 | 0 | } |
55 | 0 | start_ = 0; |
56 | 0 | } |
57 | 56.8M | } |
58 | | |
59 | | private: |
60 | 0 | uint64_t time_now() { |
61 | 0 | if (!use_cpu_time_) { |
62 | 0 | return clock_->NowNanos(); |
63 | 0 | } else { |
64 | 0 | return clock_->CPUNanos(); |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | const bool perf_counter_enabled_; |
69 | | const bool use_cpu_time_; |
70 | | uint32_t ticker_type_; |
71 | | SystemClock* const clock_; |
72 | | uint64_t start_; |
73 | | uint64_t* metric_; |
74 | | Statistics* statistics_; |
75 | | }; |
76 | | |
77 | | } // namespace ROCKSDB_NAMESPACE |