mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 10:14:09 +02:00
sync cpu time
This commit is contained in:
parent
61261ca227
commit
ad5ae35ecf
4 changed files with 31 additions and 7 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1059,6 +1059,7 @@ dependencies = [
|
|||
"heim",
|
||||
"hostname",
|
||||
"iai",
|
||||
"libc",
|
||||
"once_cell",
|
||||
"parse-display",
|
||||
"regex",
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ parse-display = "0.4"
|
|||
regex = { version = "1", default-features = false }
|
||||
once_cell = "1"
|
||||
hostname = "0.3"
|
||||
libc = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
iai = "0.1"
|
||||
|
|
|
|||
|
|
@ -11,12 +11,12 @@ use std::collections::HashSet;
|
|||
use std::fmt::Write;
|
||||
|
||||
pub async fn get_metrics() -> Result<String> {
|
||||
let (cpu, network, disks, disk_usage) = try_join! {
|
||||
cpu_time(),
|
||||
let (network, disks, disk_usage) = try_join! {
|
||||
network_stats(),
|
||||
disk_stats(),
|
||||
disk_usage(),
|
||||
}?;
|
||||
let cpu = cpu_time()?;
|
||||
let hostname = hostname()?;
|
||||
let memory = memory()?;
|
||||
let temperatures = temperatures()?;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
use color_eyre::{Report, Result};
|
||||
use futures_util::future;
|
||||
use futures_util::stream::{Stream, StreamExt};
|
||||
use heim::cpu::time;
|
||||
use heim::disk::{FileSystem, Partition};
|
||||
use heim::units::{information, time};
|
||||
use heim::units::information;
|
||||
use once_cell::sync::Lazy;
|
||||
use parse_display::Display;
|
||||
use regex::Regex;
|
||||
|
|
@ -110,9 +109,22 @@ pub fn memory() -> Result<Memory> {
|
|||
Ok(mem)
|
||||
}
|
||||
|
||||
pub async fn cpu_time() -> Result<f64> {
|
||||
let time = time().await?;
|
||||
Ok(time.user().get::<time::second>() + time.system().get::<time::second>())
|
||||
pub fn cpu_time() -> Result<u64> {
|
||||
let stat = BufReader::new(File::open("/proc/stat")?);
|
||||
let line = stat
|
||||
.lines()
|
||||
.next()
|
||||
.ok_or(Report::msg("Invalid /proc/stat"))??;
|
||||
let mut parts = line.split_ascii_whitespace();
|
||||
if let (_cpu, Some(user), _nice, Some(system)) =
|
||||
(parts.next(), parts.next(), parts.next(), parts.next())
|
||||
{
|
||||
let user: u64 = user.parse()?;
|
||||
let system: u64 = system.parse()?;
|
||||
Ok((user + system) * clock_ticks()?)
|
||||
} else {
|
||||
Err(Report::msg("Invalid /proc/stat"))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn network_stats() -> Result<impl Stream<Item = IOStats>> {
|
||||
|
|
@ -173,3 +185,13 @@ pub async fn disk_usage() -> Result<impl Stream<Item = DiskUsage>> {
|
|||
free: usage.free().get::<information::byte>(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn clock_ticks() -> Result<u64> {
|
||||
let result = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
|
||||
|
||||
if result > 0 {
|
||||
Ok(result as u64)
|
||||
} else {
|
||||
Err(Report::msg("Failed to get clock ticks"))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue