chapter 9: virtual memory joe mccarthy css 430: operating systems - virtual memory1

81
Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory 1

Upload: may-small

Post on 19-Dec-2015

231 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Chapter 9:Virtual Memory

Joe McCarthy

CSS 430: Operating Systems - Virtual Memory 1

Page 2: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Chapter 9: Virtual Memory• Background• Demand Paging• Copy-on-Write• Page Replacement• Allocation of Frames • Thrashing• Memory-Mapped Files• Allocating Kernel Memory• Other Considerations• Operating-System Examples

Material derived, in part, fromOperating Systems Concepts with Java, 8th Ed.

© 2009 Silberschatz, Galvin & Gagne

2CSS 430: Operating Systems - Virtual Memory

Page 3: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Virtual Memory

3CSS 430: Operating Systems - Virtual Memory

Page 4: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Background

• Virtual memory – separation of logical memory from physical memory– Only part of the program needs to be in memory

for execution– Logical address space vs. physical address space?

4CSS 430: Operating Systems - Virtual Memory

Page 5: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Background

• Virtual memory – separation of logical memory from physical memory– Only part of the program needs to be in memory

for execution– Logical address space >> physical address space– Degree of multiprogramming?

5CSS 430: Operating Systems - Virtual Memory

Page 6: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Background

• Virtual memory – separation of logical memory from physical memory– Only part of the program needs to be in memory

for execution– Logical address space >> physical address space– Higher degree of multiprogramming

6CSS 430: Operating Systems - Virtual Memory

Page 7: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Swapping vs. Paging

CSS 430: Operating Systems - Virtual Memory 7

Swapping Paging

Page 8: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Resident & Non-resident Pages

CSS 430: Operating Systems - Virtual Memory 8

Resident: A, C, F

Non-resident: B, D, E, G, H

Page 9: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Demand Paging• Bring a page into memory only when it is needed

– Alternative: anticipatory or pre-paging• When page is needed but not [yet] in memory

– page fault– not-in-memory bring into memory– Swapper that deals with pages is a pager

• Advantages of demand vs. anticipatory paging?

CSS 430: Operating Systems - Virtual Memory 9

Page 10: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Demand Paging• Bring a page into memory only when it is needed

– Alternative: anticipatory or pre-paging• When page is needed but not [yet] in memory

– page fault– not-in-memory bring into memory– Swapper that deals with pages is a pager

• Advantages of demand vs. anticipatory paging:– Less I/O needed– Less memory needed – Faster response– More users / programs

CSS 430: Operating Systems - Virtual Memory 10

Page 11: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Handling a Page Fault

CSS 430: Operating Systems - Virtual Memory 11

Page fault:Reference tonon-resident page

[Page table hasentries for all pages,but all pages maynot be resident]

1. Reference to non-resident page2. Trap to OS; look at PCB:

If invalid reference abortOtherwise, bring page into memory

3. Find empty frame4. Swap page into frame5. Update tables (page table, PCB)

Set valid-invalid bit = v6. Restart the instruction that caused the page fault

Page 12: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Copy on Write

CSS 430: Operating Systems - Virtual Memory 12

• Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory

• Shared page is copied only if either process modifies it• COW allows more efficient process creation as only

modified pages are copied

Page 13: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

What if there is no free frame?

CSS 430: Operating Systems - Virtual Memory 13

Page fault:Reference tonon-resident page

1. Reference to non-resident page2. Trap to OS; look at PCB:

If invalid reference abortOtherwise, bring page into memory

3. Find empty frame

4. Swap page into frame

5. Update tables (page table, PCB)Set valid-invalid bit = v

6. Restart the instruction that caused the page fault

Page 14: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

What if there is no free frame?

CSS 430: Operating Systems - Virtual Memory 14

Page fault:Reference tonon-resident page

1. Reference to non-resident page2. Trap to OS; look at PCB:

If invalid reference abortOtherwise, bring page into memory

3. Find empty frame(or select victim frame)

4. Swap page into frame(possibly swap victim out)

5. Update tables (page table, PCB)Set valid-invalid bit = v

6. Restart the instruction that caused the page fault

Page 15: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Effective Access Time

