| 1 |
|
| 2 |
|
| 3 |
use std::{io, process::Command}; |
| 4 |
|
| 5 |
use serde::{Deserialize, Deserializer}; |
| 6 |
|
| 7 |
use crate::{ |
| 8 |
collection::{Pid, deserialize_xo, processes::UnixProcessExt}, |
| 9 |
utils::int_hash::IntHashMap, |
| 10 |
}; |
| 11 |
|
| 12 |
#[derive(Deserialize, Debug, Default)] |
| 13 |
#[serde(rename_all = "kebab-case")] |
| 14 |
struct ProcessInformation { |
| 15 |
process: Vec<ProcessRow>, |
| 16 |
} |
| 17 |
|
| 18 |
#[derive(Deserialize, Debug)] |
| 19 |
#[serde(rename_all = "kebab-case")] |
| 20 |
struct ProcessRow { |
| 21 |
#[serde(deserialize_with = "pid")] |
| 22 |
pid: i32, |
| 23 |
#[serde(deserialize_with = "percent_cpu")] |
| 24 |
percent_cpu: f32, |
| 25 |
} |
| 26 |
|
| 27 |
pub(crate) struct FreeBSDProcessExt; |
| 28 |
|
| 29 |
impl UnixProcessExt for FreeBSDProcessExt { |
| 30 |
#[inline] |
| 31 |
fn has_backup_proc_cpu_fn() -> bool { |
| 32 |
true |
| 33 |
} |
| 34 |
|
| 35 |
fn backup_proc_cpu(pids: &[Pid]) -> io::Result<IntHashMap<Pid, f32>> { |
| 36 |
if pids.is_empty() { |
| 37 |
return Ok(IntHashMap::default()); |
| 38 |
} |
| 39 |
|
| 40 |
let output = Command::new("ps") |
| 41 |
.args(["--libxo", "json", "-o", "pid,pcpu", "-p"]) |
| 42 |
.args(pids.iter().map(i32::to_string)) |
| 43 |
.output()?; |
| 44 |
|
| 45 |
deserialize_xo("process-information", &output.stdout).map( |
| 46 |
|process_info: ProcessInformation| { |
| 47 |
process_info |
| 48 |
.process |
| 49 |
.into_iter() |
| 50 |
.map(|row| (row.pid, row.percent_cpu)) |
| 51 |
.collect() |
| 52 |
}, |
| 53 |
) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
fn pid<'de, D>(deserializer: D) -> Result<i32, D::Error> |
| 58 |
where |
| 59 |
D: Deserializer<'de>, |
| 60 |
{ |
| 61 |
let s = String::deserialize(deserializer)?; |
| 62 |
s.parse().map_err(serde::de::Error::custom) |
| 63 |
} |
| 64 |
|
| 65 |
fn percent_cpu<'de, D>(deserializer: D) -> Result<f32, D::Error> |
| 66 |
where |
| 67 |
D: Deserializer<'de>, |
| 68 |
{ |
| 69 |
let s = String::deserialize(deserializer)?; |
| 70 |
s.parse().map_err(serde::de::Error::custom) |
| 71 |
} |