Room 8 · The ARC

Why doesn’t a big read empty my cache?

ZFS keeps the blocks it has read in memory, in a cache called the ARC, so that the next read of the same block does not go to the disk. When the cache is full, it has to choose which block to drop. A simple cache drops the block used longest ago. With that rule, one large read, such as a backup or a scan of a big file, fills the whole cache with blocks that will not be read again and pushes out the blocks your programs use all the time.

The ARC keeps two lists instead of one. A block read once goes on the first list, called MRU (most recently used). A block read again later moves to the second list, MFU (most frequently used). Each list has a target share of the cache. When the cache is full, MRU gives up its oldest block if it is over its share, and MFU gives up its oldest block otherwise. A scan only adds blocks read once, so they go into MRU and push each other out, while the blocks in MFU stay.

The shares are not fixed. When a block is pushed out, the ARC frees its data but keeps a small record of it, its header, on a ghost list. There is one ghost list for MRU and one for MFU. A read of a block that is still on a ghost list goes to the disk, but it shows that a larger list would have kept the block, so the ARC moves a little of the cache towards that list. A workload that keeps coming back to recent blocks makes MRU grow, and one that returns to older, often-read blocks makes MFU grow.

1 Read, scan, read again

The cache below holds 16 blocks. It starts with a hot set, H1 to H6. Each of these blocks was read twice, one second apart, so all six are in MFU. Each list runs from its head, the block used last, to its tail, the block that goes first. Press Next read to read one block. The block read is outlined, and a block it pushed out has a dashed outline. A clock moves one second before each read you make, or 10 ms if you choose.

Start with Scan 12 new blocks, then press Next read for each block of the scan. After S10 the cache is full. From S11 on, each new block pushes the oldest scan block out of MRU, and the hot set stays in MFU.

Then select S2 on the MRU ghost list. It is read from the disk again and goes to MFU. The MRU target rises from 8 to 8.5 blocks, and MRU, still over that target, pushes S3 out. Then read S12, S11 and S10 again, which moves them to MFU, and press Next read once more: the new block N1 makes MFU give up H1. Read H1 back from the MFU ghost list, and the MRU target goes back down to 8 blocks.

Last, choose 10 ms, press Next read, then select the new block in MRU to read it again. The second read comes too soon to count as a second use, so the block stays in MRU.

MRU 0 blocks and MFU 6, of 16; MRU ghost 0 headers and MFU ghost 0. MRU target: 8 blocks of 16 (arc_pd 50%).

MRU ghost

headers of blocks pushed out of MRU

0 headers, no data · at most 0 blocks · ghost hits 0 KiB

empty

MRU

read once, recently

0 blocks, data in memory

empty

MFU

read more than once

6 blocks, data in memory

  1. H6
  2. H5
  3. H4
  4. H3
  5. H2
  6. H1

MFU ghost

headers of blocks pushed out of MFU

0 headers, no data · at most 0 blocks · ghost hits 0 KiB

empty

Forgotten

headers destroyed: the cache no longer knows these blocks

empty

Text version of this figure

The cache holds 16 blocks of 128 KiB. Clock: 2.01 s. Each list from head (most recent) to tail (pushed out first):

  1. MRU ghost, 0: empty.
  2. MRU, 0: empty.
  3. MFU, 6: H6, H5, H4, H3, H2, H1.
  4. MFU ghost, 0: empty.

Forgotten (headers destroyed), most recent first: empty.

MRU target: 8 blocks of 16 (arc_pd 50%). Ghost hits so far: MRU ghost 0 KiB, MFU ghost 0 KiB.

In the code

The ARC is in arc.c. Open a Source note to see the lines, taken from release zfs-2.4.4.

Read once, read twice

A read that finds its block in MRU goes through arc_access. If more than about 62 ms have passed since the block’s last counted read, the block moves to MFU. Reads closer together count as one use, so a program that reads the same block several times in a burst does not make it look popular.

Source arc_access, a hit in MRU, arc.c
arc_access Excerpt of module/zfs/arc.c, lines 5423–5428, OpenZFS zfs-2.4.4. View on GitHub
		if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access +
		    ARC_MINTIME)) {
			hdr->b_l1hdr.b_arc_access = now;
			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
			arc_change_state(arc_mfu, hdr);
		}
  1. L5423–L5424 b_arc_access is the time of the last counted read. ARC_MINTIME is hz >> 4, “62 ms”. The test is strict: exactly 62 ms does not promote.
  2. L5425–L5427 The time is refreshed and the header moves to MFU. Otherwise nothing changes, not even b_arc_access, so a series of re-reads is measured from the first one. A new block enters MRU a few lines above, and a hit on an MFU block stays in MFU.

Which list gives up a block

When the cache is over its target size, arc_c, an eviction pass runs in arc_evict. MRU’s target is a fraction of the cache, arc_pd. MRU gives up blocks from its tail only while it is over that target, and only as many as the cache is over. MFU gives up the rest. This is how a scan stays in MRU: each new scan block puts the cache one block over, and MRU, which is over its target, gives up its own oldest block.

