Skip to content
ahmad-khan~/portfolio/projects/cache-replacement-policy

← projects

Original Last-Level Cache Replacement Policy

A replacement policy I designed for a 2 MiB last-level cache: a six-state reuse state machine steered by a perceptron. It beat LRU by 3.2% and the published SHiP policy by 2.73%, in 14.2 KiB of state, and placed top 3 in the class.

Oct 2026C++ · ChampSim · SPEC CPU2006

in the classCPEN 411, UBC
Top 3
IPC over LRUgeomean, 9 SPEC traces
+3.2%
IPC over SHiPthe target was +2%
+2.73%
IPC on mcfLLC hits 15.0% → 32.6%
+14.6%
of statebudget 16 KiB
14.2 KiB
worst lossany benchmark (astar)
−0.7%

The problem

A miss in the goes to DRAM and costs hundreds of cycles, so the line the cache chooses to evict decides a lot of a program's speed. The usual choice, , breaks down exactly when it matters: once a program's working set is bigger than the cache, every new line is inserted as the most protected one and pushes out older lines that would have been used again.

The assignment was to build five known in (Random, LFU, tree PLRU, BIP and DIP) and then design an original one, GIMMIK, in no more than 16 KiB of extra state. Every policy ran on nine traces and was measured by against LRU, with provided as the published policy to beat. The target was 2% over SHiP.

GIMMIK reached 2.73% over SHiP and 3.2% over LRU, the best of the eight policies, and placed top 3 in the class.

How it works

Each section is a short summary. Go deeper opens the C++ from gimmik.cc and the reasoning behind it. Dotted terms have definitions.

block diagram
LLC accessPC · address · type · hit or fillGIMMIK, 14.2 KiBPerceptron4 clues, 6-bit weightsvotes on every fillSampler16 shadow setstrains the weightsLine states3 bits × 32,768 linesa hit promotesWritebacksskip the voteenter as NEW_OLDVictim searchevict NEW_OLD, else ageBudget checkasserted at start-upevery 128th set±1start state
Drawn from gimmik.cc. Click a block to jump to its section; the section you are reading lights its block.

A reuse state for every line

Every line in the LLC carries one of six states in 3 bits. NEW, REUSED and HOT record how often the line has been hit, and each has an OLD version meaning it has not been hit since the set last aged. A hit moves a line up one class. This is GIMMIK's original part: it keeps how often a line was reused separate from how recently, where , PLRU and SRRIP treat one hit like any number of hits.

Evicting only NEW_OLD lines

When the set is full, the victim is the first NEW_OLD line: a line that was never reused, or that has aged all the way down. If there is none, every line in the set ages one step and the search runs again. It never takes more than three passes, because three steps take even a HOT line down to NEW_OLD.

A perceptron picks where a new line starts

On every demand fill, a votes on whether the new line will be reused. Four clues about the access are each hashed into their own table of 256 six-bit weights, and the four weights are added. A vote of 21 or more says probably dead and the line starts as NEW_OLD, first in line to leave. A vote of −21 or less says probably reused and it starts as REUSED. Anything in between starts as NEW.

Learning from 16 shadow sets

The weights are trained by a : every 128th LLC set has a shadow copy with 16 ways of tags under true LRU. If a block is found again in the sampler, it was reused, so the weights that voted on it move 1 toward alive. If it falls off the end of the sampler unused, it was dead, and they move 1 toward dead. Weights saturate at −32 and +31, and training is skipped when the vote was already right by 24 or more.

Writebacks are not reuse

A is the L2 throwing a dirty line away, not the program asking for it. GIMMIK fills writebacks as NEW_OLD without asking or training the perceptron, and a writeback hit leaves a line's state alone. I learned how much this matters while debugging DIP, below.

14.2 KiB, checked at start-up

The whole policy is 116,504 bits, 11% under the 131,072-bit limit. initialize_replacement computes the total from the cache's real geometry and asserts it fits, so a change that breaks the budget cannot run at all.

What is borrowed, and what is new

The predictor is not new, and the report says so: hashed feature tables, 6-bit weights, a separate training threshold and a sampler come from Teran, Wang and Jiménez's perceptron reuse predictor (MICRO 2016), and choosing a placement by threshold comes from their multiperspective follow-up (MICRO 2017). What is new is the replacement policy built around it.

Interactive exhibits

resultsChampSim logs · 50M measured after 50M warmup

Every policy, every benchmark

GIMMIK against each of the other policies on all nine traces. Every policy here except LRU and SHiP is my implementation. Switch to the hit rate to see where the speed comes from: on mcf and soplex GIMMIK has the highest LLC hit rate of any policy.

