lecture 2: operating system structure - ku ittcheechul/courses/eecs678/f16/slides/17...lecture 2:...
TRANSCRIPT
Memory Management
1
Disclaimer: some slides are adopted from book authors’ slides with permission
Recap: Page Replacement
• On a page fault
– Step 1: allocate a free page frame
• If there’s a free frame, use it
• If there’s no free frame, choose a victim frame and evict it to disk (if necessary) swap-out
– Step 2: bring the stored page on disk (if necessary)
– Step 3: update the PTE (mapping and valid bit)
– Step 4: restart the instruction
2
Recap: Page Replacement Policies
• FIFO– Evict the oldest page first. Pros: fair; Cons: can throw out frequently
used pages
• Optimal– Evict the page that will not be used for the longest period
– Pros: optimal; Cons: you need to know the future
• Random– Randomly choose a page. Pros: simple. TLB commonly uses this
method; Cons: unpredictable
• LRU– Look at the past history, choose the one that has not been used for the
longest period. Pros: good performance; Cons: complex, requires h/w support
3
Recap: Page Table Entry (PTE)
• PTE format (architecture specific)
– Valid bit (V): whether the page is in memory
– Modify bit (M): whether the page is modified
– Reference bit (R): whether the page is accessed
– Protection bits(P): readable, writable, executable
4
Page Frame NoV M R P
20 bits21 1 1
Recap: Clock Algorithm
• Key idea
– Replace an oldpage, not the oldest page
• Algorithm– Step 1: advance the pointer
by one
– Step 2: check the reference bit of the page:1 Used recently. Clear the bit and go to Step 10 Not used recently. Selected victim. End.
5
Quiz.
Reference E D H B D E D A E B E
Page #1 E E E
Page #2 D D
Page #3 H
Mark X for a fault X X X
6
• Complete the following with the FIFO replacement policy
Quiz.
7
• Complete the following with the FIFO replacement policy
Reference E D H B D E D A E B E
Page #1 E E E B B B B A A A A
Page #2 D D D * E E E * B B
Page #3 H H H H D D D D E
Mark X for a fault X X X X X X X X X
Concepts to Learn
• Memory-mapped I/O
• Copy-on-Write (COW)
• Memory allocator
8
Recap: Program Binary Sharing
• Multiple instances of the same program
– E.g., 10 bash shells9
Bash text
Physical memoryBash #1 Bash #2
Memory Mapped I/O
• Idea: map a file on disk onto the memory space
10
Memory Mapped I/O
• Benefits: you don’t need to use read()/write() system calls, just directly access data in the file via memory instructions
• How it works?
– Just like demand paging of an executable file
– What about writes?• Mark the modified (M) bit in the PTE
• Write back the modified pages back to the original file
11
Copy-on-Write (COW)
• Fork() creates a copy of a parent process
– Copy the entire pages on new page frames?
• If the parent uses 1GB memory, then a fork() call would take a while
• Then, suppose you immediately call exec(). Was it of any use to copy the 1GB of parent process’s memory?
12
Copy-on-Write
• Better way: copy the page table of the parent
– Page table is much smaller (so copy is faster)
– Both parent and child point to the exactly same physical page frames
13
parent child
Copy-on-Write
• What happens when the parent/child reads?
• What happens when the parent/child writes?
– Trouble!!!
14
parent child
Page Table Entry (PTE)
• PTE format (architecture specific)
– Valid bit (V): whether the page is in memory
– Modify bit (M): whether the page is modified
– Reference bit (R): whether the page is accessed
– Protection bits(P): readable, writable, executable
15
Page Frame NoV M R P
20 bits21 1 1
Copy-on-Write
• All pages are marked as read-only
16
parent child
RORORO
Page tbl
RORORO
Page tbl
Copy-on-Write• Up on a write, a page fault occurs and the OS copies
the page on a new frame and maps to it with R/W protection setting
17
parent child
RORORW
Page tbl
RORORO
Page tbl
RW
Kernel/User Virtual Memory
• Kernel memory
– Kernel code, data
– Identical to all address spaces
– Fixed 1-1 mapping of physical memory
• User memory
– Process code, data, heap, stack,...
– Unique to each address space
– On-demand mapping (page fault)
18
Kernel
User
0xFFFFFFFF
0xC0000000
0x00000000
User-level Memory Allocation
• When a process actuallyallocate a memory from thekernel?
– On a page fault
– Allocate a page (e.g., 4KB)
• What does malloc() do?
– Doesn’t physically allocate pages
– Manage a process’s heap
– Variable size objects in heap
19
Kernel-level Memory Allocation
• Page-level allocator (low-level)
– Page granularity (4K)
– Buddy allocator
• Other kernel-memory allocators
– Support fine-grained allocations
– Slab, kmalloc, vmalloc allocators
20
Kernel-Level Memory Allocators
21
Kernel code
kmallocArbitrary size objects
SLAB allocatorMultiple fixed-sized object caches
Page allocator (buddy)Allocate power of two pages: 4K, 8K, 16K, …
vmalloc(large) non-physically contiguous memory
Buddy Allocator
• Linux’s page-level allocator
• Allocate power of two number of pages: 1, 2, 4, 8, … pages.
• Request rounded up to next highest power of 2
• When smaller allocation needed than is available, current chunk split into two buddies of next-lower power of 2
• Quickly expand/shrink across the lists
22
4KB
8KB
16KB
32KB
Buddy Allocator
• Example
– Assume 256KB chunk available, kernel requests 21KB
23
32 A
32 Free
64 Free
128Free
32 Free
64 Free
128Free
32 Free
64 Free
128Free
64 Free
128Free
128Free
256Free
Buddy Allocator
• Example
– Free A
24
32 A
32 Free
64 Free
128Free
32 Free
64 Free
128Free
32 Free
64 Free
128Free
64 Free
128Free
128Free
256Free
Virtual Memory Summary
• MMU and address translation
• Paging
• Demand paging
• Copy-on-write
• Page replacement
25
Quiz: Address Translation
26
2nd level offset
8 bits 8 bits
1st level
8 bits
Virtual address format (24bits)
Frame # V
4 bits 3
Unused
1
Page table entry (8bit)
Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +A +B +C +D +E +F
0x000 31
0x010
0x020 41
..
0x100 00 01 01 00 01
..
0x200
Page-table base address = 0x100
Vaddr: 0x0703FEPaddr: 0x3FE
Vaddr: 0x072370Paddr: ???
Vaddr: 0x082370Paddr: ???
Quiz: Address Translation
27
2nd level offset
8 bits 8 bits
1st level
8 bits
Virtual address format (24bits)
Frame # V
4 bits 3
Unused
1
Page table entry (8bit)
Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +A +B +C +D +E +F
0x000 31
0x010
0x020 41
..
0x100 00 01 01 00 01
..
0x200
Page-table base address = 0x100
Vaddr: 0x0703FEPaddr: 0x3FE
Vaddr: 0x072370Paddr: 0x470
Vaddr: 0x082370Paddr: invalid