mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 10:14:09 +02:00
add process memory for processes using >1% memory
This commit is contained in:
parent
c269a90332
commit
41f594cc41
6 changed files with 160 additions and 1 deletions
77
Cargo.lock
generated
77
Cargo.lock
generated
|
|
@ -331,6 +331,15 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.5"
|
||||
|
|
@ -475,6 +484,16 @@ dependencies = [
|
|||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno-dragonfly"
|
||||
version = "0.1.2"
|
||||
|
|
@ -495,6 +514,16 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
|
|
@ -1036,6 +1065,12 @@ dependencies = [
|
|||
"winapi 0.3.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.21"
|
||||
|
|
@ -1272,6 +1307,7 @@ dependencies = [
|
|||
"nvml-wrapper",
|
||||
"once_cell",
|
||||
"os-thread-local",
|
||||
"procfs",
|
||||
"regex",
|
||||
"serde",
|
||||
"sysconf",
|
||||
|
|
@ -1345,6 +1381,32 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "procfs"
|
||||
version = "0.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "731e0d9356b0c25f16f33b5be79b1c57b562f141ebfcdb0ad8ac2c13a24293b4"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"chrono",
|
||||
"flate2",
|
||||
"hex",
|
||||
"lazy_static",
|
||||
"procfs-core",
|
||||
"rustix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "procfs-core"
|
||||
version = "0.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d3554923a69f4ce04c4a754260c338f505ce22642d3830e049a399fc2059a29"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"chrono",
|
||||
"hex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.35"
|
||||
|
|
@ -1439,6 +1501,19 @@ version = "0.1.23"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno 0.3.8",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.17"
|
||||
|
|
@ -1608,7 +1683,7 @@ version = "0.3.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59e93f5d45535f49b6a05ef7ac2f0f795d28de494cf53a512751602c9849bea3"
|
||||
dependencies = [
|
||||
"errno",
|
||||
"errno 0.2.8",
|
||||
"kernel32-sys",
|
||||
"libc",
|
||||
"winapi 0.2.8",
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ if-addrs = "0.12.0"
|
|||
sysconf = "0.3.4"
|
||||
thiserror = "1.0.58"
|
||||
clap = { version = "4.4.18", features = ["derive"] }
|
||||
procfs = "0.16.0"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
|
|
|
|||
25
src/data.rs
25
src/data.rs
|
|
@ -229,3 +229,28 @@ impl SensorData for GpuPowerUsage {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProcData {
|
||||
pub pid: i32,
|
||||
pub name: String,
|
||||
pub rss_memory: u64,
|
||||
}
|
||||
|
||||
impl SensorData for ProcData {
|
||||
fn write<W: Write>(&self, mut w: W, hostname: &str) {
|
||||
writeln!(
|
||||
&mut w,
|
||||
r#"process_memory_rss{{host="{}", process="{}", pid="{}"}} {}"#,
|
||||
hostname, self.name, self.pid, self.rss_memory
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
impl SensorData for Vec<ProcData> {
|
||||
fn write<W: Write>(&self, mut w: W, hostname: &str) {
|
||||
for data in self {
|
||||
data.write(&mut w, hostname)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use procfs::ProcError;
|
||||
use std::ffi::NulError;
|
||||
use std::fmt::Write;
|
||||
use std::num::{ParseFloatError, ParseIntError};
|
||||
|
|
@ -35,6 +36,8 @@ pub enum Error {
|
|||
InvalidStringData(#[from] Utf8Error),
|
||||
#[error(transparent)]
|
||||
InvalidCStringData(#[from] NulError),
|
||||
#[error(transparent)]
|
||||
Proc(#[from] ProcError),
|
||||
#[error("Failed to query vfs stats")]
|
||||
StatVfs,
|
||||
#[cfg(target_os = "windows")]
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ pub mod disk;
|
|||
pub mod gpu;
|
||||
pub mod hwmon;
|
||||
pub mod power;
|
||||
mod proc;
|
||||
pub mod sensors;
|
||||
|
||||
use self::disk::zfs::pools;
|
||||
|
|
@ -10,6 +11,7 @@ use self::sensors::*;
|
|||
use crate::linux::disk::zfs::arcstats;
|
||||
use crate::linux::gpu::{update_gpu_power, utilization};
|
||||
use crate::linux::power::{CpuPowerSource, GpuPowerSource};
|
||||
use crate::linux::proc::ProcSource;
|
||||
use crate::{hostname, Error, MultiSensorSource, Result, SensorData, SensorSource};
|
||||
use std::fmt::Write;
|
||||
use std::sync::Mutex;
|
||||
|
|
@ -31,6 +33,7 @@ pub struct Sensors {
|
|||
disk_usage: Mutex<DiskUsageSource>,
|
||||
cpu_power: Mutex<CpuPowerSource>,
|
||||
gpu_power: Mutex<GpuPowerSource>,
|
||||
proc: Mutex<ProcSource>,
|
||||
}
|
||||
|
||||
impl Sensors {
|
||||
|
|
@ -47,6 +50,7 @@ impl Sensors {
|
|||
disk_usage: Mutex::new(DiskUsageSource::new()?),
|
||||
cpu_power: Mutex::new(CpuPowerSource::new().unwrap_or_default()),
|
||||
gpu_power: Mutex::new(GpuPowerSource),
|
||||
proc: Mutex::new(ProcSource::new()?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -63,6 +67,7 @@ pub fn get_metrics(sensors: &Sensors) -> Result<String> {
|
|||
let cpu_power = sensors.cpu_power.lock().unwrap().read()?;
|
||||
let gpu_power = sensors.gpu_power.lock().unwrap().read()?;
|
||||
let mut net = sensors.net.lock().unwrap();
|
||||
let mut proc = sensors.proc.lock().unwrap();
|
||||
let networks = net.read()?;
|
||||
let pools = pools();
|
||||
let mut result = String::with_capacity(256);
|
||||
|
|
@ -118,5 +123,9 @@ pub fn get_metrics(sensors: &Sensors) -> Result<String> {
|
|||
usage.write(&mut result, &sensors.hostname);
|
||||
}
|
||||
|
||||
for process in proc.read()? {
|
||||
process?.write(&mut result, &sensors.hostname);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
|
|
|||
46
src/linux/proc.rs
Normal file
46
src/linux/proc.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use crate::data::ProcData;
|
||||
use crate::linux::sensors::MemorySource;
|
||||
use crate::{MultiSensorSource, Result, SensorSource};
|
||||
use procfs::page_size;
|
||||
use procfs::process::all_processes;
|
||||
use std::vec::IntoIter;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ProcSource {
|
||||
page_size: u64,
|
||||
page_cutoff: u64,
|
||||
}
|
||||
|
||||
impl ProcSource {
|
||||
pub fn new() -> Result<Self> {
|
||||
let total_memory = MemorySource::new()?.read()?.total;
|
||||
let page_size = page_size();
|
||||
|
||||
Ok(ProcSource {
|
||||
page_size,
|
||||
// output processes that use >1% of memory
|
||||
page_cutoff: (total_memory / 100) / page_size,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl MultiSensorSource for ProcSource {
|
||||
type Data = ProcData;
|
||||
type Iter<'a> = IntoIter<Result<ProcData>>;
|
||||
|
||||
fn read(&mut self) -> Result<Self::Iter<'_>> {
|
||||
Ok(all_processes()?
|
||||
.flatten()
|
||||
.flat_map(|proc| proc.stat())
|
||||
.filter(|stat| stat.rss > self.page_cutoff)
|
||||
.map(|stat| {
|
||||
Ok(ProcData {
|
||||
pid: stat.pid,
|
||||
name: stat.comm,
|
||||
rss_memory: stat.rss * self.page_size,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue