Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export memory usage builder and MemoryUsage structs to users #854

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

use libc::{c_int, c_uchar, c_void};

use crate::{db::DBInner, ffi, ffi_util::from_cstr, Cache, Error, DB};
use crate::{db::DBInner, ffi, ffi_util::from_cstr, Cache, Error};
use crate::{DBCommon, ThreadMode, DB};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(i32)]
Expand Down Expand Up @@ -179,7 +180,7 @@ pub struct MemoryUsageStats {
}

/// Wrap over memory_usage_t. Hold current memory usage of the specified DB instances and caches
struct MemoryUsage {
pub struct MemoryUsage {
inner: *mut ffi::rocksdb_memory_usage_t,
}

Expand All @@ -193,28 +194,28 @@ impl Drop for MemoryUsage {

impl MemoryUsage {
/// Approximate memory usage of all the mem-tables
fn approximate_mem_table_total(&self) -> u64 {
pub fn approximate_mem_table_total(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_mem_table_total(self.inner) }
}

/// Approximate memory usage of un-flushed mem-tables
fn approximate_mem_table_unflushed(&self) -> u64 {
pub fn approximate_mem_table_unflushed(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_mem_table_unflushed(self.inner) }
}

/// Approximate memory usage of all the table readers
fn approximate_mem_table_readers_total(&self) -> u64 {
pub fn approximate_mem_table_readers_total(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_mem_table_readers_total(self.inner) }
}

/// Approximate memory usage by cache
fn approximate_cache_total(&self) -> u64 {
pub fn approximate_cache_total(&self) -> u64 {
unsafe { ffi::rocksdb_approximate_memory_usage_get_cache_total(self.inner) }
}
}

/// Builder for MemoryUsage
struct MemoryUsageBuilder {
pub struct MemoryUsageBuilder {
inner: *mut ffi::rocksdb_memory_consumers_t,
}

Expand All @@ -228,7 +229,7 @@ impl Drop for MemoryUsageBuilder {

impl MemoryUsageBuilder {
/// Create new instance
fn new() -> Result<Self, Error> {
pub fn new() -> Result<Self, Error> {
let mc = unsafe { ffi::rocksdb_memory_consumers_create() };
if mc.is_null() {
Err(Error::new(
Expand All @@ -240,21 +241,21 @@ impl MemoryUsageBuilder {
}

/// Add a DB instance to collect memory usage from it and add up in total stats
fn add_db(&mut self, db: &DB) {
pub fn add_db<T: ThreadMode, D: DBInner>(&mut self, db: &DBCommon<T, D>) {
unsafe {
ffi::rocksdb_memory_consumers_add_db(self.inner, db.inner.inner());
}
}

/// Add a cache to collect memory usage from it and add up in total stats
fn add_cache(&mut self, cache: &Cache) {
pub fn add_cache(&mut self, cache: &Cache) {
unsafe {
ffi::rocksdb_memory_consumers_add_cache(self.inner, cache.0.inner.as_ptr());
}
}

/// Build up MemoryUsage
fn build(&self) -> Result<MemoryUsage, Error> {
pub fn build(&self) -> Result<MemoryUsage, Error> {
unsafe {
let mu = ffi_try!(ffi::rocksdb_approximate_memory_usage_create(self.inner));
Ok(MemoryUsage { inner: mu })
Expand Down