CSS 430: Operating Systems - Virtual Memory 15

• Page Fault Rate 0 p 1.0

• Effective Access TimeEAT =

Page 16: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Effective Access Time

CSS 430: Operating Systems - Virtual Memory 16

• Page Fault Rate 0 p 1.0

• Effective Access TimeEAT = (1 – p) x memory access time+ p (page fault overhead + swap page out + swap page in + restart overhead)

Page 17: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Effective Access Time Example• Memory access time = 200 nanoseconds• Average page-fault service time = 8 milliseconds

• EAT = (1 – p) * 200 ns + p (8 ms) = (1 – p) * 200 ns + p * 8,000,000 ns

= 200 + p * 7,999,800 ns• If 1 access out of 1,000 (0.1%, p = 0.001)

causes a page fault, then EAT = 8.2 microseconds This is a slowdown by a factor of 40 (!)

CSS 430: Operating Systems - Virtual Memory 17

Page 18: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Page Fault rates & Effective Access Times

CSS 430: Operating Systems - Virtual Memory 18

Page 19: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Page Replacement• Prevent over-allocation of memory by modifying

page-fault service routine to include page replacement

• Use a modify (“dirty”) bit to reduce overhead of page transfers – only modified pages are written to disk

• Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory

CSS 430: Operating Systems - Virtual Memory 19

Page 20: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Need For Page Replacement

20CSS 430: Operating Systems - Virtual Memory

Page 21: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Page Replacement

CSS 430: Operating Systems - Virtual Memory 21

Page 22: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Page Replacement Algorithms

• Goal: low page-fault rate

• Evaluation?

CSS 430: Operating Systems - Virtual Memory 22

Page 23: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Page Replacement Algorithms

• Goal: low page-fault rate

• Evaluation: simulate algorithm using a particular string of memory references (reference string) and computing the number of page faults on that string

• Example reference string:

7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

CSS 430: Operating Systems - Virtual Memory 23

Page 24: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

FIFO Page Replacement

CSS 430: Operating Systems - Virtual Memory 24

Replace frame which has been in memory for longest time

Page 25: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

FIFO Page Replacement

CSS 430: Operating Systems - Virtual Memory 25

Replace frame which has been in memory for longest time

Page 26: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

First-In-First-Out (FIFO) Algorithm• Reference string:

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames

• 4 frames

CSS 430: Operating Systems - Virtual Memory 26

1

2

3

1

2

3

4

Page 27: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

First-In-First-Out (FIFO) Algorithm• Reference string:

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames

• 4 frames

CSS 430: Operating Systems - Virtual Memory 27

1

2

3

1

2

3

4

1

2

5

3

4

9 page faults

1

2

3

4

Page 28: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

First-In-First-Out (FIFO) Algorithm• Reference string:

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames

• 4 frames

CSS 430: Operating Systems - Virtual Memory 28

1

2

3

1

2

3

4

1

2

5

3

4

9 page faults

1

2

3

1

2

3

5

1

2

4

5 10 page faults

44 3

Page 29: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

First-In-First-Out (FIFO) Algorithm• Reference string:

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames

• 4 frames

• Belady’s Anomaly: more frames more page faults

CSS 430: Operating Systems - Virtual Memory 29

1

2

3

1

2

3

4

1

2

5

3

4

9 page faults

1

2

3

1

2

3

5

1

2

4

5 10 page faults

44 3

Page 30: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Optimal Page Replacement

CSS 430: Operating Systems - Virtual Memory 30

Replace frame which will not be used for longest time

Page 31: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Optimal Page Replacement

CSS 430: Operating Systems - Virtual Memory 31

Replace frame which will not be used for longest time

Page 32: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Optimal Page Replacement

CSS 430: Operating Systems - Virtual Memory 32

Replace frame which will not be used for longest timeIs this knowable?

Page 33: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Optimal Page Replacement

CSS 430: Operating Systems - Virtual Memory 33

Replace frame which will not be used for longest timeIs this knowable?

Is this useful?

Page 34: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

LRU Page Replacement

CSS 430: Operating Systems - Virtual Memory 34

Replace frame which has not been used for longest time

Page 35: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

LRU Page Replacement

CSS 430: Operating Systems - Virtual Memory 35

Replace frame which has not been used for longest time

Page 36: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

LRU Implementations

• Counter implementation– Every page table entry has a counter– When page is referenced, counter = current time– When a page needs to be replaced, look at the

counters to determine which is oldest

• Advantages / disadvantages?

CSS 430: Operating Systems - Virtual Memory 36

Page 37: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

LRU Implementations

• Stack implementation– Maintain a stack of page numbers– When page is referenced, move it to the top– When page needs to be replaced, select bottom– Stack: doubly linked list

• Advantages / disadvantages?

CSS 430: Operating Systems - Virtual Memory 37

Page 38: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

LRU Approximations• Reference bit

– Associated with each page table entry– When page is referenced, set ref bit = 1– Replace a page with ref bit = 0 (if it exists)

• pages with ref bit = 0 less recently used than pages with ref bit = 1

• Second chance algorithm– aka “Clock” replacement– For each page,

• If ref bit = 0, replace this page, stop

• Else set ref bit = 0 (for next round), continue

CSS 430: Operating Systems - Virtual Memory 38

Page 39: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Second View of Second Chance

CSS 430: Operating Systems - Virtual Memory 39

Operating Systems Internals & Design Principles, 7th Edition

by William Stallings

Find a frame for page 727

Page 40: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Second View of Second Chance

CSS 430: Operating Systems - Virtual Memory 40

Operating Systems Internals & Design Principles, 7th Edition

by William Stallings

Find a frame for page 727

Page 41: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Counting Algorithms

• Keep a counter of the number of references that have been made to each page

CSS 430: Operating Systems - Virtual Memory 41

Page 42: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Counting Algorithms

• Keep a counter of the number of references that have been made to each page

• Least Frequently Used (LFU) Algorithm: – Replace page with smallest count

• Most Frequently Used (MFU) Algorithm: – Replace page with largest count– Idea: the pages with the smallest counts were

probably just brought in and have yet to be used

CSS 430: Operating Systems - Virtual Memory 42

Page 43: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Allocation of Frames

• Each process needs a minimum number of pages– Example: IBM 370

• May require 6 pages to handle a single SS MOVE instruction• Instruction is 6 bytes, might span 2 pages• from block might span 2 pages• to block might span 2 pages

• Two major allocation schemes– fixed allocation– priority allocation

CSS 430: Operating Systems - Virtual Memory 43

Page 44: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Fixed Allocation

CSS 430: Operating Systems - Virtual Memory 44

Page 45: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Fixed Allocation• Equal allocation: all processes get the same

number of frames– E.g., if there are 100 frames and 5 processes,

give each process 20 frames.• Proportional allocation: differentially allocate

according to the size of process (in pages)

CSS 430: Operating Systems - Virtual Memory 45

mSs

pa

m

sS

ps

iii

i

ii

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

a

a

s

s

m

i

Page 46: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Fixed Allocation

CSS 430: Operating Systems - Virtual Memory 46

mSs

pa

m

sS

ps

iii

i

ii

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

a

a

s

s

m

i

• Equal allocation: all processes get the same number of frames– E.g., if there are 100 frames and 5 processes,

give each process 20 frames.• Proportional allocation: differentially allocate

according to the size of process (in pages)

Page 47: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Priority Allocation

• Proportional allocation scheme using priorities rather than size

• If process Pi generates a page fault,– select for replacement one of its frames– select for replacement a frame from a process

with lower priority number

CSS 430: Operating Systems - Virtual Memory 47

Page 48: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Global vs. Local Allocation• Global replacement – process selects a

replacement frame from the set of all frames; one process can take a frame from another– Priority allocation must use global

replacement

• Local replacement – each process selects from only its own set of allocated frames

48CSS 430: Operating Systems - Virtual Memory

Page 49: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Sufficient Allocation• If a process is not allocated enough pages,

the page-fault rate is very high. This leads to:– ? CPU utilization

49CSS 430: Operating Systems - Virtual Memory

Page 50: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Sufficient Allocation• If a process is not allocated enough pages,

