Download CS 3204 OS: Lecture 19 - Virtual Memory, Paging Techniques, and Demand Paging - Prof. Godm and more Study notes Operating Systems in PDF only on Docsity!
CS 3204
Operating Systems
Godmar Back
Lecture 19
CS 3204 Spring 2007 3/27/2007 2
Announcements
⢠Midterm March 29
- See announcement + sample midterms on classwebsite
⢠Fix remaining project 2 bugs (if any)
- Reminder: by end of semester, passing students willhave provided a deliverable that achieves >= 90%
test score on project 2 (or a 100% test score onproject 3 or 4ās regression tests.) ā multi-oom not part
of these
⢠Project 3 Design Milestoneā Will return before midterm
Virtual Memory
Paging Techniques (contād)
CS 3204 Spring 2007 3/27/2007 4
Demand paging
⢠Idea: only keep data in memory thatās being
used
- Needed for virtualization ā donāt use up physicalmemory for data processes donāt access
⢠Requires that actual allocation of physical page
frames be delayed until first access
⢠Many variations
- Lazy loading of text & data, mmapped pages & newlyallocated heap pages
- Copy-on-write
CS 3204 Spring 2007 3/27/2007 5
ustack (1)
Lazy Loading
kernelkernel
kernelkernel ucode (1)
kcodekdata
kheapkbss
C
C
FFFFFFFF
3 GB
1 GB
used
free user (1)user (1) udata (1)
user (1)
P1 Pintos loads the first process ā¦
Pintos then starts the firstprocess ⦠Process faults because codepage is not present ⦠Process faults whentouching address in data segment ⦠stack page was allocated eagerly data + code pages are notedin page table, but no physical frame has been allocated CS 3204 Spring 2007 3/27/2007 6
ustack (2)ustack (1)
Stack Growth
kernelkernel
kernelkernel ucode (1)
kcodekdata
kheapkbss
C
C
FFFFFFFF
3 GB
1 GB
used
free user (1)user (1) udata (1)
user (1)
P1 Pintos loads the first process ā¦
Pintos then starts the firstprocess ⦠Process faults because codepage is not present ⦠Process calls recursivefunction or allocates large local variable page fault at about here
CS 3204 Spring 2007 3/27/2007 7
ustack (1)
mmap()
kernelkernel
kernelkernel ucode (1)
kcodekdata
kheapkbss
C
C
FFFFFFFF
3 GB
1 GB
used
free user (1)user (1) udata (1)
user (1)
P1 Pintos loads the first process ā¦
Pintos then starts the firstprocess ⦠Process faults because codepage is not present ⦠Process opens file, callsmmap(fd, addr)
ummap (1)
Process faults whentouching mapped file Page fault handler allocspage, maps it, reads data from disk: CS 3204 Spring 2007 3/27/2007 8
Lazy Loading & Prefetching
- Typically want to do some prefetching when faulting inpage
- Q.: how many pages? which pages?ā Reduces latency on subsequent faults
- Too much: waste time & space fetching unused pagesā Too little: pay (relatively large) page fault latency too often
- Predict which pages the program will access next (how?)
- Let applications give hints to OSā If applications knows
- Example: madvise(2)ā Usual conflict: whatās best for application vs whatās best for
system as a whole
CS 3204 Spring 2007 3/27/2007 9
Copy-On-Write
- Sometimes, want to create a copy of a page:ā Example: Unix fork() creates copies of all parentās pages in the
- Optimization:child
- Donāt copy pages, copy PTEs ā now have 2 PTEs pointing toframe
- Set all PTEs read-onlyā Read accesses succeed
- On Write access, copy the page into new frame, update PTEs topoint to new & old frame
- Looks like each have their own copy, but postponeactual copying until one is writing the data
- Hope is at most one will ever touch the data ā never have tomake actual copy CS 3204 Spring 2007 3/27/2007 10
Page Eviction
- Suppose page fault occurs, but no free physical frame is there toallocate
- Must evict frameā Find victim frame (how ā later)
- ā Find & change old page table entry pointing to the victim frameIf data in it isnāt already somewhere on disk, write to special area on
- disk (āswap spaceā)Install in new page table entry
- Requires check on page fault if page has been swapped out ā faultā^ Resume
- in if soSome subtleties with locking:
- How do you prevent a process from writing to a page some otherprocess has chosen to evict from its frame?
- What do you do if a process faults on a page that another process is inthe middle of paging out?
CS 3204 Spring 2007 3/27/2007 11
Page Eviction Example
victim frame:phys addr = ā¦
PTE:process id =? (if appl.),
virtual addr = ?,dirty bit = ?,
accessed bit = ?,
Process A needs a framedecides it wants this frame
Q.: how will it find the PTE,if any, that points to it?
Linux uses a so-called ārmapā for that that links frames to PTE
CS 3204 Spring 2007 3/27/2007 12
Managing Swap Space
- Continuous region on diskā Preferably on separate disk, but typically a partition on same
- Different allocation strategies are possibledisk
- Simplest: when page must be evicted, allocate swap space forpage; deallocate when page is paged back in
- Or: allocate swap space upfrontā Should pageās position in swap space change? What if same
- Can be managed via bitmap 0100100000001page is paged out multiple times?
- Free/used bits for each page that can be storedā Pintos: note 1 page == 8 sectors
CS 3204 Spring 2007 3/27/2007 19
Buddy Example - Allocation
64 KB
16KB 16KB 32KB
16KB 16KB 32KB
16KB 4KB 4KB 8KB 32KB
16KB 4KB 4KB 8KB 32KB
16KB 4KB 4KB4KB4KB 32KB
Alloc (16KB) Alloc (32KB) Alloc (4KB) Alloc (4KB) Alloc (4KB) CS 3204 Spring 2007 3/27/2007 20
Buddy Example - Deallocation
64 KB
16KB
32KB
16KB
16KB 32KB
16KB 32KB
32KB
16KB 4KB 4KB4KB4KB 32KB
Free() Free() Free() Free() Free()
4KB 4KB4KB4KB 32KB
8KB 4KB4KB
CS 3204 Spring 2007 3/27/2007 21
Fragmentation
⢠Def: The inability to use memory that is
unused.
⢠Internal fragmentation:
ā Not all memory inside an allocated unit is
used; rest canāt be allocated to other users
⢠External fragmentation:
ā Impossible to satisfy allocation request eventhough total amount of memory > size
requested
CS 3204 Spring 2007 3/27/2007 22
Internal Fragmentation
64 KB
12KB 16KB 32KB
16KB 24KB
4KB 4KB 8KB
4KB 4KB 8KB
4KB 4KB 3KB4KB
Alloc (12KB) Alloc (24KB) Alloc (4KB) Alloc (4KB) Alloc (3KB) 12KB
12KB
12KB
12KB
24KB
24KB
24KB
CS 3204 Spring 2007 3/27/2007 23
External Fragmentation
64 KB
16KB
32KB
16KB
16KB 32KB
16KB 32KB
32KB
16KB 4KB 4KB4KB4KB 32KB
Free() Free() Free() Free() Free()
4KB 4KB4KB4KB 32KB
8KB 4KB4KB
Have 8 KB free, but canāt Alloc(8KB) Have 12 KB free, but canāt Alloc(12KB)
No external fragmentation
No external fragmentation
CS 3204 Spring 2007 3/27/2007 24
Buddy Allocator & Fragmentation
⢠Q.: what is the average internal fragmentation
(per allocated object) for
- buddy allocator with size 2^n?
- in bitmap allocator for objects of size n*s, where eachbit represents a unit of size s?
- in first-fit allocator from project 0?
⢠Q.: what external fragmentation can you expect
from buddy allocator scheme?
⢠Q.: whatās a good way to measure fragmentation
in general?
CS 3204 Spring 2007 3/27/2007 25
Page Size & Fragmentation
⢠How should a systemās architect choose thepage size? ā Trade-Off
⢠Large pages:ā Larger internal fragmentation
- (not an issue if most pages are fullā¦)ā Page-in & write-back cost larger
⢠Small pages:ā Higher overhead to store page table (more entries to
maintain)
⢠Modern architectures provide support for āsuperpagesā ā 2MB or 4MB