Download Chapter 8: Virtual Memory

Document related concepts

VS/9 wikipedia , lookup

Spring (operating system) wikipedia , lookup

Distributed operating system wikipedia , lookup

CP/M wikipedia , lookup

Process management (computing) wikipedia , lookup

Memory management unit wikipedia , lookup

Paging wikipedia , lookup

Transcript
Chapter 8: Virtual Memory
Rev. by Kyungeun Park, 2015
Operating System Concepts Essentials – 8th Edition
Silberschatz, Galvin and Gagne ©2011
Chapter 8: 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
Operating System Concepts Essentials – 8th Edition
8.2
Silberschatz, Galvin and Gagne ©2011
Objectives

To describe the benefits of a virtual memory system

To explain the concepts of demand paging, page-replacement algorithms,
and allocation of page frames

To discuss the principle of the working-set model
Operating System Concepts Essentials – 8th Edition
8.3
Silberschatz, Galvin and Gagne ©2011
Background


In Chapter 7, discussed various memory-management strategies.

Goal is to keep many processes in memory simultaneously to allow multiprogramming.

Require that an entire process be in memory before it can execute.
But entire program rarely used


Error code, unusual routines (yearly basis), large data structures (arrays, lists, and tables)
Entire program code not needed at the same time
Operating System Concepts Essentials – 8th Edition
8.4
Silberschatz, Galvin and Gagne ©2011
Background


Virtual memory

Separation of user logical memory from physical memory

Only part of the program needs to be in memory for execution

Abstracts main memory into an extremely large, uniform array of storage

Programs no longer constrained by limits of physical memory, being larger than physical
memory (major adv.)  making the programming task much easier

Allows address spaces to be shared by several processes

Allows for more efficient process creation

More programs running concurrently with each program allocated less physical memory

Less I/O needed to load or swap processes (resulting in faster execution of programs)

Not easy to implement; may substantially decrease performance
Virtual memory can be implemented via

Demand paging

Demand segmentation
Operating System Concepts Essentials – 8th Edition
8.5
Silberschatz, Galvin and Gagne ©2011
Virtual Memory That is
Larger Than Physical Memory
Operating System Concepts Essentials – 8th Edition
8.6
Silberschatz, Galvin and Gagne ©2011
Virtual Address Space
Operating System Concepts Essentials – 8th Edition
8.7
Silberschatz, Galvin and Gagne ©2011
Virtual Address Space

Virtual address space of a process: the logical (virtual) view of how a process is
stored in memory

Sparse address spaces: virtual address space that include holes which are
left for dynamic growth of stack or heap segments and dynamically linked libraries

Files and memory to be shared by two or more processes through page sharing

Benefits of virtual memory:

System libraries shared via mapping of shared object into a virtual address
space

Shared memory shared by making processes consider it part of their virtual
address space

Pages can be shared during process creation with fork(), speeding up
process creation
Operating System Concepts Essentials – 8th Edition
8.8
Silberschatz, Galvin and Gagne ©2011
Shared Library Using Virtual Memory
Physical memory
shared by
processes
Operating System Concepts Essentials – 8th Edition
8.9
Silberschatz, Galvin and Gagne ©2011
Demand Paging

Executable program:



Demand paging:






Similar to a paging system with swapping
Less I/O needed, no unnecessary I/O
Less memory needed
Faster response
More users
Lazy swapper – never swaps a page into memory unless that page will be needed



Option one: loaded entire process into memory at load time
Option two: bring a page into memory only when it is needed Demand Paging
The term, swapper manipulates entire processes,
Use a pager rather than swapper, dealing with the individual pages of a process.
Page is needed  reference to it


invalid reference  abort
not-in-memory  bring to memory
Operating System Concepts Essentials – 8th Edition
8.10
Silberschatz, Galvin and Gagne ©2011
Transfer of a Paged Memory to
Contiguous Disk Space
Operating System Concepts Essentials – 8th Edition
8.11
Silberschatz, Galvin and Gagne ©2011
Valid-Invalid Bit

Need a hardware support to distinguish between the pages in memory and the
pages on the disk
 With each page table entry, a valid–invalid bit is associated
 v  both legal and in-memory – memory resident
