Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate network collection to sysinfo #1041

Merged
merged 3 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1025](https://github.com/ClementTsang/bottom/pull/1025): Officially support M1 macOS.
- [#1035](https://github.com/ClementTsang/bottom/pull/1035): Migrate away from heim for CPU information.
- [#1036](https://github.com/ClementTsang/bottom/pull/1036): Migrate away from heim for memory information; Linux
platforms will now try to use `MemAvailable` to determine used memory if supported.
platforms will also now try to use `MemAvailable` to determine used memory if supported.
- [#1041](https://github.com/ClementTsang/bottom/pull/1041): Migrate away from heim for network information.

## Other

Expand Down
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ unicode-width = "0.1.10"
libc = "0.2.124"

[target.'cfg(target_os = "linux")'.dependencies]
heim = { version = "0.1.0-rc.1", features = ["disk", "net", "sensors"] }
heim = { version = "0.1.0-rc.1", features = ["disk", "sensors"] }
procfs = { version = "0.15.1", default-features = false }

[target.'cfg(target_os = "macos")'.dependencies]
heim = { version = "0.1.0-rc.1", features = ["disk", "net"] }
heim = { version = "0.1.0-rc.1", features = ["disk"] }
mach2 = "0.4.1"

[target.'cfg(target_os = "windows")'.dependencies]
Expand Down
8 changes: 2 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
use concat_string::concat_string;
use data_farmer::*;
use data_harvester::temperature;
use filter::*;
use layout_manager::*;
pub use states::*;
use typed_builder::*;
Expand All @@ -23,6 +24,7 @@ use crate::{

pub mod data_farmer;
pub mod data_harvester;
pub mod filter;
pub mod frozen_state;
pub mod layout_manager;
mod process_killer;
Expand Down Expand Up @@ -81,12 +83,6 @@ pub struct DataFilters {
pub net_filter: Option<Filter>,
}

#[derive(Debug, Clone)]
pub struct Filter {
pub is_list_ignored: bool,
pub list: Vec<regex::Regex>,
}

#[derive(TypedBuilder)]
pub struct App {
#[builder(default = false, setter(skip))]
Expand Down
81 changes: 30 additions & 51 deletions src/app/data_harvester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,25 @@ impl DataCollector {
self.sys.refresh_memory();
self.mem_total_kb = self.sys.total_memory();

// Refresh network list once at the start.
// TODO: may be worth refreshing every once in a while (maybe on a separate timer).
if self.widgets_to_harvest.use_net {
self.sys.refresh_networks_list();
}

if self.widgets_to_harvest.use_proc || self.widgets_to_harvest.use_cpu {
self.sys.refresh_cpu();
}

#[cfg(not(target_os = "linux"))]
{
// TODO: Would be good to get this and network list running on a timer instead...?

// Refresh components list once...
if self.widgets_to_harvest.use_temp {
self.sys.refresh_components_list();
}

// Refresh network list once...
if cfg!(target_os = "windows") && self.widgets_to_harvest.use_net {
self.sys.refresh_networks_list();
}

if cfg!(target_os = "windows") && self.widgets_to_harvest.use_proc {
self.sys.refresh_users_list();
}
Expand All @@ -183,10 +189,6 @@ impl DataCollector {
}
}

if self.widgets_to_harvest.use_proc || self.widgets_to_harvest.use_cpu {
self.sys.refresh_cpu();
}

#[cfg(feature = "battery")]
{
if self.widgets_to_harvest.use_battery {
Expand Down Expand Up @@ -238,6 +240,10 @@ impl DataCollector {
self.sys.refresh_memory();
}

if self.widgets_to_harvest.use_net {
self.sys.refresh_networks();
}

#[cfg(not(target_os = "linux"))]
{
if self.widgets_to_harvest.use_proc {
Expand All @@ -247,13 +253,6 @@ impl DataCollector {
self.sys.refresh_components();
}

#[cfg(target_os = "windows")]
{
if self.widgets_to_harvest.use_net {
self.sys.refresh_networks();
}
}

#[cfg(target_os = "freebsd")]
{
if self.widgets_to_harvest.use_disk {
Expand Down Expand Up @@ -392,31 +391,20 @@ impl DataCollector {
}
}

let network_data_fut = {
#[cfg(any(target_os = "windows", target_os = "freebsd"))]
{
network::get_network_data(
&self.sys,
self.last_collection_time,
&mut self.total_rx,
&mut self.total_tx,
current_instant,
self.widgets_to_harvest.use_net,
&self.filters.net_filter,
)
}
#[cfg(not(any(target_os = "windows", target_os = "freebsd")))]
{
network::get_network_data(
self.last_collection_time,
&mut self.total_rx,
&mut self.total_tx,
current_instant,
self.widgets_to_harvest.use_net,
&self.filters.net_filter,
)
}
};
if self.widgets_to_harvest.use_net {
let net_data = network::get_network_data(
&self.sys,
self.last_collection_time,
&mut self.total_rx,
&mut self.total_tx,
current_instant,
&self.filters.net_filter,
);

self.total_rx = net_data.total_rx;
self.total_tx = net_data.total_tx;
self.data.network = Some(net_data);
}

let disk_data_fut = disks::get_disk_usage(
self.widgets_to_harvest.use_disk,
Expand All @@ -425,16 +413,7 @@ impl DataCollector {
);
let disk_io_usage_fut = disks::get_io_usage(self.widgets_to_harvest.use_disk);

let (net_data, disk_res, io_res) =
join!(network_data_fut, disk_data_fut, disk_io_usage_fut,);

if let Ok(net_data) = net_data {
if let Some(net_data) = &net_data {
self.total_rx = net_data.total_rx;
self.total_tx = net_data.total_tx;
}
self.data.network = net_data;
}
let (disk_res, io_res) = join!(disk_data_fut, disk_io_usage_fut,);

if let Ok(disks) = disk_res {
self.data.disks = disks;
Expand Down
14 changes: 2 additions & 12 deletions src/app/data_harvester/network.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
//! Data collection for network usage/IO.
//!
//! For Linux and macOS, this is handled by Heim.
//! For Windows, this is handled by sysinfo.

cfg_if::cfg_if! {
if #[cfg(any(target_os = "linux", target_os = "macos"))] {
pub mod heim;
pub use self::heim::*;
} else if #[cfg(any(target_os = "freebsd", target_os = "windows"))] {
pub mod sysinfo;
pub use self::sysinfo::*;
}
}
pub mod sysinfo;
pub use self::sysinfo::*;

#[derive(Default, Clone, Debug)]
/// All units in bits.
Expand Down
67 changes: 0 additions & 67 deletions src/app/data_harvester/network/heim.rs

This file was deleted.

27 changes: 9 additions & 18 deletions src/app/data_harvester/network/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,24 @@

use std::time::Instant;

use crate::app::Filter;

use super::NetworkHarvest;

pub async fn get_network_data(
// TODO: Eventually make it so that this thing also takes individual usage into account, so we can show per-interface!
pub fn get_network_data(
sys: &sysinfo::System, prev_net_access_time: Instant, prev_net_rx: &mut u64,
prev_net_tx: &mut u64, curr_time: Instant, actually_get: bool,
filter: &Option<crate::app::Filter>,
) -> crate::utils::error::Result<Option<NetworkHarvest>> {
prev_net_tx: &mut u64, curr_time: Instant, filter: &Option<Filter>,
) -> NetworkHarvest {
use sysinfo::{NetworkExt, SystemExt};

if !actually_get {
return Ok(None);
}

let mut total_rx: u64 = 0;
let mut total_tx: u64 = 0;

let networks = sys.networks();
for (name, network) in networks {
let to_keep = if let Some(filter) = filter {
let mut ret = filter.is_list_ignored;
for r in &filter.list {
if r.is_match(name) {
ret = !filter.is_list_ignored;
break;
}
}
ret
filter.keep_entry(name)
} else {
true
};
Expand All @@ -52,10 +43,10 @@ pub async fn get_network_data(

*prev_net_rx = total_rx;
*prev_net_tx = total_tx;
Ok(Some(NetworkHarvest {
NetworkHarvest {
rx,
tx,
total_rx,
total_tx,
}))
}
}
Loading