Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / progress.rs
progress.rs101 lines · 3.5 KiBRaw
| 1 | //! A small stderr progress meter for the build's single-threaded scans. |
| 2 | //! |
| 3 | //! The parallel PBF passes already print a percentage of their own through |
| 4 | //! [`crate::pbf::run_pass_sink`]. This covers the sequential loops *between* |
| 5 | //! those passes — the kept-node scan, the rank index, the Morton sort setup, the |
| 6 | //! two CSR scans, the component walk and each write round — which used to run for |
| 7 | //! minutes at planet scale with nothing on screen. |
| 8 | //! |
| 9 | //! It writes to stderr rather than stdout so the meter's carriage returns never |
| 10 | //! interleave with the summary lines the build prints to stdout, and so a run |
| 11 | //! whose stdout is captured to a file still shows live progress on a terminal. |
| 12 | |
| 13 | use std::io::Write; |
| 14 | use std::time::Instant; |
| 15 | |
| 16 | /// Items to advance between redraws. A redraw formats a line and writes it under |
| 17 | /// the stderr lock, so doing it per item would dominate a tight scan; once per |
| 18 | /// this many is invisible to the loop and still smooth on screen. Loops shorter |
| 19 | /// than this simply draw at their start and their `finish`. |
| 20 | const REDRAW_STEP: u64 = 1 << 16; |
| 21 | |
| 22 | /// A live `label [pct%] done/total elapsed, ETA` meter on one stderr line. |
| 23 | pub(crate) struct Progress { |
| 24 | label: String, |
| 25 | total: u64, |
| 26 | done: u64, |
| 27 | /// Next `done` value at which to redraw. |
| 28 | next: u64, |
| 29 | start: Instant, |
| 30 | } |
| 31 | |
| 32 | impl Progress { |
| 33 | /// Start a meter for a scan of `total` items and draw its 0% line. |
| 34 | pub(crate) fn new(label: impl Into<String>, total: u64) -> Progress { |
| 35 | let p = Progress { |
| 36 | label: label.into(), |
| 37 | total, |
| 38 | done: 0, |
| 39 | next: 0, |
| 40 | start: Instant::now(), |
| 41 | }; |
| 42 | p.draw(); |
| 43 | p |
| 44 | } |
| 45 | |
| 46 | /// Record one item done, redrawing at most once per [`REDRAW_STEP`]. |
| 47 | #[inline] |
| 48 | pub(crate) fn inc(&mut self) { |
| 49 | self.done += 1; |
| 50 | if self.done >= self.next { |
| 51 | self.next = self.done + REDRAW_STEP; |
| 52 | self.draw(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn draw(&self) { |
| 57 | let pct = (self.done * 100).checked_div(self.total).unwrap_or(100); |
| 58 | let elapsed = self.start.elapsed().as_secs_f64(); |
| 59 | // ETA extrapolates the average rate so far; meaningless until something is |
| 60 | // done, so hold it at zero for the opening frame. |
| 61 | let eta = if self.done == 0 || self.total == 0 { |
| 62 | 0.0 |
| 63 | } else { |
| 64 | elapsed * (self.total - self.done) as f64 / self.done as f64 |
| 65 | }; |
| 66 | let mut err = std::io::stderr().lock(); |
| 67 | // Trailing spaces clear a longer previous frame; `\r` keeps us on one line. |
| 68 | let _ = write!( |
| 69 | err, |
| 70 | "\r{:<28} [{pct:>3}%] {}/{} {} elapsed, {} ETA ", |
| 71 | self.label, |
| 72 | self.done, |
| 73 | self.total, |
| 74 | hms(elapsed), |
| 75 | hms(eta), |
| 76 | ); |
| 77 | let _ = err.flush(); |
| 78 | } |
| 79 | |
| 80 | /// Snap to 100% and drop to a fresh line, so the next stage's first line |
| 81 | /// starts clean instead of on top of the meter. |
| 82 | pub(crate) fn finish(mut self) { |
| 83 | self.done = self.total; |
| 84 | self.draw(); |
| 85 | let _ = writeln!(std::io::stderr().lock()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Whole seconds as `HhMMmSSs`, dropping units above the largest non-zero one. |
| 90 | /// Shared with `pbf.rs` so every live meter draws the one standard shape. |
| 91 | pub(crate) fn hms(secs: f64) -> String { |
| 92 | let s = secs.max(0.0) as u64; |
| 93 | let (h, m, s) = (s / 3600, (s % 3600) / 60, s % 60); |
| 94 | if h > 0 { |
| 95 | format!("{h}h{m:02}m{s:02}s") |
| 96 | } else if m > 0 { |
| 97 | format!("{m}m{s:02}s") |
| 98 | } else { |
| 99 | format!("{s}s") |
| 100 | } |
| 101 | } |