i  either illegal or currently on disk
 Initially valid–invalid bit is set to i on all entries
 A page table snapshot:

Frame #
valid-invalid bit
v
v

v
i
….
During address translation, if valid–invalid bit
in page table entry is i
 page fault, causing a trap to the OS
 Page fault handling is needed!
i
i
page table
Operating System Concepts Essentials – 8th Edition
8.12
Silberschatz, Galvin and Gagne ©2011
Page Table When Some Pages
Are Not in Main Memory
Operating System Concepts Essentials – 8th Edition
8.13
Silberschatz, Galvin and Gagne ©2011
Handling Page Fault

If the process tried to access a page that was not brought into memory?
 Access to a page marked invalid: Page fault  trap

Page fault handling:
1. Check an internal table (within PCB) to decide:
 Invalid reference  abort, terminate the process
 Valid reference  page it in when it is not in memory
2. Find a free frame (taking one from the free-frame list)
3. Schedule a disk operation to read the desired page into the new frame
4. Reset tables to indicate the page is now in memory: Set validation bit = v
5. Restart the instruction that was interrupted by the trap caused by the page fault
Operating System Concepts Essentials – 8th Edition
8.14
Silberschatz, Galvin and Gagne ©2011
Steps in Handling a Page Fault
Operating System Concepts Essentials – 8th Edition
8.15
Silberschatz, Galvin and Gagne ©2011
Aspects of Demand Paging



Extreme case – start a process with no pages in memory

OS sets instruction pointer to the first instruction of process, non-memory-resident  page fault

And for every other process pages on first access  faults for the page

Pure demand paging : never bring a page into memory until it is required.
Actually, a given instruction could access multiple pages  multiple page faults

One page for the instruction and many for data : unacceptable system performance

Fortunately, pain decreased because of locality of reference  reasonable performance
Hardware support needed for demand paging

Page table with valid / invalid bit

Secondary memory, usually a high-speed disk. (swap device with swap space)

Instruction restart capability after a page fault

We must be able to restart the process in exactly the same place and state,

With the saved state (registers, condition code, instruction counter) of the interrupted process
Operating System Concepts Essentials – 8th Edition
8.16
Silberschatz, Galvin and Gagne ©2011
Instruction Restart

There is a situation before an executing instruction is complete, a page-fault
takes place.

c = a + b;
 1: Fetch and decode the ADD instruction, 2: Fetch a, 3: Fetch b,
4: Add a and b, 5: Store the sum in c : Page fault for c between 4 and 5

The interrupted instruction must be restarted: requiring 1 ~ 5 repeated

Consider an instruction that could access several different locations
 Block move case: a page fault after partial movement
 Source and destination overlap case:

What if source (copy from) and destination (copy to) blocks overlap?
 Checking both ends of both blocks  early page fault before modification

Temporary registers to hold the old values of overwritten location  page
fault  restore the old values  page in  repeat the instruction (update)
Operating System Concepts Essentials – 8th Edition
8.17
Silberschatz, Galvin and Gagne ©2011
Stages in Demand Paging

Stages in Demand Paging
1.
Trap to the operating system
2.
Save the user registers and process state
3.
Determine that the interrupt was a page fault
4.
Check that the page reference was legal and determine the location of the page on the disk
5.
Issue a read from the disk to a free frame:
a)
Wait in a queue for this device until the read request is serviced
b)
Wait for the device seek and/or latency time
c)
Begin the transfer of the page to a free frame
6.
While waiting, allocate the CPU to some other user : context switch
7.
Receive an interrupt from the disk I/O subsystem (I/O completed)
8.
Save the registers and process state for the other user
9.
Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory: set to v
11. Wait for the CPU to be allocated to this process again : context switch
12. Restore the user registers, process state, and new page table, and then resume the interrupted
instruction: restart
Operating System Concepts Essentials – 8th Edition
8.18
Silberschatz, Galvin and Gagne ©2011
Performance of Demand Paging (Cont.)

Demand paging can significantly affect the performance of a computer system.

Page Fault Rate 0  p  1


if p = 0, no page faults

if p = 1, every reference is a fault
Effective Access Time (EAT) for a demand-paged memory
EAT = (1 – p) x memory access + p (page fault overhead
+ swap page out
+ swap page in
Page fault time
+ restart overhead)
Operating System Concepts Essentials – 8th Edition
8.19
Silberschatz, Galvin and Gagne ©2011
Demand Paging Example

Memory access time = 200 nanoseconds (0.2 microseconds)

Average page-fault service time = 8 milliseconds

EAT = (1 – p) * 200 + p (8 milliseconds)
= (1 – p) * 200 + p * 8,000,000
= 200 + p * 7,999,800 (nanoseconds)

If one access out of 1,000 causes a page fault, then p = 1/1,000
EAT = 8.2 microseconds (8200 nanoseconds, compared with 200 nanoseconds)
This is a slowdown by a factor of 40!!

If we want performance degradation to be less than 10 percent,
(EAT =) 220 > 200 + 7,999,800 x p
20 > 7,999,800 x p
p < 0.0000025
Keep < one page fault in every 400,000 memory accesses
Operating System Concepts Essentials – 8th Edition
8.20
Silberschatz, Galvin and Gagne ©2011
Demand Paging Optimizations


Copy entire process image to swap space at process load time

Then page in and out of swap space

Used in older BSD Unix
Demand page in from program binary on disk, but discard rather than paging out
when freeing frame (no modification)

Used in Solaris and current BSD
Operating System Concepts Essentials – 8th Edition
8.21
Silberschatz, Galvin and Gagne ©2011
Copy-on-Write

Copy-on-Write (COW) allows both parent and child processes to initially share
the same pages in memory: fork() system call

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

In general, free pages are allocated from a pool of zero-fill-on-demand pages

vfork() as a variation on fork() system call has parent process suspended
and child process uses address space of parent.

Different from fork() with copy-on-write.

Do not use copy-on-write; changes made by the child process on any pages of the
parent’s address space  the altered pages will be visible to the parent once it resumes

Must be used with caution to prevent the child from modifying the parent address space

vfork() is intended to be used when the child calls exec()immediately after creation

No copy of pages, very efficient method of process creation.
Operating System Concepts Essentials – 8th Edition
8.22
Silberschatz, Galvin and Gagne ©2011
Before Process 1 Modifies Page C
Operating System Concepts Essentials – 8th Edition
8.23
Silberschatz, Galvin and Gagne ©2011
After Process 1 Modifies Page C
Operating System Concepts Essentials – 8th Edition
8.24
Silberschatz, Galvin and Gagne ©2011
What Happens if There is no Free Frame?

Used up by process pages

Also in demand from the kernel, I/O buffers, etc.

How much to allocate to each?

Page replacement – find some page in memory, but not really in use, page it
out


Algorithm – terminate? swap out? replace the page?

Performance – want an algorithm which will result in minimum number of page faults
Same page may be brought into memory several times
Operating System Concepts Essentials – 8th Edition
8.25
Silberschatz, Galvin and Gagne ©2011
Page Replacement

Prevent over-allocation of memory by modifying page-fault service routine to
include page replacement

Use modify (dirty) bit in hardware 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
Operating System Concepts Essentials – 8th Edition
8.26
Silberschatz, Galvin and Gagne ©2011
Need For Page Replacement
No free frames
on the free-frame list:
1. terminate?
2. swap out?
3. page replacement?
Operating System Concepts Essentials – 8th Edition
8.27
Silberschatz, Galvin and Gagne ©2011
Basic Page Replacement
1.
Find the location of the desired page on disk
2.
Find a free frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a victim frame
- Write the victim frame to the disk if dirty
3.
Bring the desired page into the (newly) free frame; update the page and frame tables
4.
Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers (one out and one in) for page fault – increasing EAT
Operating System Concepts Essentials – 8th Edition
8.28
Silberschatz, Galvin and Gagne ©2011
Page Replacement
Operating System Concepts Essentials – 8th Edition
8.29
Silberschatz, Galvin and Gagne ©2011
Frame Allocation and Page Replacement


Frame-allocation algorithm determines

How many frames to give each process

Which frames to replace
Page-replacement algorithm



Want lowest page-fault rate on both first access and re-access
Evaluate algorithm by running it on a particular string of memory references
(reference string) and computing the number of page faults on that string

String is just page numbers, not full addresses

Repeated access to the same page does not cause a page fault
In all our examples, the reference string is
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
Operating System Concepts Essentials – 8th Edition
8.30
Silberschatz, Galvin and Gagne ©2011
Graph of Page Faults Versus
The Number of Frames

As the number of frames increases, the number of page faults drops to some
minimal level. (Adding physical memory increases the number of frames.)
Operating System Concepts Essentials – 8th Edition
8.31
Silberschatz, Galvin and Gagne ©2011
First-In-First-Out (FIFO) Algorithm

Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1

When a page must be replaced, the oldest page is chosen.

3 frames (3 pages can be in memory at a time per process)
(refer to the following slide)

A bad replacement choice increases the page-fault rate and slows
process execution.

Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5

Adding more frames can cause more page faults!


Belady’s Anomaly
How to track ages of pages?

Just use a FIFO queue, replacing a page at the head of the queue
and inserting new page at the tail of the queue
Operating System Concepts Essentials – 8th Edition
8.32
Silberschatz, Galvin and Gagne ©2011
FIFO Illustrating Belady’s Anomaly
reference string: 1,2,3,4,1,2,5,1,2,3,4,5
Operating System Concepts Essentials – 8th Edition
8.33
Silberschatz, Galvin and Gagne ©2011
FIFO Page Replacement
15 page faults
Operating System Concepts Essentials – 8th Edition
8.34
Silberschatz, Galvin and Gagne ©2011
Optimal Page-Replacement Algorithm

Replace page that will not be used for the longest period of time


How do you know this?


9 is optimal for the following example.
Can’t read the future knowledge of the reference string (similar to the SJF CPUscheduling algorithm)
Used mainly for comparison studies, measuring how well your algorithm performs
Operating System Concepts Essentials – 8th Edition
8.35
Silberschatz, Galvin and Gagne ©2011
Least Recently Used (LRU) Algorithm

Use past knowledge rather than future

Replace page that has not been used for the longest period of time

Associate time of last use with each page

12 faults – better than FIFO but worse than OPT

Generally good algorithm and frequently used


Twelve faults

Replaced pages which is about to be used

Still better than FIFO replacement with fifteen
But how to implement LRU replacement?
Operating System Concepts Essentials – 8th Edition
8.36
Silberschatz, Galvin and Gagne ©2011
LRU Algorithm Implementation

Need hardware assistance to determine an order for the frames defined by the time
of last use.

Counter implementation

Every page entry has a time-of-use field and a logical clock (counter) is added to the CPU;

Every time a page is referenced, the clock is incremented and the clock register value is copied
into the time-of-use field in its page-table entry.

When a page needs to be changed, look at the counters to find smallest value



Search through table needed
Stack implementation

Keep a stack of page numbers in a doubly linked list

When a page referenced:

Removed from the stack and put on the top

Most recently used page is always at the top of the stack.

requires 6 pointers to be changed at worst

But each update more expensive

No search for replacement; the tail points to the bottom of the stack, the least recently used
(LRU) page.
LRU and OPT are cases of stack algorithms that don’t have Belady’s Anomaly
Operating System Concepts Essentials – 8th Edition
8.37
Silberschatz, Galvin and Gagne ©2011
Use Of A Stack to Record
The Most Recent Page References
Operating System Concepts Essentials – 8th Edition
8.38
Silberschatz, Galvin and Gagne ©2011
LRU Approximation Algorithms (1)

Depending on computer systems, hardware support (reference bit) varies

Reference bit : 0 or 1

associated with each page, initially = all 0s

When page is referenced bit set to 1

Replace any with reference bit = 0 (if one exists) as the LRU-Approximation algorithm


We do not know the order of use, however
Additional-Reference-Bits Algorithm for additional ordering information

Unsigned 8-bit byte of history of page use

Shifting the other bits right by 1

The page with the lowest number is the LRU page, and replaced
Operating System Concepts Essentials – 8th Edition
8.39
Silberschatz, Galvin and Gagne ©2011
LRU Approximation Algorithms (2)

Second-chance algorithm

Based on FIFO, plus hardware-provided reference bit scheme

Clock replacement

If page to be replaced, select a page according to FIFO


Reference bit = 0  replace it

Reference bit = 1 then give the page a second chance :
–
Set reference bit to 0, arrival time reset to the current time; leave page in memory
–
Move on to select the next FIFO page subject to same rules.
Implement the second-chance algorithm using a circular queue
Operating System Concepts Essentials – 8th Edition
8.40
Silberschatz, Galvin and Gagne ©2011
Second-Chance (clock) Page-Replacement Algorithm
Operating System Concepts Essentials – 8th Edition
8.41
Silberschatz, Galvin and Gagne ©2011
Counting Algorithms

Keep a counter of the number of references that have been made to each
page

LFU (Least-Frequently-Used) Algorithm: replaces page with the smallest
count

When used heavily during the initial phase of a process with a large count,
remains in memory
 Shifting the counts right by 1 bit at regular intervals

MFU (Most-Frequently-Used) Algorithm: based on the argument that the
page with the smallest count was probably just brought in and has yet to be
used

Neither MFU nor LFU replacement is common.
Operating System Concepts Essentials – 8th Edition
8.42
Silberschatz, Galvin and Gagne ©2011
Page-Buffering with Pool of Free Frames


Used in addition to a specific page-replacement algorithm, keeping a pool of free
frames

When page fault, a victim frame is chosen.

However, read page into free frame before the victim is written out.

Allows the process to restart ASAP

When convenient, write-out the victim and add to free-frame pool
Possibly, extended to keep a list of modified pages


When paging device is idle, a modified page is selected and is written to the disk and set to nondirty
Possibly, keep a pool of free frames but remember which page was in each frame
and reuse it if it is needed before that frame is reused.

If referenced again before reused, no need to load contents again from disk

When page fault occurs, first check whether the desired page is in the free-frame pool.

Generally useful to reduce penalty if wrong victim frame selected
Operating System Concepts Essentials – 8th Edition
8.43
Silberschatz, Galvin and Gagne ©2011
Applications and Page Replacement

All of these algorithms have OS guessing about future page access

Some applications have better knowledge – i.e. databases

Memory intensive applications can cause double buffering


OS keeps copy of page in memory as I/O buffer

Application keeps page in memory for its own work
Operating system can give special programs direct access to a disk partition, without
any file-system data structures.


The partition is called raw disk.
Bypasses buffering, locking, etc.
Operating System Concepts Essentials – 8th Edition
8.44
Silberschatz, Galvin and Gagne ©2011
Allocation of Frames

Each process needs at least minimum number of frames.

As # of frames decreases, page-fault rate increases, slowing process
execution.

In addition, when a page-fault occurs before an executing instruction is
complete, the instruction must be restarted  need enough frames to hold
all the different pages.
Operating System Concepts Essentials – 8th Edition
8.45
Silberschatz, Galvin and Gagne ©2011
Fixed Allocation

Equal allocation – For example, if there are 100 frames (after allocating
frames for the OS) and 5 processes, give each process 20 frames


Keep some as free frame buffer pool
Proportional allocation – Allocate available memory according to the size of
process

Dynamic as degree of multiprogramming, process sizes change
si  size of process pi
m  64
s1  10
s2  127
10
a1 
 64  5
137
127
a2 
 64  59
137
S   si
m  total number of frames
si
ai   m, allocation for pi
S

Operating System Concepts Essentials – 8th Edition
8.46
Silberschatz, Galvin and Gagne ©2011
Priority Allocation

Use a proportional allocation scheme using priorities rather than size

If process Pi generates a page fault,

select one of its frames for replacement

select a frame from a process with lower priority number for replacement
Operating System Concepts Essentials – 8th Edition
8.47
Silberschatz, Galvin and Gagne ©2011
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

But then process execution time can vary greatly from process to process because of
totally external circumstances.

But greater throughput so more common
Local replacement – each process selects from only its own set of allocated
frames

More consistent per-process performance

But possibly underutilized memory
Operating System Concepts Essentials – 8th Edition
8.48
Silberschatz, Galvin and Gagne ©2011
Non-Uniform Memory Access (NUMA)

So far, assumed all memory accessed equally

Many systems are NUMA – speed of access to memory varies


