diff options
author | Rupert Rutledge <eosis2 [at] gmail.com> | 2020-04-26 14:12:06 +0100 |
---|---|---|
committer | Rupert Rutledge <eosis2 [at] gmail.com> | 2020-04-26 14:12:06 +0100 |
commit | 1953c8bb8b7ec3a84e1b238421c0320f9e0b3391 (patch) | |
tree | 0fe8ea191ca7175a3346e5cbeb72a35980d91211 /src/main.rs | |
parent | 648eb0a66906035858794977cfaa2a03a75f0210 (diff) |
Working implementation of the elapsed time of the capture
This elapsed time pauses during pause sections and restarts on
resumption.
Ongoing:
- Consider a RwLock instead of a Mutex for the elapsed time and
cumulative time values, as these are only written by the UI thread
and read elsewhere.
- Combine the cumulative time and start times into a struct for
clarity.
- Separate out the elapsed time from the Bandwidth, as they are
separate considerations, leading to altering the rendering of the
header to write bandwidth and elapsed time separately.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index d91430a..50cdf16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,6 +121,8 @@ where { let running = Arc::new(AtomicBool::new(true)); let paused = Arc::new(AtomicBool::new(false)); + let last_start_time = Arc::new(Mutex::new(Instant::now())); + let cumulative_time = Arc::new(Mutex::new(std::time::Duration::new(0, 0))); let dns_shown = opts.show_dns; let mut active_threads = vec![]; @@ -148,7 +150,7 @@ where on_winch({ Box::new(move || { let mut ui = ui.lock().unwrap(); - ui.draw(paused.load(Ordering::SeqCst), dns_shown); + ui.draw(paused.load(Ordering::SeqCst), dns_shown, std::time::Duration::new(131, 0)); }) }); } @@ -163,6 +165,9 @@ where let running = running.clone(); let paused = paused.clone(); let network_utilization = network_utilization.clone(); + let last_start_time = last_start_time.clone(); + let cumulative_time = cumulative_time.clone(); + move || { while running.load(Ordering::Acquire) { let render_start_time = Instant::now(); @@ -188,10 +193,16 @@ where if !paused { ui.update_state(sockets_to_procs, utilization, ip_to_host); } + let elapsed_time = if paused { + cumulative_time.lock().unwrap().clone() + } else { + *cumulative_time.lock().unwrap() + last_start_time.lock().unwrap().elapsed() + }; + if raw_mode { ui.output_text(&mut write_to_stdout); } else { - ui.draw(paused, dns_shown); + ui.draw(paused, dns_shown, elapsed_time); } } let render_duration = render_start_time.elapsed(); @@ -223,7 +234,20 @@ where break; } Event::Key(Key::Char(' ')) => { - paused.fetch_xor(true, Ordering::SeqCst); + let restarting = paused.fetch_xor(true, Ordering::SeqCst); + if restarting { + *last_start_time.lock().unwrap() = Instant::now(); + } else { + let last_start_time_clone = { + last_start_time.lock().unwrap().clone() + }; + let current_cumulative_time_clone = { + cumulative_time.lock().unwrap().clone() + }; + let new_cumulative_time = current_cumulative_time_clone + last_start_time_clone.elapsed(); + *cumulative_time.lock().unwrap() = new_cumulative_time; + } + display_handler.unpark(); } _ => (), |