Caching in Operating Systems Design Systems Programming David

  • Slides: 42
Download presentation
Caching in Operating Systems Design & Systems Programming David E. Culler CS 162 –

Caching in Operating Systems Design & Systems Programming David E. Culler CS 162 – Operating Systems and Systems Programming Lecture 19 October 13, 2014 Reading: A&D 9. 6 -7 HW 4 going out Proj 2 out today

Objectives • Recall and solidify understanding the concept and mechanics of caching. • Understand

Objectives • Recall and solidify understanding the concept and mechanics of caching. • Understand how caching and caching effects pervade OS design. • Put together all the mechanics around TLBs, Paging, and Memory caches • Solidify understanding of Virtual Memory 10/13/14 cs 162 fa 14 L 19 2

Review: Memory Hierarchy • Take advantage of the principle of locality to: – Present

Review: Memory Hierarchy • Take advantage of the principle of locality to: – Present as much memory as in the cheapest technology – Provide access at speed offered by the fastest technology Processor Core L 2 Cache L 1 Cache Registers Core L 3 Cache L 2 Cache (shared) L 1 Cache Registers Speed (ns): 0. 3 1 Size (bytes): 100 Bs 10 k. Bs 10/13/14 3 100 k. Bs 10 -30 MBs Main Memory (DRAM) 100 GBs cs 162 fa 14 L 19 Secondary Storage (SSD) 100, 000 (0. 1 ms) 100 GBs Secondary Storage (Disk) 10, 000 (10 ms) TBs 3

Examples • vmstat –s • top • mac-os utility/activity 10/13/14 cs 162 fa 14

Examples • vmstat –s • top • mac-os utility/activity 10/13/14 cs 162 fa 14 L 19 4

Where does caching arise in Operating Systems ? 10/13/14 cs 162 fa 14 L

Where does caching arise in Operating Systems ? 10/13/14 cs 162 fa 14 L 19 5

Where does caching arise in Operating Systems ? • Direct use of caching techniques

Where does caching arise in Operating Systems ? • Direct use of caching techniques – paged virtual memory (mem as cache for disk) – TLB (cache of PTEs) – file systems (cache disk blocks in memory) – DNS (cache hostname => IP address translations) – Web proxies (cache recently accessed pages) • Which pages to keep in memory? 10/13/14 cs 162 fa 14 L 19 6

Where does caching arise in Operating Systems ? • Indirect - dealing with cache

Where does caching arise in Operating Systems ? • Indirect - dealing with cache effects • Process scheduling – which and how many processes are active ? – large memory footprints versus small ones ? – priorities ? • Impact of thread scheduling on cache performance – rapid interleaving of threads (small quantum) may degrade cache performance • increase ave MAT !!! • Designing operating system data structures for cache performance. • All of these are much more pronounced with multiprocessors / multicores 10/13/14 cs 162 fa 14 L 19 7

MP $ Memory bus 10/13/14 $ $ P P *** $ P cs 162

MP $ Memory bus 10/13/14 $ $ P P *** $ P cs 162 fa 14 L 19 8

Working Set Model (Denning ~70) Address • As a program executes it transitions through

Working Set Model (Denning ~70) Address • As a program executes it transitions through a sequence of “working sets” consisting of varying sized subsets of the address space Time 10/13/14 cs 162 fa 14 L 19 9

Cache Behavior under WS model Hit Rate 1 0 • • 10/13/14 new working

Cache Behavior under WS model Hit Rate 1 0 • • 10/13/14 new working set fits Cache Size Amortized by fraction of time the WS is active Transitions from one WS to the next Capacity, Conflict, Compulsory misses Applicable to memory caches and pages. Others ? cs 162 fa 14 L 19 10

Another model of Locality: Zipf 20% 1 18% 0, 9 16% 0, 8 14%

Another model of Locality: Zipf 20% 1 18% 0, 9 16% 0, 8 14% 0, 7 12% 0, 6 10% 0, 5 pop a=1 8% 6% Hit Rate(cache) 4% 0, 4 0, 3 0, 2 2% 0, 1 0% 0 Estimated Hit Rate Popularity (% accesses) P access(rank) = 1/rank 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 Rank • Likelihood of accessing item of rank r is α 1/ra • Although rare to access items below the top few, there are so many that it yields a “heavy tailed” distribution. • Substantial value from even a tiny cache • Substantial misses from even a very large one 10/13/14 cs 162 fa 14 L 19 11

Where does caching arise in Operating Systems ? • Maintaining the correctness of various

Where does caching arise in Operating Systems ? • Maintaining the correctness of various caches • TLB consistent with PT across context switches ? • Across updates to the PT ? • Shared pages mapped into VAS of multiple processes ? 10/13/14 cs 162 fa 14 L 19 12

Going into detail on TLB 10/13/14 cs 162 fa 14 L 19 13

Going into detail on TLB 10/13/14 cs 162 fa 14 L 19 13

What Actually Happens on a TLB Miss? • Hardware traversed page tables: – On

What Actually Happens on a TLB Miss? • Hardware traversed page tables: – On TLB miss, hardware in MMU looks at current page table to fill TLB (may walk multiple levels) • If PTE valid, hardware fills TLB and processor never knows • If PTE marked as invalid, causes Page Fault, after which kernel decides what to do afterwards • Software traversed Page tables (ala MIPS) – On TLB miss, processor receives TLB fault – Kernel traverses page table to find PTE • If PTE valid, fills TLB and returns from fault • If PTE marked as invalid, internally calls Page Fault handler • Most chip sets provide hardware traversal – Modern operating systems tend to have more TLB faults since they use translation for many things 10/13/14 cs 162 fa 14 L 19 14

What happens on a Context Switch? • Need to do something, since TLBs map

What happens on a Context Switch? • Need to do something, since TLBs map virtual addresses to physical addresses – Address Space just changed, so TLB entries no longer valid! • Options? – Invalidate TLB: simple but might be expensive • What if switching frequently between processes? – Include Process. ID in TLB • This is an architectural solution: needs hardware • What if translation tables change? – For example, to move page from memory to disk or vice versa… – Must invalidate TLB entry! • Otherwise, might think that page is still in memory! 10/13/14 cs 162 fa 14 L 19 15

What TLB organization makes sense? CPU TLB Cache Memory • Needs to be really

What TLB organization makes sense? CPU TLB Cache Memory • Needs to be really fast – Critical path of memory access – Seems to argue for Direct Mapped or Low Associativity • However, needs to have very few conflicts! – With TLB, the Miss Time extremely high! – This argues that cost of Conflict (Miss Time) is much higher than slightly increased cost of access (Hit Time) • Thrashing: continuous conflicts between accesses – What if use low order bits of page as index into TLB? • First page of code, data, stack may map to same entry • Need 3 -way associativity at least? – What if use high order bits as index? 10/13/14 • TLB mostly unused for small cs 162 fa 14 programs L 19 16

TLB organization: include protection • How big does TLB actually have to be? –

TLB organization: include protection • How big does TLB actually have to be? – Usually small: 128 -512 entries – Not very big, can support higher associativity • TLB usually organized as fully-associative cache – Lookup is by Virtual Address – Returns Physical Address + other info • What happens when fully-associative is too slow? – Put a small (4 -16 entry) direct-mapped cache in front – Called a “TLB Slice” • When does TLB lookup occur relative to memory cache access? – Before memory cache lookup? – In parallel with memory cache lookup? 10/13/14 cs 162 fa 14 L 19 17

Reducing translation time further • As described, TLB lookup is in serial with cache

Reducing translation time further • As described, TLB lookup is in serial with cache lookup: Virtual Address 10 offset V page no. TLB Lookup V Access Rights PA P page no. offset 10 Physical Address • Machines with TLBs go one step further: they overlap TLB lookup with cache access. – Works because offset available early 10/13/14 cs 162 fa 14 L 19 18

Overlapping TLB & Cache Access (1/2) • Main idea: – Offset in virtual address

Overlapping TLB & Cache Access (1/2) • Main idea: – Offset in virtual address exactly covers the “cache index” and “byte select” – Thus can select the cached byte(s) in parallel to perform address translation virtual address Virtual Page # Offset physical address 10/13/14 tag / page # index cs 162 fa 14 L 19 byte 19

Overlapping TLB & Cache Access (1/2) • Here is how this might work with

