Skip to content

Commit

Permalink
Instrument store functions to measure performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
aterentic-ethernal committed Apr 25, 2024
1 parent 2a2035e commit 9b0ffe5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/bin/avail-light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn json_subscriber(log_level: Level) -> impl Subscriber + Send + Sync {
FmtSubscriber::builder()
.json()
.with_env_filter(EnvFilter::new(format!("avail_light={log_level}")))
.with_span_events(format::FmtSpan::CLOSE)
.finish()
}

Expand Down
6 changes: 6 additions & 0 deletions src/network/p2p/kad_mem_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use libp2p::kad::{KBucketKey, ProviderRecord, Record, RecordKey};
use std::borrow::Cow;
use std::collections::{hash_map, HashMap};
use std::iter;
use tracing::{instrument, Level};

#[cfg(not(feature = "kademlia-rocksdb"))]
use tracing::trace;
Expand Down Expand Up @@ -80,6 +81,7 @@ impl MemoryStore {
}

/// Retains the records satisfying a predicate.
#[instrument(level = Level::TRACE, skip(self, f))]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&RecordKey, &mut Record) -> bool,
Expand All @@ -105,10 +107,12 @@ impl RecordStore for MemoryStore {

type ProvidedIter<'a> = ProviderIter<'a>;

#[instrument(level = Level::TRACE, skip(self))]
fn get(&self, k: &RecordKey) -> Option<Cow<'_, Record>> {
self.records.get(k).map(Cow::Borrowed)
}

#[instrument(level = Level::TRACE, skip(self))]
fn put(&mut self, r: Record) -> Result<()> {
if r.value.len() >= self.config.max_value_bytes {
return Err(Error::ValueTooLarge);
Expand All @@ -131,10 +135,12 @@ impl RecordStore for MemoryStore {
Ok(())
}

#[instrument(level = Level::TRACE, skip(self))]
fn remove(&mut self, k: &RecordKey) {
self.records.remove(k);
}

#[instrument(level = Level::TRACE, skip(self))]
fn records(&self) -> Self::RecordsIter<'_> {
self.records.values().map(Cow::Borrowed)
}
Expand Down
9 changes: 7 additions & 2 deletions src/network/p2p/kad_rocksdb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use std::collections::hash_set;
use std::iter;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::error;

use tracing::{error, instrument, Level};
#[cfg(feature = "kademlia-rocksdb")]
use {rocksdb::WriteBatch, tracing::info};

Expand Down Expand Up @@ -105,6 +104,7 @@ impl RocksDBStore {
}
}

#[instrument(level = Level::TRACE, skip(self, f))]
/// Retains records that satisfy a given predicate.
/// NOTE: This is a suboptimal implementation for store size optimization,
/// since its execution time scales linearly with the store size.
Expand Down Expand Up @@ -135,6 +135,7 @@ impl RocksDBStore {
}

impl RocksDBStore {
#[instrument(level = Level::TRACE, skip(self))]
pub fn get_cf(&self) -> Option<Arc<BoundColumnFamily>> {
let Some(cf) = self.records.cf_handle(KADEMLIA_STORE_CF) else {
error!("Couldn't get column family \"{KADEMLIA_STORE_CF}\" handle");
Expand Down Expand Up @@ -163,6 +164,7 @@ impl RecordStore for RocksDBStore {
fn(&'a ProviderRecord) -> Cow<'a, ProviderRecord>,
>;

#[instrument(level = Level::TRACE, skip(self))]
fn get(&self, key: &RecordKey) -> Option<Cow<'_, Record>> {
match self.records.get_cf(&self.get_cf()?, key) {
Ok(record) => record
Expand All @@ -176,6 +178,7 @@ impl RecordStore for RocksDBStore {
}
}

#[instrument(level = Level::TRACE, skip(self))]
fn put(&mut self, r: Record) -> Result<()> {
let cf = self.get_cf().ok_or(RocksDBStoreError)?;

Expand All @@ -193,6 +196,7 @@ impl RecordStore for RocksDBStore {
})
}

#[instrument(level = Level::TRACE, skip(self))]
fn remove(&mut self, k: &RecordKey) {
let Some(cf) = self.get_cf() else {
return;
Expand All @@ -203,6 +207,7 @@ impl RecordStore for RocksDBStore {
error!("Failed to delete record from database: {error}");
}

#[instrument(level = "trace", skip(self))]
fn records(&self) -> Self::RecordsIter<'_> {
let Some(cf) = self.get_cf() else {
return Box::new(iter::empty::<kad::Record>().map(Cow::Owned));
Expand Down

0 comments on commit 9b0ffe5

Please sign in to comment.