Source arc_evict, MRU then MFU, arc.c
arc_evict Excerpt of module/zfs/arc.c, lines 4543–4556, OpenZFS zfs-2.4.4. View on GitHub
	/* Evict MRU data. */
	wt -= m - total_evicted;
	w = wt * (int64_t)(arc_pd >> 16) >> 16;
	e = MIN((int64_t)(asize - ac), (int64_t)(mrud - w));
	bytes = arc_evict_impl(arc_mru, ARC_BUFC_DATA, e);
	total_evicted += bytes;
	mrud -= bytes;
	asize -= bytes;

	/* Evict MFU data. */
	e = asize - ac;
	bytes = arc_evict_impl(arc_mfu, ARC_BUFC_DATA, e);
	mfud -= bytes;
	total_evicted += bytes;
  1. L4545 The MRU data target: wt, what can be evicted, times arc_pd, a fraction of 232. In the figure wt is arc_c, 16 blocks, and arc_pd starts at one half.
  2. L4546–L4547 At most what MRU holds over its target, and no more than the cache is over arc_c. A negative amount evicts nothing. In the figure’s first scan MRU keeps 10 blocks against a target of 8, because each new block puts the cache only one block over.
  3. L4553–L4554 MFU gives up what is still over. Before these lines the same steps run for metadata, which the figure does not have.

Pushed out, but not forgotten

Evicting a block in arc_evict_hdr frees its data, and its header moves to the matching ghost list: MRU ghost for an MRU block, MFU ghost for an MFU block. The header stays in the ARC’s hash table, so a later read of the same block finds it.

Source arc_evict_hdr, arc.c
arc_evict_hdr Excerpt of module/zfs/arc.c, lines 3876–3882, OpenZFS zfs-2.4.4. View on GitHub
	if (hdr->b_l1hdr.b_pabd != NULL)
		arc_hdr_free_abd(hdr, B_FALSE);

	if (HDR_HAS_RABD(hdr))
		arc_hdr_free_abd(hdr, B_TRUE);

	arc_change_state(evicted_state, hdr);
  1. L3876–L3880 The data is freed, and the raw copy too if the block has one.
  2. L3882 evicted_state is chosen just above: MRU ghost for MRU, MFU ghost for MFU. Only the header of an uncached buffer is destroyed here.

How many ghosts to keep

At the end of the same pass, each ghost list is trimmed. A ghost list as large as the whole cache would remember reads from too far back, so the code holds each one to half of the other lists. In the figure that is half of MFU for the MRU ghost list, and half of MRU for the MFU ghost list. A header trimmed off a ghost list is destroyed. Reading that block again is a new block to the ARC, with no ghost hit.

Source arc_evict, the ghost lists, arc.c
arc_evict Excerpt of module/zfs/arc.c, lines 4561–4572, OpenZFS zfs-2.4.4. View on GitHub
	 * Size of each state's ghost list represents how much that state
	 * may grow by shrinking the other states.  Would it need to shrink
	 * other states to zero (that is unlikely), its ghost size would be
	 * equal to sum of other three state sizes.  But excessive ghost
	 * size may result in false ghost hits (too far back), that may
	 * never result in real cache hits if several states are competing.
	 * So choose some arbitraty point of 1/2 of other state sizes.
	 */
	gsrd = (mrum + mfud + mfum) / 2;
	e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]) -
	    gsrd;
	(void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_DATA, e);
  1. L4565–L4567 The reason for the half: a larger ghost list would give ghost hits from too far back.
  2. L4569–L4572 The target of the MRU data ghost list, gsrd, is half of the three other live lists: in the figure, which has no metadata, half of MFU. The list is trimmed to it from its tail, and the trimmed headers are destroyed. The MFU ghost list gets half of MRU in the same way. The value is kept for the next pass.

A ghost hit

A read that finds its block’s header on a ghost list is still a miss, and the data is read from the disk. arc_access adds the block’s size to that ghost list’s hit counter and puts the block back in the cache, in MFU, since this is its second use. It changes no target itself. It only adds up bytes, and the next eviction pass reads them.

Source arc_access, an MRU ghost hit, arc.c
arc_access Excerpt of module/zfs/arc.c, lines 5432–5441, OpenZFS zfs-2.4.4. View on GitHub
		 * This buffer has been accessed once recently, but was
		 * evicted from the cache.  Would we have bigger MRU, it
		 * would be an MRU hit, so handle it the same way, except
		 * we don't need to check the previous access time.
		 */
		hdr->b_l1hdr.b_mru_ghost_hits++;
		ARCSTAT_BUMP(arcstat_mru_ghost_hits);
		hdr->b_l1hdr.b_arc_access = now;
		wmsum_add(&arc_mru_ghost->arcs_hits[arc_buf_type(hdr)],
		    arc_hdr_size(hdr));
  1. L5432–L5435 The comment gives the idea: with a bigger MRU, this read would have been a hit.
  2. L5437–L5441 Counted on the header, and the block’s size (its compressed size if it is compressed) added to the MRU ghost list’s hit counter, which the figure shows under each ghost list. Then the header goes to MFU, or to MRU if the read was a prefetch. A hit on the MFU ghost list does the same with that list’s counter.

