Managing Caching for I/O
Jeff ChaseDuke University
Memory as a cache
memory(frames)
data
data
virtual address spaces
files and filesystems,databases,
other storage objects
disk and other storagenetwork RAM
page/block read/write accesses
backing storage volumes(pages and blocks)
Processes access external storage objects through file
APIs and VM abstraction. The OS kernel manages caching
of pages/blocks in main memory.
DBufferCache DBuffer
read(), write()startFetch(), startPush()waitValid(), waitClean()
DBuffer dbuf = getBlock(blockID)releaseBlock(dbuf)
The DeFiler buffer cache
Device I/O interfaceAsynchronous I/O to/from buffersblock read and writeBlocks numbered by blockIDs
File abstraction implemented in upper DFS layer.All knowledge of how files are laid out on disk is at this layer.Access underlying disk volume through buffer cache API.Obtain buffers (dbufs), write/read to/from buffers, orchestrate I/O.
read(), write()startFetch(), startPush()waitValid(), waitClean()
DBuffer dbuf = getBlock(blockID)releaseBlock(dbuf)sync()
create, destroy, read, write a dfilelist dfiles
Managing files
DBufferCache DBuffer
“inode”for DFileID
1. Fetch blocks for data and metadata (or zero new ones fresh) into cache buffers (dbufs).
2. Copy bytes to/from dbufs with read and write.
3. Track which data/metadata blocks are valid, and which valid blocks are clean and which are dirty.
4. Clean the dirty blocks by writing them back to the disk with push.
Page/block cache internals
HASH(blockID)
cache directory
List(s) of free buffers (bufs) or eviction candidates. These dbufs might be listed in the cache directory if they contain useful data, or not, if they are truly free.
To replace a dbufRemove from free/eviction list.Remove from cache directory.Change dbuf blockID and status.Enter in directory w/ new blockID.Re-register on eviction list.Beware of concurrent accesses.
Anatomy of a read
1. Compute(user mode)
2. Enter kernel for read syscall.
3. getBlock for maps, traverse cached maps,
getBlock for data, and start fetch.
seek transfer
4. sleep for I/O (stall)
5. Copy data to user buffer in read.
CPU
Disk
6. Return to user program.
Time
Prefetching for high read throughput
• Read-ahead (prefetching)– Fetch blocks into the cache in expectation that they will be used.
– Requires prediction. Common for sequential access.
1. Detect access pattern.
2. Start prefetchingReduce I/O stalls
Sequential read-ahead
n n+1
App requests block nApp requests block n+1
n+2
System prefetches block n+2
System prefetches block n+3
• Prediction is easy for sequential access.
• Read-ahead also helps reduce seeks by reading larger chunks if data is laid out sequentially on disk.
Page/block Caching Policy
Each thread/process/job utters a stream of page/block references.– reference string: e.g., abcabcdabce..
The OS tries to minimize the number of fetches/faults.– Try to arrange for the resident set of blocks to match the set of
blocks that will be needed in the near future.
Replacement policy is the key.– On each access, select a victim block to evict from memory;
read the new block into the victim’s frame/dbuf.
– Simple: replace the page whose next reference is furthest in the future (OPT). It is provably optimal.
Selecting a victim: policy
• The oldest block? (FIFO policy)
• The coldest block? (Least Recently Used)
• The hottest block? (Most Recently Used)?
• The least popular block? (Least Frequently Used)
• A random block?
• A block that has not been used recently?
X Y Z A Z B C D E Z A B C D E
Selecting a victim: policy
• The oldest block? (FIFO policy)– X Y Z A (evict X) Z (evict Y) B (evict Z) C Z…
• The coldest block? (Least Recently Used)– X Y Z A (evict X) Z (evict Y) B (evict A) C Z…
• The hottest block? (Most Recently Used)?– Consider: A B C D E A B C D E …
• The least popular block? (Least Frequently Used)
• A random block?
• A block that has not been used recently?
Replacement policy: file systems
• File systems often use a variant of LRU.– A file system sees every block access (through syscall API), so it
can do full LRU: move block to tail of LRU list on each access.
• Sequential files have a cache wiping effect with LRU.– Most file systems detect sequential access and prefer eviction of
blocks from the same file, e.g., using MRU.
– That also prevents any one file/object from consuming more than its “fair share” of the cache.
VM systems
• VM memory management is similar to file systems.– Page caching in physical memory frames
– Unified with file block caching in most systems
– Virtual address space is a collection of regions/segments, which may be considered as “objects” similar to files.
• Only it’s different.– Mapped by page tables
– VM system software does not see most references, since they are accelerated by Memory Management Unit hardware.
– Requires a sampling approximation for page replacement.
– All data goes away on a system failure: no write atomicity.
VM Page Tables: Cartoon View
PFN 0PFN 1
PFN i
page #i offset
user virtual address
PFN i+
offset
process page table (map)
physical memorypage frames
In this example, each VPN j maps to PFN j, but in practice any physical frame may be used for
any virtual page.
Each process/VAS has its own page table.
Virtual addresses are translated relative to
the current page table.
The maps are themselves stored in memory; a protected
register holds a pointer to the current map.
Example: Windows/IA32
• Each address space has a page directory
• One page: 4K bytes, 1024 4-byte entries (PTEs)
• Each PDIR entry points to a “page table”
• Each “page table” is one page with 1024 PTEs
• each PTE maps one 4K page of the address space
• Each page table maps 4MB of memory: 1024*4K
• One PDIR for a 4GB address space, max 4MB of tables
• Load PDIR base address into a register to activate the VAS
Top-level page table
[from Tanenbaum]
32 bit address with 2 page table fields
Two-level page tables
Virtual Address Translation
VPN offset12
Example: typical 32-bitarchitecture with 4KB pages.
addresstranslatio
n
Virtual address translation maps a virtual page number (VPN) to a physical page frame number (PFN): the rest is easy.
PFN
offset
+
0
physical address {
Deliver exception toOS if translation is notvalid and accessible inrequested mode.
Virtual Addressing: Under the Hood
raiseexception
probepage table
loadTLB
probe TLB
accessphysicalmemory
accessvalid?
pagefault?
signalprocess
allocateframe
page ondisk?
fetchfrom disk
zero-fillloadTLB
starthere
MMU
OS
LRU Approximations for Paging
• Pure LRU and LFU are prohibitively expensive to implement.– most references are hidden by the TLB
– OS typically sees less than 10% of all references
– can’t tweak your ordered page list on every reference
• Most systems rely on an approximation to LRU for paging.– periodically sample the reference bit on each page
• visit page and set reference bit to zero
• run the process for a while (the reference window)
• come back and check the bit again
– reorder the list of eviction candidates based on sampling
VM page replacement
• Try to guess the working set of pages in active use for each VAS.
• To determine if a page is being used, arrange for MMU to notify OS on next use.
– E.g., reference bit, or disable read access to trigger a fault.
• Sample pages systematically to approximate LRU: e.g., CLOCK algorithm, or FIFO-with-Second-Chance (FIFO-2C)
Page fault rate by resident set size
Page fault rate over timeThreads in an address space may change their working sets.
FIFO-2C in Action (FreeBSD)
What Do the Pretty Colors Mean?
This is a plot of selected internal kernel events during a run of a process that randomly reads/writes its virtual memory.– x-axis: time in milliseconds (total run is about 3 seconds)
– y-axis: each event pertains to a physical page frame, whose PFN is given on the y-axis
The machine is an Alpha with 8000 8KB pages (64MB total)
The process uses 48MB of virtual memory: force the paging daemon to do FIFO-2C bookkeeping, but little actual paging.
– events: page allocate (yellow-green), page free (red), deactivation (duke blue), reactivation (lime green), page clean (carolina blue).
What to Look For– Some low physical memory ranges are reserved to the kernel.
– Process starts and soaks up memory that was initially free.
– Paging daemon evicts pages allocated to other processes, and the system reallocates the frames to the test process.
– After an initial flurry of demand-loading activity, things settle down after most of the process memory is resident.
– Paging daemon begins to scan more frequently as memory becomes overcommitted (dark blue deactivation stripes).
– Test process touches pages deactivated by the paging daemon, causing them to be reactivated.
– Test process exits (heavy red bar).
page alloc
deactivate
activate
clean
free
“Filers”
• Network-attached (IP)
• RAID appliance
• Multiple protocols– iSCSI, NFS, CIFS
• Admin interfaces
• Flexible configuration
• Lots of virtualization: dynamic volumes
• Volume cloning, mirroring, snapshots, etc.
• NetApp technology leader since 1994 (WAFL)
Network File System
[ucla.edu]