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