YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/stop_watch.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
#pragma once
21
22
#include <string.h>
23
24
#include "yb/rocksdb/env.h"
25
#include "yb/rocksdb/statistics.h"
26
27
namespace rocksdb {
28
// Auto-scoped.
29
// Records the measure time into the corresponding histogram if statistics
30
// is not nullptr. It is also saved into *elapsed if the pointer is not nullptr.
31
class StopWatch {
32
 public:
33
  StopWatch(Env * const env, Statistics* statistics,
34
            const uint32_t hist_type,
35
            uint64_t* elapsed = nullptr)
36
    : env_(env),
37
      statistics_(statistics),
38
      hist_type_(hist_type),
39
      elapsed_(elapsed),
40
      stats_enabled_(statistics && statistics->HistEnabledForType(hist_type)),
41
      start_time_((stats_enabled_ || elapsed != nullptr) ?
42
41.5M
                  env->NowMicros() : 0) {
43
41.5M
  }
44
45
46
41.4M
  ~StopWatch() {
47
41.4M
    if (elapsed_) {
48
294k
      *elapsed_ = env_->NowMicros() - start_time_;
49
294k
    }
50
41.4M
    if (stats_enabled_) {
51
7.88M
      statistics_->measureTime(hist_type_,
52
294k
          (elapsed_ != nullptr) ? *elapsed_ :
53
7.59M
                                  (env_->NowMicros() - start_time_));
54
7.88M
    }
55
41.4M
  }
56
57
 private:
58
  Env* const env_;
59
  Statistics* statistics_;
60
  const uint32_t hist_type_;
61
  uint64_t* elapsed_;
62
  bool stats_enabled_;
63
  const uint64_t start_time_;
64
};
65
66
// a nano second precision stopwatch
67
class StopWatchNano {
68
 public:
69
  explicit StopWatchNano(Env* const env, bool auto_start = false)
70
959k
      : env_(env), start_(0) {
71
959k
    if (auto_start) {
72
843k
      Start();
73
843k
    }
74
959k
  }
75
76
1.00M
  void Start() { start_ = env_->NowNanos(); }
77
78
1.92M
  uint64_t ElapsedNanos(bool reset = false) {
79
1.92M
    auto now = env_->NowNanos();
80
1.92M
    auto elapsed = now - start_;
81
1.92M
    if (reset) {
82
1.00M
      start_ = now;
83
1.00M
    }
84
1.92M
    return elapsed;
85
1.92M
  }
86
87
43.4k
  uint64_t ElapsedNanosSafe(bool reset = false) {
88
43.4k
    return (env_ != nullptr) ? ElapsedNanos(reset) : 0U;
89
43.4k
  }
90
91
 private:
92
  Env* const env_;
93
  uint64_t start_;
94
};
95
96
} // namespace rocksdb