rocksdb/util/stop_watch.h
Peter Dillinger d79be3dca2 Changes and enhancements to compression stats, thresholds (#11388)
Summary:
## Option API updates
* Add new CompressionOptions::max_compressed_bytes_per_kb, which corresponds to 1024.0 / min allowable compression ratio. This avoids the hard-coded minimum ratio of 8/7.
* Remove unnecessary constructor for CompressionOptions.
* Document undocumented CompressionOptions. Use idiom for default values shown clearly in one place (not precariously repeated).

 ## Stat API updates
* Deprecate the BYTES_COMPRESSED, BYTES_DECOMPRESSED histograms. Histograms incur substantial extra space & time costs compared to tickers, and the distribution of uncompressed data block sizes tends to be uninteresting. If we're interested in that distribution, I don't see why it should be limited to blocks stored as compressed.
* Deprecate the NUMBER_BLOCK_NOT_COMPRESSED ticker, because the name is very confusing.
* New or existing tickers relevant to compression:
  * BYTES_COMPRESSED_FROM
  * BYTES_COMPRESSED_TO
  * BYTES_COMPRESSION_BYPASSED
  * BYTES_COMPRESSION_REJECTED
  * COMPACT_WRITE_BYTES + FLUSH_WRITE_BYTES (both existing)
  * NUMBER_BLOCK_COMPRESSED (existing)
  * NUMBER_BLOCK_COMPRESSION_BYPASSED
  * NUMBER_BLOCK_COMPRESSION_REJECTED
  * BYTES_DECOMPRESSED_FROM
  * BYTES_DECOMPRESSED_TO

We can compute a number of things with these stats:
* "Successful" compression ratio: BYTES_COMPRESSED_FROM / BYTES_COMPRESSED_TO
* Compression ratio of data on which compression was attempted: (BYTES_COMPRESSED_FROM + BYTES_COMPRESSION_REJECTED) / (BYTES_COMPRESSED_TO + BYTES_COMPRESSION_REJECTED)
* Compression ratio of data that could be eligible for compression: (BYTES_COMPRESSED_FROM + X) / (BYTES_COMPRESSED_TO + X) where X = BYTES_COMPRESSION_REJECTED + NUMBER_BLOCK_COMPRESSION_REJECTED
* Overall SST compression ratio (compression disabled vs. actual): (Y - BYTES_COMPRESSED_TO + BYTES_COMPRESSED_FROM) / Y where Y = COMPACT_WRITE_BYTES + FLUSH_WRITE_BYTES

Keeping _REJECTED separate from _BYPASSED helps us to understand "wasted" CPU time in compression.

 ## BlockBasedTableBuilder
Various small refactorings, optimizations, and name clean-ups.

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

Test Plan:
unit tests added

* `options_settable_test.cc`: use non-deprecated idiom for configuring CompressionOptions from string. The old idiom is tested elsewhere and does not need to be updated to support the new field.

Reviewed By: ajkr

Differential Revision: D45128202

Pulled By: pdillinger

fbshipit-source-id: 5a652bf5c022b7ec340cf79018cccf0686962803
2023-04-21 21:57:40 -07:00

137 lines
4.4 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/statistics.h"
#include "rocksdb/system_clock.h"
namespace ROCKSDB_NAMESPACE {
// Auto-scoped.
// When statistics is not nullptr, records the measured time into any enabled
// histograms supplied to the constructor. A histogram argument may be omitted
// by setting it to Histograms::HISTOGRAM_ENUM_MAX. 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(SystemClock* clock, Statistics* statistics,
const uint32_t hist_type_1,
const uint32_t hist_type_2 = Histograms::HISTOGRAM_ENUM_MAX,
uint64_t* elapsed = nullptr, bool overwrite = true,
bool delay_enabled = false)
: clock_(clock),
statistics_(statistics),
hist_type_1_(statistics && statistics->HistEnabledForType(hist_type_1)
? hist_type_1
: Histograms::HISTOGRAM_ENUM_MAX),
hist_type_2_(statistics && statistics->HistEnabledForType(hist_type_2)
? hist_type_2
: Histograms::HISTOGRAM_ENUM_MAX),
elapsed_(elapsed),
overwrite_(overwrite),
stats_enabled_(statistics &&
statistics->get_stats_level() >=
StatsLevel::kExceptTimers &&
(hist_type_1_ != Histograms::HISTOGRAM_ENUM_MAX ||
hist_type_2_ != Histograms::HISTOGRAM_ENUM_MAX)),
delay_enabled_(delay_enabled),
total_delay_(0),
delay_start_time_(0),
start_time_((stats_enabled_ || elapsed != nullptr) ? clock->NowMicros()
: 0) {}
~StopWatch() {
if (elapsed_) {
if (overwrite_) {
*elapsed_ = clock_->NowMicros() - start_time_;
} else {
*elapsed_ += clock_->NowMicros() - start_time_;
}
}
if (elapsed_ && delay_enabled_) {
*elapsed_ -= total_delay_;
}
if (stats_enabled_) {
const auto time = (elapsed_ != nullptr)
? *elapsed_
: (clock_->NowMicros() - start_time_);
if (hist_type_1_ != Histograms::HISTOGRAM_ENUM_MAX) {
statistics_->reportTimeToHistogram(hist_type_1_, time);
}
if (hist_type_2_ != Histograms::HISTOGRAM_ENUM_MAX) {
statistics_->reportTimeToHistogram(hist_type_2_, 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_ = clock_->NowMicros();
}
}
void DelayStop() {
if (elapsed_ && delay_enabled_ && delay_start_time_ != 0) {
total_delay_ += clock_->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:
SystemClock* clock_;
Statistics* statistics_;
const uint32_t hist_type_1_;
const uint32_t hist_type_2_;
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(SystemClock* clock, bool auto_start = false)
: clock_(clock), start_(0) {
if (auto_start) {
Start();
}
}
void Start() { start_ = clock_->NowNanos(); }
uint64_t ElapsedNanos(bool reset = false) {
auto now = clock_->NowNanos();
auto elapsed = now - start_;
if (reset) {
start_ = now;
}
return elapsed;
}
uint64_t ElapsedNanosSafe(bool reset = false) {
return (clock_ != nullptr) ? ElapsedNanos(reset) : 0U;
}
bool IsStarted() { return start_ != 0; }
private:
SystemClock* clock_;
uint64_t start_;
};
} // namespace ROCKSDB_NAMESPACE