Managing Free space External Fragmentation Many segments, different processes, OS OS Over time, memory can become full different sizes of small holes OS tracks free memory blocks (“holes”) Hard to fit more segments Initially, one big hole Hard to expand existing ones Many strategies to fit segment into free Compaction memory (think “assigning classrooms to courses”) Relocate segments to coalesce holes First Fit: first big-enough hole Next Fit: Like First Fit, but starting from where you left off Best Fit: smallest big-enough hole Worst Fit: largest big-enough hole 42 43 External Fragmentation External Fragmentation OS OS Over time, memory can become full Over time, memory can become full of small holes of small holes Hard to fit more segments Hard to fit more segments Hard to expand existing ones Hard to expand existing ones Compaction Compaction Relocate segments to coalesce holes Relocate segments to coalesce holes Copying eats up a lot of CPU time! if 4 bytes in 10ns, 8 GB in 20s! But what if a segment wants to grow? 44 45
Eliminating External Paging Fragmentation: Swapping Allocate VA & PA memory in fixed-sized chunks (pages and frames, respectively) Preempt processes and reclaim Move images of suspended their memory processes to swap space free frames can be tracked using a simple bitmap on backing store 0011111001111011110000 one bit/frame no more external fragmentation! Ready Running OS but now internal fragmentation (you just can’ t win…) Ready when memory needs are not a multiple of a page queue Suspended p 2 typical size of page/frame: 4KB to 16KB Waiting swap in p 1 swap out Adjacent pages in VA (say, within the stack) Suspended Backing Store queue need not map to contiguous frames in PA! Semaphores/condition queues 46 47 Virtual address Virtual address 32 bits p (20 bits) o (12 bits) } } } Interpret VA as comprised of two components Interpret VA as comprised of two components page: which page? page: which page? no. of bits specifies no. of pages in VA space offset: which byte within that page? offset: which byte within that page? 48 49
Virtual address Virtual address p (20 bits) o (12 bits) p (20 bits) o (12 bits) } } } } Page Table Interpret VA as comprised of two components To access a byte 0 2 page: which page? 1 1 extract page number 2 6 no. of bits specifies no. of pages in VA space 3 0 map that page number into a frame offset: which byte within that page? 4 4 . number using a page table . no. of bits specifies size of page/frame . . extract offset . . . access byte at offset in frame . . . . . 8 2 20 -1 50 51 Physical Memory Basic Paging Page Table Entries Cache Referenced Valid Protection Frame number disable Present Dirty CPU f o Valid/Invalid bit PTE: Frame Set if process can reference that no. p portion of VA space o Protection Page table Physical bits (R/W/X) Page Table Frame # V P R D C Present bit } memory 0 1 1 (stores frame nos) 1 1 Set if page is mapped to a frame 1 3 0 1 1 2 p 1 Referenced bit 3 1 1 1 4 1 1 0 Set if page has been referenced 5 1 1 2 6 0 0 The Page Table 5 Dirty bit 0 0 3 7 8 0 0 lives in memory 4 Set if page has been modified 4 1 1 9 1 0 at the physical address stored 10 9 Cache disable bit 5 11 1 1 PTBR in the Page Table Base Register 0 0 12 2 Set if page can’ t be cached 6 0 0 PTBR saved/restored on 13 0 0 14 11 context switch Protection bits (R/W/X) 7 15 0 0 52 53
Page Table Entries Sharing Cache Referenced Protection Frame number disable Present Dirty By now, it’ s old hat: Valid/Invalid bit PTE: Processes share pages by Frame Set if process can reference that no. mapping virtual pages to portion of VA space Physical Page Table Protection the same frame Physical Page table Page Table Memory Process 1 bits (R/W/X) Frame # V P R D C Process 0 Present bit memory } Fine tuning using 0 2 1 1 Shared Shared 1 1 1 1 3 Set if page is mapped to a frame 0 protection bits (RWX) 2 6 1 1 1 Referenced bit 3 0 1 1 1 We can refine COW to operate 4 4 1 1 0 Set if page has been referenced 3 1 1 5 2 at the granularity of pages 6 0 0 0 5 Dirty bit 0 0 0 7 3 on fork, mark all pages 0 0 0 8 4 Set if page has been modified 4 read only On disk 5 1 1 9 5 1 0 10 9 Cache disable bit 5 7 1 1 on write, copy only the 11 0 0 0 12 2 Set if page can’ t be cached affected page 6 0 0 0 13 0 0 0 14 11 7 Protection bits (R/W/X) set W bit in both PTEs 15 0 0 0 54 55 0 Example Space Overhead PA Space VA I 1 J Space Page size: 4 bytes Two sources, in tension: K 0 A data structure overhead (the Page Table itself) L Page B fragmentation 2 C Table How large should a page be? D 0 4 sequences of Overhead for paging: E 1 contiguous pages F E 1 3 (#PTEs x sizeofEntry) + (#“segments” x pageSize/2) = 3 G F = ((VA_Size/pagesize) x sizeofEntry) + (#“segments” x pageSize/2) H G 2 1 I H 2 What makes up sizeofEntry? J A 4 bits to identify physical page [log 2 (PA_Size / frame (aka page) size)] K B control bits (Valid, Present, Dirty, Referenced, etc) L C usually word or byte aligned (so, however many bits are needed to make it so) D 57 56
Computing What’ s not to love? Paging Overhead Space overhead 1 MB maximum VA, 1 KB page, 3 “segments” (program, stack, heap) With a 64-bit address space, size of page table can be huge PA space is 64KB and PTE has 7 control bits Time overhead What is the Paging Overhead? What before used to require one memory access, ((2 20 / 2 10 ) x sizeofEntry) + (3 x 2 9 ) bytes now needs two sizeofEntry = 6 bits (2 6 frames) + 7 control bits one to access the correct PTE and retrieve the byte aligned size of PTE entry: 16 bits correct frame number Overhead: 2 10 x 2 + 3 x 2 9 = one to access the actual physical address that contains the data of interest (2 11 + 3 x 2 9 ) bytes 58 59
Recommend
More recommend