Chapter 8b: Virtual Memory II Silberschatz, Galvin and Gagne ©2013

advertisement
Chapter 8b: Virtual Memory II
Operating System Concepts Essentials – 2nd Edition
Silberschatz, Galvin and Gagne ©2013
Chapter 8: Virtual Memory
 Frame Allocation
 Thrashing
 Allocating Kernel Memory
 Other Considerations
“Thresh” from the Hunger Games...
Operating System Concepts Essentials – 2nd Edition
8.2
Silberschatz, Galvin and Gagne ©2013
Frame Allocation
 Each process needs minimum number of frames
 E.g., IBM 370 – 6 pages for SS MOVE instruction:

Instruction is 6 bytes, might span 2 pages

2 pages to handle from

2 pages to handle to
Operating System Concepts Essentials – 2nd Edition
8.3
Silberschatz, Galvin and Gagne ©2013
Frame Allocation
 Maximum is total frames in system
 Two major allocation schemes

Fixed allocation

Priority allocation
 Many variations
Operating System Concepts Essentials – 2nd Edition
8.4
Silberschatz, Galvin and Gagne ©2013
Fixed Allocation
 Equal allocation- equal frames per process

E.g., 100 frames available, 5 processes,
... each process allocated 20 frames
Operating System Concepts Essentials – 2nd Edition
8.5
Silberschatz, Galvin and Gagne ©2013
Fixed Allocation
 Proportional allocation- # frames based on
process size
si  size of process pi
S   si
m  total number of frames
s
ai  allocation for pi  i  m
S
Operating System Concepts Essentials – 2nd Edition
8.6
m = 64
s1 = 10
s2 = 127
10
a1 =
´ 62 » 4
137
127
a2 =
´ 62 » 57
137
Silberschatz, Galvin and Gagne ©2013
Priority Allocation
 Proportional allocation scheme based on
priorities (rather than size)
 If process Pi generates page fault,

Select one of its frames for replacement
OR

Select replacement frame from process with lower
priority
Operating System Concepts Essentials – 2nd Edition
8.7
Silberschatz, Galvin and Gagne ©2013
Global vs. Local Allocation
 Global replacement– process selects replacement
frame from set of all frames

One process can take frame from another

Process execution times can vary greatly

Greater throughput

More common
Operating System Concepts Essentials – 2nd Edition
8.8
Silberschatz, Galvin and Gagne ©2013
Global vs. Local Allocation
 Local replacement – each process selects from
only its own set of allocated frames

More consistent per-process performance

Possible underutilized memory
Operating System Concepts Essentials – 2nd Edition
8.9
Silberschatz, Galvin and Gagne ©2013
Non-Uniform Memory Access
 So far, all memory access time equal
 Many systems are NUMA– memory access speed
varies

E.g., system boards containing CPUs and memory,
interconnected over system bus
 Optimal performance: allocate memory “close to”
CPU that thread is scheduled on

Modify scheduler to schedule thread on same
system board (when possible)
Operating System Concepts Essentials – 2nd Edition
8.10
Silberschatz, Galvin and Gagne ©2013
Chapter 8: Virtual Memory
 Frame Allocation
 Thrashing
 Allocating Kernel Memory
 Other Considerations
“Thresh” from the Hunger Games...
Operating System Concepts Essentials – 2nd Edition
8.11
Silberschatz, Galvin and Gagne ©2013
Thrashing
 If process does not have “enough” pages, page-fault
rate is high

Page fault to get page

Replace existing frame

But quickly needs replaced frame back

Leads to:
 Low
 OS
CPU utilization
thinks it needs to increase degree of multiprogramming
 Another
process added to system
 Thrashing- process busy swapping pages in and out
Operating System Concepts Essentials – 2nd Edition
8.12
Silberschatz, Galvin and Gagne ©2013
Thrashing (Cont.)
Operating System Concepts Essentials – 2nd Edition
8.13
Silberschatz, Galvin and Gagne ©2013
Demand Paging and Thrashing
 Why does demand paging work? Locality model

Process migrates from one locality to another

Localities may overlap
 Why does thrashing occur?

 size of locality > total memory size

Use local or priority page replacement to limit effects
Operating System Concepts Essentials – 2nd Edition
8.14
Silberschatz, Galvin and Gagne ©2013
Locality In A Memory-Reference Pattern
Operating System Concepts Essentials – 2nd Edition
8.15
Silberschatz, Galvin and Gagne ©2013
Working-Set Model
 - working-set window

Fixed number of page references
 WSSi - working set of process Pi

total number of pages referenced in most recent 
 Varies
in time
Operating System Concepts Essentials – 2nd Edition
8.16
Silberschatz, Galvin and Gagne ©2013
Working-Set Model
  too small -> will not encompass entire locality
  too large -> will encompass several localities
  =  -> will encompass entire program
Operating System Concepts Essentials – 2nd Edition
8.17
Silberschatz, Galvin and Gagne ©2013
Working-Set Model
 D =  WSSi - total demand frames

Approximation of locality
 if D > m  Thrashing occurs
 Policy if D > m, suspend or swap out a process
 Can be implemented using timer interrupt and
reference bits
Operating System Concepts Essentials – 2nd Edition
8.18
Silberschatz, Galvin and Gagne ©2013
Page-Fault Frequency
 More direct approach than WSS
 Establish “acceptable” page-fault frequency (PFF)
rate; use local replacement policy

