Skip to content

Commit

Permalink
perf(db): cache ProcessUID::own in memory (#11302)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Sep 27, 2024
1 parent 1009289 commit f31f00b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions crates/storage/db/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_tracing::tracing::error;
use std::{
path::{Path, PathBuf},
process,
sync::Arc,
sync::{Arc, OnceLock},
};
use sysinfo::{ProcessRefreshKind, RefreshKind, System};

Expand Down Expand Up @@ -91,7 +91,7 @@ impl StorageLockInner {
}
}

#[derive(Debug)]
#[derive(Clone, Debug)]
struct ProcessUID {
/// OS process identifier
pid: usize,
Expand All @@ -102,14 +102,16 @@ struct ProcessUID {
impl ProcessUID {
/// Creates [`Self`] for the provided PID.
fn new(pid: usize) -> Option<Self> {
System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::new()))
.process(pid.into())
.map(|process| Self { pid, start_time: process.start_time() })
let mut system = System::new();
let pid2 = sysinfo::Pid::from(pid);
system.refresh_process_specifics(pid2, ProcessRefreshKind::new());
system.process(pid2).map(|process| Self { pid, start_time: process.start_time() })
}

/// Creates [`Self`] from own process.
fn own() -> Self {
Self::new(process::id() as usize).expect("own process")
static CACHE: OnceLock<ProcessUID> = OnceLock::new();
CACHE.get_or_init(|| Self::new(process::id() as usize).expect("own process")).clone()
}

/// Parses [`Self`] from a file.
Expand Down

0 comments on commit f31f00b

Please sign in to comment.