Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / geodb_build / src / progress.rs
progress.rs130 lines · 3.8 KiBRaw
| 1 | //! A stderr progress meter for the steps `osm_ingest` does not cover: materializing ways and |
| 2 | //! relations, and everything in `write` and `verify`. Draws the same |
| 3 | //! `label [pct%] done/total elapsed, ETA` line as the extraction passes, and can be advanced |
| 4 | //! from worker threads, which the parallel column compression needs. |
| 5 | |
| 6 | use std::io::Write; |
| 7 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 8 | use std::time::Instant; |
| 9 | |
| 10 | /// Advance this many items between redraws; drawing per item would dominate a tight loop. |
| 11 | const REDRAW_STEP: u64 = 1 << 16; |
| 12 | |
| 13 | /// A live meter on one stderr line. |
| 14 | pub struct Progress { |
| 15 | label: String, |
| 16 | total: u64, |
| 17 | done: AtomicU64, |
| 18 | step: u64, |
| 19 | start: Instant, |
| 20 | } |
| 21 | |
| 22 | impl Progress { |
| 23 | /// Start a meter over `total` items and draw its 0% line. |
| 24 | pub fn new(label: impl Into<String>, total: u64) -> Progress { |
| 25 | Progress::with_step(label, total, REDRAW_STEP) |
| 26 | } |
| 27 | |
| 28 | /// A meter whose items are each slow (a compressed block, a dictionary), so every one |
| 29 | /// redraws. |
| 30 | pub fn coarse(label: impl Into<String>, total: u64) -> Progress { |
| 31 | Progress::with_step(label, total, 1) |
| 32 | } |
| 33 | |
| 34 | /// A step with no measurable progress, such as a single sort: shows the label and elapsed |
| 35 | /// time while it runs, then 100% on [`Progress::finish`]. |
| 36 | pub fn stage(label: impl Into<String>) -> Progress { |
| 37 | Progress::coarse(label, 1) |
| 38 | } |
| 39 | |
| 40 | fn with_step(label: impl Into<String>, total: u64, step: u64) -> Progress { |
| 41 | let p = Progress { |
| 42 | label: label.into(), |
| 43 | total, |
| 44 | done: AtomicU64::new(0), |
| 45 | step, |
| 46 | start: Instant::now(), |
| 47 | }; |
| 48 | p.draw(0); |
| 49 | p |
| 50 | } |
| 51 | |
| 52 | /// Record one item done. |
| 53 | #[inline] |
| 54 | pub fn inc(&self) { |
| 55 | self.add(1); |
| 56 | } |
| 57 | |
| 58 | /// Record `n` items done. Safe to call from several threads at once. |
| 59 | pub fn add(&self, n: u64) { |
| 60 | let before = self.done.fetch_add(n, Ordering::Relaxed); |
| 61 | let after = before + n; |
| 62 | if before / self.step != after / self.step { |
| 63 | self.draw(after); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn draw(&self, done: u64) { |
| 68 | let done = done.min(self.total); |
| 69 | let pct = (done * 100).checked_div(self.total).unwrap_or(100); |
| 70 | let elapsed = self.start.elapsed().as_secs_f64(); |
| 71 | let eta = if done == 0 || self.total == 0 { |
| 72 | 0.0 |
| 73 | } else { |
| 74 | elapsed * (self.total - done) as f64 / done as f64 |
| 75 | }; |
| 76 | let mut err = std::io::stderr().lock(); |
| 77 | let _ = write!( |
| 78 | err, |
| 79 | "\r{:<28} [{pct:>3}%] {done}/{} {} elapsed, {} ETA ", |
| 80 | self.label, |
| 81 | self.total, |
| 82 | hms(elapsed), |
| 83 | hms(eta), |
| 84 | ); |
| 85 | let _ = err.flush(); |
| 86 | } |
| 87 | |
| 88 | /// Snap to 100% and move to a fresh line for the next step. |
| 89 | pub fn finish(self) { |
| 90 | self.draw(self.total); |
| 91 | let _ = writeln!(std::io::stderr().lock()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Whole seconds as `HhMMmSSs`, dropping units above the largest non-zero one. |
| 96 | fn hms(secs: f64) -> String { |
| 97 | let s = secs.max(0.0) as u64; |
| 98 | let (h, m, s) = (s / 3600, (s % 3600) / 60, s % 60); |
| 99 | if h > 0 { |
| 100 | format!("{h}h{m:02}m{s:02}s") |
| 101 | } else if m > 0 { |
| 102 | format!("{m}m{s:02}s") |
| 103 | } else { |
| 104 | format!("{s}s") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | mod tests { |
| 110 | use super::*; |
| 111 | |
| 112 | #[test] |
| 113 | fn counts_from_many_threads_add_up() { |
| 114 | let p = Progress::new("test", 4 * 10_000); |
| 115 | std::thread::scope(|s| { |
| 116 | for _ in 0..4 { |
| 117 | s.spawn(|| (0..10_000).for_each(|_| p.inc())); |
| 118 | } |
| 119 | }); |
| 120 | assert_eq!(p.done.load(Ordering::Relaxed), 40_000); |
| 121 | p.finish(); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn durations_are_compact() { |
| 126 | assert_eq!(hms(5.0), "5s"); |
| 127 | assert_eq!(hms(65.0), "1m05s"); |
| 128 | assert_eq!(hms(3725.0), "1h02m05s"); |
| 129 | } |
| 130 | } |