Skip to content

Commit

Permalink
Reduce energy usage of gping
Browse files Browse the repository at this point in the history
  • Loading branch information
orf committed Jan 27, 2023
1 parent e150797 commit d255d84
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.idea/
36 changes: 18 additions & 18 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ members = [
]

[profile.release]
# From https://github.com/johnthagen/min-sized-rust
lto = true
# Optimize for size
opt-level = "z"
codegen-units = 1
strip = "debuginfo"
74 changes: 48 additions & 26 deletions gping/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::plot_data::PlotData;
use anyhow::{anyhow, Result};
use chrono::prelude::*;
use clap::Parser;
use crossterm::event::{KeyEvent, KeyModifiers};
use crossterm::event::KeyModifiers;
use crossterm::{
event::{self, Event as CEvent, KeyCode},
execute,
Expand All @@ -11,6 +11,7 @@ use crossterm::{
use dns_lookup::lookup_host;
use pinger::{ping_with_interval, PingResult};
use std::io;
use std::io::BufWriter;
use std::iter;
use std::net::IpAddr;
use std::ops::Add;
Expand All @@ -19,7 +20,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Sender;
use std::sync::{mpsc, Arc};
use std::thread;
use std::thread::JoinHandle;
use std::thread::{sleep, JoinHandle};
use std::time::{Duration, Instant};
use tui::backend::{Backend, CrosstermBackend};
use tui::layout::{Constraint, Direction, Layout};
Expand Down Expand Up @@ -224,7 +225,21 @@ impl From<PingResult> for Update {
#[derive(Debug)]
enum Event {
Update(usize, Update),
Input(KeyEvent),
Terminate,
Render,
}

fn start_render_thread(
kill_event: Arc<AtomicBool>,
cmd_tx: Sender<Event>,
) -> JoinHandle<Result<()>> {
thread::spawn(move || {
while !kill_event.load(Ordering::Acquire) {
sleep(Duration::from_millis(250));
cmd_tx.send(Event::Render)?;
}
Ok(())
})
}

fn start_cmd_thread(
Expand Down Expand Up @@ -260,7 +275,7 @@ fn start_cmd_thread(
Update::Timeout
};
cmd_tx.send(Event::Update(host_id, update))?;
thread::sleep(interval);
sleep(interval);
}
Ok(())
})
Expand Down Expand Up @@ -358,7 +373,7 @@ fn main() -> Result<()> {

let mut threads = vec![];

let killed = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let killed = Arc::new(AtomicBool::new(false));

for (host_id, host_or_cmd) in hosts_or_commands.iter().cloned().enumerate() {
if args.cmd {
Expand All @@ -381,11 +396,15 @@ fn main() -> Result<()> {
)?);
}
}
threads.push(start_render_thread(
std::sync::Arc::clone(&killed),
key_tx.clone(),
));

let mut app = App::new(data, args.buffer);
enable_raw_mode()?;
let stdout = io::stdout();
let mut backend = CrosstermBackend::new(stdout);
let mut backend = CrosstermBackend::new(BufWriter::with_capacity(1024 * 1024 * 4, stdout));
let rect = backend.size()?;
execute!(backend, SetSize(rect.width, rect.height),)?;

Expand All @@ -394,17 +413,26 @@ fn main() -> Result<()> {

// Pump keyboard messages into the queue
let killed_thread = std::sync::Arc::clone(&killed);
let key_thread = thread::spawn(move || -> Result<()> {
thread::spawn(move || -> Result<()> {
while !killed_thread.load(Ordering::Acquire) {
if event::poll(Duration::from_millis(100))? {
if event::poll(Duration::from_secs(5))? {
if let CEvent::Key(key) = event::read()? {
key_tx.send(Event::Input(key))?;
match key.code {
KeyCode::Char('q') | KeyCode::Esc => {
key_tx.send(Event::Terminate)?;
break;
}
KeyCode::Char('c') if key.modifiers == KeyModifiers::CONTROL => {
key_tx.send(Event::Terminate)?;
break;
}
_ => {}
}
}
}
}
Ok(())
});
threads.push(key_thread);

loop {
match rx.recv()? {
Expand All @@ -421,8 +449,9 @@ fn main() -> Result<()> {
break;
}
};
}
Event::Render => {
terminal.draw(|f| {
// Split our
let chunks = Layout::default()
.direction(Direction::Vertical)
.vertical_margin(args.vertical_margin)
Expand All @@ -438,8 +467,8 @@ fn main() -> Result<()> {

let total_chunks = chunks.len();

let header_chunks = chunks[0..total_chunks - 1].to_owned();
let chart_chunk = chunks[total_chunks - 1].to_owned();
let header_chunks = &chunks[0..total_chunks - 1];
let chart_chunk = &chunks[total_chunks - 1];

for (plot_data, chunk) in app.data.iter().zip(header_chunks) {
let header_layout = Layout::default()
Expand All @@ -457,7 +486,7 @@ fn main() -> Result<()> {
]
.as_ref(),
)
.split(chunk);
.split(*chunk);

for (area, paragraph) in
header_layout.into_iter().zip(plot_data.header_stats())
Expand Down Expand Up @@ -486,20 +515,13 @@ fn main() -> Result<()> {
.labels(app.y_axis_labels(y_axis_bounds)),
);

f.render_widget(chart, chart_chunk)
f.render_widget(chart, *chart_chunk)
})?;
}
Event::Input(input) => match input.code {
KeyCode::Char('q') | KeyCode::Esc => {
killed.store(true, Ordering::Release);
break;
}
KeyCode::Char('c') if input.modifiers == KeyModifiers::CONTROL => {
killed.store(true, Ordering::Release);
break;
}
_ => {}
},
Event::Terminate => {
killed.store(true, Ordering::Release);
break;
}
}
}
killed.store(true, Ordering::Relaxed);
Expand Down

0 comments on commit d255d84

Please sign in to comment.