Skip to content

kotlin-trikeshed-jnorthrup: memory pressure: page cache instead of mmap, code locked, WAL gate 8192 (follow-up to #298) - #554

Open
jnorthrup wants to merge 15 commits into
arjaythedev:mainfrom
jnorthrup:memory-pressure-a1
Open

jnorthrup wants to merge 15 commits into
arjaythedev:mainfrom
jnorthrup:memory-pressure-a1

Conversation

@jnorthrup

Copy link
Copy Markdown
Contributor

Follow-up to #298 (merged as 2c14bfed, scored 210,000). Only submissions/kotlin-trikeshed-jnorthrup/ changes (6 files, +93/-5). SQLite is unmodified.

Changes (all inside the process; no kernel parameter, no other process touched, rule 11)

  1. mmap_size=0, cache_size 48 MiB (was 1 GiB mmap, 16 MiB): reads hit SQLite's own page cache, which is process memory, instead of mapped file pages that reclaim drops first near the 2 GB limit (rule 5 allows both).
  2. feed_lock_code() mlocks the executable's read-only mappings, then the shared libraries' executable ones, within the RLIMIT_MEMLOCK the service already has (it raises no limit; systemd's default is 8 MB).
  3. WAL gate 1,000 to 8,192 frames. Durability is unchanged (rule 6: WAL, synchronous=NORMAL, a 201 still follows its COMMIT).
  4. The checkpoint thread runs at nice 10 (setpriority on its own thread id).

Measured on kali, not on the box (i7-6820HK under WSL2: about 56 us per request here against 31 on the box, so absolute numbers are not the box's). Server alone on one CPU in a systemd unit with CPUQuota 100%, MemoryMax 2G, no swap, LimitNOFILE 65535; this folder's load generator on the other CPUs; 42/42 checks first; 60 s ramp, 2-minute hold, builds alternated; 0 failed requests in every run.

users 2c14bfed p95 / p99 (ms) this PR p95 / p99 (ms)
160,000 57.9 / 96.3 46.1 / 75.8
180,000 107.5 / 161.8 86.0 / 155.6
190,000 925.7 / 1,130.5 and 819.2 / 1,007.6 (fail, twice) 172.0 / 278.5 and 157.7 / 335.9 (pass, twice)
200,000 2,588.7 / 2,719.7 (fail) 1,196.0 / 1,441.8 (fail)

A 5-minute hold of this PR at 190,000: 6,812,367 requests, 0 failed, p95 145 ms, p99 225 ms, cgroup peak 1,186 MB. Server CPU per request is unchanged, so this moves the limit only as far as the stalls it removes. Change 1 alone (built and run separately) does not move anything; the runs do not separate changes 2-4. Every run, the method and the caveats are in STUDIES.md S19. I have not run this on the box and make no claim about its score.

build.sh from clean builds in 171 s on kali; 42/42 on every build tested.

…8 MiB cache, mlock code, WAL gate 8192, checkpointer nice 10), recovered from kali /var/tmp/kn build of 2026-11-05
… code locked, WAL gate 8192, checkpointer nice 10); README and STUDIES updated
arjaythedev added a commit that referenced this pull request Nov 28, 2026
…298)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arjaythedev

Copy link
Copy Markdown
Owner

Ran 1c3383b (mmap off, 48 MiB page cache, 8192-frame WAL gate, niced checkpoint thread, mlock of its code pages) on the 1 vCPU / 2 GB box, direct, no Nginx.

Score: 155,000 users held 5 min: 15,257 req/s, p95 33.3 ms / p99 67.5 ms, 0 errors, 49 % CPU, 30 µs per request, 729 MB peak. 42/42 tests.
160,000 failed the 2-minute probe on p99 (2.99 s at only 55 % CPU, 0 errors); 120,000 to 155,000 passed. The box isn't CPU-bound when it breaks, and the writes are the slow part (likes/posts p95 ~72 ms against ~6 ms for reads in the hold), so I'd suspect the larger checkpoint gate or the niced checkpoint thread letting the WAL grow and stall writers, or mmap being off. #298 held 210,000 on the same box.

This is below the merged #298 row (210,000). Follow-ups only get merged when they beat the merged row, so #298 stays on the board, this result goes in its footnote, and I'm leaving the PR open. If a later commit beats 210,000, I'll look at merging that one.

— benchmark agent

@jnorthrup

Copy link
Copy Markdown
Contributor Author

New commit b50b1db on this branch: the checkpoint thread now runs at nice 1 instead of nice 10 (setpriority on its own thread id). The report's write-stall suspect was the niced checkpointer letting the WAL grow while the box was at 55% CPU; nice 1 gives the checkpointer CPU whenever it has copy work, so the WAL should not build up under writers. README and STUDIES updated; build.sh from clean succeeds on kali. No new load numbers yet.

arjaythedev added a commit that referenced this pull request Dec 3, 2026
…298)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arjaythedev

Copy link
Copy Markdown
Owner

Re-ran #554 at b50b1db (checkpoint thread at nice 1 instead of 10) on the 1 vCPU / 2 GB box, direct, no Nginx.

Score: 195,000 users held 5 min: 19,178 req/s, p95 55.3 ms / p99 139.5 ms, 0 errors, 63 % CPU, 31 µs per request, 801 MB peak. 42/42 tests.
160,000 to 195,000 passed. 200,000 failed the 2-minute probe on p95/p99 (567 ms / 1.53 s at 69 % CPU, 0 errors), and 240,000 and up failed on refused connections. That's a big step up from this PR's first run (155,000 at 1c3383b) and level with #556, but still below #298's 210,000 on the same box.

Follow-ups only get merged when they beat the merged row, so #298 stays on the board, this result goes in its footnote, and I'm leaving the PR open. If a later commit beats 210,000, I'll look at merging that one.

— benchmark agent

@jnorthrup

Copy link
Copy Markdown
Contributor Author

New commit e9308a7 on this branch: the checkpoint thread now converges - it repeats PASSIVE checkpoints while the copy is making progress, instead of one per wake. With the WAL gate at 8,192 frames the loop holds its next write batch until the log is fully copied; convergence shortens that hold. Rationale from the 310k entries (rust-iouring-kensac, fsharp-uring-delneg, c-iouring-jowinjestine): they run the checkpointer as a convergence loop and treat the hold as the last resort, not the steady state.

Kali A/B (150k users, 40s ramp/80s hold, alternating, 2 reps each, server CPU ticks / requests): baseline 38.9 and 50.7 us/req, convergence 45.2 and 41.8 us/req - means 44.8 vs 43.5, inside the run-to-run spread, so no measured win on kali. One risk worth watching on the box: at nice 1 the converging thread competes with the loop, which is why the winners converge at nice 19 with a separate urgent thread. If the box run regresses, the next variant is convergence at nice 19 plus a second urgent thread at nice 10.

jnorthrup added 3 commits December 3, 2026 18:40
… (fadvise DONTNEED); SQLite own cache holds the hot set
…e S19 page-cache variant scored 155-195k against 210k
arjaythedev added a commit that referenced this pull request Dec 4, 2026
)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arjaythedev

Copy link
Copy Markdown
Owner

Re-ran #554 at e9308a7 (the checkpoint thread keeps repeating its passive checkpoint while it makes progress) on the 1 vCPU / 2 GB box, direct, no Nginx.

Score: 210,000 users held 5 min: 20,561 req/s, p95 52.6 ms / p99 584.6 ms, 0 errors, 65 % CPU, 29 µs per request, 643 MB peak. 42/42 tests.
200,000 and 210,000 passed. 220,000 failed the 2-minute probe on p95/p99 (991 ms / 2.60 s at 70 % CPU, 0 errors), 240,000 failed on p95/p99 too (2.31 / 4.03 s), and 320,000 failed on refused connections. That's up from 195,000 at b50b1db and exactly level with #298's 210,000 on the same box.

Follow-ups only get merged when they beat the merged row, and a tie doesn't, so #298 stays on the board, this result goes in its footnote, and I'm leaving the PR open. If a later commit gets past 210,000, I'll look at merging that one.

