Skip to content

Commit

Permalink
Expose rocksdb cumulative statistics and histograms
Browse files Browse the repository at this point in the history
This exposes rocksdb statistics (tickers and histograms) to allow rust-rocksdb users to introspect individual metrics.
Additionally, this PR also adds `set_statistics_level(StatsLevel)` to `Options` for fine-grain control over which
stats are collected.

Tickers and histograms are available in `rocksdb::statistics::{Ticker, Histogram}` with the necessary helpers to
convert the ticker type to the corresponding rocksdb name (and vice-versa via the `FromStr` trait).

Test Plan:
- New test added in test_db.rs
- Sanity check unit tests in statistics.rs
  • Loading branch information
AhmedSoliman committed Jan 15, 2024
1 parent 8fccdf5 commit 1678156
Show file tree
Hide file tree
Showing 4 changed files with 862 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/db_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::Arc;

use libc::{self, c_char, c_double, c_int, c_uchar, c_uint, c_void, size_t};

use crate::statistics::{Histogram, HistogramData, StatsLevel};
use crate::{
compaction_filter::{self, CompactionFilterCallback, CompactionFilterFn},
compaction_filter_factory::{self, CompactionFilterFactory},
Expand All @@ -32,6 +33,7 @@ use crate::{
self, full_merge_callback, partial_merge_callback, MergeFn, MergeOperatorCallback,
},
slice_transform::SliceTransform,
statistics::Ticker,
ColumnFamilyDescriptor, Error, SnapshotWithThreadMode,
};

Expand Down Expand Up @@ -2664,6 +2666,28 @@ impl Options {
}
}

/// StatsLevel can be used to reduce statistics overhead by skipping certain
/// types of stats in the stats collection process.
pub fn set_statistics_level(&self, level: StatsLevel) {
unsafe { ffi::rocksdb_options_set_statistics_level(self.inner, level as c_int) }
}

pub fn get_ticker_count(&self, ticker: Ticker) -> u64 {
unsafe { ffi::rocksdb_options_statistics_get_ticker_count(self.inner, ticker as u32) }
}

pub fn get_histogram_data(&self, histogram: Histogram) -> HistogramData {
unsafe {
let data = HistogramData::default();
ffi::rocksdb_options_statistics_get_histogram_data(
self.inner,
histogram as u32,
data.inner,
);
data
}
}

/// If not zero, dump `rocksdb.stats` to LOG every `stats_dump_period_sec`.
///
/// Default: `600` (10 mins)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub mod properties;
mod slice_transform;
mod snapshot;
mod sst_file_writer;
pub mod statistics;
mod transactions;
mod write_batch;

Expand Down

0 comments on commit 1678156

Please sign in to comment.