rocksdb/monitoring/perf_step_timer.h
mrambacher 12f1137355 Add a SystemClock class to capture the time functions of an Env (#7858)
Summary:
Introduces and uses a SystemClock class to RocksDB.  This class contains the time-related functions of an Env and these functions can be redirected from the Env to the SystemClock.

Many of the places that used an Env (Timer, PerfStepTimer, RepeatableThread, RateLimiter, WriteController) for time-related functions have been changed to use SystemClock instead.  There are likely more places that can be changed, but this is a start to show what can/should be done.  Over time it would be nice to migrate most (if not all) of the uses of the time functions from the Env to the SystemClock.

There are several Env classes that implement these functions.  Most of these have not been converted yet to SystemClock implementations; that will come in a subsequent PR.  It would be good to unify many of the Mock Timer implementations, so that they behave similarly and be tested similarly (some override Sleep, some use a MockSleep, etc).

Additionally, this change will allow new methods to be introduced to the SystemClock (like https://github.com/facebook/rocksdb/issues/7101 WaitFor) in a consistent manner across a smaller number of classes.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7858

Reviewed By: pdillinger

Differential Revision: D26006406

Pulled By: mrambacher

fbshipit-source-id: ed10a8abbdab7ff2e23d69d85bd25b3e7e899e90
2021-01-25 22:09:11 -08:00

81 lines
2.0 KiB
C++

// 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/perf_level_imp.h"
#include "monitoring/statistics.h"
#include "rocksdb/system_clock.h"
namespace ROCKSDB_NAMESPACE {
class PerfStepTimer {
public:
explicit PerfStepTimer(
uint64_t* metric, const std::shared_ptr<SystemClock>& clock = nullptr,
bool use_cpu_time = false,
PerfLevel enable_level = PerfLevel::kEnableTimeExceptForMutex,
Statistics* statistics = nullptr, uint32_t ticker_type = 0)
: perf_counter_enabled_(perf_level >= enable_level),
use_cpu_time_(use_cpu_time),
clock_((perf_counter_enabled_ || statistics != nullptr)
? ((clock.get() != nullptr) ? clock : SystemClock::Default())
: nullptr),
start_(0),
metric_(metric),
statistics_(statistics),
ticker_type_(ticker_type) {}
~PerfStepTimer() {
Stop();
}
void Start() {
if (perf_counter_enabled_ || statistics_ != nullptr) {
start_ = time_now();
}
}
void Measure() {
if (start_) {
uint64_t now = time_now();
*metric_ += now - start_;
start_ = now;
}
}
void Stop() {
if (start_) {
uint64_t duration = time_now() - start_;
if (perf_counter_enabled_) {
*metric_ += duration;
}
if (statistics_ != nullptr) {
RecordTick(statistics_, ticker_type_, duration);
}
start_ = 0;
}
}
private:
uint64_t time_now() {
if (!use_cpu_time_) {
return clock_->NowNanos();
} else {
return clock_->CPUNanos();
}
}
const bool perf_counter_enabled_;
const bool use_cpu_time_;
std::shared_ptr<SystemClock> clock_;
uint64_t start_;
uint64_t* metric_;
Statistics* statistics_;
uint32_t ticker_type_;
};
} // namespace ROCKSDB_NAMESPACE