Top Banner
Chapter 7 Memory Management
97

Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Oct 14, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Chapter 7

Memory Management

Page 2: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.1 Allocation strategies

• Problem

• Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area: (real) Main Memory (and Swap Space)

7-2 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

allocated available/free

Memory

Page 3: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Structure of Memory Management

7-3 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Memory Management

allocate release

free set_occupied set_free

Interface

autonomous Algorithms

data object for memory management

Page 4: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Design parameters

• Memory management strategies can be distinguished based on: • Sequence of operation • Size of pieces • Representation of allocation • Fragmentation • Allocation strategies (with free pieces) • (Re-)integration

7-4 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 5: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Sequence of operation

• Allocation and release • in same order

• Queing approaches, FIFO = First In First Out • in reverse order

• Batch approaches, LIFO = Last In First Out • in arbitrary order

• General approach

7-5 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 6: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Size of pieces

• Constant size • NUM = 1 (unit size)

• Multiple of constant size

• NUM = k (unit size)

• Given size of partitions • NUM = k1, k2, k3, …

• Arbitrary size

• NUM = x

7-6 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 7: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Representation of allocation

• How? • Vector • Table

• Where? • Separated • Integrated

Representation by vector separated and integrated

• Example • Main Memory 128 Mbyte (227 Byte) • Unit size 512 Byte (29 Byte) • Sum 262144 Units (218) • Representation with 8192 words with 32 Bit

7-7 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

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

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

Page 8: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Representation of allocation

• Representation by table • Separated representation • Holding information about allocation in table • Sorting by address and/or length

Sorted by address Sorted by lenght

7-8 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 1 2 3 4 8 10 14 17 20

Address Length 0 3 4 4 14 3 20 13

Length Address 3 14 3 0 4 4 13 20

Page 9: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Representation by table

• Integrated representation • Pieces identify itself, specify length and provide pointer to

next element of free list.

Sorted by address Sorted by length

7-9 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 3 4 8 10 14 17 20 3 4 3 13

0 3 4 8 10 14 17 20 3 4 3 13

Page 10: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Fragmentation

• Usually memory is allocated for multiple of units. • Requests therefor are rounded up to the next multiple of

units. • This come with unused parts of the allocated memory. • The unused piece of memory is called internal

fragmentation fint. • Due to the dynamic of allocation and release of pieces it

may happen the overall amount of free memory can satisfy a request, but because of the layout of all of the pieces of free memory is cannot be fulfilled.

• So free memory is created, which is not suitable to be used for requests.

• This is called external fragmentation fext.

7-10 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 11: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Fragmentation

7-11 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

allocated

free, but not allocatable (external fragmentation)

allocated and used allocated, but unused (internal fragmentation)

Page 12: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Allocation strategies

• First Fit strategy

• Search the free list from start. • Take the first piece of free memory satisfying the

request. • Properties

• Low search effort (in case of almost empty memory space). • External fragmentation • Concentration of allocated memory at the begin of the

memory space • Increased search effort in loaded situations

7-12 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 3 4 8 10 14 17 20 3 4 3 2

25 1

27 6

Page 13: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Allocation strategies

• Next Fit strategy, Rotating First Fit strategy

• Cyclic search of list. • Search start at the point of last allocation. • Properties

• Like First Fit, but without concentration at the begin of the memory space

• Therefor slightly reduced search effort (memory space not empty).

7-13 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 3 4 8 10 14 17 20 3 4 3 2

25 1

27 6

Point of last allocation

Page 14: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Allocation strategies

• Best Fit strategy

• Allocation of the smallest piece of memory satisfying the

request. • Properties

• If sorted by address the whole free list has to be searched. • List should be sorted by size of piece of free memory. • Usually reduced external fragmentation, because requests

for small amount of memory may be served without derogation of larger pieces.

• But produces very small pieces of free memory unsuitable for any request (external fragmentation).

7-14 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 3 4 8 10 14 17 20 3 4 3 2

25 1

27 6

Page 15: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Allocation strategies

• Nearest Fit strategy

• A favored address is provided. • Search with First Fit from the point of favored allocation. • Properties

• In case of disc space minimizing the movement of disc arm. Especially if the sequence of access is known, the movement of the disc arm can be optimized.

• File directory information can located in the middle of a cylinder.

• In case of expansion of files the blocks to be allocated should be located in the neighborhood.

7-15 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 3 4 8 10 14 17 20 3 4 3 2

25 1

27 6

Point of favored allocation

Page 16: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Reintegration

• Instantly after release

• Delayed aggregation

7-16 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

used free free used

F reigabe

used free used

used used

used used

Page 17: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Examples

• Ring buffer • Allocation and release in same direction (FIFO) • Fix length of pieces • No search needed • No external fragmentation • Automatic and immediate reintegration

