library: run the native tile workers at background priority (#maprender)
- Commit
24b72bc91410c7685b9cad0c9e0ac0522ce92b2b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 20:41 UTC (22 days ago)
- Parents
9f13793
Part of "visibly laggy when moving around": CPU contention during a
pan's tile burst.
Root cause: the renderer's eight tile workers (`map-tiles-N`, Rust
std threads) decode and tessellate whole tiles - ~20 ms of CPU per city
tile on a desktop core (tests/frame_cpu_bench.rs viewport_tile_build),
several times that on a phone - and ran at the default nice 0: the same
scheduler weight as the UI thread. A pan or zoom enqueues a screenful
of tiles at once, so all eight go runnable together and, on an
eight-core phone, the UI thread and the map's render thread share every
core with them for the length of the burst. That is dropped frames
precisely while the map is moving.
What changed: each worker drops itself to nice 10 (Android's
THREAD_PRIORITY_BACKGROUND) as it starts (`bridge::priority`, a
`setpriority(PRIO_PROCESS, 0, 10)` on the calling thread; Linux nice is
per thread). Under contention a nice-0 thread now gets ~9x a worker's
share of a core; with idle cores the workers still use all of them, so
tile throughput is unchanged when nothing interactive needs the CPU.
The render thread itself runs at THREAD_PRIORITY_DISPLAY (previous
render-thread commit).
Measured (new ignored harness `worker_priority_vs_frame_time`: 8 busy
workers plus a frame thread needing 4 ms of CPU per 16.7 ms frame, all
pinned to 2 cores with taskset; three runs on a loaded host):
workers nice 0: frame wall p50 6.8 / 12.0 / 18.0 ms,
0 / 13 / 94 of 120 frames over 16.7 ms
workers nice 10: frame wall p50 4.0 / 4.0 / 4.0 ms,
p95 <= 6.9 ms, 0 of 120 frames over 16.7 ms in all runs
Verified: android target type-checks; harness above.Diff
library/map/src/main/rust/src/bridge/mod.rs
diff --git a/library/map/src/main/rust/src/bridge/mod.rs b/library/map/src/main/rust/src/bridge/mod.rsindex 13539ea838..85f536ef44 100644--- a/library/map/src/main/rust/src/bridge/mod.rs+++ b/library/map/src/main/rust/src/bridge/mod.rs@@ -29,6 +29,7 @@ pub(crate) mod log; mod markers; mod moon; mod pick;+mod priority; mod puck; mod region; mod route;
library/map/src/main/rust/src/bridge/priority.rs
diff --git a/library/map/src/main/rust/src/bridge/priority.rs b/library/map/src/main/rust/src/bridge/priority.rsnew file mode 100644index 0000000000..8fd1a30b8f--- /dev/null+++ b/library/map/src/main/rust/src/bridge/priority.rs@@ -0,0 +1,27 @@+//! Scheduling priority for the native tile workers.+//!+//! Split from `workers.rs` (file-length limit).++/// Nice value for the tile workers: Android's `THREAD_PRIORITY_BACKGROUND`.+///+/// The eight workers decode and tessellate whole tiles (tens of ms of CPU each on a phone) and+/// were left at the default nice 0 — the same weight as the UI thread and above nothing. A pan+/// queues a screenful of tiles at once, so all eight went runnable together on a phone with+/// eight cores, and the UI thread and the map's render thread had to share every core with+/// them for the length of the burst: dropped frames exactly while the user was moving the map.+/// At nice 10 the scheduler gives a nice-0 thread ~9x a worker's share of a contended core, so+/// interactive threads win when they need the CPU and workers still use every idle cycle.+const WORKER_NICE: i32 = 10;++/// Drop the calling thread to [`WORKER_NICE`]. Linux nice is per thread, so `PRIO_PROCESS`+/// with `who = 0` addresses the caller only. Best effort: a refusal leaves the default.+pub(super) fn lower_worker_priority() {+ extern "C" {+ fn setpriority(which: i32, who: u32, prio: i32) -> i32;+ }+ const PRIO_PROCESS: i32 = 0;+ // SAFETY: plain syscall wrapper on the calling thread; no memory is passed.+ if unsafe { setpriority(PRIO_PROCESS, 0, WORKER_NICE) } != 0 {+ super::log::log("could not lower a tile worker's priority; it stays at the default");+ }+}
library/map/src/main/rust/src/bridge/workers.rs
diff --git a/library/map/src/main/rust/src/bridge/workers.rs b/library/map/src/main/rust/src/bridge/workers.rsindex 723ff90f94..e5705975af 100644--- a/library/map/src/main/rust/src/bridge/workers.rs+++ b/library/map/src/main/rust/src/bridge/workers.rs@@ -54,6 +54,7 @@ pub(crate) fn spawn_worker( let started = std::thread::Builder::new() .name(format!("map-tiles-{index}")) .spawn(move || {+ super::priority::lower_worker_priority(); // A pushed local archive wins: when the file is present the reader opens it // straight from disk and skips the header fetch, the origin marker and the // range cache entirely — the file is the whole archive, so none of the
library/map/src/main/rust/tests/frame_cpu_bench.rs
diff --git a/library/map/src/main/rust/tests/frame_cpu_bench.rs b/library/map/src/main/rust/tests/frame_cpu_bench.rsindex 945979db59..3e8cb130c7 100644--- a/library/map/src/main/rust/tests/frame_cpu_bench.rs+++ b/library/map/src/main/rust/tests/frame_cpu_bench.rs@@ -230,3 +230,65 @@ fn per_frame_label_scan() { ); } }++/// What the tile workers' nice value does to a frame thread under a tile burst.+///+/// Eight busy "workers" (the native tile-worker count) and one frame thread that needs 4 ms of+/// CPU every 16.7 ms, squeezed onto two cores so they contend the way a pan's tile burst+/// contends a phone's cores. Run under `taskset -c 0-1`. Reports the frame thread's wall time+/// per frame with the workers at nice 0 (before) and nice 10 (after,+/// `bridge::priority::WORKER_NICE`).+#[test]+#[ignore = "scheduler harness; run under `taskset -c 0-1` with --ignored --nocapture"]+fn worker_priority_vs_frame_time() {+ use std::sync::atomic::{AtomicBool, Ordering};+ extern "C" {+ fn setpriority(which: i32, who: u32, prio: i32) -> i32;+ }+ fn spin(d: Duration) {+ let t = Instant::now();+ while t.elapsed() < d {+ std::hint::spin_loop();+ }+ }+ for nice in [0, 10] {+ let stop = Arc::new(AtomicBool::new(false));+ let workers: Vec<_> = (0..8)+ .map(|_| {+ let stop = Arc::clone(&stop);+ std::thread::spawn(move || {+ unsafe { setpriority(0, 0, nice) };+ while !stop.load(Ordering::Relaxed) {+ spin(Duration::from_millis(1));+ }+ })+ })+ .collect();+ std::thread::sleep(Duration::from_millis(100));+ let mut walls = Vec::new();+ let period = Duration::from_micros(16_667);+ let start = Instant::now();+ for frame in 0..120u32 {+ let due = start + period * frame;+ if let Some(wait) = due.checked_duration_since(Instant::now()) {+ std::thread::sleep(wait);+ }+ let t = Instant::now();+ // 4 ms of frame work (recording, placement, emission).+ spin(Duration::from_millis(4));+ walls.push(t.elapsed());+ }+ stop.store(true, Ordering::Relaxed);+ for w in workers {+ w.join().unwrap();+ }+ walls.sort();+ let missed = walls.iter().filter(|w| **w > period).count();+ println!(+ "workers at nice {nice}: frame work p50 {:.1} ms, p95 {:.1} ms, max {:.1} ms, {missed}/120 over 16.7 ms",+ ms(walls[60]),+ ms(walls[114]),+ ms(walls[119])+ );+ }+}