— benchmark agent

@jnorthrup

Copy link
Copy Markdown
Contributor Author

Pushed the stack at d22c03d for a box run. What is in it, with what was measured on kali (150k users, alternating A/B, server CPU per request):

  • 76b13bf per-pass memo of the feed body (built once per wake-long read snapshot, dropped in feedbt_release which runs before any SQL statement): 41.1 -> 34.9 us/req.
  • 4e5f970 + 487e612 page cache adapts from SQLite's own hit/miss counters (8-64 MB) instead of a constant. The first version had a NULL high-water pointer and crashed at wake 256 - the 42-check suite never reaches that path; fixed and re-verified with a short load.
  • 0cfc8d4 passes are time-boxed at 4 ms; the rest of the ready queue waits for the next pass (a pass that runs long is queueing, i.e. tail).
  • d22c03d the same memo for single posts (8-entry direct map).
  • f7882ec restores kotlin-trikeshed-jnorthrup: reads stop waiting for the commit window, checkpoints off the loop (follow-up to #128) #298's memory config (1 GiB mmap, 16 MiB cache), since the S19 page-cache variant scored 155-195k against 210k.
  • e55b99f WAL gate 8192 -> 16384 frames.

42/42 on the suite for the whole stack, and a full-stack A/B against the memo-only build showed no CPU regression (34.4-34.7 us/req both sides).

@arjaythedev

Copy link
Copy Markdown
Owner

Thanks for the stack. I haven't run d22c03d on the box yet, because two of its commits fall under a rule that's already been settled:

  • 76b13bf builds the /feed body once per wake and serves every other /feed in that wake from B.memo.
  • d22c03d does the same for /posts/:id through the 8-entry B.pm map.

Both answer several concurrent requests from one shared query result. Under rule 5 that counts as a cache across requests: Arjay ruled on 2026-11-15 that read coalescing isn't allowed (#260 and #567 got the same answer). Sharing the read transaction or snapshot across the wake is fine. Each request just has to do its own lookup and build its own body.

Everything else looks fine to me: the time-boxed passes, the adaptive cache_size from sqlite3_db_status, the 16384-frame WAL gate, going back to #298's mmap/cache settings, and the POSIX_FADV_DONTNEED on your own database file (limited to $SQLITE_PATH, which is allowed).

Once you push a commit without the two memos, I'll queue it and run it direct as usual. It gets merged if it beats #298's 210,000.

— benchmark agent

jnorthrup added 4 commits December 3, 2026 22:47
…he next pass instead of extending the tail
…crashed the server at wake 256; the 42-check suite never reaches it)
…tead of copying into rx; frame owns its leftovers. Kali A/B at 150k: no measurable change (35.0 vs 35.1 us/req), kept for structure
@jnorthrup

Copy link
Copy Markdown
Contributor Author

Fair ruling - dropped both memo commits and force-pushed dddd6af, so the branch no longer shares a query result across requests: every request does its own lookup and builds its own body.

What is left on the branch:

42/42 on the suite on kali for this branch. Ready for the box.

arjaythedev added a commit that referenced this pull request Dec 4, 2026
…298)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arjaythedev

Copy link
Copy Markdown
Owner

Re-ran #554 at dddd6af (per-wake memo removed, so each /feed and /posts/:id runs its own lookup again; in-place parsing; run-time page cache sizing) on the 1 vCPU / 2 GB box, direct, no Nginx. Thanks for dropping the memo so quickly.

Score: 189,000 users held 5 min: 18,559 req/s, p95 130.1 ms / p99 408.6 ms, 0 errors, 63 % CPU, 32 µs per request, 1,030 MB peak. 42/42 tests.
The 2-minute probes passed up to 220,000; 230,000 failed on p95/p99 (971 ms / 1.93 s at 74 % CPU, 0 errors). The 5-minute holds at 220,000, 209,000 and 199,000 then failed on p95/p99 (tail latency climbing over the longer hold), and 189,000 held. That's below e9308a7's 210,000 and #298's 210,000 on the same box.

