mirror of
https://codeberg.org/icewind/palantir.git
synced 2026-06-03 18:24:08 +02:00
extract file source
This commit is contained in:
parent
8d3e3e33a5
commit
553c16f225
2 changed files with 39 additions and 13 deletions
|
|
@ -1,8 +1,7 @@
|
||||||
use crate::sensors::Memory;
|
use crate::sensors::Memory;
|
||||||
|
use crate::FileSource;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::fs::{read_to_string, File};
|
use std::fs::read_to_string;
|
||||||
use std::io::{Read, Seek};
|
|
||||||
use std::path::Path;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
@ -91,10 +90,6 @@ pub fn utilization() -> impl Iterator<Item = GpuUsage> {
|
||||||
static GPU_POWER_UJ: AtomicU64 = AtomicU64::new(0);
|
static GPU_POWER_UJ: AtomicU64 = AtomicU64::new(0);
|
||||||
static GPU_POWER_LAST_READ: Mutex<Option<Instant>> = Mutex::new(None);
|
static GPU_POWER_LAST_READ: Mutex<Option<Instant>> = Mutex::new(None);
|
||||||
|
|
||||||
fn read_gpu_power() -> Option<u64> {
|
|
||||||
read_num("/sys/class/drm/card0/device/hwmon/hwmon0/power1_average")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_gpu_power_elapsed() -> Option<Duration> {
|
fn get_gpu_power_elapsed() -> Option<Duration> {
|
||||||
let mut last_read = GPU_POWER_LAST_READ.lock().unwrap();
|
let mut last_read = GPU_POWER_LAST_READ.lock().unwrap();
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|
@ -104,14 +99,13 @@ fn get_gpu_power_elapsed() -> Option<Duration> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_gpu_power() {
|
pub fn update_gpu_power() {
|
||||||
let mut buff = String::with_capacity(16);
|
if let Ok(mut file) =
|
||||||
if let Ok(mut file) = File::open("/sys/class/drm/card0/device/hwmon/hwmon0/power1_average") {
|
FileSource::open("/sys/class/drm/card0/device/hwmon/hwmon0/power1_average")
|
||||||
|
{
|
||||||
loop {
|
loop {
|
||||||
if let Some(elapsed) = get_gpu_power_elapsed() {
|
if let Some(elapsed) = get_gpu_power_elapsed() {
|
||||||
buff.clear();
|
let current_power: u64 = match file.read() {
|
||||||
file.rewind().ok();
|
Ok(current_power) => current_power,
|
||||||
let current_power: u64 = match file.read_to_string(&mut buff) {
|
|
||||||
Ok(_) => buff.trim().parse().unwrap_or_default(),
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
32
src/lib.rs
32
src/lib.rs
|
|
@ -10,6 +10,10 @@ use crate::disk::*;
|
||||||
use crate::sensors::*;
|
use crate::sensors::*;
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{ErrorKind, Read, Seek};
|
||||||
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
pub fn get_metrics() -> Result<String> {
|
pub fn get_metrics() -> Result<String> {
|
||||||
let disk_usage = disk_usage()?;
|
let disk_usage = disk_usage()?;
|
||||||
|
|
@ -115,3 +119,31 @@ pub fn get_metrics() -> Result<String> {
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct FileSource {
|
||||||
|
buff: String,
|
||||||
|
file: File,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileSource {
|
||||||
|
pub fn open<P: AsRef<Path>>(path: P) -> std::io::Result<FileSource> {
|
||||||
|
Ok(FileSource {
|
||||||
|
buff: String::with_capacity(32),
|
||||||
|
file: File::open(path)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read<T>(&mut self) -> std::io::Result<T>
|
||||||
|
where
|
||||||
|
T: FromStr,
|
||||||
|
<T as FromStr>::Err: std::error::Error + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
self.buff.clear();
|
||||||
|
self.file.rewind()?;
|
||||||
|
self.file.read_to_string(&mut self.buff)?;
|
||||||
|
self.buff
|
||||||
|
.trim()
|
||||||
|
.parse()
|
||||||
|
.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue