conversation cache: the RAM gate stops evicting when memory does not come back (#5697)

Step 2 of P1 in the cache admission design (one admission: RAM first, the SSD tier next). When a park did
not fit under the physical-RAM floor, park_source evicted the oldest parked conversations one by one until
the park fit or the cache was empty. In the #5697 field log every eviction left the memory reading
unchanged (the C library kept the freed pages, or another process took them), so the loop emptied the
cache and still refused the park.

New strata::core::evict_for_ram (conversation_memory.hpp): evicts oldest first until admit() says the
park fits, at most the cache's slot count, and stops at the first eviction whose bytes do not show up
in the reading (a gain under half of the entry AND under 64 MiB). At most one conversation is lost to a
gate that cannot be met; the rest stay parked. ConversationCache::oldest_evictable_bytes() gives the
size of the next victim. In generate.cpp each eviction also hands the segment pool's idle memory back
to the OS, so the reading can show it. The refusal line gains "; the last eviction gave no memory
back, so the rest stay parked", which the server's cache counters (#6081) count as ram_no_progress.

Test: src/core/conversation_memory_test.cpp, evict_for_ram_cases with a fake memory reading: fits at
once (no eviction), memory that comes back (one eviction, fits), half of it back (two, fits), memory
that never comes back (one eviction, no_progress, three kept; the old loop: four evictions, an empty
cache, refused anyway),
80 MiB back per 200 MiB entry counts as progress, the max_evictions bound, an empty cache, an
unknown reading (no progress check). All unit tests of the working tree on Boston (job
boston-20261116-1518-5f15) pass. The CUDA and HIP builds and the BASE-05 identity gate run on the series
head.
This commit is contained in:
Niko1221 committed 2026-11-16 16:01:33 +01:00
1 parent 64050ffaae
commit 96edb9337a
4 files changed
+133 -6

No files matched your search

@@ -451,6 +451,12 @@ public:
release_free_heap();
return true;
}
/// The bytes the next evict_oldest() frees (0: nothing can go) - for the RAM gate's progress check (evict_for_ram)
size_t oldest_evictable_bytes() const {
for (size_t i = 0; i < entries_.size(); ++i)
if (!entries_[i].pinned() && !(guarded_ != 0 && serials_[i] == guarded_)) return entries_[i].bytes();
return 0;
}
// #879: every parked conversation (pinned ones too) and the retained K/V go - after non-finite logits any of them
// may carry the poison. Returns how many were parked.
@@ -1,5 +1,6 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <istream>
#include <optional>
@@ -126,4 +127,38 @@ inline bool conversation_memory_admit(std::optional<uint64_t> available,
return available && *available >= floor && allocation <= *available - floor;
}
/// #5697: the physical-RAM gate's eviction loop (generate.cpp, park_source). `admit()` samples the memory reading and
/// says whether the park fits; `available()` is the reading admit() took last (nullopt: unknown); `evict()` drops the
/// oldest parked conversation that may go and returns its bytes (0: none could go). Evicts until the park fits, at most
/// `max_evictions` entries, and stops at the first eviction whose memory did not show up in the reading: a gain under
/// half of the entry's bytes AND under 64 MiB (the C library kept it, or another process took it). The field log of
/// #5697: the loop used to go on through every parked conversation, read no change, and refuse anyway with an empty
/// cache. Now one entry is the most such a gate costs, and the conversations after it stay parked.
struct EvictForRam {
bool fits = false;
size_t evicted = 0;
bool no_progress = false; // stopped because an eviction gave nothing back to the reading
};
template <class Admit, class Available, class Evict>
EvictForRam evict_for_ram(Admit admit, Available available, Evict evict, size_t max_evictions) {
EvictForRam r;
r.fits = admit();
while (!r.fits && r.evicted < max_evictions) {
const std::optional<uint64_t> before = available();
const size_t freed = evict();
if (freed == 0) break;
++r.evicted;
r.fits = admit();
const std::optional<uint64_t> after = available();
if (!r.fits && before && after) {
const uint64_t gain = *after > *before ? *after - *before : 0;
if (gain < freed / 2 && gain < (uint64_t(64) << 20)) {
r.no_progress = true;
break;
}
}
}
return r;
}
} // namespace strata::core
+71
View File
@@ -3,7 +3,9 @@
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <optional>
#include <sstream>
#include <vector>
#if defined(_WIN32)
#ifndef NOMINMAX
@@ -19,9 +21,78 @@ void check(bool ok, const char* label) {
++checks;
if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
}
// #5697: evict_for_ram over a fake machine. Four parked conversations of `entry` bytes; an eviction gives back `ratio` of
// its bytes to the reading (0: the C library keeps it). The park needs `need` bytes above `floor`.
struct FakeRam {
uint64_t avail;
std::vector<size_t> parked;
double ratio;
std::optional<uint64_t> last;
bool admit(uint64_t need, uint64_t floor) { last = avail; return conversation_memory_admit(last, need, floor); }
size_t evict() {
if (parked.empty()) return 0;
const size_t b = parked.front();
parked.erase(parked.begin());
avail += (uint64_t) (b * ratio);
return b;
}
};
void evict_for_ram_cases() {
constexpr uint64_t MiB = uint64_t(1) << 20, GiB = MiB << 10;
const uint64_t floor = 2 * GiB, need = 3 * GiB;
auto run = [&](FakeRam& m, size_t max) {
return evict_for_ram([&] { return m.admit(need, floor); }, [&] { return m.last; }, [&] { return m.evict(); }, max);
};
{ // the memory comes back: evict until it fits, no further
FakeRam m{4 * GiB, {GiB, GiB, GiB, GiB}, 1.0, {}};
const EvictForRam r = run(m, 4);
check(r.fits && r.evicted == 1 && !r.no_progress && m.parked.size() == 3, "memory that comes back: one eviction, it fits");
}
{ // #5697: the memory stays in the heap: one eviction shows nothing, the gate stops, three stay parked
FakeRam m{4 * GiB, {GiB, GiB, GiB, GiB}, 0.0, {}};
const EvictForRam r = run(m, 4);
check(!r.fits && r.evicted == 1 && r.no_progress && m.parked.size() == 3,
"memory that does not come back: one eviction, then the gate stops; three stay parked");
// the loop it replaces (generate.cpp before #5697's fix) went on to the slot bound and emptied the cache
FakeRam old{4 * GiB, {GiB, GiB, GiB, GiB}, 0.0, {}};
bool fits = old.admit(need, floor);
size_t evicted = 0;
while (!fits && evicted < 4 && old.evict() > 0) { ++evicted; fits = old.admit(need, floor); }
check(!fits && evicted == 4 && old.parked.empty(), "(the old loop: four evictions, an empty cache, refused anyway)");
}
{ // half comes back: progress, it goes on until it fits
FakeRam m{4 * GiB, {GiB, GiB, GiB, GiB}, 0.6, {}};
const EvictForRam r = run(m, 4);
check(r.fits && r.evicted == 2 && m.parked.size() == 2, "most of it comes back: two evictions, it fits");
}
{ // already fits: nothing is evicted
FakeRam m{8 * GiB, {GiB, GiB}, 0.0, {}};
const EvictForRam r = run(m, 4);
check(r.fits && r.evicted == 0 && m.parked.size() == 2, "a park that fits evicts nothing");
}
{ // small entries: a gain of 64 MiB or more counts as progress even under half (the reading moves in coarse steps)
FakeRam m{4 * GiB + 700 * MiB, {200 * MiB, 200 * MiB, 200 * MiB, 200 * MiB}, 0.4, {}};
const EvictForRam r = run(m, 4);
check(r.evicted == 4 && !r.no_progress && !r.fits, "80 MiB back per 200 MiB entry is progress: all four go, still short");
}
{ // the slot bound and an empty cache end the loop as before
FakeRam m{3 * GiB, {GiB, GiB}, 1.0, {}};
const EvictForRam r = run(m, 1);
check(!r.fits && r.evicted == 1 && m.parked.size() == 1, "max_evictions bounds the loop");
FakeRam e{4 * GiB, {}, 1.0, {}};
check(!run(e, 4).fits && e.parked.empty(), "nothing parked: refused, nothing to evict");
}
{ // an unknown reading never fits and never counts as no-progress
FakeRam m{4 * GiB, {GiB}, 1.0, {}};
const EvictForRam r = evict_for_ram([&] { m.last.reset(); return false; }, [&] { return m.last; }, [&] { return m.evict(); }, 4);
check(!r.fits && r.evicted == 1 && !r.no_progress, "an unknown reading: refused (the old rule), not taken for no progress");
}
}
}
int main() {
evict_for_ram_cases();
for (const char* text : {"", "MemFree: 100 kB\n", "MemAvailable: -1 kB\n",
"MemAvailable: +1 kB\n", "MemAvailable: 1 MB\n",
"MemAvailable: 1\n", "MemAvailable: 1x kB\n",
+21 -6
View File
@@ -13337,15 +13337,30 @@ int main(int argc, char** argv) {
bool fits = admit();
if (!fits && pools_budget.idle_bytes() > 0) { pools_budget.shrink((size_t) -1); fits = admit(); }
fits = fits || (strata::core::release_free_heap() && admit());
while (!fits && conversations.size() > 0 && evicted < conversations.slots() &&
conversations.evict_oldest()) {
++evicted;
fits = admit();
// #5697: oldest first until it fits, and no further than the memory that comes back: an eviction whose
// bytes the reading does not show (the C library kept them) stops the loop, and the rest stay parked.
// An evicted image's segments go back to the segment pool as idle (resident) memory: given back to the
// OS at once, or the reading could never show them.
bool no_progress = false;
if (!fits) {
const strata::core::EvictForRam r = strata::core::evict_for_ram(
admit, [&] { return mem ? std::optional<uint64_t>(mem->available) : std::nullopt; },
[&]() -> size_t {
const size_t b = conversations.oldest_evictable_bytes();
if (b == 0 || !conversations.evict_oldest()) return 0;
if (pools_budget.idle_bytes() > 0) pools_budget.shrink((size_t) -1);
return b;
},
conversations.slots());
fits = r.fits;
evicted = r.evicted;
no_progress = r.no_progress;
}
if (!fits) {
std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM admission; need %zu MiB plus %lld MiB floor, %s; evicted %zu, %zu still parked)\n",
std::fprintf(stderr, "strata serve: conversation cache: skip parking (physical RAM admission; need %zu MiB plus %lld MiB floor, %s; evicted %zu, %zu still parked%s)\n",
additional >> 20, (long long) o.conversation_cache_min_free_mib, ram_note(mem).c_str(),
evicted, conversations.size());
evicted, conversations.size(),
no_progress ? "; the last eviction gave no memory back, so the rest stay parked" : "");
return true;
}
if (evicted)