Repository navigation
Compare tall screenshots a row at a time; say when a worker ran out of memory (VISUALCI-PROD-SERVER-6) - #302
Open
ndbroadbent wants to merge 4 commits into
Open
ndbroadbent wants to merge 4 commits into
ndbroadbent wants to merge 4 commits into
Conversation
…ut of memory (VISUALCI-PROD-SERVER-6) A 2880x14000 comparison held three or four full-size RGBA buffers at once and peaked at 665 MiB. It now reads 8-bit PNGs a row at a time (image_hash::Rows), keeps a five-row window of each image, and streams the overlay into the PNG encoder: 84 MiB, most of it the input files, with identical results and overlay pixels. Fingerprints, matching approved changes and PR comment images read rows too, and whole-image decodes (other formats) go one at a time (WHOLE_DECODES). A run that finds the previous run's mark on its build counts it as died. After three, the build fails with "the worker ran out of memory (or was killed) processing build #N", and ProcessBuild's fourth try fails the build and the job with it rather than "attempted more than its 3 tries allow". visualci-core changed: the workspace version is 0.2.30.
VisualCIdocspring/visualci · build #1437 · 578 unchanged · no changes This PR vs base: 6 changed since #1418, across 1 build docspring/visualci · journeys · build #1433 · 3 removed, 53 unchanged · waiting for review This PR vs base: 3 removed since #1406, across 1 build · 3 not approved yet Removed: Updated as builds change. Agents can review with VisualCI's MCP tools. |
…ons against main The png crate's StreamWriter gave the same overlay pixels with different bytes (it hands each row to fdeflate in two pieces), so every diff blob's hash would have changed. visualci_core::overlay_png writes the overlay a row at a time exactly as the image crate's PngEncoder wrote it whole: the png crate's adaptive filter, one fdeflate call per filtered row, the stored fallback, one IDAT chunk, into one buffer that's never copied. The compare_memory example gains `replay <download_build dir>`: 110 real page comparisons (visualci #1408 and #1430, docspring #229 and #230) give identical results and overlay bytes on main and on this branch.
Review fixes for the OOM detection: - A run's mark is a guard (RunMark): the job's timeout or a caught panic drops the run's future, and the drop clears the mark, so only a worker that dies (out of memory, SIGKILL) leaves it to be counted. - Tries that run out without a death leave the build processing for the sweep, as on main; the job still fails. Only deaths fail the build. - give_up waits JOB_WAIT_SECONDS for the build, as run does, so a dead run's hold that hasn't lapsed doesn't make it give up silently. - image_hash::info and matching::same_in_files take WHOLE_DECODES before opening a file that won't stream, as the doc says; lock_whole_decodes helper. - Tests: timed-out and panicked runs aren't deaths, the job's try past its tries with and without deaths, give_up waiting out a dead hold, and an interlaced PNG comparing as its plain twin.
A cache error noting a run's start or clearing its mark is logged and the run carries on: the marks are bookkeeping, and a cache error in RunMark::end could replace a finished run's Ok(true), so run() marked a finished build failed. Test: a cache that refuses the build's mark (a trigger on the cache table) still lets an empty build conclude.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Internal: no changelog entry. Users won't see a difference, except that builds with tall full-page screenshots stop killing the worker.
Why
VISUALCI-PROD-SERVER-6: the worker (512Mi, raised to 1Gi as a stopgap) was OOM-killed three times on docspring/visualci build #1408. That build has 578 image snapshots, 12 of them changed, all tall full-page PNGs (1440x7162, about 1440x3800, and 2880-wide
-2xcopies). Peak memory with the 1Gi limit was 714Mi.A comparison decoded both images whole, converted each to RGBA with a copy (
to_rgba8), built a full-size RGBA overlay plus two full-sizeVec<bool>maps (mask and changed pixels), then PNG-encoded the overlay. For a 2880x14000 pair (161 MB per RGBA copy), that is three or four full-size buffers alive at once.Measured
Harness:
crates/visualci-core/examples/compare_memory.rs. It runs the samediff_filespath the server runs (decode, diff, overlay PNG encode) on a 2880x14000 RGB screenshot-like pair with a small change. Each compare runs on another of eight long-lived threads, as tokio's blocking pool does. Peak RSS is VmHWM. The release build was run 4 times in browser mode, unless the row says otherwise.MALLOC_ARENA_MAX=2Of the 82 MiB left, 58 MB is the two input PNGs held in memory (this synthetic page compresses badly; real screenshots are smaller). Another 21 MB is the overlay PNG the diff returns. The diff itself now holds a few rows.
Results are unchanged, overlay bytes included: see the replay of real comparisons below.
Replayed real comparisons: identical to main
I replayed real comparisons through main's compare (
origin/main, thediffs.rsbody: whole decode,diff_masked,write_to(Png)) and through this branch'sdiff_files. The base and head images came fromdownload_build:site/screenshots/*-2xcopies.There is no
screenshots/landing/*-2xsnapshot (landing has 1440 and 390 only), so the 2880x6006site/screenshots/1440-*-2xpages stand in for a 2x landing shot.Each page ran in exact and in browser mode (threshold 0.1, anti-aliasing ignored), with the page's masked areas. That is 110 page-and-mode results across 55 pages. All 110 are identical between main and this branch in every field: canvas, size change, changed pixels, anti-aliased pixels, regions, fingerprint, and the overlay PNG's SHA-256. The harness is
cargo run --release -p visualci-core --example compare_memory -- replay <unpacked download_build dir>.53 pages reproduce the overlay production stored (the archive's
diff.png, byte for byte). These projects compare in exact mode. The other two pages (ckj_fonts.liveandub-04.livepage 1) are unchanged pages, which have no stored overlay, and both versions agree there (0 changed pixels).The table has one row per page, in the mode production used. "regions" gives the count and the first 8 hex characters of the SHA-256 of the regions' JSON. Long snapshot names are shortened in the middle: the two "signature modal type" rows from #229 are the inline and the modal data request.
Peak memory on the real 1440x7162 landing shot
screenshots/landing/1440-light, #1408. VmHWM from the harness, browser mode:MALLOC_ARENA_MAX=2site/screenshots/1440-light-2x, 1 compareWhat changed
visualci_core::image_hash::Rowsreads an image file as RGBA rows. An 8-bit, non-interlaced PNG (any browser screenshot) is decoded one row at a time with the png crate. Palettes, low bit depths and tRNS chunks are expanded, as the image crate's decoder does. Anything else (JPEG, WebP, 16-bit or interlaced PNG, files pastdecode's limits) is decoded whole bydecodeas before. A file the streaming decoder gives up on part-way, including one cut short after its last row, is decoded whole again, so it gets the image crate's own pixels or error.visualci_core::diff::diff_filesis the server's comparison: decode, diff, overlay PNG. It keeps a five-row window of each image, because the anti-aliasing test looks two rows away. It computes the mask a row at a time and keeps only the 16 px changed-tile map (instead of two full-size bool maps). Each overlay row goes straight to the PNG encoder (overlay_png).diff_masked(in memory, used by the wasm build) shares the same row-by-row core.domain::diffs::find_or_computenow callsdiff_files.visualci_core::overlay_pngwrites the overlay a row at a time, byte for byte as the image crate'sPngEncoderwrote it whole: the png crate's adaptive filter (Up, Sub, Avg, Paeth by the lowest signed-byte sum), fdeflate's fast compressor fed one filtered row per call, the stored fallback when compression doesn't pay, one IDAT chunk. The png crate's ownStreamWritergave the same pixels with different bytes, so every diff blob's SHA-256 would have changed. The compressed data goes into the same buffer as the file, so the finished PNG is never copied.blobs::decode_image,image_hash::fingerprint_bytes) hash rows as they're read: the same hash asfingerprint_rgba. The 8 concurrent fingerprints (VISUALCI_FINGERPRINT_CONCURRENCY) now cost a few rows each instead of a full image each.domain::matching, runs at the end of processing) cached every decoded full-size image of every candidate, cloned one per comparison, and decoded on the runtime's threads. It now keeps the compressed files for one snapshot at a time and compares regions a row at a time on the blocking pool.image_hash::WHOLE_DECODESlets one whole-image decode run at a time in the process. It covers the fallback formats in compare, fingerprinting and matching, and the PR comment's overlay. Streamed PNGs don't take it.Runs killed by OOM now say so
When a worker ran out of memory, the job used to fail with "
builds.processhas been attempted more than its 3triesallow". The build stayed in processing.builds.process-running:<id>in the cache) while it holds the build's processing lock and clears the mark when it ends. The run can end by finishing, waiting for renders, stopping for a deploy, or failing the build. A run that takes the lock and finds a mark knows the previous run died, because a live run would still hold the lock. It logs a warning and counts the death (builds.process-died:<id>).processing::RUNS(3) runs have died, the next run fails the build with "the worker ran out of memory (or was killed) processing build #N, 3 times". This applies however the run was queued: retry, sweep, or a render finishing.RunMark) clears the mark when it's dropped. Only a worker that dies outright (out of memory, SIGKILL) runs no drop and leaves the mark.ProcessBuildhastries = 4. The fourth try doesn't process. When runs died,processing::give_upfails the build, and the job itself, with that same error. When the tries ran out without a death (errors before a run took the build, such as the database being down), the job fails but the build is left in processing for the sweep, as on main. The fourth try waits up toJOB_WAIT_SECONDSfor the build, as a run does, so a dead run's hold that hasn't lapsed yet doesn't make it skip the build. It leaves the build alone when it is no longer processing, or when another run holds it.Allocator
These are VmHWM numbers from the same harness, for synthetic pages unless a row says otherwise. Each compare runs on another of eight threads, as tokio's blocking pool does.
MALLOC_ARENA_MAX=2/ jemallocMALLOC_ARENA_MAX=2/ jemallocMALLOC_ARENA_MAX=2MALLOC_ARENA_MAX=2/ jemallocBefore the fix, glibc's per-thread arenas made it worse for 1440-wide pages. Their 10 MB bool maps sit under the mmap threshold, so each thread's arena kept its own, and eight compares nearly doubled the peak (368 MiB, against 190 MiB with two arenas). The 2880-wide pages' 160 MB buffers are mmapped, so arenas barely mattered there.
After the fix, the buffers are small, and the arena effect is about 16 MiB. jemalloc is worse in every row, so this PR doesn't switch allocators.
MALLOC_ARENA_MAX=2in the worker's deploy env (talira-ai/infra) is a cheap extra but not needed. It isn't set by this PR.Tests
overlay_png::testschecks the overlay's bytes against the image crate's encoder: flat, gradient, odd sizes, 1x1, and noise, which takes the stored fallback.diff::tests::the_row_by_row_diff_matches_the_whole_image_diff_exactlykeeps main's diff as a test oracle (tests::reference). It checks results and overlay bytes fordiff_maskedanddiff_fileson RGB and RGBA PNGs: 5 size pairs (including size changes and 1-pixel-wide images), 4 option sets and 3 masks. The cases include anti-aliased, masked and changed pixels.image_hashtests check thatRowsgivesdecode().to_rgba8()'s pixels and the same fingerprint for each PNG kind: gray, gray+alpha, RGB, RGBA, RGB+tRNS, 1-bit gray, palette+tRNS, plus 16-bit and JPEG through the fallback. A truncated PNG or an oversized one gets the decoder's own error.crates/visualci-core/tests/peak_memory.rsmeasures a 1440x20000 change with a counting global allocator, in its own test binary. Its peak is 1.9 MiB. The test fails above 8 MiB plus twice the overlay PNG's size. One RGBA copy of that page is 110 MiB, so the test fails at once if a full-size buffer comes back. The margin leaves room for allocator and library changes. The overlay term is there because the returned PNG grows by doubling.tests/processing_lock.rscovers the OOM handling: counting runs that died, the build failing after three, runs cut off by a timeout or a panic not counting, the job's last try failing build and job with the same error when runs died and leaving the build for the sweep when none did, giving up waiting out a dead run's hold, a cache that refuses the marks still letting the build conclude, a finished run clearing the count, and giving up leaving a build another run holds. The diff tests include an interlaced PNG (decoded whole) comparing exactly as its plain twin.cargo test --test app(462),cargo test --libfor app,cargo test --workspace --exclude visualci-app,cargo clippy --all-targets -- -D warnings,cargo fmt --check, andcargo check -p visualci-wasm --target wasm32-unknown-unknown.visualci-core changed, so the workspace version is bumped to 0.2.30 (Cargo.toml, Cargo.lock, the action and orb defaults).
Not changed
gallery_thumbs::DECODES).get_snapshotimages (mcp/images.rs) still decode whole.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.