perf(db): cache ProcessUID::own in memory (#11302)

This commit is contained in:
DaniPopes
2024-09-27 23:49:46 +02:00
committed by GitHub
parent 1009289c3d
commit e48f2a29cd

View File

@@ -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};
@@ -91,7 +91,7 @@ impl StorageLockInner {
}
}
#[derive(Debug)]
#[derive(Clone, Debug)]
struct ProcessUID {
/// OS process identifier
pid: usize,
@@ -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.