Skip to content

fix(SLRU, WTinyLFU): follow-up to #387, two crashes - #388

Open
marcft wants to merge 2 commits into
1a1a11a:developfrom
marcft:fix/slru-wtinylfu-crashes
Open

marcft wants to merge 2 commits into
1a1a11a:developfrom
marcft:fix/slru-wtinylfu-crashes

Conversation

@marcft

@marcft marcft commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #387. That PR fixed the SLRU rounding that let W-TinyLFU skip the sketch check and admit some objects for free. Oddly, the bug was helping, fixing it made W-TinyLFU worse in 50 of 76 cells. Numbers and details in #387.

Previous PR left two bugsI fix here, SLRU_remove didn't update the segment counts, and SLRU and W-TinyLFU could evict from an empty cache after the hill climber shrinks it.

SLRU's insert loop goes back to its pre-#387 version, so SLRU results return to their old numbers.

…rinks

The insert loop from the seg-size fix waited for the lowest segment to
fit. After SLRU_resize() shrinks that segment below an object W-TinyLFU
inserts directly, it never fits, so the loop evicted everything and
crashed on a NULL victim. The hill climber does this resize, so

  cachesim data/cloudPhysicsIO.vscsi vscsi wtinylfu 1mb -e hill-climb=1

crashed (it ran before the seg-size fix). Go back to evicting until the
whole cache fits.

SLRU_remove() unlinked the object but left lru_n_bytes and lru_n_objs
as they were, so the freed space was never reused and, with the loop
above, removing probation's last object also crashed the next insert.
Update the counts as SLRU_evict() does.

W-TinyLFU: the loop that evicts room for an admitted object also stops
when the main cache is empty, and removing an object from the main
cache clears main_full so the freed space is filled again.

Count mode results are unchanged. In byte mode SLRU goes back to its
results from before the seg-size fix, and W-TinyLFU, whose main cache
is SLRU, moves slightly with it.
@marcft
marcft requested a review from haochengxia as a code owner November 14, 2026 11:13
Copilot AI balanced review requested due to automatic review settings November 14, 2026 11:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@marcft

marcft commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

I think the more interesting question here is why does even skipping the sketch check help??

My first guess was that it makes W-TinyLFU act more like LRU. But then it should only help where LRU wins, and that only holds for the big gains (13 of 15 cells). Where it helps a little, LRU wins only half the time. So maybe it's not about letting the right objects in, just letting more in. I did test some things:

  • admitting ties: basically nothing
  • sketch forgetting 4x faster: about 90% of the bug's gain
  • admitting a random 25% of rejected objects: helps in 42 of 50 cells, almost 2x the bug's gain

So the default filter is too strict, and any extra admission helps, even random. It only hurts on the frequency-heavy msr traces, same as the bug. The hill climber fixes this by growing the window, and it has the best average of everything I tried, so would it make sense to make it the default?

@1a1a11a 1a1a11a left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up. The SLRU_remove accounting fix and resetting main_full in WTinyLFU_remove both look right (the first is exactly what _SLRU_verify_lru_size would trip on). CI is green at 5c39056. A few things worth a look:

1. The SLRU insert change alters plain SLRU behaviour, not just the crash path (SLRU.c L314-321)
The old loop evicted until segment 0 had room and the cache had room. The new loop only checks total occupancy. nth_seg == -1 means no segment can take the object, but the total can still be below cache_size (each segment can be short by up to one object), so the object is now inserted into segment 0 with no eviction and segment 0 goes over lru_max_n_bytes[0]. That is why the plain SLRU row in test_evictionAlgo.c changes, which the description doesn't mention. If the goal is only to stop evicting from an empty segment, keeping the segment-0 bound and adding a termination guard would keep the old semantics:

