Skip to content

Commit

Permalink
fix nan; readme linux dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
griccardos committed Feb 19, 2023
1 parent 9a84a67 commit f35712e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
6 changes: 3 additions & 3 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
@@ -1,6 +1,6 @@
[package]
name = "toprs"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
description = "Cross platform memory focused top"
repository = "https://github.com/griccardos/toprs/"
Expand All @@ -15,7 +15,7 @@ dioxus = "0.3.1" #gui
dioxus-desktop = "0.3.0" #gui
formato = "0.2.0" #format numbers
gumdrop = "0.8.1" #arguments
sysinfo = "0.27.7" #get system info
sysinfo = "0.28.0" #get system info
tokio = { version = "1.25.0", features = ["time"] } #sleep
tui = "0.19.0" #console tui
dirs ="4.0.0" #for load config from home
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ Flamegraph blue portion is child memory, and red/yellow is its own memory
- In Linux this is the "resident" memory i.e. used physical memory


### Dependencies
- Linux:
- `apt install libwebkit2gtk-4.0-37` (as per tauri)
- `apt install libwebkit2gtk-4.0-dev`
- `apt install libgtk-3-0` (as per tauri)
- `apt install libgtk-3-dev`
- `apt install libjavascriptcoregtk-4.0-dev`
- `apt install libpango1.0-dev`
- `apt install libatk1.0-dev`
- `apt install libsoup-gnome2.4-dev`



### Thanks
Shoutout to the following projects that made this possible:
- rust Sysinfo lib
Expand Down
2 changes: 1 addition & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn app(cx: Scope) -> Element {
let totals = man.read().get_totals();
let mem = nice_size_g_thousands(totals.memory);
let totmem = nice_size_g_thousands(totals.memory_total);
let cpu = format!("{:.1}%", totals.cpu);
let cpu = format!("{:5>.1}%", totals.cpu_avg);
let uptime = nice_time(totals.uptime);
let eval = use_eval(cx);
let live = use_state(cx, || true);
Expand Down
52 changes: 46 additions & 6 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,63 @@ impl ProcManager {
//we use sum of proc resident memory to be consistent with proc display
let memory = self.procs.iter().map(|x| x.memory).sum();
//sometimes on macos cpu is nan
let mut cpu = self.system.global_cpu_info().cpu_usage();
if cpu.is_nan() {
cpu = 0.;
}
let cpus: Vec<f32> = self
.system
.cpus()
.iter()
.map(|cpu| cpu.cpu_usage())
.collect();
let cpu_count = cpus.len();
let cpu_total: f32 = cpus.iter().sum::<f32>().zero_if_nan();
let cpu_max: f32 = cpus
.iter()
.max_by(|a, b| a.total_cmp(&b))
.copied()
.unwrap_or(0.)
.zero_if_nan();
let cpu_avg = cpu_total / cpu_count as f32;

/*let proc_sum: f32 = self
.system
.processes()
.iter()
.map(|x| x.1.cpu_usage())
.sum();
println!("cpu: {cpu_total} proc:{proc_sum}");
*/

Totals {
memory,
cpu,
cpu_avg,
cpu_total,
cpu_count,
cpu_max,
uptime: self.system.uptime(),
memory_total: self.system.total_memory(),
}
}
}

trait NoNan {
fn zero_if_nan(self) -> Self;
}
impl NoNan for f32 {
fn zero_if_nan(self) -> Self {
if self.is_nan() {
0.
} else {
self
}
}
}

pub struct Totals {
pub memory: u64,
pub memory_total: u64,
pub cpu: f32,
pub cpu_avg: f32,
pub cpu_count: usize,
pub cpu_max: f32,
pub cpu_total: f32,
pub uptime: u64,
}

Expand Down
4 changes: 2 additions & 2 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ fn draw_top(f: &mut Frame<CrosstermBackend<Stdout>>, state: &State) {
f.render_widget(mem, Rect::new(0, 0, f.size().width, 1));

let gr = LineGauge::default()
.label(format!("Cpu: {:.2}%", totals.cpu))
.ratio(totals.cpu as f64 / 100.)
.label(format!("Cpu: {:.1}%", totals.cpu_avg))
.ratio(totals.cpu_avg as f64 / 100.)
.line_set(tui::symbols::line::THICK)
.gauge_style(Style::default().fg(Color::White).bg(Color::LightBlue));

Expand Down

0 comments on commit f35712e

Please sign in to comment.