Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / par.rs
par.rs100 lines · 3.7 KiBRaw
| 1 | //! The thread budget. |
| 2 | //! |
| 3 | //! Spelled the same way `osm_ingest::par` and `tile_build::par` spell it, so one |
| 4 | //! `MAPS_THREADS` export governs a whole build and the fan-out in `build_all.sh` |
| 5 | //! does not oversubscribe the box. `available_parallelism()` reports the whole |
| 6 | //! machine to every process, so six concurrent tools each claiming 32 threads on a |
| 7 | //! 32-core box finish slower than running in sequence. |
| 8 | //! |
| 9 | //! Unlike the other two crates this one has **no rayon**, and deliberately: this |
| 10 | //! crate's documented property is zero external dependencies, and its parallel work |
| 11 | //! is a handful of whole GTFS feeds per batch rather than millions of tiny items. |
| 12 | //! One `std::thread::scope` per batch is all that shape needs; work stealing would |
| 13 | //! buy nothing. |
| 14 | |
| 15 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 16 | |
| 17 | /// 0 means "not set"; any other value is an explicit cap from `--threads`. |
| 18 | static OVERRIDE: AtomicUsize = AtomicUsize::new(0); |
| 19 | |
| 20 | /// Cap the pool for the rest of the process. Called from `main` after parsing. |
| 21 | pub fn set_threads(n: usize) { |
| 22 | OVERRIDE.store(n.max(1), Ordering::Relaxed); |
| 23 | } |
| 24 | |
| 25 | /// Drop the override again, restoring the `MAPS_THREADS`-or-the-box default. |
| 26 | /// |
| 27 | /// Not `#[cfg(test)]`: the binaries in this crate are separate compilation units, so |
| 28 | /// a test in `transit_stops` cannot see the library's test-only items. |
| 29 | pub fn clear_threads() { |
| 30 | OVERRIDE.store(0, Ordering::Relaxed); |
| 31 | } |
| 32 | |
| 33 | /// Threads to use. `--threads` beats `MAPS_THREADS` beats the box. |
| 34 | pub fn threads() -> usize { |
| 35 | resolve(OVERRIDE.load(Ordering::Relaxed), std::env::var("MAPS_THREADS").ok().as_deref()) |
| 36 | } |
| 37 | |
| 38 | /// The precedence rule, split out so it can be tested without mutating the |
| 39 | /// process-wide override or the environment. |
| 40 | fn resolve(override_n: usize, env: Option<&str>) -> usize { |
| 41 | if override_n > 0 { |
| 42 | return override_n; |
| 43 | } |
| 44 | if let Some(n) = env_threads(env) { |
| 45 | return n; |
| 46 | } |
| 47 | std::thread::available_parallelism().map(|n| n.get()).unwrap_or(4) |
| 48 | } |
| 49 | |
| 50 | /// A malformed or non-positive `MAPS_THREADS` is ignored rather than fatal: it is |
| 51 | /// usually exported once for a whole script run, and killing an eight-hour build |
| 52 | /// over a typo in an environment variable serves nobody. |
| 53 | fn env_threads(raw: Option<&str>) -> Option<usize> { |
| 54 | static WARNED: std::sync::Once = std::sync::Once::new(); |
| 55 | let trimmed = raw?.trim(); |
| 56 | match trimmed.parse::<usize>() { |
| 57 | Ok(n) if n > 0 => Some(n), |
| 58 | _ => { |
| 59 | WARNED.call_once(|| { |
| 60 | eprintln!("WARN: ignoring MAPS_THREADS='{trimmed}' (wants a positive count)"); |
| 61 | }); |
| 62 | None |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// Parse a `--threads N` value. |
| 68 | pub fn parse_threads(value: &str) -> std::result::Result<usize, String> { |
| 69 | value |
| 70 | .parse::<usize>() |
| 71 | .ok() |
| 72 | .filter(|n| *n > 0) |
| 73 | .ok_or_else(|| format!("--threads wants a positive count, not {value}")) |
| 74 | } |
| 75 | |
| 76 | #[cfg(test)] |
| 77 | mod tests { |
| 78 | use super::*; |
| 79 | |
| 80 | #[test] |
| 81 | fn parse_threads_rejects_zero_and_junk() { |
| 82 | assert_eq!(parse_threads("8"), Ok(8)); |
| 83 | assert!(parse_threads("0").is_err()); |
| 84 | assert!(parse_threads("-1").is_err()); |
| 85 | assert!(parse_threads("many").is_err()); |
| 86 | assert!(parse_threads("").is_err()); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn the_flag_beats_the_env_beats_the_box() { |
| 91 | let box_threads = resolve(0, None); |
| 92 | assert_eq!(resolve(6, Some("2")), 6, "--threads wins"); |
| 93 | assert_eq!(resolve(0, Some("2")), 2, "MAPS_THREADS wins over the box"); |
| 94 | assert_eq!(resolve(6, None), 6); |
| 95 | for junk in ["0", "many", "", "-4"] { |
| 96 | assert_eq!(resolve(0, Some(junk)), box_threads, "MAPS_THREADS={junk}"); |
| 97 | } |
| 98 | assert_eq!(resolve(0, Some(" 3 ")), 3, "surrounding space is tolerated"); |
| 99 | } |
| 100 | } |