/Users/deen/code/yugabyte-db/src/yb/util/stats/perf_step_timer.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #ifndef YB_UTIL_STATS_PERF_STEP_TIMER_H |
22 | | #define YB_UTIL_STATS_PERF_STEP_TIMER_H |
23 | | |
24 | | #include "yb/util/env.h" |
25 | | #include "yb/util/stats/perf_level_imp.h" |
26 | | |
27 | | namespace yb { |
28 | | |
29 | | class PerfStepTimer { |
30 | | public: |
31 | | explicit PerfStepTimer(uint64_t* metric, bool for_mutex = false) |
32 | | : enabled_(perf_level >= PerfLevel::kEnableTime || |
33 | | (!for_mutex && perf_level >= PerfLevel::kEnableTimeExceptForMutex)), |
34 | | env_(enabled_ ? Env::Default() : nullptr), |
35 | | start_(0), |
36 | 1.01G | metric_(metric) {} |
37 | | |
38 | 1.01G | ~PerfStepTimer() { |
39 | 1.01G | Stop(); |
40 | 1.01G | } |
41 | | |
42 | 1.03G | void Start() { |
43 | 1.03G | if (enabled_) { |
44 | 105k | start_ = env_->NowNanos(); |
45 | 105k | } |
46 | 1.03G | } |
47 | | |
48 | 0 | void Measure() { |
49 | 0 | if (start_) { |
50 | 0 | uint64_t now = env_->NowNanos(); |
51 | 0 | *metric_ += now - start_; |
52 | 0 | start_ = now; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 1.04G | void Stop() { |
57 | 1.04G | if (start_) { |
58 | 105k | *metric_ += env_->NowNanos() - start_; |
59 | 105k | start_ = 0; |
60 | 105k | } |
61 | 1.04G | } |
62 | | |
63 | | private: |
64 | | const bool enabled_; |
65 | | Env* const env_; |
66 | | uint64_t start_; |
67 | | uint64_t* metric_; |
68 | | }; |
69 | | |
70 | | } // namespace yb |
71 | | |
72 | | #endif // YB_UTIL_STATS_PERF_STEP_TIMER_H |