7-17 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Begin of allocation End of allocation

release allocate

Page 18: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Examples

• Stack

• Allocation and release in inverse direction (LIFO) • Arbitrary length of pieces • No search needed • Little external fragmentation • Automatic and immediate reintegration

7-18 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Begin of stack End of stack

release

allocate

Page 19: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Examples

• Vector based approach

• Allocation and release in arbitrary direction • Fixed length with k * unit size • Search for first fitting piece • Internal and external fragmentation • Automatic and immediate reintegration

7-19 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

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

search allocate

: = 1 release

: = 0

Page 20: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Examples

• Boundary tag system free piece

used piece

• Label for pieces • Sorted list by size (length)

7-20 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

used free used

Pointer to prevoius piece length „free“

length

„free“

Pointer to next free piece

used

length

„used“

length

„used“

Page 21: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Boundary tag system

after release after reintegration

7-21 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

L 1 f f f f f f

released

L 1 L 2 L 2 L 3 L 3

L f f L

Page 22: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Boundary tag system

• Properties • Operation in arbitrary order • Allocation of pieces with arbitrary size (length) • Integration of management and representation of pieces

• Doubly linked list sorted by size of pieces • Best Fit search strategy • External fragmentation • Explicit immediate reintegration using length field to check

with neighboring pieces • Immediate integration into linked list

7-22 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 23: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Optimizations

• Reduction of management efforts based on small pieces

• Merge requested piece and small piece (transform external fragmentation into internal fragmentation)

• Avoid integration of small pieces into free list, but merge them with released (big pieces)

7-23 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

used used free requested

too small

Page 24: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Optimizations

• Cost of search on arbitrary order of allocation and release – O(n)

• Reduce search costs • Tailored pieces

• Given size (length) of pieces • Provide number of (statistically) frequently used pieces

7-24 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

1 2 3 4

Page 25: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Reduction of search costs

• Example: access by binary tree

7-25 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

5 5 5 4 2 1 1 1 1 4

4

1

2

5

Page 26: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Memory usage

• Simulation with 32 K units • Uniform distribution of requests with mean value A and standard

deviation SA

• Uniform distribution of usage time within interval (5, 15)

• External fragmentation is increasing with size and diffusion of request

7-26 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

95%

90%

85 %

8 0%

512 1024 2048 4096

Best-Fit

First-Fit

S A =256

A

η

90%

89 %

88%

8 7 %

64 128 256 512

8 6 %

Best-Fit

First-Fit

S A

η

A=1024

Page 27: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system

• Memory is separated in 2kmax units • Smaller pieces are created by (continuously) performed

bisection of bigger pieces • Pieces split in one action can be joined by release • Properties

• Allocation and release in arbitrary order • Allocation of pieces with unit size of 20, 21, 22, …, 2k

• Separated representation • Limited search costs • Internal and external fragmentation • Explicit reintegration

7-27 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 28: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system

7-28 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

32M 16 M 8 M 4 M 2 M

Request: 3M

Request: 800K

Request: 12M

Release : 12M

Request: 3,5M

Release : 3M

Release : 800K

Release : 3,5M

Start:

Page 29: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system

• Representations as tree

7-29 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

32 M

16 M

8 M

4 M

2 M

1 M

Buddies have the same parent node.

Page 30: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Data structures of a Buddy system

• With separated representation

• Array of heads of free lists for pieces with same size

7-30 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

2 n

2 n-1

2 0

2 1

2 2

2 3

Page 31: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Operation of the Buddy system

• Handling of requests • Check for next value with power of two • Take first entry of list • In case of empty list (recursive):

• Take first entry of next list with bigger pieces • Cut piece in half • Insert second half into list of the original size • Take remaining piece to satisfy he request

• Handling release • Determine buddy of the piece to be released • If buddy is used, insert piece into list • In case buddy is free: join both (piece and buddy) • Insert emerged piece into the next list

7-31 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 32: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system – internal fragmentation

• Requests of size a: 1 2 3 4 5 6 7 8 9 10 … • Size of allocated pieces b(a): 1 2 4 4 8 8 8 8 16 16 … • pa – probability request is of size a • b(a) – size of allocated piece for request of size a

• Def.: Internal fragmentation ratio between the expected

value of the number of unused pieces and the expected value of the number of allocated pieces:

• With as the expected values of the size of the allocated piece b or of the size requested respectively the internal fragmentation is 1 – Sa/Sb.

7-32 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

( )( )

( )∑

=

=−

max

max

1

1a

aa

a

aa

abp

aabp

( ) ∑∑==

==maxmax

11: and :

a

aaa

a

aab apSabpS

Page 33: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system – internal fragmentation

• To determine the internal fragmentation an assumption about the distribution of the requests is needed.