Consider system boards containing CPUs and memory, interconnected over a system bus
Optimal performance comes from allocating memory “close to” the CPU on which the
thread is scheduled

And modifying the scheduler to schedule the thread on the same system board when possible

Solved by Solaris by creating lgroups (memory latency groups) in the kernel

Each lgroup gathers together close CPUs and memory

Form a hierarchical structure of lgroups based on the amount of latency between the groups

When possible, schedule all threads of a process and allocate all memory for that process
within an lgroup

This minimizes overall memory latency and maximizes CPU cache hit rates.
Operating System Concepts Essentials – 8th Edition
8.49
Silberschatz, Galvin and Gagne ©2011
Thrashing

If a process does not have “enough” pages, the page-fault rate is very high

Page fault to get page

Replace existing frame

But quickly need replaced frame back

This leads to:


Low CPU utilization

Operating system thinking that it needs to increase the degree of multiprogramming

Another process added to the system
High paging activity, thrashing.


A process is thrashing if it is spending more time paging than executing
Thrashing  a process is busy swapping pages in and out
Operating System Concepts Essentials – 8th Edition
8.50
Silberschatz, Galvin and Gagne ©2011
Thrashing (Cont.)
Operating System Concepts Essentials – 8th Edition
8.51
Silberschatz, Galvin and Gagne ©2011
Demand Paging and Thrashing


Why does demand paging work?
Locality model

A locality is a set of pages that are actively used together.

Process migrates from one locality to another

A program is generally composed of several different localities.

Localities may overlap.
Why does thrashing occur?
 size of locality > total memory size

We can limit the effects of thrashing by using local replacement algorithm or priority
replacement algorithm
Operating System Concepts Essentials – 8th Edition
8.52
Silberschatz, Galvin and Gagne ©2011
Locality In A Memory-Reference Pattern
Operating System Concepts Essentials – 8th Edition
8.53
Silberschatz, Galvin and Gagne ©2011
Working-Set Model

Based on the assumption of locality

  working-set window  a fixed number of page references
Example: 10,000 instructions

Working set: the set of pages in the most recent  (Ex, 10 memory
references) page reference:



The working set is an approximation of locality
WSSi (Working Set Size of Process Pi) =
total number of pages referenced in the most recent  (varies in time)

if  too small, it will not encompass entire locality

if  too large, it will overlap several localities

if  =   it will encompass entire program (the set of pages touched during the process
execution)
D =  WSSi  total demand frames

WSSi : Process i needs WSSi frames.

if D > m (total number of available frames)  Thrashing will occur.

Policy if D > m, then suspend or swap out one of the processes
Operating System Concepts Essentials – 8th Edition
8.54
Silberschatz, Galvin and Gagne ©2011
Working-set model
Operating System Concepts Essentials – 8th Edition
8.55
Silberschatz, Galvin and Gagne ©2011
Keeping Track of the Working Set

Application with a fixed-interval timer interrupt and a reference bit

Example:  = 10,000 references

A timer interrupt every 5,000 references

Store two additional historical reference bits for each page in addition to the
current reference bit.

Whenever a timer interrupts, the current reference bit is copied to one of the
two historical bits, and then cleared

If any of the three bits is set, then that page was reference within the last
15,000 references, and is considered to be in that processes reference set.

Finer resolution = reduce time interval and add more historical bits in-memory
to remember (Ex. 10 bits and interrupt every 1000 time units)
Operating System Concepts Essentials – 8th Edition
8.56
Silberschatz, Galvin and Gagne ©2011
Page-Fault Frequency (PFF) Scheme

More direct approach than Working Set Model to control thrashing

Establish “acceptable” page-fault frequency rate and use local
replacement policy

If actual rate too low, decrease number of frames

If actual rate too high, increase number of frames
Operating System Concepts Essentials – 8th Edition
8.57
Silberschatz, Galvin and Gagne ©2011
Working Sets and Page Fault Rates

Direct relationship between the working set of a process and its page-fault rate.

Under the assumption that there is sufficient memory to store the working set of
a process

Page-fault rate of the process will transit as a process moves from one locality to
another.

A peak: when begin demand-paging a new locality

A valley: once the working set of this new locality is in memory
Operating System Concepts Essentials – 8th Edition
8.58
Silberschatz, Galvin and Gagne ©2011
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 and speeds file access by driving file I/O through memory rather than
read() and write() system calls

Also allows several processes to map the same file allowing the pages in
memory to be shared

But when does written data make it to disk?

Periodically and / or at file close() time

For example, when the pager scans for dirty pages
Operating System Concepts Essentials – 8th Edition
8.59
Silberschatz, Galvin and Gagne ©2011
Memory Sharing
with Memory Mapped Files

Virtual memory map of each sharing process points to the same page of
physical memory – the page that holds a copy of the disk block
Operating System Concepts Essentials – 8th Edition
8.60
Silberschatz, Galvin and Gagne ©2011
Memory-Mapped File Technique for all I/O

Some OSes use memory mapped files for standard I/O

Process can explicitly request memory mapping a file via mmap() system call


Now file mapped into process address space
In Solaris:

A file is specified as memory-mapped using mmap() system call  maps the file into the address
space of the process.

If a file is opened and accessed using ordinary system calls such as open(), read(), write(),
close(),
 But map file into kernel address space

Process still does read() and write()


Uses efficient memory management subsystem


Copies data to and from kernel space and user space
Avoids needing separate subsystem
Memory mapped files can be used for shared memory (although via separate system
calls)
Operating System Concepts Essentials – 8th Edition
8.61
Silberschatz, Galvin and Gagne ©2011
Shared Memory in Windows
using Memory-Mapped I/O

Processes can communicate using shared memory by having the
communicating processes memory-map the same file into their virtual
address space
Operating System Concepts Essentials – 8th Edition
8.62
Silberschatz, Galvin and Gagne ©2011
Allocating Kernel Memory

Treated differently from user memory

Often allocated from a free-memory pool different from the list used to satisfy ordinary
user-mode processes

Kernel requests memory for structures of varying sizes


Some kernel memory needs to be contiguous without the benefit of a virtual memory interface


Minimize waste due to fragmentation
I.e. for device I/O
Two strategies for managing free memory that is assigned to kernel processes:

Buddy system

Slab allocation
Operating System Concepts Essentials – 8th Edition
8.63
Silberschatz, Galvin and Gagne ©2011
Buddy System

Allocates memory from fixed-size segment consisting of physically-contiguous
pages

Memory is allocated from the segment using a power-of-2 allocator

Satisfies requests in units sized as power of 2

Request rounded up to next highest power of 2

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
For example, assume 256KB chunk available, kernel requests 21KB

Split into AL and Ar of 128KB each

One further divided into BL and BR of 64KB
–
One further into CL and CR of 32KB each – one used to satisfy request

Advantage – quickly coalesce unused chunks into larger chunk

Disadvantage - fragmentation
Operating System Concepts Essentials – 8th Edition
8.64
Silberschatz, Galvin and Gagne ©2011
Buddy System Allocator
Operating System Concepts Essentials – 8th Edition
8.65
Silberschatz, Galvin and Gagne ©2011
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



Slab-allocation algorithm uses caches to store kernel objects

When cache created, filled with objects marked as free

When a new object for a kernel data structure is needed, the allocator can assign any free
object from the cache to satisfy the request.

The object assigned from the cache is marked as used.
If slab is full of used objects, next object allocated from empty slab


Each cache filled with objects - instantiations of the kernel data structure
If no empty slabs, a new slab is allocated from contiguous physical pages and assigned to
a cache
Benefits include no fragmentation, fast memory request satisfaction
Operating System Concepts Essentials – 8th Edition
8.66
Silberschatz, Galvin and Gagne ©2011
Slab Allocation

The relationship among slabs, caches, and kernel objects

Individual kernel objects are stored in their respective caches.
Operating System Concepts Essentials – 8th Edition
8.67
Silberschatz, Galvin and Gagne ©2011
Other Considerations – 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 are wasted

Assume s pages are prepaged and a fraction α of these s pages is used (0 <= α <= 1)

If cost of s * α saved pages faults is greater or less than the cost of prepaging
s * (1- α) unnecessary pages?

If α is close to zero  prepaging loses

If α is close to one  prepaging wins
Operating System Concepts Essentials – 8th Edition
8.68
Silberschatz, Galvin and Gagne ©2011
Other Issues – Page Size