If rate too low, process loses frame

If rate too high, process gains frame
Operating System Concepts Essentials – 2nd Edition
8.19
Silberschatz, Galvin and Gagne ©2013
Working Sets and Page Fault Rates

Relationship between working set of process and
page-fault rate

Working set changes over time

Peaks

Valleys
Operating System Concepts Essentials – 2nd Edition
8.20
Silberschatz, Galvin and Gagne ©2013
Chapter 8: Virtual Memory
 Frame Allocation
 Thrashing
 Allocating Kernel Memory
 Other Considerations
“Thresh” from the Hunger Games...
Operating System Concepts Essentials – 2nd Edition
8.21
Silberschatz, Galvin and Gagne ©2013
Allocating Kernel Memory
 Treated differently from user memory
 Often allocated from free-memory pool

Kernel requests memory for structures of varying
sizes

Some kernel memory needs to be contiguous
 E.g.,
for device I/O
Operating System Concepts Essentials – 2nd Edition
8.22
Silberschatz, Galvin and Gagne ©2013
Buddy System
 Allocates memory from fixed-size segment of
physically-contiguous pages
 Memory allocated using power-of-2 allocator

Satisfies requests in units with size power of 2

Request rounded up to next highest power of 2

When smaller allocation needed, current chunk split
into two buddies of next-lower power of 2
 Continue
until appropriate sized chunk available
Operating System Concepts Essentials – 2nd Edition
8.23
Silberschatz, Galvin and Gagne ©2013
Buddy System Example
 256KB chunk available,
kernel requests 21KB

Split 256KB chunk into
AL and AR (128KB each)

AL split into BL and BR
(64KB each)

BL split into CL and CR
(32KB each)

CL used to satisfy
request
Operating System Concepts Essentials – 2nd Edition
8.24
Silberschatz, Galvin and Gagne ©2013
Buddy System
 Advantage- coalesce unused chunks into larger chunk
 Disadvantage- fragmentation
Operating System Concepts Essentials – 2nd Edition
8.25
Silberschatz, Galvin and Gagne ©2013
Slab Allocator
 Slab- one or more physically contiguous pages
 Cache- one or more slabs
 Single cache for each unique kernel data structure

Each cache filled with objects- instantiations of OS
data structure
Operating System Concepts Essentials – 2nd Edition
8.26
Silberschatz, Galvin and Gagne ©2013
Slab Allocator
 When cache created, filled with objects marked free
 When structures stored, objects marked as used
 If slab full of used objects, next object allocated from
empty slab

If no empty slabs, new slab allocated
 Benefits: no fragmentation, fast memory requests
Operating System Concepts Essentials – 2nd Edition
8.27
Silberschatz, Galvin and Gagne ©2013
Slab Allocation
Operating System Concepts Essentials – 2nd Edition
8.28
Silberschatz, Galvin and Gagne ©2013
Chapter 8: Virtual Memory
 Frame Allocation
 Thrashing
 Allocating Kernel Memory
 Other Considerations
“Thresh” from the Hunger Games...
Operating System Concepts Essentials – 2nd Edition
8.29
Silberschatz, Galvin and Gagne ©2013
Other Considerations -- Prepaging
 Reduce number of page faults at process startup
 Prepage all or some pages process will need,
before referenced

If prepaged pages not used, I/O + memory wasted
Operating System Concepts Essentials – 2nd Edition
8.30
Silberschatz, Galvin and Gagne ©2013
Other Issues – Page Size
 Sometimes OS designers have choice

Especially if running on custom-built CPU
 Page size selection must take into consideration:

Fragmentation, page table size, resolution

I/O overhead, number of page faults

Locality, TLB size and effectiveness
 Always power of 2

Range: 212 (4,096 bytes) to 222 (4,194,304 bytes)
Operating System Concepts Essentials – 2nd Edition
8.31
Silberschatz, Galvin and Gagne ©2013
Other Issues – Page Size
 Increase Page Size

May lead to increased fragmentation
 Not
all applications require large page size
 Multiple Page Sizes

Great for applications that require larger page sizes

Does not increase fragmentation
Operating System Concepts Essentials – 2nd Edition
8.32
Silberschatz, Galvin and Gagne ©2013
Other Issues – TLB Reach
 TLB Reach- amount of memory accessible from TLB

TLB Reach = (TLB Size) X (Page Size)
 Ideally, working set of each process stored in TLB

Otherwise high degree of page faults
Operating System Concepts Essentials – 2nd Edition
8.33
Silberschatz, Galvin and Gagne ©2013
Other Issues – Program Structure
 Program structure
 int[128,128] data;

Each row stored in one page

Program 1:
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i,j] = 0;
128 x 128 = 16,384 page faults!
Operating System Concepts Essentials – 2nd Edition
8.34
Silberschatz, Galvin and Gagne ©2013
Other Issues – Program Structure
 Program structure
 int[128,128] data;

Each row stored in one page

Program 2:
for (i = 0; i <128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
128 page faults!!
Operating System Concepts Essentials – 2nd Edition
8.35
Silberschatz, Galvin and Gagne ©2013
Other Issues – I/O interlock
 I/O Interlock- pages locked
into memory

E.g., I/O pages used for
copying file from device
must be locked from being
selected for eviction by page
replacement algorithm
 Pinning pages to lock into
memory
Operating System Concepts Essentials – 2nd Edition
8.36
Silberschatz, Galvin and Gagne ©2013
Download