• 沒有找到結果。

Virtual Memory

N/A
N/A
Protected

Academic year: 2022

Share "Virtual Memory"

Copied!
39
0
0

加載中.... (立即查看全文)

全文

(1)

Chapter 9

Virtual-Memory Management

Virtual Memory

ƒ Virtual Memory

ƒ A technique that allows the execution of a process that may not be

completely in memory.

ƒ Motivation:

ƒ An entire program in execution may not all be needed at the same time!

ƒ e.g. error handling routines, a large array, certain program features, etc

(2)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Virtual Memory

ƒ Potential Benefits

ƒ Programs can be much larger than the amount of physical memory. Users can concentrate on their problem programming.

ƒ The level of multiprogramming increases because processes occupy less physical memory.

ƒ Each user program may run faster because less I/O is needed for loading or swapping user programs.

ƒ Implementation: demand paging,

demand segmentation (more difficult),etc.

Demand Paging – Lazy Swapping

ƒ Process image may reside on the backing store. Rather than swap in the entire

process image into memory, Lazy

Swapper only swap in a page when it is needed!

ƒ Pure Demand Paging – Pager vs Swapper

ƒ A Mechanism required to recover from the missing of non-resident referenced pages.

ƒ A page fault occurs when a process references a non-memory-resident page.

(3)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Demand Paging – Lazy Swapping

CPU p d f d

4 v

i

6 v

i

9 vi

i i

Page Table

. . . 9 -F

8 7 6 - C

5 4 - A

3 2 1 0

valid-invalid bit invalid page?

non-memory- resident page?

A B C D E F

Logical Memory

Physical Memory

A Procedure to Handle a Page Fault

OS

CPU i Free

Frame 1. Reference

6. Return to execute the instruction

5. Reset the Page Table 2. Trap

(valid disk-resident page) 3. Issue a ‘read”

instruction & find a free frame

4. Bring in the missing page

(4)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

A Procedure to Handle A Page Fault

ƒ Pure Demand Paging:

ƒ Never bring in a page into the memory until it is required!

ƒ Pre-Paging

ƒ Bring into the memory all of the pages that “will” be needed at one time!

ƒ Locality of reference

Hardware Support for Demand Paging

ƒ New Bits in the Page Table

ƒ To indicate that a page is now in memory or not.

ƒ Secondary Storage

ƒ Swap space in the backing store

ƒ A continuous section of space in the secondary storage for better

performance.

(5)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Crucial issues

ƒ Example 1 – Cost in restarting an instruction

ƒ Assembly Instruction: Add a, b, c

ƒ Only a short job!

ƒ Re-fetch the instruction, decode, fetch operands, execute, save, etc

ƒ Strategy:

ƒ Get all pages and restart the instruction from the beginning!

Crucial Issues

ƒ Example 2 – Block-Moving Assembly Instruction

ƒ MVC x, y, 256

ƒ IBM System 360/ 370

ƒ Characteristics

ƒ More expensive

ƒ “self-modifying” “operands”

ƒ Solutions:

ƒ Pre-load pages

ƒ Pre-save & recover before page-fault services

x:

y:

A B C D A B C D

Page fault!

Return??

X is destroyed MVC x, y, 4

(6)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Crucial Issues

(R2) +

- (R3)

Page Fault

When the page fault is serviced, R2, R3 are modified!

- Undo Effects!

ƒ Example 3 – Addressing Mode

MOV (R2)+, -(R3)

Performance of Demand Paging

ƒ Effective Access Time:

ƒ ma: memory access time for paging

ƒ p: probability of a page fault

ƒ pft: page fault time (1 - p) * ma + p * pft

(7)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Performance of Demand Paging

ƒ Page fault time - major components

ƒ Components 1&3 (about 103 ns ~ 105 ns)

ƒ Service the page-fault interrupt

ƒ Restart the process

ƒ Component 2 (about 25ms)

ƒ Read in the page (multiprogramming!

However, let’s get the taste!)