• To simplify matters we assume sizes of request are uniform distributed over the interval [1, 2n]. So every size of request have the same probability .

• Approximately the average size requested is

7-33 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

nap −= 2

( ) 112

12

212

2122

21

21 −−

=≈+=

+== ∑ nn

nn

ni

na

n

iS

Page 34: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system – internal fragmentation

• Keeping in mind the size of the allocated pieces is based on the next value with power of two:

• Therefor the ratio so the allocated pieces are used by ¾ and the internal fragmentation is 25%.

7-34 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

( )

32

312

21

121221

21221

21

22844221121

228888442121

112

2

21

0

2

1

times2 1

++

=

≈+

=

−−

+=

+=

++⋅+⋅+⋅+=

+++++++++++=

nn

n

n

n

n

i

in

nnn

nnnb

n

S

43223 11 //S/S nnba =⋅≈ +−

Page 35: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buddy system

• Fast operation with O(1) • Adaption to distribution of requests • Only limited number of split and join operations after

transient oscillation. • Amount of internal fragmentation fairly large.

7-35 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

25% int. fragmentation

Mean

Requests with uniform distribution: Minimum

Maximum

Page 36: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.2 Address Translation

• An address space is a contiguous set of addresses. • It holds all necessary instructions and data structures

needed to execute a program. • Parts of the address space may be undefined. Access to

undefined parts of the address space leads to an error. • We distinguish:

• Logical address space, program address space (from the view of the thread/program)

• Physical address space (defined by the width of the address bus)

• For higher efficiency and security, logical address spaces are decomposed into segments (of different size) which in turn are cut into pages (equal size)

7-36 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 37: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Address Spaces: Examples

• Address spaces of, e.g., 64 bit machines are not always as expected:

• Linux: cat /proc/cpuinfo Here: only small snippets from some example machines

• Intel, mobile CPU, 2007

model name : Intel(R) Core(TM)2 Duo CPU L7700 @ 1.80GHz address sizes : 36 bits physical, 48 bits virtual

• Intel, desktop CPU, 2011 model name : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz address sizes : 36 bits physical, 48 bits virtual

• Intel, entry server CPU, 2009 model name : Intel(R) Xeon(R) CPU X3470 @ 2.93GHz address sizes : 36 bits physical, 48 bits virtual

• Intel, server CPU, 2009 model name : Intel(R) Xeon(R) CPU X5570 @ 2.93GHz address sizes : 40 bits physical, 48 bits virtual

• AMD, desktop CPU, 2008

model name : AMD Athlon(tm) 64 X2 Dual Core Processor 5600+ address sizes : 40 bits physical, 48 bits virtual

• AMD, desktop CPU, 2011 model name : AMD FX(tm)-6100 Six-Core Processor address sizes : 48 bits physical, 48 bits virtual

• AMD, server CPU, 2009 model name : Six-Core AMD Opteron(tm) Processor 8435 address sizes : 48 bits physical, 48 bits virtual

7-38 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 38: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Two-stage hierarchical address translation

• Each segment consists of a variable number of pages.

7-39 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

segment byte

table base address

+ memory

+

page

K

segment table

page table

Program/Data address

Page 39: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Inverted page table

• While the Intel I32 processors or ARM processors support multistage segment / page tables, PowerPC and UltraSPARC-processors use inverted page tables.

7-40 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

byte

Hash function memory

page

K

Inverted page tables

program/data address

n bits

m bits

page# process#

Control bits

chaining

i

j

2m -1

0

Page 40: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Page Table: Theoretical Example

• 32 Bit addresses • 4 GB logical address space • 64 MB RAM (physical) • Pages of 1 KB • One page table for the whole logical address space

• Offset (inside pages): 10 Bit (2^10 = 1 KB) • Page addresses: 22 Bit (32-10) • Number of entries in page table: 2^22 = 4M • Size of an entry: 16 Bit = 2 Byte

(64 MB = 2^26 B = 2^16 frames) • Size of page table (ignoring managament information such as

dirty bits etc. and ignoring alignment): 8 MB (4M x 2 Byte) 7-41 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

address (32 Bit)

page address (22 Bit) offset in page (10 Bit)

Page 41: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Inverted Page Table: Theoretical Example

• 32 Bit addresses • 4 GB logical address space • 64 MB RAM (physical) • Pages of 1 KB • One page table for the whole logical address space • Offset (inside pages): 10 Bit (2^10 = 1 KB) • Number of frames: 65536 = 2^16 • Frame addresses: 16 Bit • Number of entries in inverted page table: 2^16 = 64K • Size of an entry: 22 Bit = 2.75 Byte

(page addresses are 22 bit (32-10)) • Size of page table (ignoring managament information such as

dirty bits etc. and ignoring alignment): 176 KB (64K x 2.75 Byte)

• But: Search is much more complicated (e.g. hash function)! 7-42 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 42: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Acceleration of address translation

Problem: • Segment and page tables are so large that they have to be

kept in main memory. • To build an effective main memory address, we first need to

get the page and/or segment address. • For each address (instruction or data) we need at least two

accesses to main memory. • Thus, the processing speed is reduced by a factor of 2. • To prevent that, the currently used parts of the segment/page

tables are stored in a fast set of registers. (TLB = Translation Lookaside Buffer, part of MMU)

• The TLB is an associative memory, i.e. a table in which the entry to be found is being searched simultaneously in all lines of the table.

• It is used as a sort of cache for page/segment tables. • Usually, the search can be performed in one processor cycle.

7-43 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 43: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Two stage hierarchical address translation with associative register

7-44 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

segment byte

Table base address

+

memory

+

page

K

segment table

page table

page frame segment page

TLB (associative memory)

program/data address

Page 44: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Typical properties of a TLB

• Line width : 4-8 bytes: Logical page/segment-no., page frame no., Management bits

• Time for address translation: • hit: ≤ 1 processor cycle • miss: 10 - 200 processor cycles (depending on memory speed)

• Hit rate: 99.0% - 99.99%

• TLB-size: 32 – 1024 lines (entries)

7-45 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 45: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Memory protection for hierarchical address translation

• Table base register and segment table entries are complemented by a length field indicating the appropriate amount of memory.

• Exceeding the length triggers an interrupt (segmentation fault). • It is possible to differentiate between read and write access and/or

different processor modes.

7-46 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

segment byte

Table base address

+

memory +

page

K

segment table

page table

length base

? <

≥ length base R

? ≥ <

Access rights (optional)

Page 46: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.3 Memory Hierarchy and Locality

7-47 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Register

Caches (multiple levels)

Main memory

Magnetic or solid state disk

Archive (DVD, tape,...)

Processing

faster, smaller, more expensive

slower, larger, cheaper

Page 47: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Operation of Memory Hierarchy

• Copies of the data object will be generated at time of (first) access, so it seems that the data object is “climbing up”.

• After modification of the data object changes will be propagated (step-by-step, delayed) downwards.

7-48 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Level 1

Level n-1

Level n Original

Copy

Copy

Access

Level 1

Level n-1

Level n Original

Copy

Copy

Modification

Page 48: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Locality

The memory hierarchy is based on the Principle of locality • A program limits its accesses within a small time interval ∆t to only a

small subset of its address space A. • Spatial locality: when a program accesses an address a, then

another access to a nearby address is very likely. • Temporal locality: When a program accesses an address a,

then a repeated access to the same address within short time is very likely.

Why ?

7-49 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

• Mostly, instructions are executed sequentially. • Programs spend the most time in loops. • Some parts of the program are executed only in exceptional cases. • Many arrays are only partially filled. • 90/10-Rule: A thread spends 90% of its time in 10% of its address

space.

Page 49: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Design parameters of Memory Hierarchy

• Goal • Hold data needed on highest level as possible.

• Problem • Capacity is shrinking on the way up.

• Questions • How it can be known what data object is accessed next? Knowledge about the program behavior • Who is responsible for data transport between levels? User/Programmer, Compiler, OS, Hardware • What is the size of the data objects feasible for transportation? Bytes, Words, Blocks, Files • Is there an automatic mechanism for transportation between levels? • Is there an acceleration of he data access (Caching) or enlargement

of capacity (Virtualization)?

7-50 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 50: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Caching vs. Virtualization

• Usually not all of the levels are recognizable for the programmer or user – some are hidden or transparent.

• So the user has the impression to access Level k only. • In case of Caching the access is performed on Level k-1 and

Level k is visible. • In case of Virtualization the access is performed on Level k+1,

but the user has the impression to access Level k. • Cashing is used to accelerate the data access, Virtualization is

used to enlarge the capacity.

7-51 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Level k-1

Level k

Level k+1

visible

transparent

transparent

Virtualization

Caching

Page 51: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Responsibilities

• During the runtime of the program the transport of data and instructions between main memory, cache, and processor is done by the hardware (transparent to software).

• Accesses to the disk are performed by the operating system. • Writing files to and reading from archive memory can be done either

explicitly by the user or automatically by the operating system (file system).

7-52 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Processor register

Cache

Main memory

Disk

Magnetic tape

File (variable)

Block (e.g. 4KByte)

Cache-Line (e.g. 64 Byte)

Word (e.g. 8 Byte)

Unit of transport responsibility

Hardware

Hardware

Operating system

Operating system, user

Page 52: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Volatile vs. permanent Memory

• Due to the used media the memory on higher levels usually is implemented as volatile memory. So the data stored within this memory is lost after power cutoff.

• Therefor higher levels are used to hold temporary data (program variables), while the other levels hold permanent data (files).

7-53 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Processor register

Cache

Main memory

Disc

Magnetic tape

volatile memory temporary data (program variables)

permanent memory permanent data (files)

Page 53: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Volatile vs. permanent Memory

• Using Caching and Virtualization led to weaken the difference between Main memory (for address spaces only) and Disc space (for files only).

7-54 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

volatile memory temporary data (program variables)

permanent memory permanente data (files)

Disc

Main memory

Paging area

File-Cache

Files

Program AS Caching Virtualization

Page 54: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.4 Virtual Memory

• Due to the principle of locality, only those parts of the address space that is currently in use by the program, needs to be present in the physical memory.

• The pages needed are loaded only when addressed (demand paging).

• The copy-out and copy-in operations of the pages can be automated (by some hardware support).

• For the user / programmer all these activities are transparent. • Programmer has the impression that main memory is available

in (almost) unlimited size. • But this unlimited memory is only virtually existent.

7-55 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 55: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Virtual Memory

Requirements for efficient operation: • Noncontiguous allocation (page tables)

• Pages are the units of transfer.

• Automatic detection of missing pages • Access to missing page triggers interrupt. • Loading of page from disk is initiated as part of the

interrupt handling.

7-56 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 56: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Involved components (Data structures)

• Page table • Function: address transformation • Content: for each page

• usage and presence information • physical address (page frame number)

• Page frame table, inverted page table • Function: memory management • Content: for each page frame

• state (free / occupied) • owner • occupying page

• Swap area (paging area) • Function: areas of storage to store the pages that are swapped

out • Usually mass storage such as magnetic or solid state disks • Seldom network devices

7-57 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 57: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Page table for virtual memory

In addition to the physical address, each entry provides information whether • the page is present in main memory:

• presence bit, valid bit • the page has been accessed:

• reference bit • the page has been modified (write access):

• modification bit, dirty bit

7-58 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 58: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Page table for virtual memory

7-59 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Modification

Access (Reference) Presence

1 1 0 1 0

1

1 0 0 1 0

1

1 0 0 0 0

1

pages memory (frames)

page table

Page 59: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Tasks for virtual memory management

7-60 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Allocate_VM

allocate swap area

initialize swap area

page present ?

Access Release_VM

Release allocated page frames

Release swap area

Y

N

Page fault

Page 60: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Page fault

7-61 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page fault

Free page frame available?

Select page frame to be cleared

Content of frame (page) modified ?

Move page to page area

Load new page from disk

Entry page frame table

Entry page table

Yes

No

Yes

No

Strategy issue

Time consuming! Thread switch!

page

out

pa

ge in

Page 61: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Parallelization of paging

• Paging is a time-critical component, we therefore try to speed it up by parallelization.

7-62 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

S_A(Paging_Ch, <PN, PID, RC>)

R_S(RC, < >)