the page-fault rate is very high. This leads to:– low CPU utilization

50CSS 430: Operating Systems - Virtual Memory

Page 51: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Sufficient Allocation• If a process is not allocated enough pages,

the page-fault rate is very high. This leads to:– low CPU utilization– OS decides it needs to increase the degree of

multiprogramming

51CSS 430: Operating Systems - Virtual Memory

Page 52: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Sufficient Allocation• If a process is not allocated enough pages,

the page-fault rate is very high. This leads to:– low CPU utilization– OS decides it needs to increase the degree of

multiprogramming– another process added to the system

52CSS 430: Operating Systems - Virtual Memory

Page 53: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Thrashing• If a process does not have “enough” pages, the

page-fault rate is very high. This leads to:– low CPU utilization– OS decides it needs to increase the degree of

multiprogramming– another process added to the system

• Thrashing more time spent paging than executing user processes

53CSS 430: Operating Systems - Virtual Memory

Page 54: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Memory Reference Patterns

CSS 430: Operating Systems - Virtual Memory 54

Page 55: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Demand Paging & Thrashing

• Why does demand paging work?– Locality model

• Memory references tend to cluster together• Process migrates from one locality to another• Localities may overlap

• Why does thrashing occur?– size of localities > total memory size allocated

CSS 430: Operating Systems - Virtual Memory 55

Page 56: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Working-Set Model• working-set window sequence of page references (e.g., 10,000)• WSSi (working set of Process Pi) =

total number of pages referenced in the most recent (varies in time)– if too small will not encompass entire locality– if too large will encompass several localities– if = will encompass entire program

• D = WSSi total demand frames • if D > m Thrashing ( suspend a process)

CSS 430: Operating Systems - Virtual Memory 56

Page 57: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Working-Set Model• working-set window sequence of page references (e.g., 10,000)• WSSi (working set of Process Pi) =

total number of pages referenced in the most recent (varies in time)– if too small will not encompass entire locality– if too large will encompass several localities– if = will encompass entire program

• D = WSSi total demand frames • if D > m Thrashing ( suspend a process)

CSS 430: Operating Systems - Virtual Memory 57

Page 58: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Page-Fault Frequency Scheme

• Establish “acceptable” page-fault rate– If actual rate too low, process loses frame– If actual rate too high, process gains frame

CSS 430: Operating Systems - Virtual Memory 58

Page 59: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

End of Chapter 9

59CSS 430: Operating Systems - Virtual Memory

Page 60: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Keeping Track of the Working Set• Approximate with interval timer + a reference bit• Example: = 10,000

– Timer interrupts after every 5000 time units– Keep in memory 2 bits for each page– Whenever a timer interrupts, copy & set the values of

all reference bits to 0– If one of the bits in memory = 1 page in working set

• Why is this not completely accurate?• Improvement = 10 bits and interrupt every 1000

time units

CSS 430: Operating Systems - Virtual Memory 60

Page 61: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Working Sets and Page Fault Rates

CSS 430: Operating Systems - Virtual Memory 61

Page 62: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

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.

CSS 430: Operating Systems - Virtual Memory 62

Page 63: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Memory Mapped Files

63CSS 430: Operating Systems - Virtual Memory

Page 64: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Memory-Mapped Shared Memoryin Windows

CSS 430: Operating Systems - Virtual Memory 64

Page 65: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Memory-Mapped Files in Java

CSS 430: Operating Systems - Virtual Memory 65

Page 66: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Allocating Kernel Memory

• Treated differently from user memory

• Often allocated from a free-memory pool– Kernel requests memory for various structures,

many of which have fixed sizes– Some kernel memory needs to be contiguous– All kernel memory accesses needs to be fast

CSS 430: Operating Systems - Virtual Memory 66

Page 67: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Buddy System• Allocates memory from

fixed-size segment consisting of physically-contiguous pages

• Power-of-2 allocator– Satisfies requests in units sized as

power of 2 (rounded up)– When smaller allocation needed

than is available, split a chunk into two buddies of next-lower power of 2

• Continue as needed• Coalescing:

– Combine adjacent chunks when freed

CSS 430: Operating Systems - Virtual Memory 67

