Download presentation
Presentation is loading. Please wait.
Published byJoel Small Modified over 9 years ago
1
Managing Caching for I/O Jeff Chase Duke University
2
Memory as a cache memory (frames) data virtual address spaces files and filesystems, databases, other storage objects disk and other storage network 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.
3
DBufferCache DBuffer read(), write() startFetch(), startPush() waitValid(), waitClean() DBuffer dbuf = getBlock(blockID) releaseBlock(dbuf) The DeFiler buffer cache Device I/O interface Asynchronous I/O to/from buffers block read and write Blocks 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.
4
read(), write() startFetch(), startPush() waitValid(), waitClean() DBuffer dbuf = getBlock(blockID) releaseBlock(dbuf) sync() create, destroy, read, write a dfile list 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.
5
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 dbuf Remove 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.
6
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. seektransfer 4. sleep for I/O (stall) 5. Copy data to user buffer in read. CPU Disk 6. Return to user program. Time
7
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 prefetching Reduce I/O stalls
8
Sequential read-ahead n n+1 App requests block n App 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.
9
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.
10
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
11
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?
12
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.
13
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.
14
VM Page Tables: Cartoon View PFN 0 PFN 1 PFN i page #i offset user virtual address PFN i + offset process page table (map) physical memory page 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.
15
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
16
Top-level page table [from Tanenbaum] 32 bit address with 2 page table fields Two-level page tables
17
Virtual Address Translation VPNoffset 12 Example: typical 32-bit architecture with 4KB pages. address translation 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 to OS if translation is not valid and accessible in requested mode.
18
Virtual Addressing: Under the Hood raise exception probe page table load TLB probe TLB access physical memory access valid? page fault? signal process allocate frame page on disk? fetch from disk zero-fill load TLB start here MMU OS
19
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
20
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)
21
Page fault rate by resident set size
22
Page fault rate over time Threads in an address space may change their working sets.
23
FIFO-2C in Action (FreeBSD)
24
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).
25
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).
26
page alloc
27
deactivate
28
activate
29
clean
30
free
31
“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)
32
Network File System [ucla.edu]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.