mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 10:14:09 +02:00
add nvidia power and temperature
This commit is contained in:
parent
25fde3eeb6
commit
8488ff4ec5
6 changed files with 166 additions and 31 deletions
|
|
@ -1,4 +1,5 @@
|
|||
pub mod docker;
|
||||
pub mod nvidia;
|
||||
pub mod power;
|
||||
pub mod sensors;
|
||||
pub mod zfs;
|
||||
|
|
|
|||
16
src/nvidia.rs
Normal file
16
src/nvidia.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use nvml_wrapper::enum_wrappers::device::TemperatureSensor;
|
||||
use nvml_wrapper::Nvml;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
static NVIDIA: Lazy<Option<Nvml>> = Lazy::new(|| Nvml::init().ok());
|
||||
|
||||
pub fn temperature() -> Option<f32> {
|
||||
let device = NVIDIA.as_ref()?.device_by_index(0).ok()?;
|
||||
let temp = device.temperature(TemperatureSensor::Gpu).ok()?;
|
||||
Some(temp as f32)
|
||||
}
|
||||
|
||||
pub fn power() -> Option<u64> {
|
||||
let device = NVIDIA.as_ref()?.device_by_index(0).ok()?;
|
||||
device.total_energy_consumption().ok()
|
||||
}
|
||||
36
src/power.rs
36
src/power.rs
|
|
@ -2,34 +2,45 @@ use color_eyre::{Report, Result};
|
|||
use std::fmt::Write;
|
||||
use std::fs::{read_dir, read_to_string};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tracing::warn;
|
||||
|
||||
static CAN_READ: AtomicBool = AtomicBool::new(true);
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PowerUsage {
|
||||
total_uj: u64,
|
||||
packages_uj: Vec<u64>,
|
||||
cpu_uj: u64,
|
||||
cpu_packages_uj: Vec<u64>,
|
||||
gpu_uj: u64,
|
||||
}
|
||||
|
||||
impl PowerUsage {
|
||||
pub fn write<W: Write>(&self, mut w: W, hostname: &str) {
|
||||
writeln!(
|
||||
&mut w,
|
||||
"total_power{{host=\"{}\"}} {:.3}",
|
||||
r#"total_power{{host="{}", device="cpu"}} {:.3}"#,
|
||||
hostname,
|
||||
self.total_uj as f64 / 1_000_000.0
|
||||
self.cpu_uj as f64 / 1_000_000.0
|
||||
)
|
||||
.ok();
|
||||
for (i, package) in self.packages_uj.iter().enumerate() {
|
||||
for (i, package) in self.cpu_packages_uj.iter().enumerate() {
|
||||
writeln!(
|
||||
&mut w,
|
||||
"package_power{{host=\"{}\", package=\"{}\"}} {:.3}",
|
||||
r#"package_power{{host="{}", package="{}", device="cpu"}} {:.3}"#,
|
||||
hostname,
|
||||
i,
|
||||
*package as f64 / 1_000_000.0
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
if self.gpu_uj > 0 {
|
||||
writeln!(
|
||||
&mut w,
|
||||
r#"total_power{{host="{}", device="gpu"}} {:.3}"#,
|
||||
hostname,
|
||||
self.gpu_uj as f64 / 1_000_000.0
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -59,14 +70,23 @@ pub fn power_usage() -> Result<Option<PowerUsage>> {
|
|||
let package_usage = match read_to_string(&package_path) {
|
||||
Err(e) if e.raw_os_error() == Some(13) => {
|
||||
CAN_READ.store(false, Ordering::Relaxed);
|
||||
warn!(
|
||||
package_path = display(package_path.display()),
|
||||
"can\'t read power usage"
|
||||
);
|
||||
return Ok(None);
|
||||
}
|
||||
result => result,
|
||||
}?;
|
||||
let package_usage = package_usage.trim().parse::<u64>()?;
|
||||
usage.total_uj += package_usage;
|
||||
usage.packages_uj.push(package_usage);
|
||||
usage.cpu_uj += package_usage;
|
||||
usage.cpu_packages_uj.push(package_usage);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(nvidia_power) = crate::nvidia::power() {
|
||||
usage.gpu_uj = nvidia_power;
|
||||
}
|
||||
|
||||
Ok(Some(usage))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,6 +108,10 @@ pub fn temperatures() -> Result<Temperatures> {
|
|||
temps.cpu = core_total / cores_found
|
||||
}
|
||||
|
||||
if let Some(nvidia_temperature) = crate::nvidia::temperature() {
|
||||
temps.gpu = nvidia_temperature;
|
||||
}
|
||||
|
||||
Ok(temps)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue