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

Modern-Apps.git / scripts / networklocation / geodb_build / src / progress.rs

progress.rs124 lines · 3.7 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
6use std::io::Write;
7use std::sync::atomic::{AtomicU64, Ordering};
8use std::time::Instant;
9
10/// Advance this many items between redraws; drawing per item would dominate a tight loop.
11const REDRAW_STEP: u64 = 1 << 16;
12
13/// A live meter on one stderr line.
14pub struct Progress {
15 label: String,
16 total: u64,
17 done: AtomicU64,
18 step: u64,
19 start: Instant,
20}
21
22impl 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 { label: label.into(), total, done: AtomicU64::new(0), step, start: Instant::now() };
42 p.draw(0);
43 p
44 }
45
46 /// Record one item done.
47 #[inline]
48 pub fn inc(&self) {
49 self.add(1);
50 }
51
52 /// Record `n` items done. Safe to call from several threads at once.
53 pub fn add(&self, n: u64) {
54 let before = self.done.fetch_add(n, Ordering::Relaxed);
55 let after = before + n;
56 if before / self.step != after / self.step {
57 self.draw(after);
58 }
59 }
60
61 fn draw(&self, done: u64) {
62 let done = done.min(self.total);
63 let pct = (done * 100).checked_div(self.total).unwrap_or(100);
64 let elapsed = self.start.elapsed().as_secs_f64();
65 let eta = if done == 0 || self.total == 0 {
66 0.0
67 } else {
68 elapsed * (self.total - done) as f64 / done as f64
69 };
70 let mut err = std::io::stderr().lock();
71 let _ = write!(
72 err,
73 "\r{:<28} [{pct:>3}%] {done}/{} {} elapsed, {} ETA ",
74 self.label,
75 self.total,
76 hms(elapsed),
77 hms(eta),
78 );
79 let _ = err.flush();
80 }
81
82 /// Snap to 100% and move to a fresh line for the next step.
83 pub fn finish(self) {
84 self.draw(self.total);
85 let _ = writeln!(std::io::stderr().lock());
86 }
87}
88
89/// Whole seconds as `HhMMmSSs`, dropping units above the largest non-zero one.
90fn hms(secs: f64) -> String {
91 let s = secs.max(0.0) as u64;
92 let (h, m, s) = (s / 3600, (s % 3600) / 60, s % 60);
93 if h > 0 {
94 format!("{h}h{m:02}m{s:02}s")
95 } else if m > 0 {
96 format!("{m}m{s:02}s")
97 } else {
98 format!("{s}s")
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn counts_from_many_threads_add_up() {
108 let p = Progress::new("test", 4 * 10_000);
109 std::thread::scope(|s| {
110 for _ in 0..4 {
111 s.spawn(|| (0..10_000).for_each(|_| p.inc()));
112 }
113 });
114 assert_eq!(p.done.load(Ordering::Relaxed), 40_000);
115 p.finish();
116 }
117
118 #[test]
119 fn durations_are_compact() {
120 assert_eq!(hms(5.0), "5s");
121 assert_eq!(hms(65.0), "1m05s");
122 assert_eq!(hms(3725.0), "1h02m05s");
123 }
124}