| 1 |
//! This is the main file to house data collection functions. |
| 2 |
//! |
| 3 |
//! TODO: Rename this to intake? Collection? |
| 4 |
|
| 5 |
#[cfg(feature = "nvidia")] |
| 6 |
pub mod nvidia; |
| 7 |
|
| 8 |
#[cfg(all(target_os = "linux", feature = "gpu"))] |
| 9 |
pub mod amd; |
| 10 |
|
| 11 |
#[cfg(target_os = "linux")] |
| 12 |
mod linux { |
| 13 |
pub mod cgroups; |
| 14 |
pub mod utils; |
| 15 |
} |
| 16 |
|
| 17 |
#[cfg(feature = "battery")] |
| 18 |
pub mod batteries; |
| 19 |
pub mod cpu; |
| 20 |
pub mod disks; |
| 21 |
pub mod error; |
| 22 |
pub mod memory; |
| 23 |
pub mod network; |
| 24 |
pub mod processes; |
| 25 |
pub mod temperature; |
| 26 |
|
| 27 |
use std::time::{Duration, Instant}; |
| 28 |
|
| 29 |
#[cfg(any(not(target_os = "windows"), feature = "gpu"))] |
| 30 |
use processes::Pid; |
| 31 |
#[cfg(feature = "battery")] |
| 32 |
use starship_battery::{Battery, Manager}; |
| 33 |
|
| 34 |
use super::DataFilters; |
| 35 |
use crate::app::layout_manager::UsedWidgets; |
| 36 |
#[cfg(target_os = "linux")] |
| 37 |
use crate::collection::linux::cgroups::{CgroupCpuCollector, CgroupMemCollector}; |
| 38 |
#[cfg(any(target_os = "linux", feature = "gpu"))] |
| 39 |
use crate::utils::int_hash::IntHashMap; |
| 40 |
|
| 41 |
// TODO: We can possibly reuse an internal buffer for this to reduce allocs. |
| 42 |
#[derive(Clone, Debug)] |
| 43 |
pub struct Data { |
| 44 |
pub collection_time: Instant, |
| 45 |
pub cpu: Option<cpu::CpuHarvest>, |
| 46 |
pub load_avg: Option<cpu::LoadAvgHarvest>, |
| 47 |
pub memory: Option<memory::MemData>, |
| 48 |
#[cfg(not(target_os = "windows"))] |
| 49 |
pub cache: Option<memory::MemData>, |
| 50 |
pub swap: Option<memory::MemData>, |
| 51 |
pub temperature_sensors: Option<Vec<temperature::TempSensorData>>, |
| 52 |
pub network: Option<network::NetworkHarvest>, |
| 53 |
pub list_of_processes: Option<Vec<processes::ProcessHarvest>>, |
| 54 |
pub disks: Option<Vec<disks::DiskHarvest>>, |
| 55 |
pub io: Option<disks::IoHarvest>, |
| 56 |
#[cfg(feature = "battery")] |
| 57 |
pub list_of_batteries: Option<Vec<batteries::BatteryData>>, |
| 58 |
#[cfg(feature = "zfs")] |
| 59 |
pub arc: Option<memory::MemData>, |
| 60 |
#[cfg(feature = "gpu")] |
| 61 |
pub gpu: Option<Vec<(String, memory::MemData)>>, |
| 62 |
} |
| 63 |
|
| 64 |
impl Default for Data { |
| 65 |
fn default() -> Self { |
| 66 |
Data { |
| 67 |
collection_time: Instant::now(), |
| 68 |
cpu: None, |
| 69 |
load_avg: None, |
| 70 |
memory: None, |
| 71 |
#[cfg(not(target_os = "windows"))] |
| 72 |
cache: None, |
| 73 |
swap: None, |
| 74 |
temperature_sensors: None, |
| 75 |
list_of_processes: None, |
| 76 |
disks: None, |
| 77 |
io: None, |
| 78 |
network: None, |
| 79 |
#[cfg(feature = "battery")] |
| 80 |
list_of_batteries: None, |
| 81 |
#[cfg(feature = "zfs")] |
| 82 |
arc: None, |
| 83 |
#[cfg(feature = "gpu")] |
| 84 |
gpu: None, |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl Data { |
| 90 |
pub fn cleanup(&mut self) { |
| 91 |
self.io = None; |
| 92 |
self.temperature_sensors = None; |
| 93 |
self.list_of_processes = None; |
| 94 |
self.disks = None; |
| 95 |
self.memory = None; |
| 96 |
self.swap = None; |
| 97 |
self.cpu = None; |
| 98 |
self.load_avg = None; |
| 99 |
|
| 100 |
if let Some(network) = &mut self.network { |
| 101 |
network.first_run_cleanup(); |
| 102 |
} |
| 103 |
#[cfg(feature = "zfs")] |
| 104 |
{ |
| 105 |
self.arc = None; |
| 106 |
} |
| 107 |
#[cfg(feature = "gpu")] |
| 108 |
{ |
| 109 |
self.gpu = None; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/// A wrapper around the sysinfo data source. We use sysinfo for the following |
| 115 |
/// data: |
| 116 |
/// - CPU usage |
| 117 |
/// - Memory usage |
| 118 |
/// - Network usage |
| 119 |
/// - Processes (non-Linux) |
| 120 |
/// - Disk (anything outside of Linux, macOS, and FreeBSD) |
| 121 |
/// - Temperatures (non-Linux) |
| 122 |
#[derive(Debug)] |
| 123 |
pub struct SysinfoSource { |
| 124 |
/// Handles CPU, memory, and processes. |
| 125 |
pub(crate) system: sysinfo::System, |
| 126 |
pub(crate) network: sysinfo::Networks, |
| 127 |
#[cfg(not(target_os = "linux"))] |
| 128 |
pub(crate) temps: sysinfo::Components, |
| 129 |
#[cfg(not(any(target_os = "linux", target_os = "macos")))] |
| 130 |
pub(crate) disks: sysinfo::Disks, |
| 131 |
#[cfg(target_os = "windows")] |
| 132 |
pub(crate) users: sysinfo::Users, |
| 133 |
} |
| 134 |
|
| 135 |
impl Default for SysinfoSource { |
| 136 |
fn default() -> Self { |
| 137 |
use sysinfo::*; |
| 138 |
|
| 139 |
Self { |
| 140 |
system: System::new(), |
| 141 |
network: Networks::new(), |
| 142 |
#[cfg(not(target_os = "linux"))] |
| 143 |
temps: Components::new(), |
| 144 |
#[cfg(not(any(target_os = "linux", target_os = "macos")))] |
| 145 |
disks: Disks::new(), |
| 146 |
#[cfg(target_os = "windows")] |
| 147 |
users: Users::new(), |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
#[derive(Debug)] |
| 153 |
pub struct DataCollector { |
| 154 |
pub data: Data, |
| 155 |
sys: SysinfoSource, |
| 156 |
last_collection_time: Instant, |
| 157 |
widgets_to_harvest: UsedWidgets, |
| 158 |
filters: DataFilters, |
| 159 |
include_unmounted_disks: bool, |
| 160 |
|
| 161 |
total_rx: u64, |
| 162 |
total_tx: u64, |
| 163 |
total_rx_packets: u64, |
| 164 |
total_tx_packets: u64, |
| 165 |
|
| 166 |
unnormalized_cpu: bool, |
| 167 |
use_current_cpu_total: bool, |
| 168 |
show_average_cpu: bool, |
| 169 |
get_process_threads: bool, |
| 170 |
|
| 171 |
last_list_collection_time: Instant, |
| 172 |
should_run_less_routine_tasks: bool, |
| 173 |
|
| 174 |
#[cfg(target_os = "linux")] |
| 175 |
prev_process_details: IntHashMap<Pid, processes::PrevProcDetails>, |
| 176 |
#[cfg(target_os = "linux")] |
| 177 |
prev_idle: f64, |
| 178 |
#[cfg(target_os = "linux")] |
| 179 |
prev_non_idle: f64, |
| 180 |
|
| 181 |
#[cfg(feature = "battery")] |
| 182 |
battery_manager: Option<Manager>, |
| 183 |
#[cfg(feature = "battery")] |
| 184 |
battery_list: Option<Vec<Battery>>, |
| 185 |
|
| 186 |
#[cfg(unix)] |
| 187 |
user_table: processes::UserTable, |
| 188 |
|
| 189 |
#[cfg(feature = "gpu")] |
| 190 |
gpu_pids: Option<Vec<IntHashMap<Pid, (u64, u32)>>>, |
| 191 |
#[cfg(feature = "gpu")] |
| 192 |
gpus_total_mem: Option<u64>, |
| 193 |
#[cfg(feature = "zfs")] |
| 194 |
free_arc_mem: bool, |
| 195 |
|
| 196 |
#[cfg(target_os = "linux")] |
| 197 |
cgroup_memory_data: CgroupMemCollector, |
| 198 |
|
| 199 |
#[cfg(target_os = "linux")] |
| 200 |
cgroup_cpu_data: CgroupCpuCollector, |
| 201 |
} |
| 202 |
|
| 203 |
const LESS_ROUTINE_TASK_TIME: Duration = Duration::from_secs(60); |
| 204 |
|
| 205 |
impl DataCollector { |
| 206 |
pub fn new(filters: DataFilters) -> Self { |
| 207 |
// Initialize it to the past to force it to load on initialization. |
| 208 |
let now = Instant::now(); |
| 209 |
let last_collection_time = now.checked_sub(LESS_ROUTINE_TASK_TIME * 10).unwrap_or(now); |
| 210 |
|
| 211 |
DataCollector { |
| 212 |
data: Data::default(), |
| 213 |
sys: SysinfoSource::default(), |
| 214 |
#[cfg(target_os = "linux")] |
| 215 |
prev_process_details: IntHashMap::default(), |
| 216 |
#[cfg(target_os = "linux")] |
| 217 |
prev_idle: 0_f64, |
| 218 |
#[cfg(target_os = "linux")] |
| 219 |
prev_non_idle: 0_f64, |
| 220 |
use_current_cpu_total: false, |
| 221 |
unnormalized_cpu: false, |
| 222 |
get_process_threads: false, |
| 223 |
last_collection_time, |
| 224 |
total_rx: 0, |
| 225 |
total_tx: 0, |
| 226 |
total_rx_packets: 0, |
| 227 |
total_tx_packets: 0, |
| 228 |
show_average_cpu: false, |
| 229 |
widgets_to_harvest: UsedWidgets::default(), |
| 230 |
#[cfg(feature = "battery")] |
| 231 |
battery_manager: None, |
| 232 |
#[cfg(feature = "battery")] |
| 233 |
battery_list: None, |
| 234 |
filters, |
| 235 |
#[cfg(unix)] |
| 236 |
user_table: Default::default(), |
| 237 |
#[cfg(feature = "gpu")] |
| 238 |
gpu_pids: None, |
| 239 |
#[cfg(feature = "gpu")] |
| 240 |
gpus_total_mem: None, |
| 241 |
#[cfg(feature = "zfs")] |
| 242 |
free_arc_mem: false, |
| 243 |
last_list_collection_time: last_collection_time, |
| 244 |
should_run_less_routine_tasks: true, |
| 245 |
#[cfg(target_os = "linux")] |
| 246 |
cgroup_memory_data: CgroupMemCollector::default(), |
| 247 |
#[cfg(target_os = "linux")] |
| 248 |
cgroup_cpu_data: CgroupCpuCollector::default(), |
| 249 |
include_unmounted_disks: false, |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/// Update the check for routine tasks like updating lists of batteries, |
| 254 |
/// cleanup, etc. This is useful for things that we don't want to update |
| 255 |
/// all the time. |
| 256 |
/// |
| 257 |
/// Note this should be set back to false if |
| 258 |
/// `self.last_list_collection_time` is updated. |
| 259 |
#[inline] |
| 260 |
fn run_less_routine_tasks(&mut self) { |
| 261 |
if self |
| 262 |
.data |
| 263 |
.collection_time |
| 264 |
.duration_since(self.last_list_collection_time) |
| 265 |
> LESS_ROUTINE_TASK_TIME |
| 266 |
{ |
| 267 |
self.should_run_less_routine_tasks = true; |
| 268 |
} |
| 269 |
|
| 270 |
if self.should_run_less_routine_tasks { |
| 271 |
self.last_list_collection_time = self.data.collection_time; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
pub fn set_collection(&mut self, used_widgets: UsedWidgets) { |
| 276 |
self.widgets_to_harvest = used_widgets; |
| 277 |
} |
| 278 |
|
| 279 |
pub fn set_use_current_cpu_total(&mut self, use_current_cpu_total: bool) { |
| 280 |
self.use_current_cpu_total = use_current_cpu_total; |
| 281 |
} |
| 282 |
|
| 283 |
pub fn set_unnormalized_cpu(&mut self, unnormalized_cpu: bool) { |
| 284 |
self.unnormalized_cpu = unnormalized_cpu; |
| 285 |
} |
| 286 |
|
| 287 |
pub fn set_show_average_cpu(&mut self, show_average_cpu: bool) { |
| 288 |
self.show_average_cpu = show_average_cpu; |
| 289 |
} |
| 290 |
|
| 291 |
pub fn set_get_process_threads(&mut self, get_process_threads: bool) { |
| 292 |
self.get_process_threads = get_process_threads; |
| 293 |
} |
| 294 |
|
| 295 |
pub fn set_include_unmounted_disks(&mut self, include_unmounted_disks: bool) { |
| 296 |
self.include_unmounted_disks = include_unmounted_disks; |
| 297 |
} |
| 298 |
|
| 299 |
#[cfg(feature = "zfs")] |
| 300 |
pub fn set_free_arc_mem(&mut self, free_mem: bool) { |
| 301 |
self.free_arc_mem = free_mem; |
| 302 |
} |
| 303 |
|
| 304 |
/// Refresh sysinfo data. We use sysinfo for the following data: |
| 305 |
/// - CPU usage |
| 306 |
/// - Memory usage |
| 307 |
/// - Network usage |
| 308 |
/// - Processes (non-Linux) |
| 309 |
/// - Disk (Windows, FreeBSD) |
| 310 |
/// - Temperatures (non-Linux) |
| 311 |
fn refresh_sysinfo_data(&mut self) { |
| 312 |
// Refresh the list of objects once every minute. If it's too frequent it can |
| 313 |
// cause segfaults. |
| 314 |
|
| 315 |
if self.widgets_to_harvest.use_cpu || self.widgets_to_harvest.use_proc { |
| 316 |
self.sys.system.refresh_cpu_all(); |
| 317 |
} |
| 318 |
|
| 319 |
if self.widgets_to_harvest.use_mem || self.widgets_to_harvest.use_proc { |
| 320 |
self.sys.system.refresh_memory(); |
| 321 |
} |
| 322 |
|
| 323 |
if self.widgets_to_harvest.use_net { |
| 324 |
self.sys.network.refresh(true); |
| 325 |
} |
| 326 |
|
| 327 |
// sysinfo is used on non-Linux systems for the following: |
| 328 |
// - Processes (users list as well for Windows) |
| 329 |
// - Disks (Windows/FreeBSD) |
| 330 |
// - Temperatures and temperature components list. |
| 331 |
#[cfg(not(target_os = "linux"))] |
| 332 |
{ |
| 333 |
if self.widgets_to_harvest.use_proc { |
| 334 |
self.sys.system.refresh_processes_specifics( |
| 335 |
sysinfo::ProcessesToUpdate::All, |
| 336 |
true, |
| 337 |
sysinfo::ProcessRefreshKind::everything() |
| 338 |
.without_environ() |
| 339 |
.without_cwd() |
| 340 |
.without_root(), |
| 341 |
); |
| 342 |
|
| 343 |
// For Windows, sysinfo also handles the users list. |
| 344 |
#[cfg(target_os = "windows")] |
| 345 |
if self.should_run_less_routine_tasks { |
| 346 |
self.sys.users.refresh(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if self.widgets_to_harvest.use_temp || self.widgets_to_harvest.use_temp_graph { |
| 351 |
if self.should_run_less_routine_tasks { |
| 352 |
self.sys.temps.refresh(true); |
| 353 |
} |
| 354 |
|
| 355 |
for component in self.sys.temps.iter_mut() { |
| 356 |
component.refresh(); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
#[cfg(any(target_os = "windows", target_os = "freebsd"))] |
| 361 |
if self.widgets_to_harvest.use_disk { |
| 362 |
if self.should_run_less_routine_tasks { |
| 363 |
self.sys.disks.refresh(true); |
| 364 |
} |
| 365 |
|
| 366 |
for disk in self.sys.disks.iter_mut() { |
| 367 |
disk.refresh(); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/// Update and refresh data. |
| 374 |
/// |
| 375 |
/// TODO: separate refresh steps and update steps |
| 376 |
pub fn update_data(&mut self) { |
| 377 |
self.data.collection_time = Instant::now(); |
| 378 |
|
| 379 |
self.run_less_routine_tasks(); |
| 380 |
|
| 381 |
self.refresh_sysinfo_data(); |
| 382 |
|
| 383 |
#[cfg(target_os = "linux")] |
| 384 |
{ |
| 385 |
let total_memory = self.sys.system.total_memory(); |
| 386 |
let total_swap = self.sys.system.total_swap(); |
| 387 |
self.cgroup_memory_data.refresh(total_memory, total_swap); |
| 388 |
} |
| 389 |
|
| 390 |
#[cfg(target_os = "linux")] |
| 391 |
self.cgroup_cpu_data.refresh(); |
| 392 |
|
| 393 |
self.update_cpu_usage(); |
| 394 |
self.update_memory_usage(); |
| 395 |
self.update_temps(); |
| 396 |
|
| 397 |
#[cfg(feature = "battery")] |
| 398 |
self.update_batteries(); |
| 399 |
|
| 400 |
#[cfg(feature = "gpu")] |
| 401 |
self.update_gpus(); |
| 402 |
|
| 403 |
self.update_processes(); |
| 404 |
self.update_network_usage(); |
| 405 |
self.update_disks(); |
| 406 |
|
| 407 |
// Make sure to run this to refresh the setting. |
| 408 |
self.should_run_less_routine_tasks = false; |
| 409 |
|
| 410 |
// Update times for future reference. |
| 411 |
self.last_collection_time = self.data.collection_time; |
| 412 |
} |
| 413 |
|
| 414 |
/// Gets GPU data. Note this will usually append to other previously |
| 415 |
/// collected data fields at the moment. |
| 416 |
#[cfg(feature = "gpu")] |
| 417 |
#[inline] |
| 418 |
fn update_gpus(&mut self) { |
| 419 |
if self.widgets_to_harvest.use_gpu { |
| 420 |
let mut local_gpu: Vec<(String, memory::MemData)> = Vec::new(); |
| 421 |
let mut local_gpu_pids: Vec<IntHashMap<Pid, (u64, u32)>> = Vec::new(); |
| 422 |
let mut local_gpu_total_mem: u64 = 0; |
| 423 |
|
| 424 |
#[cfg(feature = "nvidia")] |
| 425 |
if let Some(data) = nvidia::get_nvidia_vecs( |
| 426 |
&self.filters.temp_filter, |
| 427 |
&self.filters.temp_graph_filter, |
| 428 |
&self.widgets_to_harvest, |
| 429 |
) { |
| 430 |
if let Some(mut temp) = data.temperature { |
| 431 |
if let Some(sensors) = &mut self.data.temperature_sensors { |
| 432 |
sensors.append(&mut temp); |
| 433 |
} else { |
| 434 |
self.data.temperature_sensors = Some(temp); |
| 435 |
} |
| 436 |
} |
| 437 |
if let Some(mut mem) = data.memory { |
| 438 |
local_gpu.append(&mut mem); |
| 439 |
} |
| 440 |
if let Some(mut proc) = data.procs { |
| 441 |
local_gpu_pids.append(&mut proc.1); |
| 442 |
local_gpu_total_mem += proc.0; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
#[cfg(target_os = "linux")] |
| 447 |
if let Some(data) = |
| 448 |
amd::get_amd_vecs(&self.widgets_to_harvest, self.last_collection_time) |
| 449 |
{ |
| 450 |
if let Some(mut mem) = data.memory { |
| 451 |
local_gpu.append(&mut mem); |
| 452 |
} |
| 453 |
if let Some(mut proc) = data.procs { |
| 454 |
local_gpu_pids.append(&mut proc.1); |
| 455 |
local_gpu_total_mem += proc.0; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
self.data.gpu = (!local_gpu.is_empty()).then_some(local_gpu); |
| 460 |
self.gpu_pids = (!local_gpu_pids.is_empty()).then_some(local_gpu_pids); |
| 461 |
self.gpus_total_mem = (local_gpu_total_mem > 0).then_some(local_gpu_total_mem); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
#[inline] |
| 466 |
fn update_cpu_usage(&mut self) { |
| 467 |
if self.widgets_to_harvest.use_cpu { |
| 468 |
self.data.cpu = cpu::get_cpu_data_list(self).ok(); |
| 469 |
|
| 470 |
#[cfg(unix)] |
| 471 |
{ |
| 472 |
self.data.load_avg = Some(cpu::get_load_avg()); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
#[inline] |
| 478 |
fn update_processes(&mut self) { |
| 479 |
if self.widgets_to_harvest.use_proc |
| 480 |
&& let Ok(mut process_list) = self.get_processes() |
| 481 |
{ |
| 482 |
// NB: To avoid duplicate sorts on rerenders/events, we sort the processes by |
| 483 |
// PID here. We also want to avoid re-sorting *again* later on |
| 484 |
// if we're sorting by PID, since we already did it here! |
| 485 |
process_list.sort_unstable_by_key(|p| p.pid); |
| 486 |
self.data.list_of_processes = Some(process_list); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
#[inline] |
| 491 |
fn update_temps(&mut self) { |
| 492 |
if self.widgets_to_harvest.use_temp || self.widgets_to_harvest.use_temp_graph { |
| 493 |
#[cfg(not(target_os = "linux"))] |
| 494 |
if let Ok(data) = temperature::get_temperature_data( |
| 495 |
&self.sys.temps, |
| 496 |
&self.filters.temp_filter, |
| 497 |
&self.filters.temp_graph_filter, |
| 498 |
) { |
| 499 |
self.data.temperature_sensors = data; |
| 500 |
} |
| 501 |
|
| 502 |
#[cfg(target_os = "linux")] |
| 503 |
if let Ok(data) = temperature::get_temperature_data( |
| 504 |
&self.filters.temp_filter, |
| 505 |
&self.filters.temp_graph_filter, |
| 506 |
) { |
| 507 |
self.data.temperature_sensors = data; |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
#[inline] |
| 513 |
fn update_memory_usage(&mut self) { |
| 514 |
if self.widgets_to_harvest.use_mem { |
| 515 |
self.data.memory = memory::get_ram_usage(self); |
| 516 |
|
| 517 |
#[cfg(feature = "zfs")] |
| 518 |
{ |
| 519 |
#[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 520 |
if let Some(arc) = memory::arc::get_arc_usage() { |
| 521 |
if let Some(mem) = &mut self.data.memory { |
| 522 |
if self.free_arc_mem { |
| 523 |
if arc.0.used_bytes > arc.1 { |
| 524 |
#[cfg(target_os = "linux")] |
| 525 |
{ |
| 526 |
// Keep arc min like htop; the subtraction below won't underflow because of |
| 527 |
// the above check. |
| 528 |
mem.used_bytes = |
| 529 |
mem.used_bytes.saturating_sub(arc.0.used_bytes - arc.1); |
| 530 |
} |
| 531 |
#[cfg(target_os = "freebsd")] |
| 532 |
{ |
| 533 |
mem.used_bytes += arc.1; // sysinfo subtracts arc_size on freebsd |
| 534 |
} |
| 535 |
} else { |
| 536 |
#[cfg(target_os = "freebsd")] |
| 537 |
{ |
| 538 |
mem.used_bytes += arc.0.used_bytes; |
| 539 |
} |
| 540 |
} |
| 541 |
} else { |
| 542 |
#[cfg(target_os = "freebsd")] |
| 543 |
{ |
| 544 |
mem.used_bytes += arc.0.used_bytes; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
self.data.arc = Some(arc.0); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
#[cfg(not(target_os = "windows"))] |
| 554 |
if self.widgets_to_harvest.use_cache { |
| 555 |
self.data.cache = memory::get_cache_usage(&self.sys.system); |
| 556 |
} |
| 557 |
|
| 558 |
self.data.swap = memory::get_swap_usage(self); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
#[inline] |
| 563 |
fn update_network_usage(&mut self) { |
| 564 |
if self.widgets_to_harvest.use_net { |
| 565 |
let net_data = network::get_network_data( |
| 566 |
&self.sys.network, |
| 567 |
self.last_collection_time, |
| 568 |
&mut self.total_rx, |
| 569 |
&mut self.total_tx, |
| 570 |
&mut self.total_rx_packets, |
| 571 |
&mut self.total_tx_packets, |
| 572 |
self.data.collection_time, |
| 573 |
&self.filters.net_filter, |
| 574 |
); |
| 575 |
|
| 576 |
self.total_rx = net_data.total_rx; |
| 577 |
self.total_tx = net_data.total_tx; |
| 578 |
self.total_rx_packets = net_data.total_rx_packets; |
| 579 |
self.total_tx_packets = net_data.total_tx_packets; |
| 580 |
self.data.network = Some(net_data); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
/// Update battery information. |
| 585 |
/// |
| 586 |
/// If the battery manager is not initialized, it will attempt to initialize |
| 587 |
/// it if at least one battery is found. |
| 588 |
/// |
| 589 |
/// This function also refreshes the list of batteries if |
| 590 |
/// `self.should_run_less_routine_tasks` is true. |
| 591 |
#[inline] |
| 592 |
#[cfg(feature = "battery")] |
| 593 |
fn update_batteries(&mut self) { |
| 594 |
let battery_manager = match &self.battery_manager { |
| 595 |
Some(manager) => { |
| 596 |
// Also check if we need to refresh the list of batteries. |
| 597 |
if self.should_run_less_routine_tasks { |
| 598 |
let battery_list = manager |
| 599 |
.batteries() |
| 600 |
.map(|batteries| batteries.filter_map(Result::ok).collect::<Vec<_>>()); |
| 601 |
|
| 602 |
if let Ok(battery_list) = battery_list { |
| 603 |
if battery_list.is_empty() { |
| 604 |
self.battery_list = None; |
| 605 |
} else { |
| 606 |
self.battery_list = Some(battery_list); |
| 607 |
} |
| 608 |
} else { |
| 609 |
self.battery_list = None; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
manager |
| 614 |
} |
| 615 |
None => { |
| 616 |
if let Ok(manager) = Manager::new() { |
| 617 |
let Ok(batteries) = manager.batteries() else { |
| 618 |
return; |
| 619 |
}; |
| 620 |
|
| 621 |
let battery_list = batteries.filter_map(Result::ok).collect::<Vec<_>>(); |
| 622 |
|
| 623 |
if battery_list.is_empty() { |
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
self.battery_list = Some(battery_list); |
| 628 |
self.battery_manager.insert(manager) |
| 629 |
} else { |
| 630 |
return; |
| 631 |
} |
| 632 |
} |
| 633 |
}; |
| 634 |
|
| 635 |
self.data.list_of_batteries = self |
| 636 |
.battery_list |
| 637 |
.as_mut() |
| 638 |
.map(|battery_list| batteries::refresh_batteries(battery_manager, battery_list)); |
| 639 |
} |
| 640 |
|
| 641 |
#[inline] |
| 642 |
fn update_disks(&mut self) { |
| 643 |
if self.widgets_to_harvest.use_disk || self.widgets_to_harvest.use_disk_io_graph { |
| 644 |
self.data.disks = disks::get_disk_usage(self).ok(); |
| 645 |
self.data.io = disks::get_io_usage(self).ok(); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/// Returns the total memory of the system. |
| 650 |
#[inline] |
| 651 |
fn total_memory(&self) -> u64 { |
| 652 |
if let Some(memory) = &self.data.memory { |
| 653 |
memory.total_bytes.get() |
| 654 |
} else { |
| 655 |
self.sys.system.total_memory() |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
#[cfg(target_os = "freebsd")] |
| 661 |
/// Deserialize [libxo](https://www.freebsd.org/cgi/man.cgi?query=libxo&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html) JSON data |
| 662 |
fn deserialize_xo<T>(key: &str, data: &[u8]) -> Result<T, std::io::Error> |
| 663 |
where |
| 664 |
T: serde::de::DeserializeOwned, |
| 665 |
{ |
| 666 |
let mut value: serde_json::Value = serde_json::from_slice(data)?; |
| 667 |
value |
| 668 |
.as_object_mut() |
| 669 |
.and_then(|map| map.remove(key)) |
| 670 |
.ok_or_else(|| std::io::Error::other("key not found")) |
| 671 |
.and_then(|val| serde_json::from_value(val).map_err(|err| err.into())) |
| 672 |
} |
| 673 |
|
| 674 |
#[cfg(test)] |
| 675 |
mod tests { |
| 676 |
use super::*; |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn test_data_collection() { |
| 680 |
let mut collector = DataCollector::new(DataFilters { |
| 681 |
disk_filter: None, |
| 682 |
mount_filter: None, |
| 683 |
temp_filter: None, |
| 684 |
temp_graph_filter: None, |
| 685 |
disk_io_graph_filter: None, |
| 686 |
net_filter: None, |
| 687 |
}); |
| 688 |
|
| 689 |
// #[cfg(feature = "battery")] |
| 690 |
// { |
| 691 |
// collector.widgets_to_harvest.use_battery = true; |
| 692 |
// } |
| 693 |
|
| 694 |
// #[cfg(feature = "zfs")] |
| 695 |
// { |
| 696 |
// collector.widgets_to_harvest.use_cache = true; |
| 697 |
// } |
| 698 |
|
| 699 |
// #[cfg(feature = "gpu")] |
| 700 |
// { |
| 701 |
// collector.widgets_to_harvest.use_gpu = true; |
| 702 |
// } |
| 703 |
|
| 704 |
collector.widgets_to_harvest.use_cpu = true; |
| 705 |
collector.widgets_to_harvest.use_disk = true; |
| 706 |
collector.widgets_to_harvest.use_mem = true; |
| 707 |
collector.widgets_to_harvest.use_net = true; |
| 708 |
collector.widgets_to_harvest.use_proc = true; |
| 709 |
collector.widgets_to_harvest.use_temp = true; |
| 710 |
|
| 711 |
collector.update_data(); |
| 712 |
|
| 713 |
let data = collector.data; |
| 714 |
|
| 715 |
assert!(!data.cpu.unwrap().is_empty()); |
| •716 |
// assert!(!data.disks.unwrap().is_empty()); |
| 717 |
assert!(data.memory.is_some()); |
| 718 |
assert!(data.network.is_some()); |
| 719 |
assert!(!data.list_of_processes.unwrap().is_empty()); |
| 720 |
assert!(data.temperature_sensors.is_some()); |
| 721 |
} |
| 722 |
} |