while ((params->lru_n_bytes[0] > 0 &&
        params->lru_n_bytes[0] + sz > params->lru_max_n_bytes[0]) ||
       cache->occupied_byte + sz > cache->cache_size) {

If the new behaviour is intended, a sentence in the PR and commit message would help, since it moves SLRU's published miss ratios.

2. The W-TinyLFU guard can move the crash into SLRU_insert (WTinyLFU.c L426-435)
The new get_n_obj > 0 guard stops the loop once main is empty, but main_cache->insert still runs even when WTinyLFU_main_fits is false. With hill-climb, the window can grow to 80% (MAX_WINDOW_STEP 16) and main shrink to 20%, so a window object that passed can_insert against the earlier, larger main cache can be bigger than the current main. Then SLRU_insert finds no segment, its loop sees 0 + sz > cache_size, calls evict on an empty cache, SLRU_to_evict returns NULL, and SLRU_evict dereferences it (obj->SLRU.lru_id, L385). For an LRU main cache it would instead overfill main. Suggest checking fit after the loop and, if it still doesn't fit, dropping the window victim (window->evict) instead of inserting it.

3. Same NULL path in SLRU_evict generally
SLRU_to_evict can return NULL (L370-375) and SLRU_evict doesn't check. A if (obj == NULL) return; (or an ERROR) would turn any remaining case into a clean failure rather than a segfault, and makes loops like the one above terminate.

4. No regression tests for the two crashes
Only the expected miss counts changed. A small test that calls cache->remove on SLRU and then runs _SLRU_verify_lru_size (or checks lru_n_bytes), plus one that runs W-TinyLFU with hill-climb=1 on a small cache with large objects, would keep these from coming back.

On your question in the thread: the random-admission result is a nice data point. Making hill-climb the default seems reasonable if it holds up across the full trace set, but I'd do it as its own PR so the default change and its numbers are reviewed separately from these fixes.

Automated review posted on behalf of @1a1a11a.

@marcft

marcft commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! About those points

  1. On purpose, it's the same loop SLRU had before PR 387, so plain SLRU goes back to its old numbers.
  2. Only happens with LRU as the main cache, I can push a fix.
  3. SLRU_evict never had that check, I can fix it here.
  4. We can implement one, but I'd keep it small (sometimes ai gets too motivated with regression tests)

With an LRU main cache, the hill climber can shrink the main cache below
an object waiting in the window. Admitting it emptied the main cache and
then crashed. Such an object is now dropped from the window, and an
empty main cache is filled again without a comparison.

SLRU_evict stops with an error instead of crashing when there is nothing
to evict.

Add a CLI test that crashes on develop.

@1a1a11a 1a1a11a left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated re-review of head fad8147. My last review was at 5c39056; this covers the new commit ("drop window objects larger than the main cache"). Thanks for the replies. Point 1 (SLRU going back to its pre-#387 loop on purpose) makes sense; it would still be worth one line in the PR body, since it changes SLRU's published byte-mode numbers. CI is green on this head (ubuntu, macos/clang, fuzz, CodeQL, quality checks).

The new commit covers points 2 and 3 and adds a CLI regression test. It looks correct to me. A few small things:

1. ERROR is not a guaranteed stop (SLRU.c L384-386)
ERROR in logging.h aborts only when LOGLEVEL <= ERROR_LEVEL. In the #else branch it expands to nothing, and then SLRU_evict goes on to obj->SLRU.lru_id with obj == NULL, the same crash as before. Adding return; after the ERROR(...) (or abort()) makes the guard hold in every build. A plain return also means W-TinyLFU's do { main_cache->evict } while (!fits) loop would spin instead of crashing, so abort() is probably the safer choice.

2. The oversized-object drop is silent (WTinyLFU.c L397-401)
The new branch correctly bills against main_cache->obj_md_size and evicts the window victim. With that check in place, the do { main_cache->evict(...) } while (!WTinyLFU_main_fits(...)) loop below is now bounded, since the object always fits in an empty main cache. One thing to consider: a dropped window object isn't counted anywhere. A counter next to n_admit_bytes (or a DEBUG line) would show how often hill-climb sizing throws objects away, which matters when you compare hill-climb results across traces.

3. Test coverage
The new test_cli.sh case covers the LRU-main crash. The SLRU_remove accounting fix from the first commit still has no direct test. A short C test in test_evictionAlgo.c that inserts, calls cache->remove, and checks that occupied_byte and the segment counts go back down (or that the next inserts don't evict early) would pin it. It can stay small, as you suggested.

Your design question in the thread (why extra admission helps, and whether hill-climb should become the default) is better answered by a human than by this automated pass, so I've left it for @1a1a11a.

Automated review posted on behalf of @1a1a11a. Reviewed from the diff and the files at this head; I didn't run the tests.

@marcft

marcft commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

I think the more interesting question here is why does even skipping the sketch check help??

Don't want this comment to get buried, curious what you think. Happy to do the dynamic window default as its own PR

This branch has not been deployed

No deployments
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.

3 participants