<<

Managing Free space External Fragmentation

Many segments, different processes, OS OS different sizes Over time, memory can become full 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 Preempt processes and reclaim Move images of suspended (pages and frames, respectively) 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 queue when memory needs are not a multiple of a page Suspended p2 Waiting swap in typical size of page/frame: 4KB to 16KB p1 swap out

Suspended Adjacent pages in VA (say, within the stack) queue Backing Store Semaphores/condition queues need not map to contiguous frames in PA! 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) } } } }

Interpret VA as comprised of two components To acce s s a by te 0 2 page: which page? 1 1 no. of bits specifies no. of pages in VA space extract page number 2 6 3 0 4 4 offset: which byte within that page? map that page number into a frame . . no. of bits specifies size of page/frame number using a page table . . extract offset . . . access byte at offset in frame . . . . . 220 -1 8 50 51

Physical Memory Basic Paging Page Table Entries f Cache Vali d Referenced Protection disable Frame number Present Dirty CPU f o Vali d/Invali d bit PTE: Frame Set if can reference that p o no. portion of VA space Protection Page table bits (R/W/X) Physical Frame # V P R D Page Table } memory (stores frame nos) Present bit 0 1 1 Set if page is mapped to a frame 1 1 1 0 2 1 1 3 p Referenced bit 3 1 1 1 1 f 4 1 1 Set if page has been referenced 5 1 1 0 2 6 0 0 The Page Table Dirty bit 7 0 0 5 3 8 0 0 lives in memory 4 Set if page has been modified 9 1 1 4 10 1 0 at the physical address stored 5 11 1 1 9 in the Page Table Base Register Cache disable bit PTBR 12 0 0 Set if page can’t be cached 6 PTBR saved/restored on 13 0 0 2 0 0 14 7 Protection bits (R/W/X) 15 0 0 11 52 53 Page Table Entries Sharing

Cache Referenced Protection disable Frame number Present Dirty By now, it’s old hat: Vali d/Invali d bit PTE: Frame Processes share pages by Set if process can reference that no. mapping virtual pages to portion of VA space Protection Physical Page Table Page table bits (R/W/X) Physical the same frame Page Table Memory Process 1 Frame # V P R D C Process 0 } Present bit 0 2 1 1 memory Fine tuning using Shared Set if page is mapped to a frame 1 1 1 1 0 Shared 2 6 1 1 3 protection bits (RWX) Referenced bit 3 0 1 1 1 1 4 4 1 1 We can refine COW to operate Set if page has been referenced 5 3 1 1 0 2 6 0 0 0 at the granularity of pages Dirty bit 7 0 0 0 5 3 8 0 0 0 on fork, mark all pages 4 Set if page has been modified On disk 9 5 1 1 4 read only 10 5 1 0 5 Cache disable bit 11 7 1 1 9 on write, copy only the 12 0 0 0 6 Set if page can’t be cached 13 0 0 0 2 affected page 14 0 0 0 7 Protection bits (R/W/X) 15 0 0 0 11 54 set W bit in both PTEs 55

0 PA Example Space Space Overhead

VA I 1 Space J Two sources, in tension: Page size: 4 bytes K 0 A L data structure overhead (the Page Table itself) Page B 2 fragmentation C Table How large should a page be? D 0 4 sequences of 1 E Overhead for paging: contiguous pages F 1 E 3 (#PTEs x sizeofEntry) + (#“segments” x pageSize/2) = G 3 F H = ((VA_Size/pagesize) x sizeofEntry) + (#“segments” x pageSize/2) 2 1 G 2 I H What makes up sizeofEntry? J A 4 bits to identify physical page [log2 (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) 56 D 57 Computing What’s not to love? Paging Overhead

1 MB maximum VA, 1 KB page, 3 “segments” (program, Space overhead stack, heap) With a 64-bit address space, size of page table can PA space is 64KB and PTE has 7 control bits be huge What is the Paging Overhead? Time overhead What before used to require one memory access, 20 10 9 ((2 / 2 ) x sizeofEntry) + (3 x 2 ) bytes now needs two sizeofEntry = 6 bits (26 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: 210 x 2 + 3 x 29 = one to access the actual physical address that (211 + 3 x 29) bytes contains the data of interest 58 59