Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / scripts / maps / osm_ingest / src / progress.rs

progress.rs106 lines · 3.7 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
13use std::io::Write;
14use 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`.
20const REDRAW_STEP: u64 = 1 << 16;
21
22/// A live `label [pct%] done/total elapsed, ETA` meter on one stderr line.
23pub(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
32impl 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 { label: label.into(), total, done: 0, next: 0, start: Instant::now() };
36 p.draw();
37 p
38 }
39
40 /// Record one item done, redrawing at most once per [`REDRAW_STEP`].
41 #[inline]
42 pub(crate) fn inc(&mut self) {
43 self.done += 1;
44 if self.done >= self.next {
45 self.next = self.done + REDRAW_STEP;
46 self.draw();
47 }
48 }
49
50 /// Record `n` items done at once (a parallel stage's batch), redrawing at
51 /// most once per [`REDRAW_STEP`].
52 #[inline]
53 pub(crate) fn add(&mut self, n: u64) {
54 self.done += n;
55 if self.done >= self.next {
56 self.next = self.done + REDRAW_STEP;
57 self.draw();
58 }
59 }
60
61 fn draw(&self) {
62 let pct = (self.done * 100).checked_div(self.total).unwrap_or(100);
63 let elapsed = self.start.elapsed().as_secs_f64();
64 // ETA extrapolates the average rate so far; meaningless until something is
65 // done, so hold it at zero for the opening frame.
66 let eta = if self.done == 0 || self.total == 0 {
67 0.0
68 } else {
69 elapsed * (self.total - self.done) as f64 / self.done as f64
70 };
71 let mut err = std::io::stderr().lock();
72 // Trailing spaces clear a longer previous frame; `\r` keeps us on one line.
73 let _ = write!(
74 err,
75 "\r{:<28} [{pct:>3}%] {}/{} {} elapsed, {} ETA ",
76 self.label,
77 self.done,
78 self.total,
79 hms(elapsed),
80 hms(eta),
81 );
82 let _ = err.flush();
83 }
84
85 /// Snap to 100% and drop to a fresh line, so the next stage's first line
86 /// starts clean instead of on top of the meter.
87 pub(crate) fn finish(mut self) {
88 self.done = self.total;
89 self.draw();
90 let _ = writeln!(std::io::stderr().lock());
91 }
92}
93
94/// Whole seconds as `HhMMmSSs`, dropping units above the largest non-zero one.
95/// Shared with `pbf.rs` so every live meter draws the one standard shape.
96pub(crate) 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}