06 Memory
06 Memory
Operating Systems
Brad Campbell – [email protected]
https://www.cs.virginia.edu/~bjc8c/class/cs6456-f19/
2
3 2
4
3
SegID = 1 0x4000
0x4000 0x4800
0x5C00
0x8000
Space for
0xC000
Other Apps
0xF000
Virtual Physical
Address Space Address Space
Segmentation
• Internal fragmentation?
• We can have large, unused gaps in virtual address space 0x0000
that don’t correspond to any allocated segment
• But segments must be contiguous in physical memory
0x4000
• Reserve extra space at beginning?
0x4800
• Expansion is difficult
0x5C00
• External fragmentation?
• Yes – may need to rearrange chunks of memory
• Segments are smaller than full process memory image,
hopefully less work
• Sharing? 0xF000
• Yes! Allow virtual addresses in two processes to map to
same segment in physical mem. Virtual
• But can only share at segment granularity Address Space
• One shared library per segment?
• Will we run out of segments?
Swapping
• Problem: What if all segments don’t fit in memory?
• Breaks the illusion of a dedicated machine
• Potential solution: When process isn’t running, move one
or more segments to disk
• Load segment back into main memory before resuming
• An “extreme context switch”
• Need to track whether or not a segment is resident in
memory in processor segment map
• For modern systems, swapping performance is
unacceptable
• Maybe swap some idle memory to disk, but never something
active
How can we do better?
• Allocate at finer granularity than segments
• Allocate memory in fixed-size chunks: pages
• All allocations are full pages
• All holes are multiples of a page size
• Expanding memory footprint: allocate new page
• No need to relocate existing allocations
• No struggle to find free region of proper size
What’s the catch?
• More bookkeeping
• Need to track physical memory location of each page
• Need to track metadata (valid, permissions) per page
• Page size represents a tradeoff
• Larger: Fewer pages to keep track of, more internal
fragmentation
• Smaller: Less wasted space, more pages to track
• Less locality: Sequential access in virtual space
is not necessarily sequential in physical space
• Jumping across page boundaries
Implementing Paging
Virtual
Virtual Address: Page # Offset
1110 0000
11100 null stack 1110 0000
11011 null
11010 null
11001 null
What
1100 0000 11000 null
happens if 10111
10110
null
null
stack grows 10101 null
10100 null
to 1110 0000? 10011 null
heap 10010 10000
1000 0000 10001
10000
01111
01110
heap 0111 000
01111 null
01110 null data
01101 null 0101 000
data 01100 null
0100 0000 01011 01101
01010 01100
01001 01011
01000 01010
00111 null code
00110 null 0001 0000
code 00101 null
0000 0000 00100 null 0000 0000
00011 00101
page #offset 00010 00100
00001 00011
00000 00010
Summary: Paging
Page Table
Virtual memory view 11111 11101 Physical memory view
1111 1111 11110 11100
stack 11101 10111
1110 0000
11100 10110 stack 1110 0000
11011 null
11010 null
11001 null
1100 0000 11000 null
10111 null stack
10110 null
10101 null
10100 null Allocate
heap
10011
10010
null
10000
new pages
1000 0000 where
10001 01111
heap
10000
01111
01110
null room!0111 000
01110 null data
01101 null 0101 000
data 01100 null
0100 0000 01011 01101
01010 01100
01001 01011
01000 01010
00111 null code
00110 null 0001 0000
code 00101 null
0000 0000 00100 null 0000 0000
00011 00101
page #offset 00010 00100
00001 00011
00000 00010
Paging: Hardware Support
• Special Register to store page table pointer
• Modified in kernel mode only
• Change register values on context switch
PageTablePtr
4 bytes
• A tree of page tables
• Each "node" has a fixed
size
• x86: 1024 4-byte entries
• Still just one register to
change on context switch 4 bytes
Two-Level Page Tables
Physical Physical
Offset
10 bits 10 bits 12 bits Address:
Page #
Virtual Virtual Virtual
P1 index P2 index Offset
Address:
4KB
PageTablePtr
4 bytes
PageTablePtr
4 bytes
1111 0000
10 11100 stack 1110 0000
01 10111
00 10110
1100 0000
stack
111 11 null
110 null 10 10000
101 null 01 01111
00 01110
heap 100
1000 0000 011 null
010 heap 0111 000
001 null
000 11 01101
10 01100 data
01 01011 0101 000
data 00 01010
0100 0000
11 00101
page2 #
10 00100
01 00011
code
0001 0000
code 00 00010
0000 0000 0000 0000
page1 #offset
Page Table Discussion
• Good Points:
• Easy memory allocation (fixed size chunks)
• Easy sharing among processes
• No physical memory used for holes in virtual
address sp.
• Bad Points:
• Overhead: At least 1 pointer per 4K page
• Page tables need to be contiguous (tricky
allocation)
• Clever design (x86) can limit them to one page
• Several memory accesses per translation
• Later: making this faster
Page Tables for Huge Address Spaces
• Want full 64 bits of address space?
• How many levels of indirection needed?
Virtual Virtual
Process B: Seg # Page # Offset
Combining Segments & Pages
• Good:
• Just need one page table per segment, even if
shared
• Easy memory allocation (no external fragmentation)
• Easy sharing of segments, just change segment
table entry
• Can still share at page granularity if necessary
• Bad
• Overhead: 1 pointer per page
• At least two extra lookups per memory access
Address Translation Comparison
Advantages Disadvantages
Simple Fast context External fragmentation
Segmentation switching: Segment
mapping maintained
by CPU
Paging (single- No external Large table size ~ virtual
level page) fragmentation, fast memory
easy allocation Internal fragmentation
PCD
PWT
Page Frame Number Free
0 L D A U W P
(Physical Page Number) (OS)
31-12 11-9 8 7 6 5 4 3 2 1 0
Faulting
User
Inst.
Inst.
Load
OS Page
Precise Exceptions
1000 CPU
µProc
“Moore’s Law” 60%/yr.
(2X/1.5yr)
Performance
100 Processor-Memory
Performance Gap:
(grows 50% / year)
10
“Less’ Law?” DRAM
DRAM
9%/yr.
1 (2X/10 yrs)
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
Time
Average Access Time
• Average Access Time =
(Hit Rate x Hit Time) + (Miss Rate x Miss
Time)
0 2n - 1
Address Space
Blk X
From Processor Blk Y
Memory Hierarchy
• Take advantage of the principle of
locality to:
• Present as much memory as in the cheapest
technology
• Provide access
Processor at speed offered by the fastest
technology
Control Tertiary
Secondary
Second Main Storage Storage
(Tape)
Cache
On-Chip
Registers
Block 1111111111222222222233
no. 01234567890123456789012345678901