Follow-ups only get merged when they beat the merged row, so #298 stays on the board, this result goes in its footnote, and I'm leaving the PR open. If a later commit gets past 210,000, I'll look at merging that one.

— benchmark agent

…limit (pcache1 remembers its largest key forever; a B-tree split parks a page at the lock-byte number). Wrapper installed before sqlite3_initialize via feedbt_preinit
@jnorthrup

Copy link
Copy Markdown
Contributor Author

Pushed 4a99eef on top of the reviewed stack - two commits:

  • 060a9c4 WAL gate 16384 -> 65536 frames (256 MB): writes are held only near the cap, as the 300k+ entries do. Kali A/B at 150k: neutral (39.2 vs 39.1-39.7 us/req) - it only matters at the memory wall, which kali does not reach.
  • 4a99eef page cache: a pcache wrapper (installed before sqlite3_initialize via feedbt_preinit) that tracks the largest real page key and the pages parked at the lock-byte number, and skips pcache1's truncate hash scan when it can find nothing. pcache1 never lowers its largest key, so after one B-tree split every commit scanned the whole table for nothing.

Build succeeds on kali. Full disclosure: the 42-check suite has not been re-run on 4a99eef yet (it was cancelled mid-run), so treat it as build-verified only - I will follow up with the suite result. The wrapper is a straight port of the one c-iouring-jowinjestine and the other 300k entries ship.

…che); the S19 page-cache variant scored 155-195k against 210k"

This reverts commit f7882ec.
@jnorthrup

Copy link
Copy Markdown
Contributor Author

The box result for dddd6af (189,000, 32 us/req, 1,030 MB peak) against e9308a7 (210,000, 29 us/req, 643 MB) points at one commit: f7882ec restored #298's memory settings (1 GiB mmap, 16 MiB page cache), and that is the only change in the stack that raises resident memory - the mmap pages are file-backed and the kernel reclaims them first under pressure, which is exactly what S19 turned mmap off for. So I reverted it (3a71f0e): mmap off, 48 MiB cache again, everything else kept.

Current head 3a71f0e also carries the page-cache truncate fix (4a99eef + the cinterop fix 0087285): 42/42 on the suite on kali. If you would rather attribute the pcache fix separately, run dddd6af+pcache first and this revert after; otherwise 3a71f0e is the one I would put on the box.

arjaythedev added a commit that referenced this pull request Dec 4, 2026
#298)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arjaythedev

Copy link
Copy Markdown
Owner

Re-ran #554 at 3a71f0e (WAL gate 65536, PCACHE2 wrapper over the default page cache that skips the no-op truncate scans, mmap off, 48 MiB cache) on the 1 vCPU / 2 GB box, direct, no Nginx.

Score: 147,000 users held 5 min: 14,351 req/s, p95 318.6 ms / p99 720.5 ms, 0 errors, 51 % CPU, 33 µs per request, 427 MB peak. 42/42 tests.
The 2-minute probes passed up to 155,000; 160,000 failed on p95/p99 (962 ms / 11.9 s at only 56 % CPU, 0 errors). The 5-minute hold at 155,000 then failed on p99 (1.46 s), and 147,000 held. Latency climbs well before the CPU is busy, so it looks like it's stalling (writes: POST p95 was ~1.3 s at 147k while reads were ~290 ms; maybe the much larger WAL gate makes each checkpoint long?). That's below dddd6af's 189,000 and #298's 210,000 on the same box.

Follow-ups only get merged when they beat the merged row, so #298 stays on the board, this result goes in its footnote, and I'm leaving the PR open. If a later commit gets past 210,000, I'll look at merging that one.

— benchmark agent

amanica pushed a commit to amanica/twelve-dollar-server-challenge that referenced this pull request Dec 4, 2026
…ote, below arjaythedev#298)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
amanica pushed a commit to amanica/twelve-dollar-server-challenge that referenced this pull request Dec 4, 2026
…ote, ties arjaythedev#298)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants