all cache-related code excluded on windows, in the process refactored src/data_conversion.rs convert_mem_label() to convert a single label instead of all at once

This commit is contained in:
Twan Stok 2023-03-13 22:00:48 +01:00
parent 4afd71926f
commit 1300ade98b
10 changed files with 106 additions and 99 deletions

View File

@ -37,6 +37,7 @@ pub struct TimedData {
pub cpu_data: Vec<Value>,
pub load_avg_data: [f32; 3],
pub mem_data: Option<Value>,
#[cfg(not(target_os = "windows"))]
pub cache_data: Option<Value>,
pub swap_data: Option<Value>,
#[cfg(feature = "zfs")]
@ -111,6 +112,7 @@ pub struct DataCollection {
pub timed_data_vec: Vec<(Instant, TimedData)>,
pub network_harvest: network::NetworkHarvest,
pub memory_harvest: memory::MemHarvest,
#[cfg(not(target_os = "windows"))]
pub cache_harvest: memory::MemHarvest,
pub swap_harvest: memory::MemHarvest,
pub cpu_harvest: cpu::CpuHarvest,
@ -136,6 +138,7 @@ impl Default for DataCollection {
timed_data_vec: Vec::default(),
network_harvest: network::NetworkHarvest::default(),
memory_harvest: memory::MemHarvest::default(),
#[cfg(not(target_os = "windows"))]
cache_harvest: memory::MemHarvest::default(),
swap_harvest: memory::MemHarvest::default(),
cpu_harvest: cpu::CpuHarvest::default(),
@ -211,7 +214,8 @@ impl DataCollection {
self.eat_network(network, &mut new_entry);
}
// Memory and Swap
// Memory, Swap, and Cache if not windows
#[cfg(not(target_os = "windows"))]
if let (Some(memory), Some(cache), Some(swap)) = (
harvested_data.memory,
harvested_data.cache,
@ -220,6 +224,11 @@ impl DataCollection {
self.eat_memory_and_swap(memory, cache, swap, &mut new_entry);
}
#[cfg(target_os = "windows")]
if let (Some(memory), Some(swap)) = (harvested_data.memory, harvested_data.swap) {
self.eat_memory_and_swap(memory, swap, &mut new_entry);
}
#[cfg(feature = "zfs")]
if let Some(arc) = harvested_data.arc {
self.eat_arc(arc, &mut new_entry);
@ -271,21 +280,28 @@ impl DataCollection {
}
fn eat_memory_and_swap(
&mut self, memory: memory::MemHarvest, cache: memory::MemHarvest, swap: memory::MemHarvest,
&mut self, memory: memory::MemHarvest,
#[cfg(not(target_os = "windows"))] cache: memory::MemHarvest, swap: memory::MemHarvest,
new_entry: &mut TimedData,
) {
// Memory
new_entry.mem_data = memory.use_percent;
// Cache
new_entry.cache_data = cache.use_percent;
#[cfg(not(target_os = "windows"))]
{
new_entry.cache_data = cache.use_percent;
}
// Swap
new_entry.swap_data = swap.use_percent;
// In addition copy over latest data for easy reference
self.memory_harvest = memory;
self.cache_harvest = cache;
#[cfg(not(target_os = "windows"))]
{
self.cache_harvest = cache;
}
self.swap_harvest = swap;
}

View File

@ -34,6 +34,7 @@ pub struct Data {
pub cpu: Option<cpu::CpuHarvest>,
pub load_avg: Option<cpu::LoadAvgHarvest>,
pub memory: Option<memory::MemHarvest>,
#[cfg(not(target_os = "windows"))]
pub cache: Option<memory::MemHarvest>,
pub swap: Option<memory::MemHarvest>,
pub temperature_sensors: Option<Vec<temperature::TempHarvest>>,
@ -56,6 +57,7 @@ impl Default for Data {
cpu: None,
load_avg: None,
memory: None,
#[cfg(not(target_os = "windows"))]
cache: None,
swap: None,
temperature_sensors: None,
@ -399,7 +401,10 @@ impl DataCollector {
fn update_memory_usage(&mut self) {
if self.widgets_to_harvest.use_mem {
self.data.memory = memory::get_ram_usage(&self.sys);
self.data.cache = memory::get_cache_usage(&self.sys);
#[cfg(not(target_os = "windows"))]
{
self.data.cache = memory::get_cache_usage(&self.sys);
}
self.data.swap = memory::get_swap_usage(
#[cfg(not(target_os = "windows"))]
&self.sys,

View File

@ -1,9 +1,10 @@
//! Memory data collection.
pub mod sysinfo;
#[cfg(not(target_os = "windows"))]
pub(crate) use self::sysinfo::get_cache_usage;
pub(crate) use self::sysinfo::get_ram_usage;
pub mod sysinfo;
cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] {
pub mod windows;

View File

@ -38,7 +38,8 @@ pub(crate) fn get_swap_usage(sys: &System) -> Option<MemHarvest> {
}
/// Returns cache usage. sysinfo has no way to do this directly but it should equal the difference
/// between the available and free memory. On windows, this will always be 0. TODO: hide on windows?
/// between the available and free memory. On windows, this will always be 0.
#[cfg(not(target_os = "windows"))]
pub(crate) fn get_cache_usage(sys: &System) -> Option<MemHarvest> {
let mem_used_in_kib = (sys.available_memory() - sys.free_memory()) / 1024;
let mem_total_in_kib = sys.total_memory() / 1024;

View File

@ -19,6 +19,13 @@ use std::{
};
use anyhow::{Context, Result};
use crossterm::{
event::{EnableBracketedPaste, EnableMouseCapture},
execute,
terminal::{enable_raw_mode, EnterAlternateScreen},
};
use tui::{backend::CrosstermBackend, Terminal};
use bottom::{
canvas::{self, canvas_styling::CanvasColours},
constants::*,
@ -26,12 +33,6 @@ use bottom::{
options::*,
*,
};
use crossterm::{
event::{EnableBracketedPaste, EnableMouseCapture},
execute,
terminal::{enable_raw_mode, EnterAlternateScreen},
};
use tui::{backend::CrosstermBackend, Terminal};
fn main() -> Result<()> {
let matches = clap::get_matches();
@ -239,8 +240,11 @@ fn main() -> Result<()> {
if app.used_widgets.use_mem {
app.converted_data.mem_data =
convert_mem_data_points(&app.data_collection);
app.converted_data.cache_data =
convert_cache_data_points(&app.data_collection);
#[cfg(not(target_os = "windows"))]
{
app.converted_data.cache_data =
convert_cache_data_points(&app.data_collection);
}
app.converted_data.swap_data =
convert_swap_data_points(&app.data_collection);
#[cfg(feature = "zfs")]
@ -253,12 +257,17 @@ fn main() -> Result<()> {
app.converted_data.gpu_data =
convert_gpu_data(&app.data_collection);
}
let (memory_labels, cache_labels, swap_labels) =
convert_mem_labels(&app.data_collection);
app.converted_data.mem_labels = memory_labels;
app.converted_data.cache_labels = cache_labels;
app.converted_data.swap_labels = swap_labels;
app.converted_data.mem_labels =
convert_mem_label(&app.data_collection.memory_harvest);
app.converted_data.swap_labels =
convert_mem_label(&app.data_collection.swap_harvest);
#[cfg(not(target_os = "windows"))]
{
app.converted_data.cache_labels =
convert_mem_label(&app.data_collection.cache_harvest);
}
#[cfg(feature = "zfs")]
{
let arc_labels = convert_arc_labels(&app.data_collection);

View File

@ -18,6 +18,7 @@ pub struct CanvasColours {
pub currently_selected_text_style: Style,
pub table_header_style: Style,
pub ram_style: Style,
#[cfg(not(target_os = "windows"))]
pub cache_style: Style,
pub swap_style: Style,
pub arc_style: Style,
@ -55,6 +56,7 @@ impl Default for CanvasColours {
.bg(currently_selected_bg_colour),
table_header_style: Style::default().fg(HIGHLIGHT_COLOUR),
ram_style: Style::default().fg(FIRST_COLOUR),
#[cfg(not(target_os = "windows"))]
cache_style: Style::default().fg(FOURTH_COLOUR),
swap_style: Style::default().fg(SECOND_COLOUR),
arc_style: Style::default().fg(THIRD_COLOUR),

View File

@ -53,6 +53,33 @@ impl Painter {
.gauge_style(self.colours.ram_style),
);
#[cfg(not(target_os = "windows"))]
{
let cache_data = &app_state.converted_data.cache_data;
let cache_percentage = if let Some(cache) = cache_data.last() {
cache.1
} else {
0.0
};
if let Some((_, label_frac)) = &app_state.converted_data.cache_labels {
let cache_fraction_label = if app_state.basic_mode_use_percent {
format!("{:3.0}%", cache_percentage.round())
} else {
label_frac.trim().to_string()
};
draw_widgets.push(
PipeGauge::default()
.ratio(cache_percentage / 100.0)
.start_label("CHE")
.inner_label(cache_fraction_label)
.label_style(self.colours.cache_style)
.gauge_style(self.colours.cache_style),
);
}
}
let swap_data = &app_state.converted_data.swap_data;
let swap_percentage = if let Some(swap) = swap_data.last() {

View File

@ -56,8 +56,9 @@ impl Painter {
name: Some(mem_label.into()),
});
}
#[cfg(not(target_os = "windows"))]
if let Some((label_percent, label_frac)) = &app_state.converted_data.cache_labels {
let cache_label = format!("CACHE:{}{}", label_percent, label_frac);
let cache_label = format!("CCH:{}{}", label_percent, label_frac);
points.push(GraphData {
points: &app_state.converted_data.cache_data,
style: self.colours.cache_style,

View File

@ -3,6 +3,7 @@
use kstring::KString;
use crate::app::data_harvester::memory::MemHarvest;
use crate::app::{
data_farmer::DataCollection,
data_harvester::{cpu::CpuDataType, temperature::TemperatureType},
@ -68,10 +69,12 @@ pub struct ConvertedData {
pub network_data_tx: Vec<Point>,
pub mem_labels: Option<(String, String)>,
#[cfg(not(target_os = "windows"))]
pub cache_labels: Option<(String, String)>,
pub swap_labels: Option<(String, String)>,
pub mem_data: Vec<Point>, /* TODO: Switch this and all data points over to a better data structure... */
#[cfg(not(target_os = "windows"))]
pub cache_data: Vec<Point>,
pub swap_data: Vec<Point>,
@ -220,6 +223,7 @@ pub fn convert_mem_data_points(current_data: &DataCollection) -> Vec<Point> {
result
}
#[cfg(not(target_os = "windows"))]
pub fn convert_cache_data_points(current_data: &DataCollection) -> Vec<Point> {
let mut result: Vec<Point> = Vec::new();
let current_time = current_data.current_instant;
@ -256,13 +260,7 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec<Point> {
result
}
pub fn convert_mem_labels(
current_data: &DataCollection,
) -> (
Option<(String, String)>,
Option<(String, String)>,
Option<(String, String)>,
) {
pub fn convert_mem_label(harvest: &MemHarvest) -> Option<(String, String)> {
/// Returns the unit type and denominator for given total amount of memory in kibibytes.
fn return_unit_and_denominator_for_mem_kib(mem_total_kib: u64) -> (&'static str, f64) {
if mem_total_kib < 1024 {
@ -280,77 +278,21 @@ pub fn convert_mem_labels(
}
}
(
if current_data.memory_harvest.total_kib > 0 {
Some((
format!(
"{:3.0}%",
current_data.memory_harvest.use_percent.unwrap_or(0.0)
),
{
let (unit, denominator) = return_unit_and_denominator_for_mem_kib(
current_data.memory_harvest.total_kib,
);
if harvest.total_kib > 0 {
Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), {
let (unit, denominator) = return_unit_and_denominator_for_mem_kib(harvest.total_kib);
format!(
" {:.1}{}/{:.1}{}",
current_data.memory_harvest.used_kib as f64 / denominator,
unit,
(current_data.memory_harvest.total_kib as f64 / denominator),
unit
)
},
))
} else {
None
},
if current_data.cache_harvest.total_kib > 0 {
Some((
format!(
"{:3.0}%",
current_data.cache_harvest.use_percent.unwrap_or(0.0)
),
{
let (unit, denominator) = return_unit_and_denominator_for_mem_kib(
current_data.cache_harvest.total_kib,
);
format!(
" {:.1}{}/{:.1}{}",
current_data.cache_harvest.used_kib as f64 / denominator,
unit,
(current_data.cache_harvest.total_kib as f64 / denominator),
unit
)
},
))
} else {
None
},
if current_data.swap_harvest.total_kib > 0 {
Some((
format!(
"{:3.0}%",
current_data.swap_harvest.use_percent.unwrap_or(0.0)
),
{
let (unit, denominator) = return_unit_and_denominator_for_mem_kib(
current_data.swap_harvest.total_kib,
);
format!(
" {:.1}{}/{:.1}{}",
current_data.swap_harvest.used_kib as f64 / denominator,
unit,
(current_data.swap_harvest.total_kib as f64 / denominator),
unit
)
},
))
} else {
None
},
)
format!(
" {:.1}{}/{:.1}{}",
harvest.used_kib as f64 / denominator,
unit,
(harvest.total_kib as f64 / denominator),
unit
)
}))
} else {
None
}
}
pub fn get_rx_tx_data_points(

View File

@ -386,7 +386,10 @@ pub fn update_data(app: &mut App) {
// TODO: [OPT] Prefer reassignment over new vectors?
if app.mem_state.force_update.is_some() {
app.converted_data.mem_data = convert_mem_data_points(data_source);
app.converted_data.cache_data = convert_cache_data_points(data_source);
#[cfg(not(target_os = "windows"))]
{
app.converted_data.cache_data = convert_cache_data_points(data_source);
}
app.converted_data.swap_data = convert_swap_data_points(data_source);
#[cfg(feature = "zfs")]
{