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