Page 68: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Buddy System

CSS 430: Operating Systems - Virtual Memory 68

Operating Systems Internals & Design Principles, 7th Edition

by William Stallings

Page 69: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Tree Representation

CSS 430: Operating Systems - Virtual Memory 69

Operating Systems Internals & Design Principles, 7th Edition

by William Stallings

Page 70: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Slab Allocation

CSS 430: Operating Systems - Virtual Memory 70

Page 71: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Slab Allocator• Alternate strategy• Slab is one or more physically contiguous pages• Cache consists of one or more slabs• Single cache for each unique kernel data structure

– Each cache filled with objects – instantiations of the data structure• When cache created, filled with objects marked as free• When structures stored, objects marked as used• If slab is full of used objects, next object allocated from empty slab

– If no empty slabs, new slab allocated• Benefits?

CSS 430: Operating Systems - Virtual Memory 71

Page 72: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Slab Allocator• Alternate strategy• Slab is one or more physically contiguous pages• Cache consists of one or more slabs• Single cache for each unique kernel data structure

– Each cache filled with objects – instantiations of the data structure• When cache created, filled with objects marked as free• When structures stored, objects marked as used• 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

CSS 430: Operating Systems - Virtual Memory 72

Page 73: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Other Issues

• Pre-paging:– [when] is pre-paging worthwhile?

• Page size– What factors influence page size?– What factors are influenced by page size?

CSS 430: Operating Systems - Virtual Memory 73

Page 74: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Other Issues -- 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

– Assume s pages are prepaged and α of the pages is used

• Is cost of s * α save pages faults > or < than the cost of prepaging s * (1- α) unnecessary pages?

• α near zero prepaging loses

CSS 430: Operating Systems - Virtual Memory 74

Page 75: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Other Issues – Page Size

• Page size selection must take into consideration:– fragmentation– table size – I/O overhead– locality

CSS 430: Operating Systems - Virtual Memory 75

Page 76: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Other Issues – TLB Reach • TLB Reach - The amount of memory accessible from the TLB• TLB Reach = (TLB Size) X (Page Size)• Ideally, the working set of each process is stored in the TLB

– Otherwise there is a high degree of page faults

• Increase the Page Size– This may lead to an increase in fragmentation as not all

applications require a large page size

• Provide Multiple Page Sizes– This allows applications that require larger page sizes the

opportunity to use them without an increase in fragmentation

CSS 430: Operating Systems - Virtual Memory 76

Page 77: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Other Issues – Program Structure• Program structure

– Int[128,128] data;– Each row is stored in one page – Program 1

for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i,j] = 0;

Page faults:

– Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0;

Page faults:

CSS 430: Operating Systems - Virtual Memory 77

Page 78: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Other Issues – Program Structure• Program structure

– Int[128,128] data;– Each row is stored in one page – Program 1

for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i,j] = 0;

Page faults: 128 x 128 = 16,384

– Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0;

Page faults: 1 x 128 = 128

CSS 430: Operating Systems - Virtual Memory 78

Page 79: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Operating System Examples

• Windows XP

• Solaris

CSS 430: Operating Systems - Virtual Memory 79

Page 80: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Windows XP• Uses demand paging with clustering. Clustering brings in pages

surrounding the faulting page.• Processes are assigned working set minimum and working set

maximum (typically 50 and 345 pages)• Working set minimum is the minimum number of pages the process

is guaranteed to have in memory.• A process may be assigned as many pages up to its working set

maximum.• When the amount of free memory in the system falls below a

threshold, automatic working set trimming is performed to restore the amount of free memory (clock or FIFO algorithm)

• Working set trimming removes pages from processes that have pages in excess of their working set minimum.

CSS 430: Operating Systems - Virtual Memory 80

Page 81: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1

Solaris • Maintains free list with

thresholds– Lotsfree: start paging

(1/64 size of phys. memory)– Desfree: increase paging– Minfree: start swapping

• Pageout process• 2-handed clock algorithm

– Handspread– Scanrate: slowscan

fastscan

CSS 430: Operating Systems - Virtual Memory 81

Operating Systems Internals & Design Principles, 7th Edition

by William Stallings