ƒ pft ≈ 25ms = 25,000,000 ns

ƒ Effect Access Time (when ma = 100ns)

ƒ (1-p) * 100ns + p * 25,000,000 ns

ƒ 100ns + 24,999,900ns * p

Performance of Demand Paging

ƒ Example (when ma = 100ns)

ƒ p = 1/1000

ƒ Effect Access Time ≈ 25,000 ns

→ Slowed down by 250 times

ƒ How to only 10% slow-down?

110 > 100 * (1-p) + 25,000,000 * p p < 0.0000004

p < 1 / 2,500,000

(8)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Performance of Demand Paging

ƒ How to keep the page fault rate low?

ƒ Effective Access Time ≈ 100ns + 24,999,900ns * p

ƒ Handling of Swap Space – A Way to Reduce Page Fault Time (pft)

ƒ Disk I/O to swap space is generally faster than that to the file system.

ƒ Preload processes into the swap space before they start up.

ƒ Demand paging from file system but do page replacement to the swap space. (BSD UNIX)

Copy-on-Write

ƒ Rapid Process Creation and Reducing of New Pages for the New Process

ƒ fork(); execve()

ƒ Shared pages Æ copy-on-write pages

ƒ Only the pages that are modified are copied!

3 4 6 1

3 4 6 1

* data1

*

* ed1

*

* ed2

*

* ed3

?? ::

Page Table 1

Page Table 2

P1

P2

(9)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Copy-on-Write

ƒ zero-fill-on-demand

ƒ Zero-filled pages, e.g., those for the stack or bss.

ƒ vfork() vs fork() with copy-on-write

ƒ vfork() lets the sharing of the page table and pages between the parent and child processes.

ƒ Where to keep the needs of copy-on- write information for pages?

Page Replacement

ƒ Demand paging increases the

multiprogramming level of a system by

“potentially” over-allocating memory.

ƒ Total physical memory = 40 frames

ƒ Run six processes of size equal to 10 frames but with only five frames. => 10 spare frames

ƒ Most of the time, the average memory usage is close to the physical memory size if we increase a system’s

multiprogramming level!

(10)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Page Replacement

ƒ Q: Should we run the 7th processes?

ƒ How if the six processes start to ask their shares?

ƒ What to do if all memory is in use, and more memory is needed?

ƒ Answers

ƒ Kill a user process!

ƒ But, paging should be transparent to users?

ƒ Swap out a process!

ƒ Do page replacement!

Page Replacement

ƒ A Page-Fault Service

ƒ Find the desired page on the disk!

ƒ Find a free frame

ƒ Select a victim and write the victim page out when there is no free frame!

ƒ Read the desired page into the selected frame.

ƒ Update the page and frame tables, and restart the user process.

(11)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

B M 0

E 7

A 6

J 5

M/B 4

H 3

D 2

1

OS v

5 i v 4

v 3

v 2

v 7

i v 6

3 2 1 0

J Load

M H

3 2 1 0

E D B A

P1

P2 PC

Page Replacement

Page Table Logical Memory

OS

ƒ Two page transfers per page fault if no frame is available!

Y V

7

Y V

3

N V

4

N V

6

Modify (/Dirty) Bit! To

“eliminate” ‘swap out” =>

Reduce I/O time by one-half

Page Replacement

Page Table

Valid-Invalid Bit

Modify Bit is set by the hardware automatically!

(12)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Page Replacement

ƒ Two Major Pieces for Demand Paging

ƒ Frame Allocation Algorithms

ƒ How many frames are allocated to a process?

ƒ Page Replacement Algorithms

ƒ When page replacement is required, select the frame that is to be

replaced!

ƒ Goal: A low page fault rate!

ƒ Note that a bad replacement choice does not cause any incorrect execution!

Page Replacement Algorithms

ƒ Evaluation of Algorithms

ƒ Calculate the number of page faults on strings of memory references, called reference strings, for a set of algorithms

