Repository navigation
fix(SLRU): rounding leak let W-TinyLFU skip many admission checks - #387
Conversation
SLRU_parse_params() rounds each seg-size segment down on its own, so the segments add up to less than the cache unless the cache size divides evenly. 1a1a11a#343 fixed this for the default layout but kept explicit seg-size layouts as they were. With W-TinyLFU's seg-size=1:4 main cache this is one object short unless the main size is a multiple of 5. Once every segment is full, a hit in probation evicts inside find(), and W-TinyLFU then admits its next window victim without the frequency check. Call SLRU_resize() after parsing, which gives the remainder to the last segment. The seg-size=1:2:3:4 print in test_cli.sh changes from 9:19:29:39 to 9:19:29:40.
1a1a11a
left a comment
There was a problem hiding this comment.
Thanks, this is a clean fix and a really useful write-up. Reusing SLRU_resize() makes parsing and the hill climber use the same rounding rule, which is the right call. It's also safe to call that early: SLRU_resize (SLRU.c:525-534) only touches lru_max_n_bytes, seg_ratio and cache_size, so it's fine inside SLRU_parse_params before SLRU_init allocates the list heads and byte counters (SLRU.c:148-160). CI is green on 2a04d238. No blocking issues.
A few things to consider:
-
The admission bypass can still happen in byte mode (
SLRU.c:275-279,SLRU.c:551-573,WTinyLFU.c:387-389). With the segments now summing to the cache size, count mode can't leave a free slot. With variable object sizes, though, a promotion that overflows protected cools its tail into probation, andSLRU_cool(…, 0)evicts whole probation objects until probation fits. That can leave main belowcache_sizeby up to one object's size. W-TinyLFU'soccupied + obj_size + md <= cache_sizecheck then admits any window victim small enough to fit the slack, with no sketch comparison. You already flagged byte mode as untested; it may be worth an issue, or a note in the PR, that this fix is exact only for--ignore-obj-size=1. -
Related, pre-existing and out of scope:
SLRU_coolmovesobj->obj_sizebetween segments (SLRU.c:567-568), but promote, insert and evict useobj_size + obj_md_size. Withconsider_obj_metadata, each promote-then-demote leaves+mdon the upper segment and-mdon the lower one, so per-segment occupancy drifts away from the real contents. That's the same family of segment-accounting bug, so it could go in the same follow-up. -
Published numbers change. As the description says, default W-TinyLFU gets worse in 50 of 76 cells, and cluster26 goes from about 0.26 to 0.47 at the smallest size. Since anyone comparing against earlier libCacheSim W-TinyLFU results will see this jump, a CHANGELOG/README line saying the fixed-window W-TinyLFU numbers moved because of this fix would save people some confusion.
-
Cache name nit (
SLRU.c:164-186). An explicitseg-sizewith equal parts on a size the parts don't divide now gives unequal segments. For example,seg-size=1:1on 101 objects used to be 50/50 and is now 50/51. That flipssame_sizeto false, so the name goes fromS2LRU(50:50)toS2LRU(49:50), and the same goes for-e print. This already happens on the default path since #343, so it's consistent, but scripts that key on cache names will see new labels. Comparingseg_ratioinstead oflru_max_n_bytesforsame_sizeand the printed percentages would keep the labels stable. -
Test (
test/test_evictionAlgo.c,test_SLRU_seg_size_fills_cache). This catches the regression throughget_n_obj. Since the bug that mattered was in W-TinyLFU, a small W-TinyLFU-level test would pin the effect you care about: a main size not divisible by 5 (e.g. total 1223, window 0.14), then assert that main is full after warm-up, or that a cold window victim is rejected once main is full.
Posted automatically as a COMMENT review on behalf of @1a1a11a.
SLRU_cool() moved obj_size between segments while promote, insert and evict moved obj_size + obj_md_size, so with --consider-obj-metadata each promote-then-demote left the metadata on the upper segment's count. When no segment had room, SLRU_insert() evicted until the whole cache fit, which could push the lowest segment past its share when objects differ in size; it now evicts until that segment fits too. The cache name and -e print now show the shares asked for (seg_ratio) instead of the byte limits, so seg-size=1:1 is S2LRU(50:50) at every cache size and seg-size=1:2:3:4 prints 10:20:30:40. Count mode (--ignore-obj-size=1) is unchanged.
In byte mode the main cache evicts whole objects, so it can sit a little below its size. A window victim that fit that space was admitted without the frequency check, even long after the main cache had filled. Remember that the main cache has filled, and compare from then on; the hill climber clears this when it grows the main cache. A winning window victim larger than the main victim now evicts until it fits. Two tests push a cold object out of the window into a full main cache with free space: from SLRU segments that did not add up (count mode; fails on develop 4e3521a) and from evicting whole objects (byte mode; fails without this fix). Count mode (--ignore-obj-size=1) is unchanged.
-e print exits from SLRU_parse_params() without freeing the cache. LeakSanitizer stayed quiet only while the cache pointer was still live in the print call; since the label change no longer passes it, the Ubuntu job reports the whole cache as leaked. Free it before exiting. The cache name loops now stop once the name fills the buffer, as the -e print loop already does.
1a1a11a
left a comment
There was a problem hiding this comment.
Thanks for following up on the byte-mode and metadata points. The SLRU_cool byte fix and the matching _SLRU_verify_lru_size change, the seg_ratio-based names, the main_full latch and the two W-TinyLFU tests all look right to me. CI is green on 497a50e6. This is a re-review of the three commits since 2a04d238.
-
New insert loop trusts
lru_n_bytes[0], butSLRU_removenever updates it (SLRU.c:316-320,SLRU.c:407-420). The old loop only checkedcache->occupied_byte, whichcache_remove_obj_basekeeps correct. The new condition also waits forlru_n_bytes[0] + sz <= lru_max_n_bytes[0].SLRU_remove(an existing bug) unlinks the object but never decrementslru_n_bytes/lru_n_objs. After a user remove from probation, then,lru_n_bytes[0]stays too high. If probation's list is now empty,SLRU_to_evictskips past it to higher segments, and evicting those never lowerslru_n_bytes[0]. The loop can then drain the whole cache and end up inSLRU_evictwith a NULL victim.WTinyLFU_removereaches this throughmain_cache->remove. The simplest fix is to makeSLRU_removedecrementlru_n_bytes[id]/lru_n_objs[id]the same waySLRU_evictdoes. Adding acache->n_obj > 0guard to the loop would also be cheap insurance. -
main_fullis only cleared when the climber grows main (WTinyLFU.c:261-263,WTinyLFU.c:397-411). Once it's set, an explicitWTinyLFU_remove(WTinyLFU.c:464) that frees space in main leaves the latch on. Every later admission is then compared, and a winning victim also evicts a main object even though there's room. That's not wrong for TinyLFU, but it's a behaviour change worth either a comment or amain_full = falsein the main-cache branch ofWTinyLFU_remove. -
Plain SLRU byte-mode numbers move too (
test/test_evictionAlgo.c:205-207). The SLRU truth values change in both directions, e.g. 89624 → 89862 at the smallest size and 75388 → 75447 at the fifth. I'd guess this comes from the insert loop now evicting until probation fits. The PR description still covers only the count-mode rounding fix and says byte mode is untested. Since this now changes default SLRU results in byte mode, and W-TinyLFU admission in byte mode, it's worth updating the description and CHANGELOG so people comparing against older numbers know why they moved. -
Minor: in
WTinyLFU_evict, the newdo { evict } while (!fits)(WTinyLFU.c:426-428) is safe becauseWTinyLFU_can_insertalready requires the object to fit main's probation segment. A one-line comment saying so would help the next reader, since the loop has no other bound.
No blocking issues besides (1). That one is a real crash path, though only reachable through explicit removes.
Posted automatically as a COMMENT review on behalf of @1a1a11a.
Hi, while working on #385 I kept seeing weird spikes in the fixed-window W-TinyLFU curves, and the reason I gave for them in that PR was wrong.
The reason was SLRU rounds both segment down, so W-TinyLFU's main ends up one slot short (unless its size is a multiple of 5). W-TinyLFU sees the free slot and puts the next object without checking the sketch. For example in some few traces, more than half the objects just skipped the sketch check at all. The spikes were the few sizes where the bug doesn't happen.
And this is the plot twist, the bug often actually helps. Fixing it makes default W-TinyLFU worse in 50 of the 76 cells from #385. On cluster26 at the smallest size the miss ratio goes from about 0.26 to 0.47.
I give you here the AI summary ⬇️
What does this PR do?
SLRU_parse_params()rounds eachseg-sizesegment down on its own, so the segments can add up to less than the cache.seg-size=1:4, so a main of 1,001 objects gets 200 + 800 = 1,000 slots. Once both segments are full, every promotion out of probation evicts insidefind(). W-TinyLFU's next miss sees the free slot and admits the window victim without the frequency check. On a typical trace from feat(WTinyLFU): adapt the window size with a hill climber #385 (median of 26, smallest size), 22% of admissions into main skip the check.SLRU_resize(cache, cache->cache_size)after parsing. It already gives the remainder to the last segment, so parsing and resizing now share one rule. Add Merlin eviction algorithm and fix capacity remainders in SR-LRU and SLRU #343 fixed the same rounding for the default layout; this covers explicitseg-size.SLRU_resize(), so it never leaked, while most fixed-window baselines did.seg-sizethat doesn't divide the cache (seg-size=1:2:3:4on 1 GB:9:19:29:39becomes9:19:29:40).Type of change
How was it tested?
New unit test
test_SLRU_seg_size_fills_cache: fails on develop (5 == 6), passes with the fix.test/test_cli.sh: 428 passed, 0 failed.ctest --test-dir _build --output-on-failurepasses (16/16)Build is warning-free (CI uses
-Wall -Wextra -Werror)Results
All runs:
--ignore-obj-size=1 --num-thread=1, Release.The leak is gone.
At 1223 this PR matches develop with the segments forced to add up exactly, and sizes that were already exact are untouched (13/13 in #385). Over 208 cells on cluster26 and alibabaBlock_117, the spikes are exactly the cells with main % 5 == 0.
Effect on the 76 cells of #385 (miss ratio, pp; "same" = within 0.05 pp; negative = first one better):
Not tested: byte mode with variable object sizes, where SLRU may still overfill probation.
Checklist
clang-format(or the pre-commit hook fromscripts/setup_hooks.sh)