Skip to content

Commit

Permalink
some progress on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Sep 12, 2024
1 parent 48ad94b commit ba014ca
Show file tree
Hide file tree
Showing 29 changed files with 406 additions and 204 deletions.
3 changes: 0 additions & 3 deletions src/data_collection/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ pub struct CpuData {
}

pub type CpuHarvest = Vec<CpuData>;

pub type PastCpuWork = f64;
pub type PastCpuTotal = f64;
2 changes: 1 addition & 1 deletion src/data_collection/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ pub mod arc;
pub struct MemHarvest {
pub used_bytes: u64,
pub total_bytes: u64,
pub use_percent: Option<f64>, /* TODO: Might be find to just make this an f64, and any
pub use_percent: Option<f64>, /* TODO: Might be fine to just make this an f64, and any
* consumer checks NaN. */
}
43 changes: 34 additions & 9 deletions src/new_data_collection/collectors/common.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
//! Common code amongst all data collectors.

use crate::new_data_collection::{
error::CollectionResult,
sources::common::{
disk::DiskHarvest, processes::ProcessHarvest, temperature::TemperatureData,
use crate::{
data_collection::Data,
new_data_collection::{
error::CollectionResult,
sources::{
cpu::CpuHarvest, disk::DiskHarvest, memory::MemHarvest, processes::ProcessHarvest,
temperature::TemperatureData,
},
},
};

#[cfg(feature = "battery")]
use crate::new_data_collection::sources::battery::BatteryHarvest;

// /// Represents data collected at an instance.
// #[derive(Debug)]
// pub struct Data {
// pub collection_time: Instant,
// pub temperature_data: Option<Vec<TemperatureData>>,
// pub process_data: Option<Vec<ProcessHarvest>>,
// pub disk_data: Option<DiskHarvest>,
// }

/// The trait representing what a per-platform data collector should implement.
pub(crate) trait DataCollector {
/// Refresh inner data sources to prepare them for gathering data.
pub trait DataCollector {
/// Return data.
///
/// Note that depending on the implementation, this may
/// not actually need to do anything.
fn refresh_data(&mut self) -> CollectionResult<()>;
/// For now, this returns the old data type for cross-compatibility as we migrate.
fn get_data(&mut self) -> Data;

/// Return temperature data.
fn get_temperature_data(&mut self) -> CollectionResult<Vec<TemperatureData>>;
Expand All @@ -23,4 +38,14 @@ pub(crate) trait DataCollector {

/// Return disk data.
fn get_disk_data(&mut self) -> CollectionResult<DiskHarvest>;

/// Return CPU data.
fn get_cpu_data(&mut self) -> CollectionResult<CpuHarvest>;

/// Return memory data.
fn get_memory_data(&mut self) -> CollectionResult<MemHarvest>;

#[cfg(feature = "battery")]
/// Return battery data.
fn get_battery_data(&mut self) -> CollectionResult<Vec<BatteryHarvest>>;
}
36 changes: 0 additions & 36 deletions src/new_data_collection/collectors/freebsd.rs

This file was deleted.

87 changes: 73 additions & 14 deletions src/new_data_collection/collectors/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@

use std::time::Instant;

use starship_battery::{Battery, Manager};

use crate::{
app::filter::Filter,
data_collection::Data,
new_data_collection::{
error::CollectionResult,
sources::{
common::{
disk::DiskHarvest,
processes::ProcessHarvest,
temperature::{TemperatureData, TemperatureType},
},
linux::{
processes::{linux_process_data, ProcessCollector},
temperature::get_temperature_data,
cpu::CpuHarvest,
disk::DiskHarvest,
linux::{get_temperature_data, linux_process_data, ProcessCollector},
memory::MemHarvest,
processes::ProcessHarvest,
sysinfo::{
cpu::{get_cpu_data_list, get_load_avg},
memory::{get_cache_usage, get_ram_usage, get_swap_usage},
},
temperature::{TemperatureData, TemperatureType},
},
},
};

use super::common::DataCollector;

cfg_if::cfg_if! {
if #[cfg(feature = "battery")] {
use starship_battery::{Battery, Manager};
use crate::new_data_collection::sources::battery::BatteryHarvest;
}

}

/// The [`DataCollector`] for Linux.
pub struct LinuxDataCollector {
current_collection_time: Instant,
Expand All @@ -37,19 +45,30 @@ pub struct LinuxDataCollector {
system: sysinfo::System,
network: sysinfo::Networks,

show_average_cpu: bool,

#[cfg(feature = "battery")]
battery_manager: Option<Manager>,
#[cfg(feature = "battery")]
battery_list: Option<Vec<Battery>>,
batteries: Option<(Manager, Vec<Battery>)>,

#[cfg(feature = "gpu")]
nvml: nvml_wrapper::Nvml,

#[cfg(feature = "gpu")]
gpus_total_mem: Option<u64>,
}

impl DataCollector for LinuxDataCollector {
impl LinuxDataCollector {
fn refresh_data(&mut self) -> CollectionResult<()> {
Ok(())
}
}

impl DataCollector for LinuxDataCollector {
fn get_data(&mut self) -> Data {
let collection_time = Instant::now();

todo!()
}

fn get_temperature_data(&mut self) -> CollectionResult<Vec<TemperatureData>> {
Ok(get_temperature_data(&self.temp_type, &self.temp_filters))
Expand All @@ -73,4 +92,44 @@ impl DataCollector for LinuxDataCollector {
fn get_disk_data(&mut self) -> CollectionResult<DiskHarvest> {
todo!()
}

fn get_cpu_data(&mut self) -> CollectionResult<CpuHarvest> {
let usages = get_cpu_data_list(&self.system, self.show_average_cpu);
let load_average = get_load_avg();

CollectionResult::Ok(CpuHarvest {
usages,
load_average,
})
}

fn get_memory_data(&mut self) -> CollectionResult<MemHarvest> {
let memory = get_ram_usage(&self.system);
let swap = get_swap_usage(&self.system);
let cache = get_cache_usage(&self.system);

CollectionResult::Ok(MemHarvest {
memory,
swap,
cache,
#[cfg(feature = "zfs")]
arc: crate::new_data_collection::sources::linux::get_arc_usage(),
#[cfg(feature = "gpu")]
gpu: crate::new_data_collection::sources::nvidia::get_gpu_memory_usage(&self.nvml),
})
}

#[cfg(feature = "battery")]
fn get_battery_data(&mut self) -> CollectionResult<Vec<BatteryHarvest>> {
use crate::new_data_collection::{
error::CollectionError, sources::starship::refresh_batteries,
};

match &mut self.batteries {
Some((battery_manager, battery_list)) => {
CollectionResult::Ok(refresh_batteries(battery_manager, battery_list))
}
None => CollectionResult::Err(CollectionError::NoData),
}
}
}
33 changes: 0 additions & 33 deletions src/new_data_collection/collectors/macos.rs

This file was deleted.

33 changes: 0 additions & 33 deletions src/new_data_collection/collectors/windows.rs

This file was deleted.

6 changes: 6 additions & 0 deletions src/new_data_collection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub enum CollectionError {
/// A general error to propagate back up. A wrapper around [`anyhow::Error`].
General(anyhow::Error),

/// No data.
NoData,

/// Collection is unsupported.
Unsupported,
}
Expand All @@ -14,6 +17,9 @@ impl std::fmt::Display for CollectionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CollectionError::General(err) => err.fmt(f),
CollectionError::NoData => {
write!(f, "no data found")
}
CollectionError::Unsupported => {
write!(
f,
Expand Down
18 changes: 9 additions & 9 deletions src/new_data_collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ mod collectors {
if #[cfg(target_os = "linux")] {
pub mod linux;
pub use linux::LinuxDataCollector as DataCollectorImpl;
} else if #[cfg(target_os = "macos")] {
pub mod macos;
pub use macos::MacOsDataCollector as DataCollectorImpl;
} else if #[cfg(target_os = "windows")] {
pub mod windows;
pub use windows::WindowsDataCollector as DataCollectorImpl;
} else if #[cfg(target_os = "freebsd")] {
pub mod freebsd;
pub use freebsd::FreeBsdDataCollector as DataCollectorImpl;
// } else if #[cfg(target_os = "macos")] {
// pub mod macos;
// pub use macos::MacOsDataCollector as DataCollectorImpl;
// } else if #[cfg(target_os = "windows")] {
// pub mod windows;
// pub use windows::WindowsDataCollector as DataCollectorImpl;
// } else if #[cfg(target_os = "freebsd")] {
// pub mod freebsd;
// pub use freebsd::FreeBsdDataCollector as DataCollectorImpl;
} else {
pub mod fallback;
pub use fallback::FallbackDataCollector as DataCollectorImpl;
Expand Down
20 changes: 20 additions & 0 deletions src/new_data_collection/sources/common/battery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Common code for retrieving battery data.

#[derive(Debug, Clone)]
pub enum State {
Unknown,
Charging,
Discharging,
Empty,
Full,
}

#[derive(Debug, Clone)]
pub struct BatteryHarvest {
pub charge_percent: f64,
pub secs_until_full: Option<i64>,
pub secs_until_empty: Option<i64>,
pub power_consumption_rate_watts: f64,
pub health_percent: f64,
pub state: State,
}
21 changes: 21 additions & 0 deletions src/new_data_collection/sources/common/cpu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Common code for retrieving CPU data.

#[derive(Debug, Clone, Copy)]
pub(crate) enum CpuDataType {
Avg,
Cpu(usize),
}

/// Represents a single core/thread and its usage.
#[derive(Debug, Clone)]
pub(crate) struct CpuData {
pub entry_type: CpuDataType,
pub usage: f64,
}

/// Collected CPU data at an instance.
#[derive(Debug, Clone)]
pub(crate) struct CpuHarvest {
pub usages: Vec<CpuData>,
pub load_average: [f32; 3],
}
Loading

0 comments on commit ba014ca

Please sign in to comment.