Sometimes OS designers have a choice

Especially if running on custom-built CPU

Always power of 2, usually in the range 212 (4,096 bytes) to 222 (4,194,304
bytes)

Page size selection must take into consideration:


Internal Fragmentation (to reduce) : Smaller pages preferred

Page table size (to keep small) : Large page preferred

Better Resolution : with a small page size

Locality : improved with smaller page size

Usually, a smaller page size results in less I/O and less total allocated memory.

I/O overhead (usually seek time and latency > transfer times) : Large page preferred
when comparing the time difference

Number of page faults : Large page preferred

TLB size and effectiveness : Larger page preferred
On average, growing over time
Operating System Concepts Essentials – 8th Edition
8.69
Silberschatz, Galvin and Gagne ©2011
Other Issues – TLB Reach

TLB (translation look-aside buffer) is associative, high-speed memory.

TLB contains only a few of the page-table entries.

TLB hit ratio : the percentage of times that a particular number is found in the TLB

TLB Reach : The amount of memory accessible from the TLB


Ideally, the working set of each process is stored in the TLB


Otherwise process will spend a considerable amount of time resolving memory references in
the page table rather than the TLB
Increase the Page Size


TLB Reach = (TLB Size) X (Page Size)
This may lead to an increase in fragmentation as not all applications require a large page size
Provide Multiple Page Sizes

TLB needs to be managed by the OS

This allows applications that require larger page sizes to use them without an increase in
fragmentation
Operating System Concepts Essentials – 8th Edition
8.70
Silberschatz, Galvin and Gagne ©2011
Other Issues – Program Structure

Program structure




In some cases, system performance may be improved if the user has an awareness of
the underlying demand paging.
int[128,128] data;
Notice that the array is stored row major.  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;
 causes 128 x 128 = 16,384 page faults

Program 2:
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i][j] = 0;
 causes 128 page faults
Operating System Concepts Essentials – 8th Edition
8.71
Silberschatz, Galvin and Gagne ©2011
Other Issues – I/O Interlock

I/O Interlock – When demand paging is used, we sometimes allow some of the
pages to be locked in memory.

Consider I/O

Pages that are used for copying a file from a device must be locked from
being selected for eviction by a page replacement algorithm

When the I/O is complete, the pages are unlocked.
Operating System Concepts Essentials – 8th Edition
8.72
Silberschatz, Galvin and Gagne ©2011
Reason Why Frames used for I/O
must be in Memory

A controller for a USB storage device is generally given the number of bytes to
transfer and a memory address for the buffer while waiting the I/O.

What if other processes cause page fault and OS replaces the page containing the
memory buffer for the waiting process?
• I/O between system memory and the I/O device
• Lock bit to prevent the page from being selected for replacement
Operating System Concepts Essentials – 8th Edition
8.73
Silberschatz, Galvin and Gagne ©2011
Operating System Examples

How Windows and Solaris implement virtual memory.

Windows

Solaris
Operating System Concepts Essentials – 8th Edition
8.74
Silberschatz, Galvin and Gagne ©2011
Windows

Uses demand paging with clustering. Clustering brings in pages surrounding
the faulting page

Processes are assigned working set minimum and working set maximum

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 by referring to its working set minimum

Working set trimming removes pages from processes that have pages in
excess of their working set minimum
Operating System Concepts Essentials – 8th Edition
8.75
Silberschatz, Galvin and Gagne ©2011
Solaris

Maintains a list of free pages to assign faulting processes

Lotsfree – threshold parameter (amount of free memory) to begin paging

Desfree – threshold parameter to increase paging

Minfree – threshold parameter to being swapping

Paging is performed by pageout process

Pageout scans pages using modified clock algorithm

Scanrate is the rate at which pages are scanned. This ranges from slowscan
to fastscan

Pageout is called more frequently depending upon the amount of free
memory available

Pages from shared libraries are skipped during the page-scanning process.
Operating System Concepts Essentials – 8th Edition
8.76
Silberschatz, Galvin and Gagne ©2011
Solaris 2 Page Scanner
Operating System Concepts Essentials – 8th Edition
8.77
Silberschatz, Galvin and Gagne ©2011