ƒ Sources of Reference Strings

ƒ Reference strings are generated artificially.

ƒ Reference strings are recorded as system traces:

ƒ How to reduce the number of data?

(13)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Page Replacement Algorithms

ƒ Two Observations to Reduce the Number of Data:

ƒ Consider only the page numbers if the page size is fixed.

ƒ Reduce memory references into page references

ƒ If a page p is referenced, any immediately following references to page p will never cause a page fault.

ƒ Reduce consecutive page references of page p into one page reference.

Page Replacement Algorithms

Does the number of page faults decrease when the number of page frames available increases?

XX XX page# offset

0100, 0432, 0101, 0612, 0103, 0104, 0101, 0611 01, 04, 01, 06, 01, 01, 01, 06

01, 04, 01, 06, 01, 06

ƒ Example

(14)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

FIFO Algorithm

ƒ A FIFO Implementation

1. Each page is given a time stamp when it is brought into memory.

2. Select the oldest page for replacement!

reference string page frames

FIFO queue

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

7 7

0 7 0 1

2 0 1

2 3 1

4 3 0 2 3 0

4 2 0

4 2 3

0 2 3

7 7

0 7 0 1

0 1 2

1 2 3

2 3 0

3 0 4

0 4 2

4 2 3

2 3 0

0 1 3

0 1 2

7 1 2

7 0 2

7 0 1 3

0 1

0 1 2

1 2 7

2 7 0

7 0 1

FIFO Algorithm

ƒ The Idea behind FIFO

ƒ The oldest page is unlikely to be used again.

??Should we save the page which will be used in the near future??

ƒ Belady’s anomaly

ƒ For some page-replacement algorithms, the page fault rate may increase as the number of allocated frames increases.

(15)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

FIFO Algorithm

Run the FIFO algorithm on the following reference:

1 2 3 4 1 2 5 1 2 3 4 5 1 1 1 2 3 4 1 1 1 2 5 5 2 2 3 4 1 2 2 2 5 3 3 3 4 1 2 5 5 5 3 4 4 1 1 1 1 1 1 2 3 4 5 1 2 2 2 2 2 2 3 4 5 1 2 3 3 3 3 3 4 5 1 2 3 4 4 4 4 5 1 2 3 4 5

Push out pages

that will be used later!

string:

3 frames

4 frames

Optimal Algorithm (OPT)

ƒ Optimality

ƒ One with the lowest page fault rate.

ƒ Replace the page that will not be used for the longest period of time. ÅÆ Future Prediction

reference string page frames

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

7 7

0 7 0 1

2 0 1

2 0 3

2 4 3

2 0 3

2 0 1

7 0 1 next 7

next 0

next 1

(16)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Least-Recently-Used Algorithm (LRU)

ƒ The Idea:

ƒ OPT concerns when a page is to be used!

ƒ “Don’t have knowledge about the future”?!

ƒ Use the history of page referencing in the past to predict the future!

S ? SR ( SR is the reverse of S !)

LRU Algorithm

reference string page frames

LRU queue

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

7 7

0 7 0 1

2 0 1

2 0 3

4 0 3

4 0 2

4 3 2

0 3 2

0 0

7 1 0 7

2 1 0

3 0 2

0 3 2

4 0 3

2 4 0

3 2 4

0 3 2

1 3 2

1 0 2

7 0 7 1

2 3

2 1 3

1 0 2

7 1 0

0 7 1 0

2 1

3 0 2

2 3 0

0 2 1

1 0 7 a wrong prediction!

ƒ Example

(17)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

LRU Implementation – Counters

CPU p d f d

frame # v/i timetag p

f

cnt++

Time of Last Use!

……

Page Table for Pi Logical

Address

Physical Memory

Disk Update the

“time-of-use”

field A Logical Clock

LRU Implementation – Counters

ƒ Overheads

ƒ The logical clock is incremented for every memory reference.