Overlapping TLB & Cache Access (1/2) • Here is how this might work with a 4 K cache: assoc lookup 32 index TLB 20 page # 10 2 disp 00 4 K Cache 1 K 4 bytes Hit/ Miss PA 10/13/14 = cs 162 fa 14 L 19 PA Data Hit/ Miss 20

Putting Everything Together: Address Translation Virtual Address: Virtual P 1 index P 2 index

Putting Everything Together: Address Translation Virtual Address: Virtual P 1 index P 2 index Offset Physical Memory: Page. Table. Ptr Physical Address: Physical Page # Offset Page Table (1 st level) Page Table (2 nd level) 10/13/14 cs 162 fa 14 L 19 21

Putting Everything Together: TLB Virtual Address: Virtual P 1 index P 2 index Offset

Putting Everything Together: TLB Virtual Address: Virtual P 1 index P 2 index Offset Physical Memory: Page. Table. Ptr Physical Address: Physical Page # Offset Page Table (1 st level) Page Table (2 nd level) TLB: … 10/13/14 cs 162 fa 14 L 19 22

Putting Everything Together: Cache Virtual Address: Virtual P 1 index P 2 index Offset

Putting Everything Together: Cache Virtual Address: Virtual P 1 index P 2 index Offset Physical Memory: Page. Table. Ptr Physical Address: Physical Page # Offset tag Page Table (1 st level) Page Table (2 nd level) index byte cache: tag: block: TLB: … … 10/13/14 cs 162 fa 14 L 19 23

Admin: Projects • Project 1 – deep understanding of OS structure, threads, thread implementation,

Admin: Projects • Project 1 – deep understanding of OS structure, threads, thread implementation, synchronization, scheduling, and interactions of scheduling and synchronization – work effectively in a team • effective teams work together with a plan => schedule three 1 -hour joint work times per week • Project 2 – exe load and VAS creation provided for you – syscall processing, FORK+EXEC, file descriptors backing user file handles, ARGV • registers & stack frames – two development threads for team • but still need to work together 10/13/14 cs 162 fa 14 L 19 24

Virtual Memory – the disk level 10/13/14 cs 162 fa 14 L 19 25

Virtual Memory – the disk level 10/13/14 cs 162 fa 14 L 19 25

Reacall: the most basic OS function 10/13/14 cs 162 fa 14 L 19 26

Reacall: the most basic OS function 10/13/14 cs 162 fa 14 L 19 26

Loading an executable into memory disk (huge) memory info data code exe • .

Loading an executable into memory disk (huge) memory info data code exe • . exe – lives on disk in the file system – contains contents of code & data segments, relocation entries and symbols – OS loads it into memory, initializes registers (and initial stack pointer) – program sets up stack and heap upon initialization: CRT 0 10/13/14 cs 162 fa 14 L 19 27

Create Virtual Address Space of the Process disk (huge) process VAS memory kernel user

Create Virtual Address Space of the Process disk (huge) process VAS memory kernel user page frames stack heap data code sbrk user pagetable kernel code & data • Utilized pages in the VAS are backed by a page block on disk – called the backing store – typically in an optimized block store, but can think of it like a file 10/13/14 cs 162 fa 14 L 19 28

Create Virtual Address Space of the Process disk (huge, TB) process VAS (GBs) memory

Create Virtual Address Space of the Process disk (huge, TB) process VAS (GBs) memory kernel stack user page frames heap user pagetable heap data code kernel code & data • User Page table maps entire VAS • All the utilized regions are backed on disk – swapped into and out of memory as needed • For every process 10/13/14 cs 162 fa 14 L 19 29

Create Virtual Address Space of the Process disk (huge, TB) VAS – per process

Create Virtual Address Space of the Process disk (huge, TB) VAS – per process PT memory kernel stack user page frames heap user pagetable heap data code kernel code & data • User Page table maps entire VAS – resident pages to the frame in memory they occupy – the portion of it that the HW needs to access must be resident in memory 10/13/14 cs 162 fa 14 L 19 30

Provide Backing Store for VAS disk (huge, TB) VAS – per process memory kernel

