[PR #2253] [Engine] serve: the RAM gate releases the heap and stops evicting when it cannot help (rebased #1394) #3949

Open
opened 2026-11-07 15:31:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2253
Author: @alanthinker
Created: 10/13/2026
Status: 🔄 Open

Base: main ← Head: conversation-cache-ram-gate-release


📝 Commits (5)

  • ab963c9 serve: the RAM gate releases the heap and stops evicting when it cannot help
  • f09887f serve: the evict_oldest comment matches the raised mmap threshold (review)
  • 6e62d38 serve: the RAM gate releases the heap and stops evicting when it cannot help
  • 3dbc9ac serve: the evict_oldest comment matches the raised mmap threshold (review)
  • 3ee9597 Merge the pre-rebase branch tip so the closed PR's head stays reachable for /reopen

📊 Changes

6 files changed (+203 additions, -14 deletions)

View changed files

📝 include/strata/core/conversation_cache.hpp (+40 -2)
📝 include/strata/core/conversation_memory.hpp (+5 -0)
📝 src/core/conversation_cache_test.cpp (+102 -0)
📝 src/core/conversation_memory.cpp (+15 -0)
📝 src/core/conversation_memory_test.cpp (+29 -0)
📝 src/program/generate.cpp (+12 -12)

📄 Description

Problem and result

The physical-RAM gate of the parking path assumes an evicted parked conversation is visible to /proc/meminfo before the next check reads it. That assumption is wrong for the allocations this cache actually makes: ConversationBuffer is a list of 16 MiB segments, and glibc's mmap threshold is dynamic - the first generation of segments is mmap'd, freeing one raises the threshold past 16 MiB (capped at 32 MiB), and every later segment comes from the main heap. Freeing one of those only moves the bytes onto the allocator's free list: RSS, and with it MemAvailable, does not move.

On today's main (0.2.0-beta) the gate added for #1347 evicts parked conversations one at a time and re-reads the figure after each. In steady state the figure never moves, so the loop can empty the whole cache and still refuse the snapshot - worse than the refusal it was meant to remove: every parked conversation is lost and every later request re-reads its whole prompt (measured in #1347: 270k tokens, 96 s, 32 times in one session).

After this change a release (malloc_trim(0)) runs before the first re-read and after every eviction, so a dropped conversation shows up in the next reading; and where the missing bytes exceed what the parked conversations could give back, the gate gives up before evicting anything.

#1347 (the issue, reproduced on an RTX 4080 SUPER with 91 GB RAM), #1394 (the original PR by @Arthur031221, unmergeable after #879 / #1601 / #1573 landed), and this thread:

  • @qiuzan2001's point about the two contradicting comments is fixed in the second commit.
  • @alfre2v reproduced the bug independently on an RTX 3090 / 64 GB box (glibc 2.35, sm_86) and tested this branch there: unpatched v0.1.44.1 failed in every run (8 refusals in one, the engine heap reaching 6.9 GiB, a 214K-token snapshot re-read in 90 s), and this PR admitted the snapshot in all three of its runs with no cost on fresh 4K-32K prompts. Their numbers also answer the trim-timing question raised above: on a heap holding up to 5.5 GiB of freed segments a trim took 37-482 ms (median about 111 ms, roughly 80-90 ms per GiB returned), 12-14 trims per 19-minute run, about 2.4 s in total - against a 90 s re-read. They also measured two side effects (the heap stays large between trims; long prompts read about 2% slower right after a trim) and one residual case outside this PR, the post-capture floor check dropping a freshly captured snapshot in 1 of 3 runs.

Approach

  • conversation_release_freed_memory() (malloc_trim(0) under __GLIBC__, a no-op elsewhere) is called before the first re-read and after every eviction.
  • ConversationCache::admit_ram() gives up before evicting anything when the missing bytes exceed evictable_bytes() - what evict_oldest() may actually take, i.e. entries not holding a pinned shared prefix - and re-checks the figure after each reading, so losing telemetry ends the loop.
  • The reading stays the memory guard's (sample_host_memory(): MemAvailable, the cgroup limits above the engine, --memory-limit-mib) and the refusal line keeps ram_note().

Validation

Rebased on main at e88ee39d (0.2.0-beta, engine 0.1.45.3); branch tip 3dbc9aca. Built and tested by me on the serving box from #1347: Linux, RTX 4080 SUPER (32 GB), 94 GB RAM, CUDA 13.2, sm_89.

Passed:

  • cmake -B build -DSTRATA_BUILD_CONVERSATION_TESTS=ON -DSTRATA_ENABLE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=89 && cmake --build build --target conversation_cache_test conversation_memory_test
  • conversation_cache_test: 4829 checks passed
  • conversation_memory_test: 24 checks passed
  • the cache tests main added since: session_save_reclaim_test 119 checks, conversation_file_test 201 checks, conversation_split_failure_test passed
  • the full engine target strata compiles (CUDA 13.2, sm_89)

Failed: none.

Not run: running the rebuilt 0.2.0-beta engine on the GPU (this box still serves on the deployed 0.1.x build; the binary compiled, it was not started); HIP, SYCL and Metal builds.

Separate from my checks: @alfre2v ran this branch (at ab963c96 + f09887f6) end-to-end on an RTX 3090 / 64 GB box, results in Related discussion; their soak numbers are theirs, not mine.

Limits

  • The -0.3 MB / 127.7 MB figures in the first comment were measured on the v0.1.43.1-era serving heap; I have not reproduced that heap shape on 0.2.0-beta, so I quote no new trim numbers of my own - @alfre2v's trim timings above are the measured answer.
  • malloc_trim(0) returns what has coalesced to an arena's top; a mid-heap segment that cannot coalesce stays invisible even after the release. The loop still converges: further evictions eventually coalesce, and evictable_bytes() bounds the case where even emptying the cache could not help.
  • The trim runs only when a reading comes out short, so between parks the freed segments stay in the heap (@alfre2v: up to 8.9 GiB on their box); other programs on the machine see less free RAM in between, and long prompts read about 2% slower right after a trim (fresh pages fault in).
  • The post-capture floor check keeps its own "freed heap is visible at once" assumption and can still drop a freshly captured snapshot (@alfre2v saw it in 1 of 3 runs); this PR does not touch it.
  • --session-save-reclaim (#1573) keeps the same assumption between its steps; left alone here to keep this PR to the parking gate.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/2253 **Author:** [@alanthinker](https://github.com/alanthinker) **Created:** 10/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `conversation-cache-ram-gate-release` --- ### 📝 Commits (5) - [`ab963c9`](https://github.com/Niko1221/Strata/commit/ab963c964998e2396ba37a505fea3a07e0341657) serve: the RAM gate releases the heap and stops evicting when it cannot help - [`f09887f`](https://github.com/Niko1221/Strata/commit/f09887f6143b84eebb01b30f9cd51c5a31d53cea) serve: the evict_oldest comment matches the raised mmap threshold (review) - [`6e62d38`](https://github.com/Niko1221/Strata/commit/6e62d3819bfc0e91ec40373677537ad864aa7da3) serve: the RAM gate releases the heap and stops evicting when it cannot help - [`3dbc9ac`](https://github.com/Niko1221/Strata/commit/3dbc9aca77b2218ffc76fd93655868816d5ac94b) serve: the evict_oldest comment matches the raised mmap threshold (review) - [`3ee9597`](https://github.com/Niko1221/Strata/commit/3ee9597a55815384e6732b78f5c788506b804227) Merge the pre-rebase branch tip so the closed PR's head stays reachable for /reopen ### 📊 Changes **6 files changed** (+203 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/core/conversation_cache.hpp` (+40 -2) 📝 `include/strata/core/conversation_memory.hpp` (+5 -0) 📝 `src/core/conversation_cache_test.cpp` (+102 -0) 📝 `src/core/conversation_memory.cpp` (+15 -0) 📝 `src/core/conversation_memory_test.cpp` (+29 -0) 📝 `src/program/generate.cpp` (+12 -12) </details> ### 📄 Description ## Problem and result The physical-RAM gate of the parking path assumes an evicted parked conversation is visible to `/proc/meminfo` before the next check reads it. That assumption is wrong for the allocations this cache actually makes: `ConversationBuffer` is a list of 16 MiB segments, and glibc's mmap threshold is dynamic - the first generation of segments is mmap'd, freeing one raises the threshold past 16 MiB (capped at 32 MiB), and every later segment comes from the main heap. Freeing one of those only moves the bytes onto the allocator's free list: RSS, and with it `MemAvailable`, does not move. On today's `main` (0.2.0-beta) the gate added for #1347 evicts parked conversations one at a time and re-reads the figure after each. In steady state the figure never moves, so the loop can empty the whole cache and still refuse the snapshot - worse than the refusal it was meant to remove: every parked conversation is lost and every later request re-reads its whole prompt (measured in #1347: 270k tokens, 96 s, 32 times in one session). After this change a release (`malloc_trim(0)`) runs before the first re-read and after every eviction, so a dropped conversation shows up in the next reading; and where the missing bytes exceed what the parked conversations could give back, the gate gives up before evicting anything. ## Related discussion #1347 (the issue, reproduced on an RTX 4080 SUPER with 91 GB RAM), #1394 (the original PR by @Arthur031221, unmergeable after #879 / #1601 / #1573 landed), and this thread: - @qiuzan2001's point about the two contradicting comments is fixed in the second commit. - @alfre2v reproduced the bug independently on an RTX 3090 / 64 GB box (glibc 2.35, sm_86) and tested this branch there: unpatched v0.1.44.1 failed in every run (8 refusals in one, the engine heap reaching 6.9 GiB, a 214K-token snapshot re-read in 90 s), and this PR admitted the snapshot in all three of its runs with no cost on fresh 4K-32K prompts. Their numbers also answer the trim-timing question raised above: on a heap holding up to 5.5 GiB of freed segments a trim took 37-482 ms (median about 111 ms, roughly 80-90 ms per GiB returned), 12-14 trims per 19-minute run, about 2.4 s in total - against a 90 s re-read. They also measured two side effects (the heap stays large between trims; long prompts read about 2% slower right after a trim) and one residual case outside this PR, the post-capture floor check dropping a freshly captured snapshot in 1 of 3 runs. ## Approach - `conversation_release_freed_memory()` (`malloc_trim(0)` under `__GLIBC__`, a no-op elsewhere) is called before the first re-read and after every eviction. - `ConversationCache::admit_ram()` gives up **before** evicting anything when the missing bytes exceed `evictable_bytes()` - what `evict_oldest()` may actually take, i.e. entries **not** holding a pinned shared prefix - and re-checks the figure after each reading, so losing telemetry ends the loop. - The reading stays the memory guard's (`sample_host_memory()`: `MemAvailable`, the cgroup limits above the engine, `--memory-limit-mib`) and the refusal line keeps `ram_note()`. ## Validation Rebased on `main` at `e88ee39d` (0.2.0-beta, engine 0.1.45.3); branch tip `3dbc9aca`. Built and tested by me on the serving box from #1347: Linux, RTX 4080 SUPER (32 GB), 94 GB RAM, CUDA 13.2, sm_89. Passed: - `cmake -B build -DSTRATA_BUILD_CONVERSATION_TESTS=ON -DSTRATA_ENABLE_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=89 && cmake --build build --target conversation_cache_test conversation_memory_test` - `conversation_cache_test`: 4829 checks passed - `conversation_memory_test`: 24 checks passed - the cache tests main added since: `session_save_reclaim_test` 119 checks, `conversation_file_test` 201 checks, `conversation_split_failure_test` passed - the full engine target `strata` compiles (CUDA 13.2, sm_89) Failed: none. Not run: running the rebuilt 0.2.0-beta engine on the GPU (this box still serves on the deployed 0.1.x build; the binary compiled, it was not started); HIP, SYCL and Metal builds. Separate from my checks: @alfre2v ran this branch (at `ab963c96` + `f09887f6`) end-to-end on an RTX 3090 / 64 GB box, results in Related discussion; their soak numbers are theirs, not mine. ## Limits - The -0.3 MB / 127.7 MB figures in the first comment were measured on the v0.1.43.1-era serving heap; I have not reproduced that heap shape on 0.2.0-beta, so I quote no new trim numbers of my own - @alfre2v's trim timings above are the measured answer. - `malloc_trim(0)` returns what has coalesced to an arena's top; a mid-heap segment that cannot coalesce stays invisible even after the release. The loop still converges: further evictions eventually coalesce, and `evictable_bytes()` bounds the case where even emptying the cache could not help. - The trim runs only when a reading comes out short, so between parks the freed segments stay in the heap (@alfre2v: up to 8.9 GiB on their box); other programs on the machine see less free RAM in between, and long prompts read about 2% slower right after a trim (fresh pages fault in). - The post-capture floor check keeps its own "freed heap is visible at once" assumption and can still drop a freshly captured snapshot (@alfre2v saw it in 1 of 3 runs); this PR does not touch it. - `--session-save-reclaim` (#1573) keeps the same assumption between its steps; left alone here to keep this PR to the parking gate. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3949
No description provided.