ƒ Update the “time-of-use” field for each page reference.

ƒ Search the LRU page for replacement.

ƒ Overflow prevention of the clock & the maintenance of the “time-of-use” field of each page table.

(18)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

LRU Implementation – Stack

CPU p d f d

frame # v/i p

f

……

Page Table Logical

Address

Physical Memory

Disk

Head

Tail

(The LRU page!) A LRU

Stack

move

Overheads: Stack maintenance per memory reference ~ no search for page replacement!

A Stack Algorithm

ƒ Need hardware support for efficient implementations.

ƒ Note that LRU maintenance needs to be done for every memory reference.

memory- resident pages

memory- resident pages

n frames

available

(n +1) frames available

(19)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

LRU Approximation Algorithms

ƒ Motivation

ƒ No sufficient hardware support

ƒ Most systems provide only “reference bit”

which only indicates whether a page is used or not, instead of their order.

ƒ Additional-Reference-Bit Algorithm

ƒ Second-Chance Algorithm

ƒ Enhanced Second Chance Algorithm

ƒ Counting-Based Page Replacement

Additional-Reference-Bits Algorithm

ƒ Motivation

ƒ Keep a history of reference bits

1 01101101

0 10100011

0 11101010

1 00000001

OS shifts all

history registers right by one bit at each regular interval!!

reference

bit one byte per page in memory

(20)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

ƒ History Registers

ƒ But, how many bits per history register should be used?

ƒ Fast and cost-effective!

ƒ The more bits, the better the approximation is.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 LRU

(smaller value!)

MRU

Not used for 8 times

Used at least once every time

Additional-Reference-Bits Algorithm

Second-Chance (Clock) Algorithm

ƒ Motivation

ƒ Use the reference bit only

ƒ Basic Data Structure:

ƒ Circular FIFO Queue

ƒ Basic Mechanism

ƒ When a page is selected

ƒ Take it as a victim if its reference bit = 0

ƒ Otherwise, clear the bit and advance to the 0

0 1 1

1 0

Reference Bit

Page

0 0 0 0

1 0

Reference Bit

Page

(21)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Enhanced Second-Chance Algorithm

ƒ Motivation:

ƒ Consider the cost in swapping out pages.

ƒ 4 Classes (reference bit, modify bit)

ƒ (0,0) – not recently used and not “dirty”

ƒ (0,1) – not recently used but “dirty”

ƒ (1,0) – recently used but not “dirty”

ƒ (1,1) – recently used and “dirty”

low priority

high priority

Enhanced Second-Chance Algorithm

ƒ Use the second-chance algorithm to replace the first page encountered in the lowest nonempty class.

=> May have to scan the circular queue several times before find the right page.

ƒ Macintosh Virtual Memory

Management

(22)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Counting-Based Algorithms

ƒ Motivation:

ƒ Count

the # of references made to each page, instead of their referencing times.

ƒ Least Frequently Used Algorithm (LFU)

ƒ LFU pages are less actively used pages!

ƒ Potential Hazard: Some heavily used pages may no longer be used !

ƒ A Solution – Aging

ƒ Shift counters right by one bit at each regular interval.

Counting-Based Algorithms

ƒ Most Frequently Used Algorithm (MFU)

ƒ Pages with the smallest number of references are probably just brought in and has yet to be used!

ƒ LFU & MFU replacement schemes can be fairly expensive!

ƒ They do not approximate OPT very well!

(23)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Page Buffering

ƒ Basic Idea

a. Systems keep a pool of free frames

b. Desired pages are first “swapped in” some frames in the pool.

c. When the selected page (victim) is later written out, its frame is returned to the pool.

ƒ Variation 1

a. Maintain a list of modified pages.

b. Whenever the paging device is idle, a modified page is written out and reset its

“modify bit”.

Page Buffering

ƒ Variation 2

a. Remember which page was in each frame of the pool.

b. When a page fault occurs, first check

whether the desired page is there already.

