Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / planet_build.sh
planet_build.sh638 lines · 32.7 KiBRaw
| 1 | #!/bin/bash |
| 2 | # planet_build.sh — Linux port of scripts/maps/build_graph.ps1 (world region). |
| 3 | # Faithful phase order/flags; Windows-only bits (Tee-Object bars, RAM sampler) |
| 4 | # replaced with plain per-phase logs. Survives session end: run as a |
| 5 | # systemd --user unit (see launch notes in ~/planet-build-logs/). |
| 6 | # |
| 7 | # LOCATION + ENVIRONMENT (2026-11-10): moved here from ~/planet-build/ on |
| 8 | # devvm24805 (which keeps a thin wrapper that execs this file). Defaults are |
| 9 | # exactly the devvm behaviour; a rented build box (location_share_server |
| 10 | # scripts/vast/jobs/planet.sh) overrides them: |
| 11 | # PB_REPO Modern-Apps checkout (default: the tree this file is in) |
| 12 | # PB_GO_FILE POI go-file (default: $HOME/planet-build/poi_go) |
| 13 | # PB_LOGDIR master/phase logs (default: $HOME/planet-build-logs) |
| 14 | # PB_DISK_PATH filesystem the disk gates check (default: /) |
| 15 | # PB_OUT the archive to write (default: $IN/world.mamaps; its .buildtmp beside it) |
| 16 | # |
| 17 | # PLANET5 (2026-11-14, owner: "balanced with planet5 for 60 GB"; see the planet-size report): |
| 18 | # mamaps_build runs with the "balanced" size flags; after tiles + POI the build converts the |
| 19 | # three big inputs to the planet5 formats before packing -- graph_thin (1 m DP, +-25 m junction |
| 20 | # zone, graph v8 varint shape points), transit_planet5 (TRIX v8: ZSEC sections, ROUTE_TRIPS |
| 21 | # dropped, shapes thinned at 1 m) and mamaps_planet5 (v10 header: dictionary-zstd v11 bodies). |
| 22 | # The graph's ZBLK blocks are zstd-19 straight out of road_graph (tilecodec BLOCK_LEVEL). |
| 23 | # mamaps_pack stores the POI attrs (kind 10) and region (kind 15) sidecars as DEFLATE block |
| 24 | # sections (--poi-zsec 10,15), which the app inflates per lookup with java.util.zip. |
| 25 | # Memory admission reads MemAvailable, capped by the cgroup's own headroom when |
| 26 | # the process runs under a memory limit below the host's RAM (containers, vast |
| 27 | # slices); with no such limit (devvm) it is plain MemAvailable as before. |
| 28 | # |
| 29 | # POI HOLD: poi_extract waits for the go-file $GO before it runs. |
| 30 | # A sibling agent is stamping each POI-index record with its containing admin |
| 31 | # boundary id; do NOT create $GO until that fix has landed in |
| 32 | # scripts/maps/osm_ingest/src (or the sibling confirms). Everything else runs. |
| 33 | # |
| 34 | # OVERLAP + MEMORY BUDGET (2026-11-10, round 3) |
| 35 | # --------------------------------------------- |
| 36 | # Only road_graph (phase 2) feeds mamaps_build's graph-reading stages, so |
| 37 | # everything else may run beside it: transit shaping (needs world.transit), |
| 38 | # the POI sidecars (planet.osm.pbf + reviews), and -- when mamaps_build has |
| 39 | # --graph-ready-file -- mamaps_build's own passes 1-3 / lane / materialise, |
| 40 | # which then block on $GRAPH/.ready until road_graph + road_overlay succeed. |
| 41 | # |
| 42 | # Overlap is memory-admitted, never assumed. Budgets are ANONYMOUS memory |
| 43 | # (RssAnon), because that is what cannot be reclaimed; page cache is left to |
| 44 | # the kernel. A background tool starts only when |
| 45 | # MemAvailable - sum(budget - RssAnon of tools already running) |
| 46 | # >= its budget + MEM_HEADROOM_GB |
| 47 | # checked under one lock, so two admissions cannot both spend the same |
| 48 | # headroom; a tool that does not fit waits (polls). Measured planet peaks |
| 49 | # (GiB, Nov 10): |
| 50 | # road_graph 107.0 Peak RSS in graph.log; budgeted as all anonymous |
| 51 | # transit_shapes 16.4 sampled |
| 52 | # poi_extract 40.3 sampled planet run, all anonymous (write stage) |
| 53 | # mamaps_build ~56 anonymous (pass 1/3; tiling <= ~35), PLUS the |
| 54 | # ~83 GB file-backed nodeloc map that lane-inherit |
| 55 | # and materialise read at random |
| 56 | # |
| 57 | # CGROUPS (2026-11-11, after fb-oomd killed the round-3 early mamaps_build): |
| 58 | # every tool runs in its own `systemd-run --user --scope` with |
| 59 | # MemorySwapMax=0 (the box has a 254 GB swapfile; never lean on it). |
| 60 | # * NO tool gets MemoryMax or MemoryHigh. Both count page cache, which is |
| 61 | # charged to the cgroup that first reads or writes it, and these tools |
| 62 | # stream the 95 GB planet PBF plus ~100+ GB of spill and mapped files. |
| 63 | # The round-3 MemoryHigh=65G on mamaps_build turned ordinary cache use |
| 64 | # into constant in-cgroup reclaim, which is a memory stall; user.slice |
| 65 | # PSI sat above 40% for 300 s and fb-oomd's "user session protection" |
| 66 | # rule (pressure_above user.slice 40/300s -> kill_by_memory_size_or_growth) |
| 67 | # killed the biggest cgroup, pb-mb (139.6 GB memory.current, mostly |
| 68 | # cache), 13 min in; pass 1 had also slowed 3m42s vs 1m05s. The Nov 11 |
| 69 | # confirmation run showed the same with a plain MemoryMax: pb-poi pinned |
| 70 | # at its 58G cap (33G anon + 24G PBF cache), 53% PSI inside it and |
| 71 | # user.slice PSI 51% within a minute. Unlimited, clean cache is reclaimed |
| 72 | # globally by kswapd and costs no stall. |
| 73 | # * Runaway protection is an in-script ANON guard instead: a tool whose |
| 74 | # RssAnon passes ANON_GUARD_PCT% of its budget is killed and the build |
| 75 | # fails. |
| 76 | # * road_graph and mamaps_build (the critical path) also get |
| 77 | # ManagedOOMPreference=avoid: systemd sets user.oomd_avoid=1 on the scope |
| 78 | # and fb-oomd-cpp honours it, so under pressure it picks another cgroup |
| 79 | # first. The anon guard keeps `avoid` from sheltering a runaway. |
| 80 | # A pressure log ($TMP/pressure.log, every 30 s) records user.slice PSI, |
| 81 | # MemAvailable and each scope's anon/file, so the next failure is diagnosable. |
| 82 | # |
| 83 | # EARLY mamaps_build (start it beside road_graph with --graph-ready-file) |
| 84 | # starts once the transit routes exist (it reads them before its graph gate; |
| 85 | # transit_shapes takes ~30 s) while road_graph still runs, and |
| 86 | # overlaps only its graph-independent, anonymous-only head: pass 1, corridors, |
| 87 | # member ways, refs-collect, id-index (~1.5 min alone on the planet, peak |
| 88 | # ~51 GB anon). It is FROZEN (cgroup freezer, `systemctl --user freeze`) the |
| 89 | # moment it reaches "Pass 3" (coords-resolve) if road_graph is still running, |
| 90 | # and thawed when the graph is ready. Pass 3 writes and lane/materialise then |
| 91 | # randomly re-read the 83 GB nodeloc map; beside road_graph's 107 GB that |
| 92 | # working set does not fit this ~235 GB box, so it would be evicted and every |
| 93 | # refault is a memory stall (PSI counts workingset refaults) -- the same |
| 94 | # fb-oomd rule, reached without any cgroup limit. Frozen tasks are not |
| 95 | # stalled, keep their (admitted) anon, and their page cache stays reclaimable. |
| 96 | # Admission is therefore road_graph + PEAK_MB_EARLY_GB (default PEAK_MB_GB, |
| 97 | # 60) + headroom; PB_MB_EARLY=0|1 forces it off/on (1 still honours the disk |
| 98 | # gate). PB_MB_FREEZE=0 lets it run on (only for a box with RAM for both |
| 99 | # working sets: >= ~300 GB). |
| 100 | # |
| 101 | # DISK: phase 4's measured peak is ~202 GB (tiles.features.tmp 98.5 + |
| 102 | # tilechunks 72.8 + bodies 31); road_graph's out dir peaks at ~3.6x its final |
| 103 | # 19 GB (~68 GB) while it runs. Overlapped, the worst case is graph (19) + |
| 104 | # phase-4 peak (202) + POI (7.2) ~= 228 GB, so early mamaps_build also needs |
| 105 | # >= 270 GiB free at phase-2 start; otherwise the build runs phase 4 after the |
| 106 | # graph as before, behind the unchanged 240 GiB phase-4 gate. |
| 107 | set -u |
| 108 | REPO=${PB_REPO:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)} |
| 109 | IN=$REPO/scripts/maps/inputs |
| 110 | OUT=${PB_OUT:-$IN/world.mamaps} |
| 111 | OUTDIR=$(dirname "$OUT") |
| 112 | OUTNAME=$(basename "$OUT") |
| 113 | TMP=$OUT.buildtmp |
| 114 | GRAPH=$TMP/graph |
| 115 | ROUTES=$TMP/transit-routes.geojsonseq |
| 116 | TILES=$TMP/tiles.mamaps |
| 117 | POI=$TMP/poi |
| 118 | WT=$IN/world.transit |
| 119 | GO=${PB_GO_FILE:-$HOME/planet-build/poi_go} |
| 120 | LOGDIR=${PB_LOGDIR:-$HOME/planet-build-logs} |
| 121 | DISK_PATH=${PB_DISK_PATH:-/} |
| 122 | LOG=$LOGDIR/planet-build.log |
| 123 | # Inline archive hash written by `mamaps_pack --sha256-out` (moved beside the |
| 124 | # archive only after the phase-7 header gate passes). |
| 125 | PACK_SHA=$TMP/$OUTNAME.sha256 |
| 126 | # mamaps_build --graph-ready-file: created only after road_graph + road_overlay. |
| 127 | READY=$GRAPH/.ready |
| 128 | # Running-tool registry for memory admission (one "pid budget_gib" per file). |
| 129 | JOBS=$TMP/.jobs |
| 130 | |
| 131 | # Memory budget (GiB). Override from the unit's environment if a box differs. |
| 132 | MEM_HEADROOM_GB=${MEM_HEADROOM_GB:-30} |
| 133 | PEAK_RG_GB=${PEAK_RG_GB:-110} |
| 134 | PEAK_TS_GB=${PEAK_TS_GB:-18} |
| 135 | PEAK_POI_GB=${PEAK_POI_GB:-44} |
| 136 | PEAK_MB_GB=${PEAK_MB_GB:-60} # anonymous |
| 137 | PEAK_MB_FILE_GB=${PEAK_MB_FILE_GB:-90} # nodeloc map working set (file-backed) |
| 138 | PEAK_MB_EARLY_GB=${PEAK_MB_EARLY_GB:-$PEAK_MB_GB} # its head is anon-only (EARLY above) |
| 139 | PB_MB_EARLY=${PB_MB_EARLY:-auto} # auto | 0 | 1 |
| 140 | PB_MB_FREEZE=${PB_MB_FREEZE:-1} # freeze early mamaps_build at Pass 3 while road_graph runs |
| 141 | ANON_GUARD_PCT=${ANON_GUARD_PCT:-150} # kill a tool whose RssAnon passes this % of its budget |
| 142 | PEAK_OVL_GB=${PEAK_OVL_GB:-16} |
| 143 | ADMIT_POLL_S=${ADMIT_POLL_S:-10} |
| 144 | # Disk gates (KiB): phase 4 alone after the graph, and phase 2+4 overlapped. |
| 145 | DISK_GATE_P4_KB=251658240 # 240 GiB |
| 146 | DISK_GATE_OVERLAP_KB=283115520 # 270 GiB |
| 147 | |
| 148 | export PATH=$HOME/.cargo/bin:$PATH |
| 149 | export RUSTUP_TOOLCHAIN=1.97.0-x86_64-unknown-linux-gnu |
| 150 | unset MAPS_THREADS RAYON_NUM_THREADS MAPS_PREFETCH_LANES |
| 151 | # Tile-chunk spill through its scratch file, never anonymous staging: |
| 152 | # anonymous staging is commit (= swap) backed, and every tool here runs with |
| 153 | # MemorySwapMax=0. Same chunks, same bytes either way; the planet already |
| 154 | # chose files on its own (642 GB needed vs ~196 GB of commit headroom). |
| 155 | export MAPS_ANON_SPILL=0 |
| 156 | |
| 157 | ts(){ date '+%F %T'; } |
| 158 | log(){ echo "[$(ts)] $*" | tee -a "$LOG"; } |
| 159 | BG_PIDS=() |
| 160 | # Fail fast: also stop any background job (and the tool it is running), so a |
| 161 | # failed phase never leaves a 192-thread tool behind. |
| 162 | die(){ |
| 163 | log "FATAL: $*" |
| 164 | for p in ${BG_PIDS[@]+"${BG_PIDS[@]}"}; do pkill -TERM -P "$p" 2>/dev/null; kill "$p" 2>/dev/null; done |
| 165 | # The tools themselves live in their scopes; stop those too (thawed first, |
| 166 | # so a frozen early mamaps_build can act on the TERM). |
| 167 | for u in $(systemctl --user list-units "pb-*-$$.scope" --no-legend --plain 2>/dev/null | awk '{print $1}'); do |
| 168 | systemctl --user thaw "$u" 2>/dev/null |
| 169 | systemctl --user kill --signal=TERM "$u" 2>/dev/null |
| 170 | done |
| 171 | [ -s "$JOBS/.mb-stopped" ] && kill -CONT "$(cat "$JOBS/.mb-stopped")" 2>/dev/null |
| 172 | exit 1 |
| 173 | } |
| 174 | # Inside a background job: report, then make the main line fail now (USR1) |
| 175 | # rather than at its next join -- a POI failure must not let a 20-minute |
| 176 | # mamaps_build run to completion first. |
| 177 | MAIN_PID=$$ |
| 178 | bgdie(){ log "FATAL: $*"; kill -USR1 "$MAIN_PID" 2>/dev/null; exit 1; } |
| 179 | trap 'die "a background job failed (see above)"' USR1 |
| 180 | |
| 181 | mem_avail_gb(){ |
| 182 | if [ -n "${PB_TEST_MEMAVAIL_GB:-}" ]; then echo "$PB_TEST_MEMAVAIL_GB"; return; fi |
| 183 | local host lim cur inact total cg |
| 184 | host=$(awk '/^MemAvailable:/{print int($2/1048576)}' /proc/meminfo) |
| 185 | lim=$(cat /sys/fs/cgroup/memory.max 2>/dev/null || echo max) |
| 186 | total=$(awk '/^MemTotal:/{print $2 * 1024}' /proc/meminfo) |
| 187 | # No cgroup limit, or one at/above the host's RAM: plain MemAvailable. |
| 188 | if [ "$lim" = max ] || [ "$lim" -ge "$total" ] 2>/dev/null; then echo "$host"; return; fi |
| 189 | # A real limit (container / vast slice; /proc/meminfo shows the HOST): |
| 190 | # headroom = limit - usage + reclaimable page cache, never above the host's. |
| 191 | cur=$(cat /sys/fs/cgroup/memory.current 2>/dev/null || echo 0) |
| 192 | inact=$(awk '/^inactive_file /{print $2}' /sys/fs/cgroup/memory.stat 2>/dev/null || echo 0) |
| 193 | cg=$(( (lim - cur + inact) / 1073741824 )) |
| 194 | echo $(( cg < host ? cg : host )) |
| 195 | } |
| 196 | SCOPE_ROOT=/sys/fs/cgroup/user.slice/user-$(id -u).slice/user@$(id -u).service/app.slice |
| 197 | # Anonymous memory (GiB) of tool NAME: its scope's memory.stat `anon` (covers |
| 198 | # every process in it, whatever PID systemd-run left us), else RssAnon of PID. |
| 199 | anon_gb(){ |
| 200 | local st=$SCOPE_ROOT/pb-$1-$$.scope/memory.stat |
| 201 | if [ -r "$st" ]; then |
| 202 | awk '/^anon /{print int($2/1073741824)}' "$st" |
| 203 | else |
| 204 | awk '/^RssAnon:/{print int($2/1048576)}' "/proc/$2/status" 2>/dev/null |
| 205 | fi |
| 206 | } |
| 207 | # Anonymous memory the running tools may still grow into: sum(budget - anon). |
| 208 | outstanding_gb(){ |
| 209 | local f pid budget anon sum=0 |
| 210 | for f in "$JOBS"/*; do |
| 211 | [ -f "$f" ] || continue |
| 212 | read -r pid budget < "$f" || continue |
| 213 | [ -d "/proc/$pid" ] || continue |
| 214 | anon=$(anon_gb "$(basename "$f")" "$pid") |
| 215 | [ "${budget:-0}" -gt "${anon:-0}" ] && sum=$((sum + budget - ${anon:-0})) |
| 216 | done |
| 217 | echo "$sum" |
| 218 | } |
| 219 | # GiB a new tool could have now without eating the headroom (may be negative). |
| 220 | spare_gb(){ echo $(( $(mem_avail_gb) - $(outstanding_gb) - MEM_HEADROOM_GB )); } |
| 221 | |
| 222 | SCOPES=0 |
| 223 | if systemd-run --user --scope --quiet -p MemoryMax=1G -p MemorySwapMax=0 -- true 2>/dev/null; then |
| 224 | SCOPES=1 |
| 225 | fi |
| 226 | |
| 227 | # run_tool NAME BUDGET_GB AVOID ADMIT -- cmd... |
| 228 | # Runs cmd in its own scope (when available) and waits for it. |
| 229 | # BUDGET_GB: anonymous peak (admission + anon guard). AVOID=1: ask fb-oomd to |
| 230 | # pick other cgroups first (critical-path tools). ADMIT=1: first wait until |
| 231 | # BUDGET_GB fits; ADMIT=try:N: start only if N GiB fit right now, else return |
| 232 | # 75 without starting. No memory caps (see CGROUPS above). The tool's PID and |
| 233 | # budget are registered for later admissions while it runs. |
| 234 | run_tool(){ |
| 235 | local name=$1 budget=$2 avoid=$3 admit=$4; shift 5 |
| 236 | local fd p rc spare anon guard i=0 tripped=0 props=() |
| 237 | exec {fd}>"$JOBS/.lock" |
| 238 | local waited=0 |
| 239 | while :; do |
| 240 | flock "$fd" |
| 241 | spare=$(spare_gb) |
| 242 | case $admit in |
| 243 | try:*) |
| 244 | [ "$spare" -ge "${admit#try:}" ] && break |
| 245 | flock -u "$fd"; exec {fd}>&- |
| 246 | log " $name not started: ${spare}G spare < ${admit#try:}G needed now (MemAvailable $(mem_avail_gb)G)" |
| 247 | return 75 ;; |
| 248 | esac |
| 249 | if [ "$admit" = 0 ] || [ "$spare" -ge "$budget" ]; then break; fi |
| 250 | # Nothing of ours left to wait for: the shortfall is other tenants, and |
| 251 | # running alone is what the sequential build always did. |
| 252 | if [ "$(outstanding_gb)" -eq 0 ]; then |
| 253 | log " WARN: $name needs ${budget}G + ${MEM_HEADROOM_GB}G headroom, only $((spare + MEM_HEADROOM_GB))G available and no other build tool running; starting it alone" |
| 254 | break |
| 255 | fi |
| 256 | [ "$waited" = 0 ] && log " $name waits for memory (needs ${budget}G, ${spare}G spare after headroom)" |
| 257 | waited=1 |
| 258 | flock -u "$fd" |
| 259 | sleep "$ADMIT_POLL_S" |
| 260 | done |
| 261 | if [ "$SCOPES" = 1 ]; then |
| 262 | props=(-p MemorySwapMax=0) |
| 263 | [ "$avoid" = 1 ] && props+=(-p ManagedOOMPreference=avoid) |
| 264 | systemd-run --user --scope --quiet --collect --unit="pb-$name-$$" "${props[@]}" -- "$@" {fd}>&- & |
| 265 | else |
| 266 | "$@" {fd}>&- & |
| 267 | fi |
| 268 | p=$! |
| 269 | echo "$p $budget" > "$JOBS/$name" |
| 270 | flock -u "$fd"; exec {fd}>&- |
| 271 | log " started $name (pid $p; anon budget ${budget}G, guard $(( budget * ANON_GUARD_PCT / 100 ))G, spare was ${spare}G, oomd avoid=$avoid, scopes=$SCOPES)" |
| 272 | # Anon guard (also the only runaway guard for the uncapped tools). |
| 273 | guard=$(( budget * ANON_GUARD_PCT / 100 )) |
| 274 | while kill -0 "$p" 2>/dev/null; do |
| 275 | sleep 1 |
| 276 | i=$((i + 1)); [ $((i % 5)) = 0 ] || continue |
| 277 | anon=$(anon_gb "$name" "$p") |
| 278 | if [ "${anon:-0}" -gt "$guard" ]; then |
| 279 | log " FATAL: $name RssAnon ${anon}G passed its guard ${guard}G (${ANON_GUARD_PCT}% of ${budget}G); killing it" |
| 280 | kill -TERM "$p" 2>/dev/null |
| 281 | [ "$SCOPES" = 1 ] && systemctl --user kill --signal=TERM "pb-$name-$$.scope" 2>/dev/null |
| 282 | sleep 10 |
| 283 | kill -KILL "$p" 2>/dev/null |
| 284 | [ "$SCOPES" = 1 ] && systemctl --user kill --signal=KILL "pb-$name-$$.scope" 2>/dev/null |
| 285 | tripped=1 |
| 286 | break |
| 287 | fi |
| 288 | done |
| 289 | wait "$p"; rc=$? |
| 290 | rm -f "$JOBS/$name" |
| 291 | [ "$tripped" = 1 ] && return 99 |
| 292 | return "$rc" |
| 293 | } |
| 294 | |
| 295 | mkdir -p "$TMP" "$POI" "$LOGDIR" "$JOBS" |
| 296 | rm -f "$JOBS"/* "$JOBS/.mb-stopped" 2>/dev/null |
| 297 | BIN=$REPO/target/release |
| 298 | cd "$REPO" || die "cannot cd $REPO" |
| 299 | |
| 300 | log "preflight: required static inputs" |
| 301 | for f in "$IN/planet.osm.pbf" "$IN/coastline.shp" "$IN/world.mdem" "$IN/reviews_archive.tsv"; do |
| 302 | [ -f "$f" ] || die "missing input: $f" |
| 303 | log " ok $(basename $f) ($(stat -c%s "$f") B)" |
| 304 | done |
| 305 | |
| 306 | log "[1/7] tools: cargo build --release (all phase 2-7 bins, poi_extract included)" |
| 307 | # Whether the go-file was already there when poi_extract/mamaps_pack were |
| 308 | # built: if not, the POI job rebuilds them once it appears (fix must be in tree). |
| 309 | GO_AT_BUILD=0; [ -f "$GO" ] && GO_AT_BUILD=1 |
| 310 | cargo build --release \ |
| 311 | --bin road_graph --bin road_overlay \ |
| 312 | --bin transit_shapes \ |
| 313 | --bin mamaps_build --bin mamaps_pack --bin mamaps_dump \ |
| 314 | --bin poi_extract --bin mamaps_planet5 --bin transit_planet5 \ |
| 315 | >>"$LOGDIR/cargo-build.log" 2>&1 || die "cargo build failed (see cargo-build.log)" |
| 316 | # planet5 graph post-pass: an examples/ target of the engine crate (it drives the app's own |
| 317 | # graph reader), so the Android cdylib never links it. |
| 318 | cargo build --release -p offlinerouter --example graph_thin \ |
| 319 | >>"$LOGDIR/cargo-build.log" 2>&1 || die "cargo build graph_thin failed (see cargo-build.log)" |
| 320 | [ -x "$BIN/examples/graph_thin" ] || die "no graph_thin after build" |
| 321 | for b in road_graph road_overlay transit_shapes mamaps_build mamaps_pack mamaps_dump poi_extract mamaps_planet5 transit_planet5; do |
| 322 | [ -x "$BIN/$b" ] || die "no $b after build" |
| 323 | done |
| 324 | log " all binaries present (go-file at build time: $GO_AT_BUILD)" |
| 325 | # `--sha256-out` (inline archive hash) exists only in a mamaps_pack new enough |
| 326 | # to have it; an older one says "unknown flag". Probe once, fall back to |
| 327 | # sha256sum in phase 7 if absent. |
| 328 | if "$BIN/mamaps_pack" --sha256-out /dev/null 2>&1 | grep -q 'unknown flag'; then |
| 329 | PACK_SHA_ARGS=() |
| 330 | log " mamaps_pack has no --sha256-out; phase 7 will run sha256sum" |
| 331 | else |
| 332 | PACK_SHA_ARGS=(--sha256-out "$PACK_SHA") |
| 333 | log " mamaps_pack hashes inline (--sha256-out)" |
| 334 | fi |
| 335 | GRAPH_READY_FLAG=0 |
| 336 | if "$BIN/mamaps_build" --help 2>&1 | grep -q -- '--graph-ready-file'; then GRAPH_READY_FLAG=1; fi |
| 337 | log " memory: MemAvailable $(mem_avail_gb)G, headroom ${MEM_HEADROOM_GB}G, scopes=$SCOPES; mamaps_build --graph-ready-file: $GRAPH_READY_FLAG" |
| 338 | |
| 339 | MB_ARGS=(--input "$IN/planet.osm.pbf" --out "$TILES" |
| 340 | --coastline "$IN/coastline.shp" --graph "$GRAPH" |
| 341 | --transit-routes "$ROUTES" --dem "$IN/world.mdem" --region world |
| 342 | # planet5 "aggressive" (owner, ~50 GB revision): DEM error <= 1 ground pixel per zoom, 4x |
| 343 | # simplification below z14, a 2-unit z14 floor for washable layers, zoom-adaptive coordinate |
| 344 | # grid, z13 wash kept. Error table in the planet-size / planet50 reports. |
| 345 | # No junction layer: lane connectors are drawn only by carriageway layers, which the release |
| 346 | # style drops (library/map style::LANE_RENDERING = false). |
| 347 | --dem-px 1 --simplify-scale 4 --z14-tolerance 2 --coord-quant --keep-z13-wash --no-junction-layer) |
| 348 | |
| 349 | # Pressure log: user.slice memory PSI (what fb-oomd watches), MemAvailable and |
| 350 | # each build scope's anon/file, every 30 s, for the life of the build. |
| 351 | pressure_logger(){ |
| 352 | local a=$SCOPE_ROOT d l |
| 353 | while :; do |
| 354 | l="$(ts) $(awk '/^some/{split($2,x,"=");split($3,y,"=");printf "user.slice psi some avg10=%s avg60=%s",x[2],y[2]}' /sys/fs/cgroup/user.slice/memory.pressure 2>/dev/null) MemAvailable=$(mem_avail_gb)G" |
| 355 | for d in "$a"/pb-*-$$.scope; do |
| 356 | [ -d "$d" ] || continue |
| 357 | l="$l $(basename "$d" "-$$.scope"):anon=$(awk '/^anon /{print int($2/1073741824)}' "$d/memory.stat")G,file=$(awk '/^file /{print int($2/1073741824)}' "$d/memory.stat")G" |
| 358 | done |
| 359 | echo "$l" >> "$TMP/pressure.log" |
| 360 | sleep 30 |
| 361 | done |
| 362 | } |
| 363 | pressure_logger & PRESSURE_PID=$! |
| 364 | trap 'kill "$PRESSURE_PID" 2>/dev/null' EXIT |
| 365 | |
| 366 | # Phase-4 footprint cut (2026-11-08, ENOSPC at 13:23 after 289G stage-A file |
| 367 | # spill): free what mamaps_build does not need BEFORE it starts -- now at the |
| 368 | # start of phase 2, since it may start then. cop90 (~67G) is the DEM source |
| 369 | # for $IN/world.mdem (mamaps_build reads world.mdem). TILES partials from a |
| 370 | # prior crashed run must not co-reside with the new spill. The transit mirror |
| 371 | # $IN/world_transit_work is freed only once transit-phase.service is done. |
| 372 | free_for_phase4(){ |
| 373 | if [ -d "$TILES" ]; then |
| 374 | rm -rf "$TILES" && log " freed stale partial TILES ($TILES)" \ |
| 375 | || die "cannot clear stale $TILES -- disk too tight to retry phase 4" |
| 376 | fi |
| 377 | rm -f "$TMP"/tiles.features.tmp "$TMP"/tiles.features.ways.tmp 2>/dev/null |
| 378 | if [ -d "$IN/cop90" ]; then |
| 379 | rm -rf "$IN/cop90" && log " freed DEM source cop90 pre-phase-4" \ |
| 380 | || log " WARN: cop90 delete failed, disk may be tight" |
| 381 | fi |
| 382 | } |
| 383 | free_transit_work(){ |
| 384 | if [ -d "$IN/world_transit_work" ]; then |
| 385 | rm -rf "$IN/world_transit_work" && log " freed transit workdir pre-phase-4" \ |
| 386 | || log " WARN: world_transit_work delete failed" |
| 387 | fi |
| 388 | } |
| 389 | free_kb(){ |
| 390 | if [ -n "${PB_TEST_FREE_KB:-}" ]; then echo "$PB_TEST_FREE_KB"; return; fi |
| 391 | df -k "$DISK_PATH" | awk 'NR==2{print $4}' |
| 392 | } |
| 393 | |
| 394 | # --- background job: transit shapes (old phase 3) --------------------------- |
| 395 | transit_job(){ |
| 396 | BG_PIDS=() |
| 397 | log "[3/7] transit shapes (background): waiting for $WT (transit-phase.service merges it)" |
| 398 | while :; do |
| 399 | if [ -s "$WT" ] && ! systemctl --user is-active -q transit-phase.service; then break; fi |
| 400 | if systemctl --user is-failed -q transit-phase.service && [ ! -s "$WT" ]; then |
| 401 | bgdie "transit-phase.service failed and $WT never appeared" |
| 402 | fi |
| 403 | sleep 300 |
| 404 | done |
| 405 | log " world.transit present ($(stat -c%s "$WT") B); shaping routes when memory admits" |
| 406 | # Written aside and renamed, so mamaps_build can never read a partial file. |
| 407 | run_tool ts "$PEAK_TS_GB" 0 1 -- \ |
| 408 | "$BIN/transit_shapes" "$ROUTES.tmp" --pack "$WT" >"$TMP/transit.log" 2>&1 \ |
| 409 | || bgdie "transit_shapes failed (see $TMP/transit.log)" |
| 410 | mv -f "$ROUTES.tmp" "$ROUTES" || bgdie "cannot place $ROUTES" |
| 411 | log " routes shaped" |
| 412 | } |
| 413 | |
| 414 | # --- background job: POI sidecars (old phase 5) ----------------------------- |
| 415 | poi_job(){ |
| 416 | BG_PIDS=() |
| 417 | log "[5/7] POI sidecars (background): HOLDING for sibling admin-boundary fix (go-file $GO)" |
| 418 | while [ ! -f "$GO" ]; do sleep 60; done |
| 419 | if [ "$GO_AT_BUILD" = 0 ]; then |
| 420 | log " go-file appeared after phase 1; rebuilding poi_extract + mamaps_pack fresh (fix must be in tree)" |
| 421 | nice -n 10 cargo build --release --bin poi_extract --bin mamaps_pack >>"$LOGDIR/cargo-build.log" 2>&1 \ |
| 422 | || bgdie "cargo build poi_extract/mamaps_pack failed" |
| 423 | else |
| 424 | log " go-file present; poi_extract (built in phase 1) starts when memory admits" |
| 425 | fi |
| 426 | # The early-mamaps_build decision goes first: it is on the critical path, |
| 427 | # POI is not (needed only at phase 6). |
| 428 | if [ -n "${MB_PID:-}" ]; then |
| 429 | while kill -0 "$MB_PID" 2>/dev/null && [ ! -f "$JOBS/mb" ]; do sleep 1; done |
| 430 | fi |
| 431 | # Niced: it may run beside mamaps_build, which is the critical path. |
| 432 | run_tool poi "$PEAK_POI_GB" 0 1 -- \ |
| 433 | nice -n 10 "$BIN/poi_extract" "$IN/planet.osm.pbf" \ |
| 434 | --geojson "$POI/poi.geojsonseq" --names "$POI/poi_names.bin" \ |
| 435 | --index "$POI/poi_index.bin" --region world --reviews "$IN/reviews_archive.tsv" \ |
| 436 | >"$TMP/poi.log" 2>&1 || bgdie "poi_extract failed (see $TMP/poi.log)" |
| 437 | # Sibling admin-boundary fix (982f7b92): poi_region.bin defaults beside |
| 438 | # --index; mamaps_pack auto-stages it as kind 15. No new flags either end. |
| 439 | [ -f "$POI/poi_region.bin" ] || bgdie "poi_extract wrote no $POI/poi_region.bin -- fix regressed?" |
| 440 | log " poi sidecars done ($(stat -c%s "$POI/poi_region.bin") B region sidecar)" |
| 441 | # poi_admin.bin (boundary names/levels/parents) rides beside poi_region.bin the same way; |
| 442 | # mamaps_pack auto-stages it as kind 16. Optional: an older poi_extract simply omits it. |
| 443 | if [ -f "$POI/poi_admin.bin" ]; then log " admin hierarchy $(stat -c%s "$POI/poi_admin.bin") B"; fi |
| 444 | } |
| 445 | |
| 446 | # --- background job: hold early mamaps_build at Pass 3 while road_graph runs - |
| 447 | # Freezes the scope (or SIGSTOPs the PID without scopes) as soon as build.log |
| 448 | # shows "Pass 3" while road_graph ($RG_PID) still runs; the main line thaws it |
| 449 | # when road_graph exits (and again at the graph gate, which covers the race of |
| 450 | # a freeze landing just after that). |
| 451 | mb_hold_job(){ |
| 452 | BG_PIDS=() |
| 453 | local pid |
| 454 | while kill -0 "$RG_PID" 2>/dev/null; do |
| 455 | if [ -f "$JOBS/mb" ] && grep -aq 'Pass 3' "$TMP/build.log" 2>/dev/null; then |
| 456 | kill -0 "$RG_PID" 2>/dev/null || break |
| 457 | if [ "$SCOPES" = 1 ]; then |
| 458 | systemctl --user freeze "pb-mb-$$.scope" \ |
| 459 | && log " early mamaps_build reached Pass 3 while road_graph runs: frozen until it is done (anon $(anon_gb mb 0)G)" |
| 460 | else |
| 461 | read -r pid _ < "$JOBS/mb" && kill -STOP "$pid" \ |
| 462 | && echo "$pid" > "$JOBS/.mb-stopped" \ |
| 463 | && log " early mamaps_build reached Pass 3 while road_graph runs: stopped until it is done" |
| 464 | fi |
| 465 | break |
| 466 | fi |
| 467 | sleep 1 |
| 468 | done |
| 469 | } |
| 470 | mb_release(){ |
| 471 | if [ "$SCOPES" = 1 ]; then |
| 472 | systemctl --user thaw "pb-mb-$$.scope" 2>/dev/null |
| 473 | elif [ -s "$JOBS/.mb-stopped" ]; then |
| 474 | kill -CONT "$(cat "$JOBS/.mb-stopped")" 2>/dev/null |
| 475 | fi |
| 476 | } |
| 477 | |
| 478 | # --- background job: early mamaps_build (phase 4 started in phase 2) -------- |
| 479 | # mamaps_build reads the transit routes before its graph gate, so the decision |
| 480 | # waits for them (transit_shapes, ~30 s on the planet). It then starts only if |
| 481 | # road_graph is still running, the disk gate passes, and its anon head fits |
| 482 | # now (PB_MB_EARLY=1 skips the memory rule). Not started: the job ends 0 and |
| 483 | # the main line runs phase 4 after the graph as before. |
| 484 | mb_early_job(){ |
| 485 | BG_PIDS=() |
| 486 | local fkb admit="try:$PEAK_MB_EARLY_GB" rc |
| 487 | [ "$PB_MB_EARLY" = 1 ] && admit=0 |
| 488 | while [ ! -s "$ROUTES" ]; do |
| 489 | kill -0 "$RG_PID" 2>/dev/null || { log " early mamaps_build: NO -- road_graph finished before the transit routes"; return 0; } |
| 490 | sleep 1 |
| 491 | done |
| 492 | kill -0 "$RG_PID" 2>/dev/null || { log " early mamaps_build: NO -- road_graph already finished"; return 0; } |
| 493 | fkb=$(free_kb) |
| 494 | if [ "$fkb" -lt "$DISK_GATE_OVERLAP_KB" ]; then |
| 495 | log " early mamaps_build: NO -- $((fkb / 1048576))G free < 270G needed for graph + phase-4 spill together" |
| 496 | return 0 |
| 497 | fi |
| 498 | : > "$TMP/build.log" |
| 499 | run_tool mb "$PEAK_MB_GB" 1 "$admit" -- \ |
| 500 | "$BIN/mamaps_build" "${MB_ARGS[@]}" --graph-ready-file "$READY" \ |
| 501 | >>"$TMP/build.log" 2>&1 |
| 502 | rc=$? |
| 503 | case $rc in |
| 504 | 0) ;; |
| 505 | 75) log " early mamaps_build: NO -- does not fit beside road_graph now"; return 0 ;; |
| 506 | *) bgdie "mamaps_build failed (see $TMP/build.log)" ;; |
| 507 | esac |
| 508 | } |
| 509 | |
| 510 | log "[2/7] road graph: road_graph --rounds 3 (planet pbf is >10GB); transit, POI and early tiles beside it when they fit" |
| 511 | rm -f "$READY" "$ROUTES.tmp" |
| 512 | free_for_phase4 |
| 513 | run_tool rg "$PEAK_RG_GB" 1 0 -- \ |
| 514 | "$BIN/road_graph" "$IN/planet.osm.pbf" --out "$GRAPH" --region world --rounds 3 \ |
| 515 | >"$TMP/graph.log" 2>&1 & |
| 516 | RG_PID=$!; BG_PIDS+=("$RG_PID") |
| 517 | # Admissions below must see road_graph's reservation. |
| 518 | for _ in $(seq 1 300); do [ -f "$JOBS/rg" ] && break; sleep 0.1; done |
| 519 | [ -f "$JOBS/rg" ] || die "road_graph did not start" |
| 520 | |
| 521 | # Early mamaps_build (see EARLY above): decided in the background once the |
| 522 | # routes exist; a hold job freezes it at Pass 3 while road_graph runs. |
| 523 | MB_PID="" |
| 524 | : > "$TMP/build.log" |
| 525 | if [ "$GRAPH_READY_FLAG" = 1 ] && [ "$PB_MB_EARLY" != 0 ]; then |
| 526 | mb_early_job & MB_PID=$!; BG_PIDS+=("$MB_PID") |
| 527 | if [ "$PB_MB_FREEZE" = 1 ]; then |
| 528 | mb_hold_job & HOLD_PID=$!; BG_PIDS+=("$HOLD_PID") |
| 529 | fi |
| 530 | fi |
| 531 | transit_job & TRANSIT_PID=$!; BG_PIDS+=("$TRANSIT_PID") |
| 532 | poi_job & POI_PID=$!; BG_PIDS+=("$POI_PID") |
| 533 | log " background: transit shapes (pid $TRANSIT_PID), POI sidecars (pid $POI_PID)" |
| 534 | |
| 535 | wait "$RG_PID" || die "road_graph failed (see $TMP/graph.log)" |
| 536 | # road_graph's memory is back: an early mamaps_build held at Pass 3 goes on |
| 537 | # (it still blocks on $READY before the stages that read the graph). |
| 538 | if [ -f "$JOBS/mb" ]; then |
| 539 | mb_release && log " road_graph done: early mamaps_build released" |
| 540 | fi |
| 541 | [ -f "$GRAPH/metadata.bin" ] || die "road_graph produced no $GRAPH/metadata.bin" |
| 542 | log " metadata.bin present; running road_overlay --graph" |
| 543 | run_tool ovl "$PEAK_OVL_GB" 0 0 -- \ |
| 544 | "$BIN/road_overlay" --graph "$GRAPH" >>"$TMP/graph.log" 2>&1 \ |
| 545 | || die "road_overlay failed (see $TMP/graph.log)" |
| 546 | log " graph done" |
| 547 | |
| 548 | log "[3/7] transit shapes: joining background job (pid $TRANSIT_PID)" |
| 549 | wait "$TRANSIT_PID" || die "transit shapes job failed (see $LOG / $TMP/transit.log)" |
| 550 | [ -s "$ROUTES" ] || die "transit shapes job wrote no $ROUTES" |
| 551 | log " routes ready ($(stat -c%s "$ROUTES") B)" |
| 552 | free_transit_work |
| 553 | |
| 554 | log "[4/7] tiles: mamaps_build (this is the long pole)" |
| 555 | # Collect the early decision (the job ends 0 when it did not start one). |
| 556 | MB_EARLY=0 |
| 557 | if [ -n "$MB_PID" ]; then |
| 558 | while kill -0 "$MB_PID" 2>/dev/null && [ ! -f "$JOBS/mb" ]; do sleep 1; done |
| 559 | if [ -f "$JOBS/mb" ]; then |
| 560 | MB_EARLY=1 |
| 561 | else |
| 562 | wait "$MB_PID" || die "early mamaps_build job failed (see $TMP/build.log)" |
| 563 | fi |
| 564 | fi |
| 565 | if [ "$MB_EARLY" = 1 ]; then |
| 566 | # Graph and routes are final: release mamaps_build past its graph gate |
| 567 | # (and out of the freezer, if the hold job put it there). |
| 568 | touch "$READY" || die "cannot create $READY" |
| 569 | mb_release |
| 570 | log " graph ready -> early mamaps_build continues (job $MB_PID); pre-phase-4 free: $(df -h "$DISK_PATH" | awk 'NR==2{print $4}')" |
| 571 | wait "$MB_PID" || die "mamaps_build failed (see $TMP/build.log)" |
| 572 | else |
| 573 | # Disk gate (2026-11-10): measured phase-4 peak on two full planet runs was |
| 574 | # ~202 GB (tiles.features.tmp 98.5 + tilechunks 72.8 + bodies 31), far below |
| 575 | # mamaps_build's own 289 GB estimate, so gate at 240 GiB (~40 GB margin). |
| 576 | free_for_phase4 |
| 577 | log " pre-phase-4 free: $(df -h "$DISK_PATH" | awk 'NR==2{print $4}') avail; commit: $(grep -E '^(CommitLimit|Committed_AS):' /proc/meminfo | tr '\n' ' ')" |
| 578 | [ "$(free_kb)" -ge "$DISK_GATE_P4_KB" ] \ |
| 579 | || die "phase 4 needs ~240G free for the stage-A feature spill; free space or shrink staging first (see build.log WARN)" |
| 580 | # Admitted like the others: waits for a running poi_extract if both would not fit. |
| 581 | run_tool mb "$PEAK_MB_GB" 1 1 -- \ |
| 582 | "$BIN/mamaps_build" "${MB_ARGS[@]}" \ |
| 583 | >"$TMP/build.log" 2>&1 || die "mamaps_build failed (see $TMP/build.log)" |
| 584 | fi |
| 585 | log " tiles done; dem consumed -> freeing COP90 source tiles (~67G)" |
| 586 | rm -rf "$IN/cop90" && log " inputs/cop90 deleted" || log " WARN: cop90 delete failed, disk may be tight" |
| 587 | |
| 588 | log "[5/7] POI sidecars: joining background job (pid $POI_PID; holds for go-file $GO)" |
| 589 | wait "$POI_PID" || die "POI job failed (see $LOG / $TMP/poi.log)" |
| 590 | [ -f "$POI/poi_region.bin" ] || die "poi_extract wrote no $POI/poi_region.bin -- fix regressed?" |
| 591 | log " poi sidecars ready" |
| 592 | |
| 593 | log "[5b/7] planet5: graph v8, transit v8, dictionary tiles" |
| 594 | GRAPH5=$TMP/graph5 |
| 595 | TRANSIT5=$TMP/world5.transit |
| 596 | TILES5=$TMP/tiles5.mamaps |
| 597 | rm -rf "$GRAPH5" "$TRANSIT5" "$TILES5" |
| 598 | # Three independent conversions, run together (graph_thin is one thread, ~48 GB; the others |
| 599 | # use the pool). All read their inputs only. |
| 600 | # Graph v9 (column ZBLK, zstd-block shape points / names / name offsets). Thinning stays 1 m: the |
| 601 | # aggressive preset's 2 m changed the step list of ~half of all routes on US West. |
| 602 | run_tool g5 60 0 1 -- nice -n 10 "$BIN/examples/graph_thin" "$GRAPH" "$GRAPH5" 100 25 --v9 \ |
| 603 | >"$TMP/graph5.log" 2>&1 & G5_PID=$! |
| 604 | run_tool t5 24 0 1 -- nice -n 10 "$BIN/transit_planet5" "$WT" "$TRANSIT5" --shape-tol-m 1 \ |
| 605 | >"$TMP/transit5.log" 2>&1 & T5_PID=$! |
| 606 | run_tool m5 120 1 1 -- "$BIN/mamaps_planet5" "$TILES" "$TILES5" >"$TMP/tiles5.log" 2>&1 \ |
| 607 | || die "mamaps_planet5 failed (see $TMP/tiles5.log)" |
| 608 | wait "$T5_PID" || die "transit_planet5 failed (see $TMP/transit5.log)" |
| 609 | wait "$G5_PID" || die "graph_thin failed (see $TMP/graph5.log)" |
| 610 | log " graph5 $(stat -c%s "$GRAPH5/intermediate.bin") B intermediate; transit5 $(stat -c%s "$TRANSIT5") B; tiles5 $(stat -c%s "$TILES5") B" |
| 611 | |
| 612 | log "[6/7] pack: mamaps_pack -> $OUTNAME" |
| 613 | # A stale inline hash from an earlier run must never be taken for this one. |
| 614 | rm -f "$PACK_SHA" |
| 615 | "$BIN/mamaps_pack" --tiles "$TILES5" --graph "$GRAPH5" --poi "$POI" \ |
| 616 | --transit "$TRANSIT5" --poi-zsec 10,15 --out "$OUT" ${PACK_SHA_ARGS[@]+"${PACK_SHA_ARGS[@]}"} >"$TMP/pack.log" 2>&1 \ |
| 617 | || die "mamaps_pack failed (see $TMP/pack.log)" |
| 618 | log " packed $(stat -c%s "$OUT") B; freeing transit workdir" |
| 619 | rm -rf "$IN/world_transit_work" && log " inputs/world_transit_work deleted" \ |
| 620 | || log " WARN: world_transit_work delete failed" |
| 621 | |
| 622 | log "[7/7] verify: header dump gates + sha256" |
| 623 | # mamaps_dump maps the archive (ecea9ca8f): the header gate reads ~9 KB, not 81 GB. |
| 624 | "$BIN/mamaps_dump" "$OUT" --mode header >"$TMP/header.tsv" 2>"$TMP/dump.err" \ |
| 625 | || die "mamaps_dump failed (see $TMP/dump.err)" |
| 626 | # The pack's inline SHA-256 (same `sha256sum` line, computed over the bytes as |
| 627 | # written) when it is present, well-formed and newer than the archive's own |
| 628 | # rename; otherwise hash the file as before. |
| 629 | if [ -s "$PACK_SHA" ] && [ "$(wc -l < "$PACK_SHA")" -eq 1 ] \ |
| 630 | && grep -Eq "^[0-9a-f]{64} ${OUTNAME//./\\.}\$" "$PACK_SHA" && ! [ "$PACK_SHA" -ot "$OUT" ]; then |
| 631 | mv -f "$PACK_SHA" "$OUT.sha256" || die "cannot place $OUT.sha256" |
| 632 | log " sha256 (inline, from mamaps_pack): $(cut -d' ' -f1 "$OUT.sha256")" |
| 633 | else |
| 634 | log " no usable inline hash; running sha256sum" |
| 635 | ( cd "$OUTDIR" && sha256sum "$OUTNAME" > "$OUTNAME.sha256" ) || die "sha256sum failed" |
| 636 | log " sha256: $(cut -d' ' -f1 "$OUT.sha256")" |
| 637 | fi |
| 638 | log "BUILD COMPLETE: $OUT" |