GIMMIK vs

Geometric mean over LRU: GIMMIK 1.032, SHiP 1.005. GIMMIK is ahead on 6 of 9 benchmarks, behind on 2.

IPC normalized to LRU, GIMMIK and SHiP
benchmarkGIMMIKSHiP
astar0.9930.998
gcc1.0261.002
gobmk1.0001.000
hmmer1.0011.000
lbm0.9970.998
mcf1.1461.019
milc1.0301.001
soplex1.1081.027
wrf1.0021.000
setAGE and PROMOTE tables, gimmik.cc

Run the state machine yourself

One 16-way set, following the same rules as the C++. Hit a few lines to protect them, then bring new lines in and watch the set age until a NEW_OLD line appears. The transitions each action used light up in the diagram.

miss, the vote says

An example set with no NEW_OLD line in it, so the next miss has to age the set first. Click a line to hit it, or bring in a new line with one of the buttons.

fill, vote ≤ −21fillfillvote ≥ 21,writebackevicthithitageageNEWNEW_OLDREUSEDREUSED_OLDHOTHOT_OLD

0 hits · 0 fills · 0 aging passes. Only NEW_OLD lines (dashed) can be evicted. Darker fill means more protected. The rules are the AGE and PROMOTE tables from gimmik.cc; the starting set is an example.

trialsreport, Table 4 · IPC over SHiP

Sixteen versions, one kept

Every version of GIMMIK I simulated, each across all nine traces. The submitted version is the top row, and none of the fifteen changes beat it. Select a row to see what changed and how it moved each benchmark.

  • submitted

  • vote thresholds and training cutoff

  • which clues the perceptron sees

  • sampler size

  • the state machine itself

21/21 (final): dead and alive thresholds at ±21, training stops at 24. Nothing that followed beat it.

IPC over SHiP per benchmark, 21/21 (final) against the submitted version
vs SHiPastargcclbmmcfmilcsoplex
submitted0.9951.0230.9991.1241.0301.079

Evidence

