maps: keep the nodeloc coordinate scratch uncompressed (#perf)
- Commit
b54e0473b8d22b77ba11a140f2ced7e7239d0263- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-10 21:06 UTC (25 days ago)
- Parents
51063d3
What: osm_ingest gains mem::mark_scratch_uncompressed(), a best-effort FS_IOC_SETFLAGS(FS_NOCOW_FL) on an empty scratch file (no-op off Linux, errors ignored), and nodeloc's mmap'd coordinate table calls it right after creating its temp file, before set_len. Why / root cause: the build box mounts btrfs compress-force=zstd:3, which compresses every 128 KiB extent whether or not it shrinks. The planet coordinate table is ~83 GB of high-entropy e7 pairs (compsize: 94%, i.e. no gain), read through a mapping at random 8-byte offsets. Every page fault that missed the page cache inflated a whole 128 KiB zstd extent: ~32x read amplification. On the Nov 10 planet build the lane-inheritance scan ran 6 min at 4-5 cores with ~190 threads in D state on exactly those faults, and materialise (30+ min) read through the same table. btrfs skips compression, even under compress-force, only for NOCOW inodes, and NOCOW only takes on an empty file. Bytes are identical either way: the flag changes only how the filesystem stores a scratch file the process deletes. NOCOW also drops data checksums, acceptable for a file deleted on drop. Verified: osm_ingest tests 300 passed; new marking_scratch_uncompressed_never_changes_the_bytes. California mamaps_build archive sha256 unchanged (94cb7eef...) vs frozen baseline.
Diff
scripts/maps/osm_ingest/src/mem.rs
diff --git a/scripts/maps/osm_ingest/src/mem.rs b/scripts/maps/osm_ingest/src/mem.rsindex 2a86b331d1..a2cc289ad1 100644--- a/scripts/maps/osm_ingest/src/mem.rs+++ b/scripts/maps/osm_ingest/src/mem.rs@@ -414,10 +414,79 @@ mod platform { } } +/// Ask the filesystem to store an EMPTY scratch file uncompressed. Best effort: a no-op+/// anywhere but Linux, and any refusal (not btrfs, flag unsupported, file already has+/// data) is ignored -- the file then behaves exactly as it did before.+///+/// Why this exists: the build box mounts btrfs with `compress-force=zstd:3`, which+/// compresses every extent in 128 KiB units regardless of whether the data shrinks. For+/// `nodeloc`'s 83 GB coordinate table (planet) the data does not shrink (`compsize`: 94%)+/// and the access is random 8-byte reads through a mapping, so every 4 KiB page fault that+/// misses the page cache inflates a whole 128 KiB extent: ~32x read amplification and a+/// zstd decode per fault. Measured on the Nov 10 planet build, the lane-inheritance scan+/// ran at 4-5 cores with ~190 threads in `D` state on those faults.+///+/// btrfs only skips compression (even under `compress-force`) for a NOCOW inode+/// (`FS_NOCOW_FL`, `chattr +C`), and NOCOW only takes on a file with no data yet -- so+/// call this right after `create`/`truncate`, before the first write. Scratch only: NOCOW+/// also drops data checksums, which is fine for a file the same process deletes.+///+/// Bytes read back are identical either way; this changes only how the filesystem stores+/// them.+pub fn mark_scratch_uncompressed(file: &std::fs::File) {+ #[cfg(target_os = "linux")]+ {+ use std::os::fd::AsRawFd;+ const FS_NOCOW_FL: libc::c_long = 0x0080_0000;+ let fd = file.as_raw_fd();+ // `c_long` per the ioctl's declared size; the kernel reads/writes the low int.+ let mut flags: libc::c_long = 0;+ // SAFETY: `fd` is a live descriptor owned by `file`; both ioctls only read or write+ // the flag word through the pointer, which outlives the calls.+ unsafe {+ if libc::ioctl(fd, libc::FS_IOC_GETFLAGS, &mut flags as *mut libc::c_long) != 0 {+ return;+ }+ if flags & FS_NOCOW_FL != 0 {+ return;+ }+ flags |= FS_NOCOW_FL;+ let _ = libc::ioctl(fd, libc::FS_IOC_SETFLAGS, &flags as *const libc::c_long);+ }+ }+ #[cfg(not(target_os = "linux"))]+ {+ let _ = file;+ }+}+ #[cfg(test)] mod tests { use super::*; + /// Best effort means it must never fail or disturb the file: whatever the filesystem+ /// under the temp dir, the file stays writable and reads back what was written.+ #[test]+ fn marking_scratch_uncompressed_never_changes_the_bytes() {+ use std::io::{Read, Seek, Write};+ let path = std::env::temp_dir().join(format!("osm_nocow_test_{}.bin", std::process::id()));+ let mut f = std::fs::OpenOptions::new()+ .read(true)+ .write(true)+ .create(true)+ .truncate(true)+ .open(&path)+ .unwrap();+ mark_scratch_uncompressed(&f);+ f.write_all(b"same bytes either way").unwrap();+ f.rewind().unwrap();+ let mut back = String::new();+ f.read_to_string(&mut back).unwrap();+ drop(f);+ let _ = std::fs::remove_file(&path);+ assert_eq!(back, "same bytes either way");+ }+ #[test] fn vm_hwm_is_read_in_kilobytes() { let status = "Name:\troad_graph\nVmPeak:\t 9999999 kB\nVmHWM:\t 5212672 kB\nVmRSS:\t 12 kB\n";
scripts/maps/osm_ingest/src/nodeloc.rs
diff --git a/scripts/maps/osm_ingest/src/nodeloc.rs b/scripts/maps/osm_ingest/src/nodeloc.rsindex 1d1770c8ac..d57db8c88d 100644--- a/scripts/maps/osm_ingest/src/nodeloc.rs+++ b/scripts/maps/osm_ingest/src/nodeloc.rs@@ -81,6 +81,10 @@ impl Locs { .truncate(true) .open(&path) .map_err(|e| Error(format!("cannot create {}: {e}", path.display())))?;+ // Uncompressed on a `compress-force` btrfs: random 8-byte reads through the mapping+ // must not inflate a 128 KiB extent per page fault, and coordinates do not compress+ // anyway. Must precede `set_len`/any data; see `mark_scratch_uncompressed`.+ crate::mem::mark_scratch_uncompressed(&file); // Sets the length without writing anything: on both platforms this leaves a sparse or // zero-filled file, so the 1.23 GB is not touched until a page is used. file.set_len(bytes)