Provide Backing Store for VAS disk (huge, TB) VAS – per process memory kernel stack user page frames heap user pagetable heap data code kernel code & data • User Page table maps entire VAS • Resident pages mapped to memory frames • For all other pages, OS must record where to find them on disk 10/13/14 cs 162 fa 14 L 19 31

What data structure is required to map non-resident pages to disk? • Find. Block(PID,

What data structure is required to map non-resident pages to disk? • Find. Block(PID, page#) => disk_block • Like the PT, but purely software • Where to store it? • Usually want backing store for resident pages too. • Could use hash table (like Inverted PT) 10/13/14 cs 162 fa 14 L 19 32

Provide Backing Store for VAS disk (huge, TB) VAS 1 PT 1 memory kernel

Provide Backing Store for VAS disk (huge, TB) VAS 1 PT 1 memory kernel stack heap data code heap VAS 2 PT 2 data code kernel user page frames user pagetable kernel co & data stack heap data 10/13/14 code cs 162 fa 14 L 19 33

On page Fault … disk (huge, TB) VAS 1 PT 1 memory kernel stack

On page Fault … disk (huge, TB) VAS 1 PT 1 memory kernel stack heap data code heap VAS 2 PT 2 data code kernel user page frames user pagetable kernel co & data stack active process & PT heap data 10/13/14 code cs 162 fa 14 L 19 34

On page Fault … find & start load disk (huge, TB) VAS 1 PT

On page Fault … find & start load disk (huge, TB) VAS 1 PT 1 memory kernel stack heap data code heap VAS 2 PT 2 data code kernel user page frames user pagetable kernel co & data stack active process & PT heap data 10/13/14 code cs 162 fa 14 L 19 35

On page Fault … schedule other P or T disk (huge, TB) VAS 1

On page Fault … schedule other P or T disk (huge, TB) VAS 1 PT 1 memory kernel stack heap data code heap VAS 2 PT 2 data code kernel user page frames user pagetable kernel co & data stack active process & PT heap data 10/13/14 code cs 162 fa 14 L 19 36

On page Fault … update PTE disk (huge, TB) VAS 1 PT 1 memory

On page Fault … update PTE disk (huge, TB) VAS 1 PT 1 memory kernel stack heap data code heap VAS 2 PT 2 data code kernel user page frames user pagetable kernel co & data stack active process & PT heap data 10/13/14 code cs 162 fa 14 L 19 37

Eventually reschedule faulting thread disk (huge, TB) VAS 1 PT 1 memory kernel stack

Eventually reschedule faulting thread disk (huge, TB) VAS 1 PT 1 memory kernel stack heap data code heap VAS 2 PT 2 data code kernel user page frames user pagetable kernel co & data stack active process & PT heap data 10/13/14 code cs 162 fa 14 L 19 38

Where does the OS get the frame? • Keeps a free list • Unix

Where does the OS get the frame? • Keeps a free list • Unix runs a “reaper” if memory gets too full • As a last resort, evict a dirty page first 10/13/14 cs 162 fa 14 L 19 39

How many frames per process? • Like thread scheduling, need to “schedule” memory resources

How many frames per process? • Like thread scheduling, need to “schedule” memory resources – allocation of frames per process • utilization? fairness? priority? – allocation of disk paging bandwith 10/13/14 cs 162 fa 14 L 19 40

Historical Perspective • Mainframes and minicomputers (servers) were “always paging” – memory was limited

Historical Perspective • Mainframes and minicomputers (servers) were “always paging” – memory was limited – processor rates <> disk xfer rates were much closer • When overloaded would THRASH – with good OS design still made progress • Modern systems hardly every page – primarily a safety net + lots of untouched “stuff” – plus all the other advantages of managing a VAS 10/13/14 cs 162 fa 14 L 19 41

Summary • Virtual address space for protection, efficient use of memory, AND multi-programming. –

Summary • Virtual address space for protection, efficient use of memory, AND multi-programming. – hardware checks & translates when present – OS handles EVERYTHING ELSE • Conceptually memory is just a cache for blocks of VAS that live on disk – but can never access the disk directly • Address translation provides the basis for sharing – shared blocks of disk AND shared pages in memory • How else can we use this mechanism? – sharing ? ? ? – disks transfers on demand ? ? ? – accessing objects in blocks using load/store instructions 10/13/14 cs 162 fa 14 L 19 42