Lines copied from the ChampSim logs of the submitted runs (only the # labels are added), and the table computed from all 72 of them.

log · mcf, LRU against GIMMIKoutput/lru/mcf.trace.log · output/gimmik/mcf.trace.log
# LRU
CPU 0 cumulative IPC: 0.1593 instructions: 50000002 cycles: 313931089
LLC LOAD         ACCESS:    4202310 HIT:     660962 MISS:    3541348

# GIMMIK
CPU 0 cumulative IPC: 0.1825 instructions: 50000002 cycles: 273911508
LLC LOAD         ACCESS:    4202732 HIT:    1481863 MISS:    2720869
gimmik (LLC) storage: 116504 bits (14.2217 KB)
The same 50 million instructions of mcf, finished in 40,019,581 fewer cycles. LLC load hits went from 660,962 to 1,481,863.Notice: Look at the HIT column: 2.24 times as many loads served from the LLC, with almost the same number of loads.
log · soplex, LRU against GIMMIKoutput/lru/soplex.trace.log · output/gimmik/soplex.trace.log
# LRU
CPU 0 cumulative IPC: 0.6554 instructions: 50000000 cycles: 76288244
LLC LOAD         ACCESS:    1393887 HIT:     327389 MISS:    1066498

# GIMMIK
CPU 0 cumulative IPC: 0.7263 instructions: 50000000 cycles: 68846304
LLC LOAD         ACCESS:    1393898 HIT:     620099 MISS:     773799
soplex, GIMMIK's second-largest gain: +10.8% IPC over LRU, with LLC load hits up from 327,389 to 620,099.
log · speedup over LRU, all nine tracesprinted IPC from all 72 logs · report, Table 3
bench    LRU IPC    SHiP     DIP     BIP  GIMMIK
astar      0.557   0.998   0.994   0.895   0.993
gcc        0.922   1.002   1.004   1.005   1.026
gobmk      1.341   1.000   1.000   1.000   1.000
hmmer      3.859   1.000   0.995   0.999   1.001
lbm        1.213   0.998   1.031   1.031   0.997
mcf        0.159   1.019   1.083   1.087   1.146
milc       1.386   1.001   1.036   1.043   1.030
soplex     0.655   1.027   1.028   1.002   1.108
wrf        2.204   1.000   1.003   1.006   1.002
geomean        -   1.005   1.019   1.007   1.032
The numbers behind the chart above. GIMMIK has the highest geometric mean, and it gets there by winning big on mcf and soplex without BIP's 10.5% loss on astar.

Problems I hit

What went wrong, what I thought it was, how I narrowed it down, what it actually was, and what I changed.

DIP looked no better than LRU, because writebacks were voting

shaped GIMMIK's writeback rule
Symptom

My first DIP scored a geometric mean of 0.9997 against LRU: no gain at all. That made no sense, because BIP on its own, one of the two policies DIP chooses between, was 8.7% faster than LRU on mcf.

Isolated by

DIP picks a policy with : a counter called PSEL goes up on misses in LRU's leader sets and down on misses in BIP's. Splitting the LLC misses by type showed what PSEL was seeing. On mcf, BIP removed 10.9 demand misses per thousand instructions and added 11.1 writeback misses. Counted together, the two policies looked almost exactly equal, so PSEL had nothing to go on.

Root cause

Under LRU a line the L2 writes back is almost always still in the LLC, because it was inserted at the most-recent position. Under BIP most lines are inserted at the bottom, so they are evicted before the writeback arrives and the writeback misses. Those misses cost the program nothing, since nobody is waiting on a writeback, but PSEL was counting them as if they were BIP's fault.

Fix

Count only demand misses in PSEL, and use 64 leader sets per policy instead of 32. The geometric mean went from 0.9997 to 1.019, and DIP now follows BIP on mcf, lbm and milc and LRU on astar. The same rule is built into GIMMIK: a writeback never counts as reuse and never trains the perceptron.

Protecting hot lines for longer made GIMMIK slower

Symptom

It seemed obvious that lines hit many times deserve more protection than HOT gives them. I added a SUPER class above HOT, entered by a hit on a HOT line (8 states, still 3 bits), and the geometric mean over SHiP fell from 1.0273 to 1.0265. Making SUPER lines age only every second pass, so they outlast HOT by exactly four steps, was worse again at 1.0254.

Isolated by

In the half-rate version the loss was concentrated on gcc, where the load hit rate fell from 7.6% to 2.3%.

Root cause

Protecting frequently hit lines for longer did not help. The likely reason is the one that cost LFU 16.6% on astar: protection that outlasts a line's usefulness holds space that newer lines need. That explanation was not measured directly.

Fix

Kept six states. The opposite idea, demoting a predicted-dead line when it is hit (similar to the promotion rule in the multiperspective paper), was the worst version of all at 1.0166: a hit is proof of reuse, and overriding it with a prediction throws away a line that was just shown to be useful.

A smaller sampler starved the predictor

Symptom

Shrinking the sampler from 16 sets to 4 would have saved 9,024 bits (107,480 total). The geometric mean over SHiP dropped to 1.0187, under the 2% target, and lbm fell to 0.947.

Root cause

A quarter of the sampler sets means a quarter of the training samples for the same 1,024 weights, so each weight is trained far less often.

Fix

Kept 16 sampler sets, which still fit with 14,568 bits to spare. Removing the PC-history clue was the cheaper way to save storage: 3,584 bits for a 0.04% loss.

The baseline was FIFO, so every number had to be rerun

Symptom

A course announcement pointed out that the starter lru.cc only updated recency on misses, which makes it FIFO, not LRU.

Root cause

The same lru.cc manages the L1I, L1D, L2 and TLBs in every configuration, not just the LLC. Fixing it changed the whole memory hierarchy under every policy, including SHiP, so none of the earlier runs were comparable any more.

Fix

Corrected it to update recency on every fill and every hit except writeback hits, then re-simulated all eight policies on all nine traces. Every number on this page comes from runs made after the fix.

What I would do differently, and next

  • Test it on traces it never saw. GIMMIK was tuned on the same nine traces it was scored on, so the 2.73% over SHiP may be optimistic for other programs. Each benchmark was also a single 50M-instruction window, so phase behaviour outside that window is not captured.
  • Drop the PC-history clue. Three clues cost only 0.04%, and the best three-clue setting (19/19) came within 0.03% of the submitted version with 3.1% less storage.
  • Add bypass for streaming data. On lbm and milc GIMMIK serves only about 1% of demand accesses from the LLC. Not inserting lines that are never reused at all would protect the rest of the set from them.
  • Run it on a shared cache. Everything here is one core. With several cores competing for the LLC, the per-line reuse state and the PC clues would have to tell programs apart.

No public repository for this project.

A course assignment for CPEN 411 (computer architecture) at UBC, so the code is not public. The excerpts on this page are from the submitted gimmik.cc, and every number is from the submitted ChampSim logs.