R _S(Paging_Ch, <Page_No, P, Ret_Ch>

Swap_out(Frame_No)

Swap_in(Frame_No)

S_A(Ret_Ch, < >)

Page fault Pager

Page 62: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Buffering

• Since page faults often occur in bulks, it is recommend to have some amount of free page frames available to avoid costly page-out operations when time is tight.

• To that purpose we parallelize by applying the buffering principle to get a stock of free page frames.

7-63 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

S_A(PIC, < >)

R_S(BC, < >)

R_S(PIC, < >)

swap_in()

S_A(BC, < >)

Page fault Page_in

S_A(POC, < >) S_A(POC, < >)

S_A(POC, < >)

p times

R_S(PIBC, < >)

Page_out

stock:= number

R_S(POC, < >)

stock≤ p?

stock:=stock+1

S _A(PIBC, < >)

S_A(POC, < >)

stock:=stock-1

swap_out()

Page 63: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.5 Page replacement strategies

A small calculation: • Let ppf be the probability of a page fault, tm the memory access time

and tpf the time needed for handling a page fault. • Then we obtain as effective memory access time teff in the virtual

memory:

• Using roughly realistic numbers, e.g. tm = 20 nsec and tpf = 20 msec

• With a page fault probability of ppf = 0.001, we get an effective

access time of 20 µsec, i.e. a slow-down by a factor of 1000! • Even with a value of ppf = 10-6 the effective access time doubles. • Thus it is of utmost importance to keep the number of page faults

extremely low.

7-64 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

( ) pfpfmpfeff tptp:t ⋅+⋅−= 1

( )pf

pfpfeff

p..

..ppt

⋅+=

⋅+⋅−=

9809991920

00000020201

Page 64: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Selection strategy

• The page fault rate strongly depends on which pages are kept in real memory and which are stored on disk.

• Selection strategy: When a page fault occurs and no page frame is free, which page

frame should be emptied?

• Differentiation • Local selection strategy: We clear a page frame of that process that caused

the page fault. • Global selection strategy: An arbitrary page frame (belonging to other

processes) is cleared.

7-65 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 65: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Classical Strategies

• FIFO (First-In-First-Out) The page that is longest in memory is swapped out.

• LFU (Least Frequently Used) The page that has been least frequently referenced is

swapped out.

• LRU (Least Recently Used) The page not been referenced for the longest period is swapped out.

• RNU (Recently Not Used) The page not been referenced within some specified

time period is swapped out.

7-66 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 66: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Second-Chance-Algorithm (Clock-Algorithm)

The Clock- Algorithm is smarter, since it resets the reference bits not all at once but only smaller subsets: • The vector of reference bits is scanned cyclically. • For searching the next candidate for swap out, the next page is

selected which has a reference bit of 0. • During this linear search all visited reference bits are reset to 0. • They have – until the scan pointer revisits the page again

during the next cycle – a second chance to be referenced and stay in memory.

• That means that the selected page has the property that it has not been referenced during the last scan cycle.

7-67 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 67: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Second-Chance-Algorithm

7-68 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0

0

1

1

0

1

1 1

1

0

1

0

0

0

1

1

0

0

0 0

0

1

1

0

1

0

1

1

0

0

1 1

0

1

1

1

Scan pointer

Scan pointer

Scan pointer

Reference bits

Before selection After selection, but before moving pointer

After further references

Page 68: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.6 Performance aspects of virtual memory

• The virtual memory works the better, the higher the programs‘ locality.

• Locality is good, if few pages are referenced with high probability, and many pages with low probability (small a).

7-69 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 s

Page reference probability

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8 p(s)

a = 0.1

a = 0.2

a = 0.3

a = 0.5 a = 1.0

s 0 Pages sorted by reference probability

a: Parameter expressing dispersion

Page 69: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.6.1 Modeling paging

• In memory should only be those pages that are referenced with high probability.

7-70 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

in memory on disk

page fault

hit

s

page reference probability p(s)

0

0.1

0.2

0.3

0.4

0.5

s 0

Pages sorted by reference probability

Page 70: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Modeling paging

• Let be s number of available frames s0 size of address space • The s most frequently referenced pages are assumed to be in

memory (i.e., in the s available frames). Then we have: • Hit probability

• Page fault probability

Normalized to size of address space:

• Memory offer

• Normalized page fault probability 7-71 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

( ) ( )dzzpsps

hit ∫=0

( ) ( )spsp hitpf −= 1

0ss=σ

( ) ( )spp pfpf =σ′

Page 71: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Dependence of page fault probability on memory offer

7-72 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0 0.2 0.4 0.6 0.8 1 σ

p'pf (σ) page fault probability

0.2

0.4

0.6

0.8

1

a = 0.1

a = 0.2

a = 0.3

a = 0.5

a = 1.0

Page 72: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Multiprogramming

K number of memory frames n number of programs in memory (Multiprogramming Level, MPL) s0 size of program address space (in pages) ts time between two page faults tT page transfer time s memory offer in pages σ = s/s0 memory offer normalized to program

address space • For n identical programs the following holds: s(n) = K / n or σ (n) = K / (n s0), resp.

7-73 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 73: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Inter page fault time

• The time between two page faults depends on the amount of available memory

• Left of the „knee“ the function can be approximated by a parabola:

i.e. ts decreases with growing n

7-74 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

σ

t s time between two page faults

Memory offer

„Lifetime-Function“

2

0

2

⋅=σ⋅≈sn

Kaat s

Page 74: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Interleaving of compute and page transfer phases

• In the second case, we experience phases where the processor is idle since all processes wait for their pages to be swapped in.

7-75 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

t S t T ...

t

Case: tT < tS

t S t T

t S t T

t S t T

t S t T

t S t T

P1 P2 P3

t

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

t S t T

Case: tT > tS

Processor idle

P1 P2 P3

Page 75: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.6.2 Thrashing Effect

• The system is completely occupied with paging and cannot perform regular useful work.

7-76 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Blocked (wait for page)

running ready

Page 76: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Thrashing

7-77 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Goal: High processor utilization

Many programs executed simultaneously

High multiprogramming degree n

Low memory space s per process

Short time between successive page faults

Congestion at paging device (disk)

Almost all processes blocked

Result: poor processor utilization

Page 77: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Thrashing Curve

• We have to take care that the system does not enter the overload region.

7-78 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Thrashing area

Multiprogramming degree n

Processor utilization

n max

Page 78: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Overload phenomena

• Thrashing is a special variant of an overload phenomenon that can be found in many areas (not only in computer science) and always leads to a performance collapse.

• Examples:

• Computer networks too many packets • Telephone networks: too many calls • Database systems: too many transactions • Road traffic too many cars • Parallel computing too many processors

• Reason: Overhead for coordination grows overlinearly.

7-79 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 79: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Overload prevention

• To prevent the thrashing effect, the multiprogramming level must be limited.

• Problem: How to find an optimal nmax ? • Difficulty: Program behavior changes over time:

• Individual program behavior changes • Combination of program set in memory changes

(multiprogramming mix)

7-80 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Virtual memory Wait n < n max

n ≥ n max

Page 80: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Thrashing curve dynamics

7-81 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Multiprogramming level n

Processor utilization

n max

t=0 t=2 t=1

Page 81: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Thrashing prevention

• The optimal nmax turns out in operation and has to be adopted dynamically.

• Thrashing prevention is therefore done by feedback control.

7-82 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Virtual memory Wait n < n max

n ≥ nmax

control

Page 82: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Thrashing prevention

Two strategic approaches: • Indirect or local strategy For each process i a reasonable number of frames si is

determined dynamically. The maximum multiprogramming level can be found

indirectly:

• Direct or global strategy The measurement of the global paging activity leads to the

calculation of an optimal nmax.

7-83 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

≤= ∑=

Ksnmax:nn

iimax

1

Page 83: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.6.3 Local control of paging activity

• The Working-Set Model • The Working-Set of a program i is defined as the set of pages that

have been referenced within the last τ time units.

• With suitable choice of t the size of the working set indicates the number of page frames that the process needs for efficient work.

• is estimated using the reference information for each process.

• A new process x is loaded into memory, only if

• It is the goal of the algorithm that all processes can accommodate their particular working sets in the memory.

7-84 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

r t r t-1 r t-2 r t-3 r t-4 r t- τ +1 r t- τ r t- τ -1

Time window of size τ

time

( ) ( )τ=τ ,tW:,tw ii

( )τ,tw i

( )∑=

τ−≤n

iix twKw

1

,

Page 84: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

The Page-Fault-Frequency Model (PFF)

• For each process, the page fault rate (#page faults per time unit) is measured and serves to adjust the number of frames.

• Control mechanism:

• The multiprogramming level can be calculated indirectly as with the Working-Set algorithm.

7-85 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

11

2

1

+=>

−=<

s:s:s:s:

ρρρρ

Page fault rate

ρ 1

ρ 2

ρ

Increase s

Decrease s

s Number of Frames

Page 85: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.6.4 Global control of paging activity

The criterion of the interpagefault time (L=S-criterion)

• The time between two page faults ts (or L resp.) should be roughly the same as the page transfer time tT (or S, resp.). The resulting operation point

is in most cases too far at the right which can be taken into account in the control laws.

7-86 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

n

1

Processor utilization

n opt n L=S

t S / t T

Page 86: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

The 50%-Rule

• Thrashing happens, when many processes are blocked due to paging, i.e. when the mean queue length at the paging device is larger than 1.

• According to queuing theory this corresponds to a device utilization of > 50%. • New processes are loaded to main memory only if the utilization of the disk

(measured over a longer time period) is below 50%.

7-87 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

n

1 utilization

n opt n 50%

0,5

Paging disk

Processor

Page 87: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Parabola approximation

• The thrashing curve can be approximated by a parabola in the region of the maximum.

7-88 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

n

1 Processor utilization

estimated parabola

real thrashing curve

Measurements

Page 88: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Parabola approximation

• Approximation formula

• The coefficients are dynamically estimated based on measurements .

• The apex n* of the parabola can be calculated and used as upper bound of the multiprogramming level.

• If the estimation results in parabola that opens upward, the apex (extreme point) cannot be used as optimum.

• In this case the first derivative indicates the slope, i.e. whether we are left or right of the optimum.

• The current upper bound can then be incremented or decremented.

7-89 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

210 a,a,a

2210 nanaa ++=η

( )tt n,η

Page 89: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.7 Examples 7.7.1 Memory management in Unix

Swapping • Early Unix systems did not have a virtual memory. • The main memory had been managed as a resource with

preemption, i.e. processes and their address spaces were swapped to disk, if • No space for process generation (fork) was available, • A dynamic memory request could not be satisfied.

• The process to be swapped out was chosen according to the following criteria: • State – blocked processes were favored for swapping out • Priority and residence time in memory

• Priority and time since its last swap-in are added. • The process with the highest value is swapped out. • Management of memory and swap area is done using a

separate list-based mechanism with First-Fit.

7-90 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 90: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Paging

• Today‘s Unix Systems all provide virtual memory (demand paging).

• When a page fault occurs the missing page is loaded into an empty page frame.

• A special server process (page-daemon) has to take care that a sufficient number of empty frames (lotsfree) is always available.

• If there are too few empty frames available, the page daemon starts to flush pages to disk.

• For that, a global Second-Chance-Algorithm is used. • The different Unix-systems use different variants. • To prevent thrashing, Unix uses swapping, i.e. entire

processes (address spaces) are swapped out.

7-91 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 91: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Activating the Page daemon

• AT&T System V: • Original Second-chance-Algorithm • Instead of lotsfree two parameters min and max are used

• Activation, if current no. of frame < min • stop, if current no. of frame > max

• 4.3BSD:

• Modified Second-chance-Algorithm (Two-Hand-Clock- Algorithm)

• Parameter lotsfree • Activation, if current no. of frame < lotsfree • stop, if current no. of frame > lotsfree

7-92 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 92: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Modified “Second Chance”-Algorithm (Two-Hand-Clock-Algorithm, Unix 4.2BSD)

7-93 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

0

0

1

1

0

1

1

1

1

0

1

0

0

0

1

1

0

0

0

0

0

1

1

0

1

0

1

1

0

0

1

1

0

1

1

1

pointer 1: reset

Reference bits

before activation of page daemon

pointer 2: flush

pointer 1: reset

pointer 2: flush

pointer 1: reset

pointer 2: flush

after activation of page daemon

before next activation of page daemon

hand spread

Page 93: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Solaris

• Solaris (Sun Microsystems) also uses the 2-Hand-Clock (page out) with the following parameters • hand spread: difference between the two hands (# Frames) • scan speed: speed of frame scanning

(slow: 100 frames/sec, fast: 8192 frames/sec) • lotsfree: amount at which paging sets in (e.g. 1/64 of total number of frames) • desfree: desirable amount of empty frames • minfree: minimal amount of empty frames

7-94 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

minfree desfree lotsfree

slowscan

fastscan

# free page frames

scan speed

pageout act. 4/sec

pageout act. 100/sec

pageout activated for each page request

no pageout

starts swapping

Page 94: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

7.7.2 Memory management in Windows

In contrast to Unix, Windows uses a local paging strategy : • If, as a consequence of a page fault, a page needs to be

swapped out, always a page of that process which caused the page fault is chosen.

• Not only the missing page, but also some more of the „neighborhood“ of that page is swapped in (clustering).

• The set of all currently loaded pages of a process is called working set.

7-95 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 95: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Memory management in Windows

• The paging strategy depends on the hardware: : • FIFO (modified) for Alpha processors and Intel

multiprocessor systems • Clock for Intel monoprocessors

• The size of the working sets is initialized by default

values (Min and Max). • On demand Working Sets can grow beyond the

maximum (Working set expansion) and shrink again (Working set trimming).

• Both are dependent on the page fault rate and on the number of free frames.

• For the OS itself also a working set mechanism is used. 7-96 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 96: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Further reading

• Stallings,W.: Operating Systems, 5th ed., Prentice Hall, 2005, Chapter 7 (7.1+7.2) , Chapter 8

• Tanenbaum, A.: Moderne Betriebssysteme, 2.Aufl., Hanser, 1995, Kapitel 3, Abschnitt 3.1+3.2

• Knuth, D.E.: The Art of Computer Programming, Vol. 1, 3rd ed., 1997, pp. 435-451

• Peterson, J.; Norman T. A.: Buddy systems. CACM 20, 6 (June 1977, pp.

421-431 • Shore, J.E.: Anomalous behavior of the fifty-percent rule

in dynamic memory allocation. CACM 20, 11 (Nov. 1977) pp. 812 - 820

• Denning,P.J.: Working Sets Past and Present, IEEE TOSE, Vol 6, (Jan. 1980) pp. 64-84.

7-97 Barry Linnert, [email protected], Betriebssysteme WS 2016/17

Page 97: Chapter 7 · 7.1 Allocation strategies • Problem • Selection of memory sections/pieces • Efficiency of algorithms • Memory usage • Problem conditions • Application area:

Further reading

• Heiss, H.-U.: Verhinderung von Überlast in Rechensystemen, Springer (Informatik-Fachberichte), 1988

• Heiss, H.-U.: Overload Effects and their Prevention. Performance Evaluation Vol.12, No.4 (July 1991), S. 219-235.

• Markatos, E.: Visualizing Working Sets, ACM Operating Systems Review 31,4 (1997), pp.3-11

• Megiddo,N.; Modha,D.S.: Outperforming LRU with an Adaptive Replacement Cache Algorithm, IEEE Computer, April 2004

7-98 Barry Linnert, [email protected], Betriebssysteme WS 2016/17