Os Virtual Memory
Os Virtual Memory
• Benefits:
– Only part of the program needs to be in memory for execution
• more concurrent programs
– Logical address space can therefore be much larger than physical
address space
• execute programs larger than RAM size
– Easy sharing of address spaces by several processes
• Library or a memory segment can be shared
– Allows for more efficient process creation
1
Virtual Memory That is Larger Than
Physical Memory
Page 0
0
Page 1 1
2 Page
3 Page22 Page 0 Page 1
Page 2
4 unavail
unavail
Page 3 Page 2 Page 3
Page
Page00 move
Page 4 pages
… unavail
unavail Page 4
… Page
Page33
n-2 Page
n-1 Page11 page n-2 Page n-1
page table Physical memory
page n-2
page n-1 all pages of program sitting on physical Disk
Virtual memory
2
A typical virtual-address space layout of a
process
function parameters;
local variables;
return addresses
3
Shared Library Using Virtual Memory
4
Implementing Virtual Memory
– Demand paging
• Bring pages into memory when they are used, i.e. allocate memory for
pages when they are used
– Demand segmentation
• Bring segments into memory when they are used, i.e. allocate memory
for segments when they are used.
5
Demand Paging
• Pager never brings a page into memory unless page will be needed
6
Valid-Invalid Bit
i
i
page table
• During address translation, if valid–invalid bit in page table entry
is i page fault
7
Page Table When Some Pages Are Not in Main
Memory
8
Page Fault
• When CPU makes a memory reference (i.e. page reference), HW consults the
page table. If entry is invalid, then exception occurs and kernel gets executed.
Kernel handling such as case:
1. Kernel looks at another table to decide:
– Invalid reference (page is in unused portion of address space) Abort
– Just not in memory (page is in used portion, but not in RAM) Page Fault
2. Get empty frame
(we may need to remove a page; if removed page is modified, we need disk
I/O to swap it out)
3. Swap page into frame
(we need disk I/O)
4. Reset tables (install mapping into page table)
5. Set validation bit = v
6. Restart the instruction that caused the page fault
9
Page Fault (Cont.)
10
Steps in Handling a Page Fault
swap
space
11
Performance of Demand Paging
EAT = (1 – p) x memory_access_time
+ p x (page fault overhead time
+ time to swap page out (sometimes)
+ time swap page in
+ restart overhead time)
page fault
service time
12
Demand Paging Example
13
Process Creation
- Copy-on-Write
14
Copy-on-Write
• Copy-on-Write (COW) allows both parent and child processes to initially share
the same pages in memory
If either process modifies a shared page, only then is the page copied
• COW allows more efficient process creation as only modified pages are copied
15
Before Process 1 Modifies Page C
16
After Process 1 Modifies Page C
17
Page Replacement
18
What happens if there is no free frame?
• Page replacement – find some page in memory, but not really in use, swap it
out
• With page replacement, same page may be brought into memory several
times
19
Page Replacement
• Use modify (dirty) bit to reduce overhead of page transfers – only modified
pages are written to disk while removing/replacing a page.
20
Need For Page Replacement
3. Bring the desired page into the (new) free frame; update the page and frame
tables
22
Page Replacement
23
Page Replacement Algorithms
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
24
Driving reference string
• 0100 0432 0101 0612 0102 0103 0104 0101 0611 0102 0103 0104
0101 0610 0102 0103 0104 0609 0102 0105
• Example: Bytes (addresses) 0…99 will be in page 0
• Pages referenced with each memory reference
– 1, 4, 1, 6, 1, 1, 1, 1, 6, 1, 1, 1, 1, 6, 1, 1, 6, 1, 1
25
Graph of Page Faults Versus The Number of
Frames
26
First-In-First-Out (FIFO) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frames (3 pages can be in memory at a time per process)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
• 4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
28
FIFO Illustrating Belady’s Anomaly
29
Optimal Algorithm
• Replace page that will not be used for longest period of time
• 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
6 page
2
faults
3
4 5
30
Optimal Page Replacement
31
Least Recently Used (LRU) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3
8 page faults
32
LRU Page Replacement
33
LRU Algorithm Implementation
• Counter implementation
– Every page entry has a counter field; every time page is referenced
through this entry, copy the clock into the counter field
34
LRU Algorithm Implementation
35
Use of a Stack to Record The Most Recent Page
References
36
LRU Approximation Algorithms
• Reference bit
• Additional Reference bits
• Second Chance 1
• Second Chance 1 (clock)
• Enhanced Second Chance
37
Reference Bit
38
Additional Reference Bits
• Besides the reference bit (R bit) for each page, we can keep an
AdditionalReferenceBits (say ARB) field associated with each page. For
example, an 8-bit field that can store 8 reference bits.
• At each timer interrupt (or periodically), the reference bit of a page is shifted
from right to the AdditionalReferenceBits field of the page. All other bits of
AdditionalReferenceBits field is shifted to the right as well.
– The value in the AdditionalReferenceBits field will indicate when the page
is accessed (referenced), approximately.
39
Additional Reference Bits
Example
• At tick 1: R: 0, ARB: 0000000
• R is set (R:1)
• At tick 2: R:0, ARB: 1000000
• R is not set
• At tick 3: R:0, ARB: 0100000
• R is set (R:1)
• At tick 4: R:0, ARB: 1010000
• ….
40
Second-Chance Algorithm 1
Head Tail
(oldest) (Youngest)
41
Second-Chance Algorithm 1
• Example: Head
1 1 0 0 1
Before page removal C A B E D
Access page H
0 1 0 0 1
After page removal E D C A H
42
Second-Chance Algorithm 2
(Clock Algorithm)
43
Enhanced Second-Change Algorithm
• Consider also the reference bits and the modified bits of pages
– Reference (R) bit: page is referenced in the last interval
– Modified (M) bit: page is modified after being loaded into memory
• We may need to scan the list several times until we find the page to replace
44
Counting Algorithms
• Keep a counter of the number of references that have been made to each
page
• MFU Algorithm: based on the argument that the page with the smallest count
was probably just brought in and has yet to be used
45
Allocation of Frames
46
Fixed Allocation
• Equal allocation – For example, if there are 100 frames and 5
processes, give each process 20 frames.
• Proportional allocation – Allocate according to the size of process
si size of process pi
S si
m total number of frames
m 64
si
ai allocation for pi m si 10
S
s2 127
Example: 10
a1 64 5
137
127
a2 64 59
137
47
Priority Allocation
48
Global versus Local Allocation
• When a page fault occurs for a process and we need page replacement, there
are two general approaches:
– Global replacement – select a victim frame from the set of all frames;
• one process can take a frame from another
– Local replacement – select a victim frame only from the frames allocated
to the process.
• A process uses always its allocated frames
49
Thrashing
• If a process does not have “enough” pages, the page-fault rate is very high.
This leads to:
– low CPU utilization
– operating system thinks that it needs to increase the degree of
multiprogramming
– another process added to the system
50
Thrashing (Cont.)
51
Demand Paging and Thrashing
52
Locality In A Memory-Reference Pattern
53
Working-Set Model
• WSSi (working set size of Process Pi) = total number of distinct pages
referenced in the most recent
– WSS varies in time
– Value of is important
• if too small will not encompass entire locality
• if too large will encompass several localities
• if = will encompass entire program
54
Working-Set Model
55
Working-Set Model
56
Keeping Track of Working-Set
a method
additional
ref_bits Physical Memory
R_bit (ARB) page x frame 0
x 0 x 0 0
y 0 y 0 0 Page y frame 1
z 0 z 0 0
w 0 w 0 0 Page z frame 2
Page w frame 3
page table
57
Keeping Track of Working-Set
a method
Whenever timer interrupts, for a page, shift the R bit from right into
ARB and clear R bit.
you can increases granularity by increasing the size of ARB and decreasing
the timer interrupt interval
58
Working Sets and Page Fault Rates
59
Page-Fault Frequency (PFF) Scheme
60
Memory-Mapped Files
• Memory-mapped file I/O allows file I/O to be treated as routine memory access
by mapping a disk block to a page in memory
• A file is initially read using demand paging. A page-sized portion of the file is
read from the file system into a physical page. Subsequent reads/writes
to/from the file are treated as ordinary memory accesses.
• Simplifies file access by treating file I/O through memory rather than read()
write() system calls
• Also allows several processes to map the same file allowing the pages in
memory to be shared
61
Memory Mapped Files
62
Memory-Mapped Shared Memory in
Windows
63
Allocating Kernel Memory
• But using first-fit like strategies (heap management strategies) cause external
fragmentation
64
Allocating Kernel Memory
– Slab Allocator
65
Buddy System Allocator
– When smaller allocation needed than is available, current chunk split into
two buddies of next-lower power of 2
• Continue until appropriate sized chunk available
66
Buddy System Allocator
67
Example
68
Example
512 KB of Memory (physically contiguous area)
A C B D
Alloc A 45 KB
512
Alloc B 70 KB
Alloc C 50 KB
256 256 Alloc D 90 KB
Free C
Free A
128 128(B)
128 128
128(D) 128 Free B
Free D
64(A)
64 64(C)
64
69
Slab Allocator
• Alternate strategy
70
Slab Allocator
• If slab is full of used objects, next object allocated from empty slab
– If no empty slabs, new slab allocated
• Benefits include
– no fragmentation,
– fast memory request satisfaction
71
Slabs and Caches
cache cache
structure structure
slab slab
structure structure
73
Prepaging
• Prepaging
– To reduce the large number of page faults that occurs at process startup
– Prepage all or some of the pages a process will need, before they are
referenced
– But if prepaged pages are unused, I/O and memory was wasted
74
Other Issues – Page Size
75
Other Issues – TLB Reach
76
Other Issues – Program Structure
– Program 2
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
Process A
• Consider I/O - Pages that are used for
copying a file from a device must be pages
locked from being selected for eviction
by a page replacement algorithm
Process B
pages
79
Operating System Examples
• Windows XP
• Solaris
80
Windows XP
81
Solaris
82
Solaris 2 Page Scanner
83
Slab Allocation in Linux Kernel
84
Cache structure
struct kmem_cache_s {
struct list_head slabs_full; /* points to the full slabs */
struct list_head slabs_partial; /* points to the partial slabs */
struct list_head slabs_free; /* points to the free slabs */
unsigned int objsize; /* size of objects stored in this cache */
unsigned int flags;
unsigned int num;
spinlock_t spinlock;
…
…
}
85
Slab structure
86
Layout of Slab Allocator
an object
87
Slab Allocator in Linux
• cat /proc/slabinfo will give info about the current slabs and objects
cache names: one cache for each different object type
# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <
sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
ip_fib_alias 15 113 32 113 1 : tunables 120 60 8 : slabdata 1 1 0
ip_fib_hash 15 113 32 113 1 : tunables 120 60 8 : slabdata 1 1 0
dm_tio 0 0 16 203 1 : tunables 120 60 8 : slabdata 0 0 0
dm_io 0 0 20 169 1 : tunables 120 60 8 : slabdata 0 0 0
uhci_urb_priv 4 127 28 127 1 : tunables 120 60 8 : slabdata 1 1 0
jbd_4k 0 0 4096 1 1 : tunables 24 12 8 : slabdata 0 0 0
ext3_inode_cache 128604 128696 504 8 1 : tunables 54 27 8 : slabdata 16087 16087 0
ext3_xattr 24084 29562 48 78 1 : tunables 120 60 8 : slabdata 379 379 0
journal_handle 16 169 20 169 1 : tunables 120 60 8 : slabdata 1 1 0
journal_head 75 144 52 72 1 : tunables 120 60 8 : slabdata 2 2 0
revoke_table 2 254 12 254 1 : tunables 120 60 8 : slabdata 1 1 0
revoke_record 0 0 16 203 1 : tunables 120 60 8 : slabdata 0 0 0
scsi_cmd_cache 35 60 320 12 1 : tunables 54 27 8 : slabdata 5 5 0
….
files_cache 104 170 384 10 1 : tunables 54 27 8 : slabdata 17 17 0
signal_cache 134 144 448 9 1 : tunables 54 27 8 : slabdata 16 16 0
sighand_cache 126 126 1344 3 1 : tunables 24 12 8 : slabdata 42 42 0
task_struct 179 195 1392 5 2 : tunables 24 12 8 : slabdata 39 39 0
anon_vma 2428 2540 12 254 1 : tunables 120 60 8 : slabdata 10 10 0
pgd 89 89 4096 1 1 : tunables 24 12 8 : slabdata 89 89 0
pid 170 303 36 101 1 : tunables 120 60 8 : slabdata 3 3 0
• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 7th & 8th editions, Wiley.
• Operating System Concepts, 7th and 8th editions, Silberschatz et al. Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, 2009.
89