Replace the procfs by libproc

This commit is contained in:
Clément Renault 2024-02-06 18:12:04 +01:00 committed by Louis Dureuil
parent d78ada07b5
commit 02dcaf07db
No known key found for this signature in database
6 changed files with 100 additions and 90 deletions

View file

@ -100,46 +100,29 @@ pub struct SpanClose {
pub time: std::time::Duration,
}
/// A struct with a lot of memory allocation stats akin
/// to the `procfs::Process::StatsM` one plus the OOM score.
///
/// Note that all the values are in bytes not in pages.
/// A struct with a memory allocation stat.
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
pub struct MemoryStats {
/// Resident set size, measured in bytes.
/// (same as VmRSS in /proc/<pid>/status).
pub resident: u64,
/// Number of resident shared bytes (i.e., backed by a file).
/// (same as RssFile+RssShmem in /proc/<pid>/status).
pub shared: u64,
/// The current score that the kernel gives to this process
/// for the purpose of selecting a process for the OOM-killer
///
/// A higher score means that the process is more likely to be selected
/// by the OOM-killer. The basis for this score is the amount of memory used
/// by the process, plus other factors.
///
/// (Since linux 2.6.11)
pub oom_score: u32,
}
impl MemoryStats {
#[cfg(target_os = "linux")]
pub fn fetch() -> procfs::ProcResult<Self> {
let process = procfs::process::Process::myself().unwrap();
let procfs::process::StatM { resident, shared, .. } = process.statm()?;
let oom_score = process.oom_score()?;
let page_size = procfs::page_size();
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn fetch() -> Option<Self> {
use libproc::libproc::pid_rusage::{pidrusage, RUsageInfoV0};
Ok(MemoryStats { resident: resident * page_size, shared: shared * page_size, oom_score })
match pidrusage(std::process::id() as i32) {
Ok(RUsageInfoV0 { ri_resident_size, .. }) => {
Some(MemoryStats { resident: ri_resident_size })
}
Err(_) => None, /* ignoring error to avoid spamming */
}
}
pub fn checked_sub(self, other: Self) -> Option<Self> {
Some(Self {
resident: self.resident.checked_sub(other.resident)?,
shared: self.shared.checked_sub(other.shared)?,
oom_score: self.oom_score.checked_sub(other.oom_score)?,
})
Some(Self { resident: self.resident.checked_sub(other.resident)? })
}
}

View file

@ -96,12 +96,12 @@ impl TraceLayer {
self.start_time.elapsed()
}
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn memory_stats(&self) -> Option<MemoryStats> {
Some(MemoryStats::fetch().unwrap())
}
#[cfg(not(target_os = "linux"))]
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn memory_stats(&self) -> Option<MemoryStats> {
None
}

View file

@ -322,18 +322,6 @@ impl<'a> ProfilerMarker for SpanMarker<'a> {
format: MarkerFieldFormat::Bytes,
searchable: false,
}),
MarkerSchemaField::Dynamic(MarkerDynamicField {
key: "shared",
label: "Number of resident shared pages (i.e., backed by a file) while this function was executing",
format: MarkerFieldFormat::Bytes,
searchable: false,
}),
MarkerSchemaField::Dynamic(MarkerDynamicField {
key: "oom_score",
label: "The current score that the kernel gives to this process for the purpose of selecting a process for the OOM-killer while this function was executing",
format: MarkerFieldFormat::Integer,
searchable: false,
}),
];
MarkerSchema {
@ -366,10 +354,8 @@ impl<'a> ProfilerMarker for SpanMarker<'a> {
"thread_id": thread_id,
});
if let Some(MemoryStats { resident, shared, oom_score }) = self.memory_delta {
if let Some(MemoryStats { resident }) = self.memory_delta {
value["resident"] = json!(resident);
value["shared"] = json!(shared);
value["oom_score"] = json!(oom_score);
}
value
@ -423,18 +409,6 @@ impl<'a> ProfilerMarker for EventMarker<'a> {
format: MarkerFieldFormat::Bytes,
searchable: false,
}),
MarkerSchemaField::Dynamic(MarkerDynamicField {
key: "shared",
label: "Number of resident shared pages (i.e., backed by a file) while this function was executing",
format: MarkerFieldFormat::Bytes,
searchable: false,
}),
MarkerSchemaField::Dynamic(MarkerDynamicField {
key: "oom_score",
label: "The current score that the kernel gives to this process for the purpose of selecting a process for the OOM-killer while this function was executing",
format: MarkerFieldFormat::Integer,
searchable: false,
}),
];
MarkerSchema {
@ -467,10 +441,8 @@ impl<'a> ProfilerMarker for EventMarker<'a> {
"thread_id": thread_id,
});
if let Some(MemoryStats { resident, shared, oom_score }) = self.memory_delta {
if let Some(MemoryStats { resident }) = self.memory_delta {
value["resident"] = json!(resident);
value["shared"] = json!(shared);
value["oom_score"] = json!(oom_score);
}
value

View file

@ -188,9 +188,8 @@ fn print_duration(duration: std::time::Duration) -> String {
}
/// Format only the allocated bytes, deallocated bytes and reallocated bytes in GiB, MiB, KiB, Bytes.
fn print_memory(MemoryStats { resident, shared, oom_score }: MemoryStats) -> String {
fn print_memory(MemoryStats { resident }: MemoryStats) -> String {
use byte_unit::Byte;
let rss_bytes = Byte::from_bytes(resident).get_appropriate_unit(true);
let shared_bytes = Byte::from_bytes(shared).get_appropriate_unit(true);
format!("RSS {rss_bytes:.2}, Shared {shared_bytes:.2}, OOM score {oom_score}")
format!("RSS {rss_bytes:.2}")
}