forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop_watch.h
118 lines (105 loc) · 3.44 KB
/
stop_watch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
//
#pragma once
#include "monitoring/statistics.h"
#include "rocksdb/env.h"
namespace rocksdb {
// Auto-scoped.
// Records the measure time into the corresponding histogram if statistics
// is not nullptr. It is also saved into *elapsed if the pointer is not nullptr
// and overwrite is true, it will be added to *elapsed if overwrite is false.
class StopWatch {
public:
StopWatch(Env* const env, Statistics* statistics, const uint32_t hist_type,
uint64_t* elapsed = nullptr, bool overwrite = true,
bool delay_enabled = false)
: env_(env),
statistics_(statistics),
hist_type_(hist_type),
elapsed_(elapsed),
overwrite_(overwrite),
stats_enabled_(statistics &&
statistics->get_stats_level() >=
StatsLevel::kExceptTimers &&
statistics->HistEnabledForType(hist_type)),
delay_enabled_(delay_enabled),
total_delay_(0),
delay_start_time_(0),
start_time_((stats_enabled_ || elapsed != nullptr) ? env->NowMicros()
: 0) {}
~StopWatch() {
if (elapsed_) {
if (overwrite_) {
*elapsed_ = env_->NowMicros() - start_time_;
} else {
*elapsed_ += env_->NowMicros() - start_time_;
}
}
if (elapsed_ && delay_enabled_) {
*elapsed_ -= total_delay_;
}
if (stats_enabled_) {
statistics_->reportTimeToHistogram(
hist_type_, (elapsed_ != nullptr)
? *elapsed_
: (env_->NowMicros() - start_time_));
}
}
void DelayStart() {
// if delay_start_time_ is not 0, it means we are already tracking delay,
// so delay_start_time_ should not be overwritten
if (elapsed_ && delay_enabled_ && delay_start_time_ == 0) {
delay_start_time_ = env_->NowMicros();
}
}
void DelayStop() {
if (elapsed_ && delay_enabled_ && delay_start_time_ != 0) {
total_delay_ += env_->NowMicros() - delay_start_time_;
}
// reset to 0 means currently no delay is being tracked, so two consecutive
// calls to DelayStop will not increase total_delay_
delay_start_time_ = 0;
}
uint64_t GetDelay() const { return delay_enabled_ ? total_delay_ : 0; }
uint64_t start_time() const { return start_time_; }
private:
Env* const env_;
Statistics* statistics_;
const uint32_t hist_type_;
uint64_t* elapsed_;
bool overwrite_;
bool stats_enabled_;
bool delay_enabled_;
uint64_t total_delay_;
uint64_t delay_start_time_;
const uint64_t start_time_;
};
// a nano second precision stopwatch
class StopWatchNano {
public:
explicit StopWatchNano(Env* const env, bool auto_start = false)
: env_(env), start_(0) {
if (auto_start) {
Start();
}
}
void Start() { start_ = env_->NowNanos(); }
uint64_t ElapsedNanos(bool reset = false) {
auto now = env_->NowNanos();
auto elapsed = now - start_;
if (reset) {
start_ = now;
}
return elapsed;
}
uint64_t ElapsedNanosSafe(bool reset = false) {
return (env_ != nullptr) ? ElapsedNanos(reset) : 0U;
}
private:
Env* const env_;
uint64_t start_;
};
} // namespace rocksdb