From ghost hits to a new share

At the start of each eviction pass, arc_evict takes the ghost hits counted since the previous pass and moves the shares with arc_evict_adj. Hits on the MRU ghost list raise MRU’s share of data, arc_pd, and hits on the MFU ghost list lower it. arc_meta and arc_pm do the same for metadata, which the figure leaves out.

Source arc_evict, the balance, arc.c
arc_evict Excerpt of module/zfs/arc.c, lines 4493–4497, OpenZFS zfs-2.4.4. View on GitHub
	/* Adjust ARC states balance based on ghost hits. */
	arc_meta = arc_evict_adj(arc_meta, gsrd + gsrm + gsfd + gsfm,
	    grm + gfm, grd + gfd, zfs_arc_meta_balance);
	arc_pd = arc_evict_adj(arc_pd, gsrd + gsfd, grd, gfd, 100);
	arc_pm = arc_evict_adj(arc_pm, gsrm + gsfm, grm, gfm, 100);
  1. L4494–L4495 arc_meta, the share of metadata in the cache: up with metadata ghost hits, down with data ghost hits, divided by zfs_arc_meta_balance / 100, 5 by default.
  2. L4496 arc_pd, the MRU share of data and the figure’s gauge: up with MRU ghost data hits (grd), down with MFU ghost data hits (gfd), both counted since the previous pass. The second argument is the size of the two ghost lists’ targets at the previous pass.
  3. L4497 arc_pm, the same for metadata. All three are fractions of 232 and start at 1/4, 1/2 and 1/2. They replace the arc_p of older releases.

A small step each time

arc_evict_adj moves a share by about the hit bytes divided by the ghost lists’ targets, and never by more than about 1/32 at once. In the figure one ghost hit of 128 KiB, against ghost-list targets of 8 blocks in all, reaches that limit: arc_pd goes from 50% to 53.125%, and the MRU target from 8 to 8.5 blocks.

Source arc_evict_adj, arc.c
arc_evict_adj Excerpt of module/zfs/arc.c, lines 4412–4422, OpenZFS zfs-2.4.4. View on GitHub
	/*
	 * We should not have more ghost hits than ghost size, but they may
	 * get close.  To avoid overflows below up/down should not be bigger
	 * than 1/5 of total.  But to limit maximum adjustment speed restrict
	 * it some more.
	 */
	if (up + down >= total / 16) {
		uint64_t scale = (up + down) / (total / 32);
		up /= scale;
		down /= scale;
	}
  1. L4412–L4416 Two reasons for the limit: no overflow in the arithmetic below, and a cap on how fast the shares move.
  2. L4418–L4422 Hits of 1/16 of the ghost total or more are scaled down to about 1/32 of it. In the figure a hit of 217 bytes against a total of 220 is scaled by 4 to 215, which moves arc_pd by 227, 1/32 of its range. The rest of the function also slows a share as it nears 0 or 1.

A second cache on fast disks

A cache device, the L2ARC, can extend the ARC, on solid-state disks for instance. Eviction does not write to it. A feed thread copies blocks from the tails of MRU and MFU to the device before they are pushed out, and a ghost header whose block is on the device is kept when it is trimmed.

Source The L2ARC design comment, arc.c
Level 2 ARC Excerpt of module/zfs/arc.c, lines 8266–8270, OpenZFS zfs-2.4.4. View on GitHub
 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
 * It does this by periodically scanning buffers from the eviction-end of
 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
 * not already there. It scans until a headroom of buffers is satisfied,
 * which itself is a buffer for ARC eviction. If a compressible buffer is
  1. L8266–L8268 The feed thread, l2arc_feed_thread, wakes about once a second and reads the lists from their tails, the end that goes first. Point 1 of the same comment says that eviction writes nothing to the device.
  2. L8269–L8270 It copies only what is not already there, and keeps a margin ahead of eviction. A ghost header whose block is on the device stays as L2C only, and a read of it goes back to MRU.

What the figure leaves out

The figure runs the functions above over data blocks only, all of 128 KiB and uncompressed. The metadata lists are empty, so the metadata shares, arc_meta and arc_pm, change nothing and are not drawn. Each state is one list. The code spreads headers over several sublists and evicts from them in turn, so its order is close to the order of last use but not exactly that order. Here a pass runs right after each read that puts the cache over its size. In the code a thread runs it, woken once the cache is over by more than the largest record size, 16 MiB on 64-bit systems, which at the figure’s scale would be eight caches. arc_c is fixed, while the code grows it as data arrives and shrinks it under memory pressure. The clock is logical, and each block is released as soon as it is read. There is no prefetch, no uncached or dirty buffer, no compressed or encrypted copy and no L2ARC.