ƒ Pages which were in frames of the pool must be “clean”.

ƒ “Swapping-in” time is saved!

ƒ VAX/VMS with the FIFO replacement algorithm adopt it to improve the

performance of the FIFO algorithm.

(24)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Frame Allocation – Single User

ƒ Basic Strategy:

ƒ User process is allocated any free frame.

ƒ User process requests free frames from the free-frame list.

ƒ When the free-frame list is exhausted, page replacement takes place.

ƒ All allocated frames are released by the ending process.

ƒ Variations

ƒ O.S. can share with users some free frames for special purposes.

ƒ Page Buffering - Frames to save “swapping”

time

Frame Allocation – Multiple Users

ƒ Fixed Allocation

a. Equal Allocation

m frames, n processes Æ m/n frames per process

b. Proportional Allocation 1. Ratios of Frames ∝ Size

S = Σ Si, Ai ∝ (Si/ S) x m, where (sum <= m) &

(Ai>= minimum # of frames required)

2. Ratios of Frames ∝ Priority

Si : relative importance

(25)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Frame Allocation – Multiple Users

ƒ Dynamic Allocation

a. Allocated frames ∝ the multiprogramming level

b. Allocated frames ∝ Others

ƒ The minimum number of frames

required for a process is determined by the instruction-set architecture.

ƒ ADD A,B,C Æ 4 frames needed

ƒ ADD (A), (B), (C) Æ 1+2+2+2 = 7 frames, where (A) is an indirect addressing.

Frame Allocation – Multiple Users

ƒ Minimum Number of Frames (Continued)

ƒ How many levels of indirect

addressing should be supported?

ƒ It may touch every page in the logical address space of a process

=> Virtual memory is collapsing!

ƒ A long instruction may cross a page boundary.

MVC X, Y, 256 Æ 2 + 2 + 2 = 6 frames

ƒ The spanning of the instruction and the operands.

address

16 bits

1 indirect 0 direct

(26)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Frame Allocation – Multiple Users

ƒ Global Allocation

ƒ Processes can take frames from others. For example, high-priority processes can

increase its frame allocation at the expense of the low-priority processes!

ƒ Local Allocation

ƒ Processes can only select frames from their own allocated frames Æ Fixed Allocation

ƒ The set of pages in memory for a process is affected by the paging behavior of only that process.

Frame Allocation – Multiple Users

ƒ Remarks

a.Global replacement generally results in a better system throughput

b.Processes can not control their own page fault rates such that a process can affect each another easily.

(27)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Thrashing

ƒ Thrashing – A High Paging Activity:

ƒ A process is thrashing if it is spending more time paging than executing.

ƒ Why thrashing?

ƒ Too few frames allocated to a process!

Thrashing

low CPU utilization Dispatch a new process

under a global page- replacement algorithm

degree of multiprogramming

CPU utilization

thrashing

Thrashing

ƒ Solutions:

ƒ Decrease the multiprogramming level Æ Swap out processes!

ƒ Use local page-replacement algorithms

ƒ Only limit thrashing effects “locally”

ƒ Page faults of other processes also slow down.

ƒ Give processes as many frames as they need!

ƒ But, how do you know the right number of frames for a process?

(28)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Locality Model

ƒ A program is composed of several different (overlapped) localities.

ƒ Localities are defined by the program

structures and data structures (e.g., an array, hash tables)

ƒ How do we know that we allocate enough frames to a process to accommodate its current locality?

localityi= {Pi,1,Pi,2,…,Pi,ni}

control flow

localityj= {Pj,1,Pj,2,…,Pj,nj}

Working-Set Model

ƒ The working set is an approximation of a program’s locality.

Page references

…2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4

working-set windowΔ t1

working-set(t1) = {1,2,5,6,7}

working-set windowΔ t2

working-set(t2) = {3,4}

The minimum allocation

Δ

All touched pages may cover several

(29)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Working-Set Model

where M is the total number of available frames.

= working set size M

D

i

Suspend some processes and

swap out their pages.

“Safe”

D>M

Extra frames are available, and initiate new processes.

D>M

D≦M

Working-Set Model

ƒ The maintenance of working sets is expensive!

ƒ Approximation by a timer and the reference bit

ƒ Accuracy v.s. Timeout Interval!

0 1

1 0

…… …… …… ……

shift or copy

timer!

reference bit in-memory history

(30)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Page-Fault Frequency

ƒ Motivation

ƒ Control thrashing directly through the observation on the page-fault rate!

increase # of frames!

decrease # of frames!

upper bound lower bound

number of frames

*Processes are suspended and swapped out if the number of available frames is reduced to that under the minimum needs.

page-fault rate

Memory-Mapped Files

ƒ File writes might not cause any disk write!

ƒ Solaris 2 uses memory-mapped files for open(), read(), write(), etc.

24 51 6 3

2 45 1

6 3

2 45 1

6 3

P1 VM P2 VM

(31)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Shared Memory – Win32 API

ƒ Producer

1. hfile=CreateFile(“temp,txt”, …);

2. hmapfile=CreateFileMapping(hfi le, …, TEXT(“Shared Object”));

3. lpmapaddr=MapViewOfFile(hm apfile, …);

4. sprintf(lpmapaddr,”for consumer”);

5. UnmapViewOfFile(lpmapaddr);

6. CloseHandle(hfile);

7. CloseHandle(hmapfile);

ƒ Consumer

1. hmapfile=OpenFileMapping(

hfile, …, TEXT(“Shared Object”));

2. lpmapaddr=MapViewOfFile(

hmapfile, …);

3. printf(lpmapaddr,”for consumer”);

4. UnmapViewOfFile(“Get

%s\n”, lpmapaddr);

5. CloseHandle(hfile);

6. CloseHandle(hmapfile);

* Named shared-memory objects

Memory-Mapped I/O

ƒ Processor can have direct access!

ƒ Intermediate storage for data in the registers of device controllers

ƒ Memory-Mapped I/O (PC & Mac)

(1) Frequently used devices

(2) Devices must be fast, such as video controller, or special I/O instructions is used to move data between

memory & device controller registers

ƒ Programmed I/O – polling

ƒ or interrupt-driven handling

R1 R2

R3 . . .

Memory

Device Controller

(32)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Kernel Memory Allocation

ƒ Separation from user-mode memory allocation

ƒ The kernel might request memory of various sizes, that are often less than a page in size.

ƒ Certain hardware devices interact directly with physical memory, and the accesses memory must be in physically contiguous pages!

Kernel Memory Allocation

ƒ The Buddy System

ƒ A fixed-size segment of physically contiguous pages

ƒ A power-of-2 allocator

ƒ Advantage: quick

coalescing algorithms

ƒ Disadvantage: internal

256KB

128KB 128KB

64KB 64KB

(33)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Kernel Memory Allocation

ƒ Slab Allocation

ƒ Slab: one or more physically contiguous pages

ƒ Cache: one or more slabs

Kernel Objects Caches Slabs

• Slab States

• Full

• Empty

• Partial

Kernel Memory Allocation

ƒ Slab Allocator

ƒ Look for a free object in a partial slab.

ƒ Otherwise, allocate a new slab and assign it to a cache.

ƒ Benefits

ƒ No space wasted in fragmentation.

ƒ Memory requests are satisfied quickly.

ƒ Implementations

ƒ Solaris 2.4 kernel, Linux version 2.2+

(34)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Other Considerations

ƒ Pre-Paging

ƒ Bring into memory at one time all the pages that will be needed!

ƒ Issue

Pre-Paging Cost Cost of Page Fault Services ready

processes

suspended processes resumed

swapped out

Do pre-paging if the working set is known!

Not every page in the working set will be used!

Other Considerations

ƒ Page Size

ƒ Trends - Large Page Size

∵ The CPU speed and the memory capacity grow much faster than the disk speed!

small p d large

Smaller Page Table Size &

Better I/O Efficiency Better

Resolution for Locality &

Internal

Fragmentation 512B(29)~16,384B(212) Page Size

(35)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Other Considerations

ƒ TLB Reach

ƒ TLB-Entry-Number * Page-Size

ƒ Wish

ƒ The working set is stored in the TLB!

ƒ Solutions

ƒ Increase the page size

ƒ Have multiple page sizes –

UltraSparc II (8KB - 4MB) + Solaris 2 (8KB or 4MB)

Other Considerations

ƒ Inverted Page Table

ƒ The objective is to reduce the

amount of physical memory for page tables, but they are needed when a page fault occurs!

ƒ More page faults for page tables will occur!!!

(36)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Other Considerations

ƒ Program Structure

ƒ Motivation – Improve the system performance by an awareness of the underlying demand paging.

var A: array [1..128,1..128] of integer;

for j:=1 to 128

for i:=1 to 128 A(i,j):=0 A(1,1)

A(1,2) . . A(1,128)

A(2,1) A(2,2)

. . A(2,128)

A(128,1) A(128,2)

. . A(128,128)

……

128 words

128 pages

128x128 page faults if the process has less than 128 frames!!

Other Considerations

ƒ Program Structures:

ƒ Data Structures

ƒ Locality: stack, hash table, etc.

ƒ Search speed, # of memory references, # of pages touched, etc.

ƒ Programming Language

ƒ Lisp, PASCAL, etc.

ƒ Compiler & Loader

ƒ Separate code and data

ƒ Pack inter-related routines into the same page

(37)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

I/O Interlock

buffer Drive

• DMA gets the following information of the buffer:

• Base Address in Memory

• Chunk Size

• Could the buffer-residing pages be swapped out?

Physical Memory

I/O Interlock

ƒ Solutions

ƒ I/O Device ÅÆ System Memory ÅÆ User Memory

ƒ Extra Data Copying!!

ƒ Lock pages into memory

ƒ The lock bit of a page-faulting page is set until the faulting process is dispatched!

ƒ Lock bits might never be turned off!

ƒ Multi-user systems usually take locks as

“hints” only!

(38)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

Real-Time Processing

ƒ Solution:

ƒ Go beyond locking hints Î Allow

privileged users to require pages being locked into memory!

Predictable Behavior

Virtual memory

introduces unexpected, long-term delays in the execution of a program.

OS Examples – XP

ƒ Virtual Memory – Demand Paging with Clustering

ƒ Clustering brings in more pages surrounding the faulting page!

ƒ Working Set

ƒ A Min and Max bounds for a process

ƒ Local page replacement when the max number of frames are allocated.

ƒ Automatic working-set trimming reduces allocated frames of a process to its min when the system threshold on the

available frames is reached.

(39)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.

OS Examples – Solaris

ƒ Process pageout first clears the reference bit of all pages to 0 and then later returns all pages with the reference bit = 0 to the system (handspread).

ƒ 4HZ Æ 100HZ when desfree is reached!

ƒ Swapping starts when desfree fails for 30s.

ƒ pageout runs for every

request to a new page when minfree is reached.

lotsfree 100

slowscan 8192 fastscan

desfree minfree

Demand Segmentation

ƒ Motivation

ƒ Segmentation captures better the logical structure of a process!

ƒ Demand paging needs a significant amount of hardware!

ƒ Mechanism

ƒ Like demand paging!

ƒ However, compaction may be needed!

ƒ Considerable overheads!

參考文獻

相關文件

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005!.

Tei-Wei Kuo, Embedded System and Wireless Networking Lab, National Taiwan University.. Real-Time

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

All rights reserved.. 1

All rights reserved.... All

National Taiwan University July 9, 2005 Page 5..

National Taiwan University August 28, 2005 Page 5..

* All rights reserved, Tei-Wei Kuo, National Taiwan University,