Virtual Memory - Part2

Preview:

Citation preview

Virtual Memory (Part II)

Amir H. Payberahamir@sics.se

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 1 / 53

Reminder

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 2 / 53

Reminder

I Partially-loaded programs

I Virtual memory: much larger than physical memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 3 / 53

Reminder

I Demand paging similar to paging + swapping

I Locality

I Page fault

I Page replacement algorithms:• FIFO, optimal, LRU, LRU-approximate, counting-based

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 4 / 53

Allocation of Frames

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 5 / 53

Allocation of Frames

I How do we allocate the fixed amount of free memory among thevarious processes?

I If we have 93 free frames and two processes, how many frames doeseach process get?

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 6 / 53

Allocation of Frames

I How do we allocate the fixed amount of free memory among thevarious processes?

I If we have 93 free frames and two processes, how many frames doeseach process get?

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 6 / 53

Frame Allocation Constraints (1/2)

I The maximum number of frames is the total frames in the system.

I Each process needs minimum number of frames.

• Example: IBM 370: 6 pages to handle MOVE instruction:• Instruction is 6 bytes, might span 2 pages• 2 pages to handle from• 2 pages to handle to

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53

Frame Allocation Constraints (1/2)

I The maximum number of frames is the total frames in the system.

I Each process needs minimum number of frames.

• Example: IBM 370: 6 pages to handle MOVE instruction:• Instruction is 6 bytes, might span 2 pages• 2 pages to handle from• 2 pages to handle to

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53

Frame Allocation Constraints (1/2)

I The maximum number of frames is the total frames in the system.

I Each process needs minimum number of frames.• Example: IBM 370: 6 pages to handle MOVE instruction:• Instruction is 6 bytes, might span 2 pages• 2 pages to handle from• 2 pages to handle to

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53

Frame Allocation Constraints (2/2)

I Why the minimum number of frames for each process?

I Decreases the number of frames:• Increases the page-fault rate

I When a page fault occurs before an executing instruction is com-plete, the instruction must be restarted.

• We must have enough frames to hold all the different pages thatany single instruction can reference.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53

Frame Allocation Constraints (2/2)

I Why the minimum number of frames for each process? performance

I Decreases the number of frames:• Increases the page-fault rate

I When a page fault occurs before an executing instruction is com-plete, the instruction must be restarted.

• We must have enough frames to hold all the different pages thatany single instruction can reference.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53

Frame Allocation Constraints (2/2)

I Why the minimum number of frames for each process? performance

I Decreases the number of frames:• Increases the page-fault rate

I When a page fault occurs before an executing instruction is com-plete, the instruction must be restarted.

• We must have enough frames to hold all the different pages thatany single instruction can reference.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53

Frame Allocation Constraints (2/2)

I Why the minimum number of frames for each process? performance

I Decreases the number of frames:• Increases the page-fault rate

I When a page fault occurs before an executing instruction is com-plete, the instruction must be restarted.

• We must have enough frames to hold all the different pages thatany single instruction can reference.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53

Frame Allocation Constraints (2/2)

I Why the minimum number of frames for each process? performance

I Decreases the number of frames:• Increases the page-fault rate

I When a page fault occurs before an executing instruction is com-plete, the instruction must be restarted.

• We must have enough frames to hold all the different pages thatany single instruction can reference.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53

Allocation Schemes

I Fixed allocation

• Equal allocation• Proportional allocation

I Priority allocation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53

Allocation Schemes

I Fixed allocation• Equal allocation• Proportional allocation

I Priority allocation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53

Fixed Allocation (1/4)

I Equal allocation

I Split m frames among n processes: mn frames to each process.

I Example, if there are 93 frames and 5 processes• Each process will get 18 frames.• The 3 leftover frames can be used as a free-frame buffer pool.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 10 / 53

Fixed Allocation (1/4)

I Equal allocation

I Split m frames among n processes: mn frames to each process.

I Example, if there are 93 frames and 5 processes• Each process will get 18 frames.• The 3 leftover frames can be used as a free-frame buffer pool.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 10 / 53

Fixed Allocation (2/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: gives each process 31 frames

I Wasting 21 frames

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53

Fixed Allocation (2/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: gives each process 31 frames

I Wasting 21 frames

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53

Fixed Allocation (2/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: gives each process 31 frames

I Wasting 21 frames

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53

Fixed Allocation (2/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: gives each process 31 frames

I Wasting 21 frames

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53

Fixed Allocation (3/4)

I Proportional allocation

I Allocate according to the size of process.

I Dynamic as degree of multiprogramming, process sizes change.

I si = size of process pi

I S =∑

si

I m = total number of frames

I ai = siS ×m: allocation for pi

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53

Fixed Allocation (3/4)

I Proportional allocation

I Allocate according to the size of process.

I Dynamic as degree of multiprogramming, process sizes change.

I si = size of process pi

I S =∑

si

I m = total number of frames

I ai = siS ×m: allocation for pi

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53

Fixed Allocation (3/4)

I Proportional allocation

I Allocate according to the size of process.

I Dynamic as degree of multiprogramming, process sizes change.

I si = size of process pi

I S =∑

si

I m = total number of frames

I ai = siS ×m: allocation for pi

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53

Fixed Allocation (3/4)

I Proportional allocation

I Allocate according to the size of process.

I Dynamic as degree of multiprogramming, process sizes change.

I si = size of process pi

I S =∑

si

I m = total number of frames

I ai = siS ×m: allocation for pi

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53

Fixed Allocation (3/4)

I Proportional allocation

I Allocate according to the size of process.

I Dynamic as degree of multiprogramming, process sizes change.

I si = size of process pi

I S =∑

si

I m = total number of frames

I ai = siS ×m: allocation for pi

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53

Fixed Allocation (4/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: s1 = 10, s2 = 127,S = 137,m = 62

I a1 = 10137 × 62 ≈ 4 a2 = 127

137 × 62 ≈ 57

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53

Fixed Allocation (4/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: s1 = 10, s2 = 127,S = 137,m = 62

I a1 = 10137 × 62 ≈ 4 a2 = 127

137 × 62 ≈ 57

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53

Fixed Allocation (4/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: s1 = 10, s2 = 127,S = 137,m = 62

I a1 = 10137 × 62 ≈ 4 a2 = 127

137 × 62 ≈ 57

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53

Fixed Allocation (4/4)

I Assume 62 free frames, and the frame size is 1KB

I Two processes:• A student process: 10KB• An interactive database: 127KB

I Equal allocation: s1 = 10, s2 = 127,S = 137,m = 62

I a1 = 10137 × 62 ≈ 4 a2 = 127

137 × 62 ≈ 57

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53

Priority Allocation

I Use a proportional allocation scheme using priorities rather thansize.

I If process pi generates a page fault:• Select for replacement one of its frames.• Select for replacement a frame from a process with lower priority

number.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 14 / 53

Priority Allocation

I Use a proportional allocation scheme using priorities rather thansize.

I If process pi generates a page fault:• Select for replacement one of its frames.• Select for replacement a frame from a process with lower priority

number.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 14 / 53

Global vs. Local Allocation

I Global replacement: process selects a replacement frame from theset of all frames; one process can take a frame from another

• The process execution time can vary greatly.• The greater throughput so more common.

I Local replacement: each process selects from only its own set ofallocated frames

• More consistent per-process performance• Possibly underutilized memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53

Global vs. Local Allocation

I Global replacement: process selects a replacement frame from theset of all frames; one process can take a frame from another

• The process execution time can vary greatly.• The greater throughput so more common.

I Local replacement: each process selects from only its own set ofallocated frames

• More consistent per-process performance• Possibly underutilized memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53

Global vs. Local Allocation

I Global replacement: process selects a replacement frame from theset of all frames; one process can take a frame from another

• The process execution time can vary greatly.• The greater throughput so more common.

I Local replacement: each process selects from only its own set ofallocated frames

• More consistent per-process performance• Possibly underutilized memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53

Global vs. Local Allocation

I Global replacement: process selects a replacement frame from theset of all frames; one process can take a frame from another

• The process execution time can vary greatly.• The greater throughput so more common.

I Local replacement: each process selects from only its own set ofallocated frames

• More consistent per-process performance• Possibly underutilized memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53

Non-Uniform Memory Access

I So far all memory accessed equally.

I Many systems are NUMA: speed of access to memory varies.

I Optimal performance: allocate memory close to the CPU on whichthe thread is scheduled.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 16 / 53

Non-Uniform Memory Access

I So far all memory accessed equally.

I Many systems are NUMA: speed of access to memory varies.

I Optimal performance: allocate memory close to the CPU on whichthe thread is scheduled.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 16 / 53

Thrashing

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 17 / 53

Thrashing (1/2)

I If a process does not have enough pages, the page-fault rate is veryhigh.

• Page fault to get page• Replace existing frame• But quickly need replaced frame back

I This leads to:• Low CPU utilization• OS thinks it needs to increase the degree of multiprogramming• Another process added to the system

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 18 / 53

Thrashing (1/2)

I If a process does not have enough pages, the page-fault rate is veryhigh.

• Page fault to get page• Replace existing frame• But quickly need replaced frame back

I This leads to:• Low CPU utilization• OS thinks it needs to increase the degree of multiprogramming• Another process added to the system

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 18 / 53

Thrashing (2/2)

I Thrashing: a process is busy swapping pages in and out.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 19 / 53

Prevent Thrashing

I Providing a process with as many frames as it needs.

I How do we know how many frames it needs?

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 20 / 53

Prevent Thrashing

I Providing a process with as many frames as it needs.

I How do we know how many frames it needs?

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 20 / 53

Locality Model (1/2)

I A locality is a set of pages that are actively used together.

I A process moves from locality to locality.

I A program is generally composed of several different localities, whichmay overlap.

I For example, when a function is called, it defines a new locality:consists of memory references to the instructions of the functioncall, its local variables, and a subset of the global variables.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53

Locality Model (1/2)

I A locality is a set of pages that are actively used together.

I A process moves from locality to locality.

I A program is generally composed of several different localities, whichmay overlap.

I For example, when a function is called, it defines a new locality:consists of memory references to the instructions of the functioncall, its local variables, and a subset of the global variables.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53

Locality Model (1/2)

I A locality is a set of pages that are actively used together.

I A process moves from locality to locality.

I A program is generally composed of several different localities, whichmay overlap.

I For example, when a function is called, it defines a new locality:consists of memory references to the instructions of the functioncall, its local variables, and a subset of the global variables.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53

Locality Model (1/2)

I A locality is a set of pages that are actively used together.

I A process moves from locality to locality.

I A program is generally composed of several different localities, whichmay overlap.

I For example, when a function is called, it defines a new locality:consists of memory references to the instructions of the functioncall, its local variables, and a subset of the global variables.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53

Locality Model (2/2)

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 22 / 53

Locality and Thrashing

I A process will fault for the pages in its locality, until all these pagesare in memory.

I After allocating all the pages of the locality, it will not fault againuntil it changes localities.

I If we do not allocate enough frames to accommodate the size of thecurrent locality, the process will thrash.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53

Locality and Thrashing

I A process will fault for the pages in its locality, until all these pagesare in memory.

I After allocating all the pages of the locality, it will not fault againuntil it changes localities.

I If we do not allocate enough frames to accommodate the size of thecurrent locality, the process will thrash.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53

Locality and Thrashing

I A process will fault for the pages in its locality, until all these pagesare in memory.

I After allocating all the pages of the locality, it will not fault againuntil it changes localities.

I If we do not allocate enough frames to accommodate the size of thecurrent locality, the process will thrash.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53

Working-Set Model (1/2)

I 4: working-set window: a fixed number of page references

I WSSi : working set of process pi : total number of pages referencedin the most recent 4 (varies in time).

• If 4 too small will not encompass entire locality• If 4 too large will encompass several localities• If 4 =∞ will encompass entire program

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 24 / 53

Working-Set Model (1/2)

I 4: working-set window: a fixed number of page references

I WSSi : working set of process pi : total number of pages referencedin the most recent 4 (varies in time).

• If 4 too small will not encompass entire locality• If 4 too large will encompass several localities• If 4 =∞ will encompass entire program

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 24 / 53

Working-Set Model (2/2)

I m: total number of frames

I D: total demand frames: D =∑

WSSi :• Approximation of locality

I If D > m, then thrashing

I Policy: if D > m, then suspend or swap out one of the processes.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53

Working-Set Model (2/2)

I m: total number of frames

I D: total demand frames: D =∑

WSSi :• Approximation of locality

I If D > m, then thrashing

I Policy: if D > m, then suspend or swap out one of the processes.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53

Working-Set Model (2/2)

I m: total number of frames

I D: total demand frames: D =∑

WSSi :• Approximation of locality

I If D > m, then thrashing

I Policy: if D > m, then suspend or swap out one of the processes.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53

Keeping Track of the Working Set

I Approximate with interval timer + a reference bit

I Example: 4 = 10000• Timer interrupts after every 5000 time units.• Keep in memory 2 bits for each page.• Whenever a timer interrupts we copy and clear the reference-bit

values for each page• If a page fault occurs: examine the 2 bits to determine whether a

page was used within the last 10,000 to 15,000 references.

I Why is this not completely accurate?

I Improvement = 10 bits and interrupt every 1000 time units

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53

Keeping Track of the Working Set

I Approximate with interval timer + a reference bit

I Example: 4 = 10000• Timer interrupts after every 5000 time units.• Keep in memory 2 bits for each page.• Whenever a timer interrupts we copy and clear the reference-bit

values for each page• If a page fault occurs: examine the 2 bits to determine whether a

page was used within the last 10,000 to 15,000 references.

I Why is this not completely accurate?

I Improvement = 10 bits and interrupt every 1000 time units

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53

Keeping Track of the Working Set

I Approximate with interval timer + a reference bit

I Example: 4 = 10000• Timer interrupts after every 5000 time units.• Keep in memory 2 bits for each page.• Whenever a timer interrupts we copy and clear the reference-bit

values for each page• If a page fault occurs: examine the 2 bits to determine whether a

page was used within the last 10,000 to 15,000 references.

I Why is this not completely accurate?

I Improvement = 10 bits and interrupt every 1000 time units

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53

Keeping Track of the Working Set

I Approximate with interval timer + a reference bit

I Example: 4 = 10000• Timer interrupts after every 5000 time units.• Keep in memory 2 bits for each page.• Whenever a timer interrupts we copy and clear the reference-bit

values for each page• If a page fault occurs: examine the 2 bits to determine whether a

page was used within the last 10,000 to 15,000 references.

I Why is this not completely accurate?

I Improvement = 10 bits and interrupt every 1000 time units

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53

Page-Fault Frequency

I More direct approach than WSS

I Establish acceptable page-fault frequency (PFF) rate and use localreplacement policy

• If actual rate too low, process loses frame• If actual rate too high, process gains frame

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 27 / 53

Working Sets and Page-Fault Rates

I Direct relationship between working set of a process and its page-fault rate.

I Working set changes over time.

I Peaks and valleys over time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 28 / 53

Allocating Kernel Memory

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 29 / 53

Allocating Kernel Memory

I Treated differently from user memory

I Often allocated from a free-memory pool• Kernel requests memory for structures of varying sizes• Some kernel memory needs to be contiguous, i.e. for I/O devices

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 30 / 53

Allocating Kernel Memory

I Treated differently from user memory

I Often allocated from a free-memory pool• Kernel requests memory for structures of varying sizes• Some kernel memory needs to be contiguous, i.e. for I/O devices

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 30 / 53

Managing Free Memory Strategies

I Buddy system

I Slab allocation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 31 / 53

Buddy System (1/2)

I Allocates memory from fixed-size segment consisting of physically-contiguous pages

I Memory allocated using 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53

Buddy System (1/2)

I Allocates memory from fixed-size segment consisting of physically-contiguous pages

I Memory allocated using 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53

Buddy System (1/2)

I Allocates memory from fixed-size segment consisting of physically-contiguous pages

I Memory allocated using 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53

Buddy System (1/2)

I Allocates memory from fixed-size segment consisting of physically-contiguous pages

I Memory allocated using 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 chunksplit into two buddies of next-lower power of 2.

• Continue until appropriate sized chunk available.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53

Buddy System (1/2)

I Allocates memory from fixed-size segment consisting of physically-contiguous pages

I Memory allocated using 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53

Buddy System (1/2)

I Allocates memory from fixed-size segment consisting of physically-contiguous pages

I Memory allocated using 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53

Buddy System (2/2)

I 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.

I Advantage: quickly coalesce unusedchunks into larger chunk

I Disadvantage: fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 33 / 53

Buddy System (2/2)

I 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.

I Advantage: quickly coalesce unusedchunks into larger chunk

I Disadvantage: fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 33 / 53

Slab Allocation (1/2)

I Alternate strategy

I Slab is one or more physically contiguous pages.

I Cache consists of one or more slabs.

I Single cache for each uniquekernel data structure, e.g.,a separate cache for file objects,a separate cache for semaphores,and so forth.

I Objects: instantiations of thedata structure

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53

Slab Allocation (1/2)

I Alternate strategy

I Slab is one or more physically contiguous pages.

I Cache consists of one or more slabs.

I Single cache for each uniquekernel data structure, e.g.,a separate cache for file objects,a separate cache for semaphores,and so forth.

I Objects: instantiations of thedata structure

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53

Slab Allocation (1/2)

I Alternate strategy

I Slab is one or more physically contiguous pages.

I Cache consists of one or more slabs.

I Single cache for each uniquekernel data structure, e.g.,a separate cache for file objects,a separate cache for semaphores,and so forth.

I Objects: instantiations of thedata structure

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53

Slab Allocation (1/2)

I Alternate strategy

I Slab is one or more physically contiguous pages.

I Cache consists of one or more slabs.

I Single cache for each uniquekernel data structure, e.g.,a separate cache for file objects,a separate cache for semaphores,and so forth.

I Objects: instantiations of thedata structure

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53

Slab Allocation (2/2)

I When cache created, filled with objects marked as free.

I When structures stored, objects marked as used.

I If slab is full of used objects, next object allocated from empty slab.

I If no empty slabs, new slab allocated.

I Benefits include no fragmentation and fast memory request satis-faction.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53

Slab Allocation (2/2)

I When cache created, filled with objects marked as free.

I When structures stored, objects marked as used.

I If slab is full of used objects, next object allocated from empty slab.

I If no empty slabs, new slab allocated.

I Benefits include no fragmentation and fast memory request satis-faction.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53

Slab Allocation (2/2)

I When cache created, filled with objects marked as free.

I When structures stored, objects marked as used.

I If slab is full of used objects, next object allocated from empty slab.

I If no empty slabs, new slab allocated.

I Benefits include no fragmentation and fast memory request satis-faction.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53

Slab Allocation (2/2)

I When cache created, filled with objects marked as free.

I When structures stored, objects marked as used.

I If slab is full of used objects, next object allocated from empty slab.

I If no empty slabs, new slab allocated.

I Benefits include no fragmentation and fast memory request satis-faction.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53

Slab Allocation (2/2)

I When cache created, filled with objects marked as free.

I When structures stored, objects marked as used.

I If slab is full of used objects, next object allocated from empty slab.

I If no empty slabs, new slab allocated.

I Benefits include no fragmentation and fast memory request satis-faction.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 36 / 53

Slab Allocator in Linux (1/3)

I Process descriptor: type struct task struct

I Approx 1.7KB of memory

I New task → allocate new struct from cache• Will use existing free struct task struct

I Slab can be in three possible states• Full: all used• Empty: all free• Partial: mix of free and used

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 37 / 53

Slab Allocator in Linux (2/3)

I Upon request, slab allocator1 Uses free struct in partial slab.2 If none, takes one from empty slab.3 If no empty slab, create new empty.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 38 / 53

Slab Allocator in Linux (3/3)

I Linux originally used the buddy system.

I Kernel 2.2 had SLAB.

I Recent distribution added two more allocators:• SLOB (Simple List of Blocks): for systems with limited memory,

maintains 3 list objects for small, medium, large objects• SLUB is performance-optimized SLAB, removes per-CPU queues,

metadata stored in page structure, from kernel 2.6.24

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53

Slab Allocator in Linux (3/3)

I Linux originally used the buddy system.

I Kernel 2.2 had SLAB.

I Recent distribution added two more allocators:• SLOB (Simple List of Blocks): for systems with limited memory,

maintains 3 list objects for small, medium, large objects• SLUB is performance-optimized SLAB, removes per-CPU queues,

metadata stored in page structure, from kernel 2.6.24

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53

Slab Allocator in Linux (3/3)

I Linux originally used the buddy system.

I Kernel 2.2 had SLAB.

I Recent distribution added two more allocators:• SLOB (Simple List of Blocks): for systems with limited memory,

maintains 3 list objects for small, medium, large objects• SLUB is performance-optimized SLAB, removes per-CPU queues,

metadata stored in page structure, from kernel 2.6.24

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53

Memory-Mapped Files

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 40 / 53

Memory-Mapped Files (1/3)

I Memory-mapped file I/O allows file I/O to be treated as routinememory access by mapping a disk block to a page in memory.

I 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 41 / 53

Memory-Mapped Files (1/3)

I Memory-mapped file I/O allows file I/O to be treated as routinememory access by mapping a disk block to a page in memory.

I 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.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 41 / 53

Memory-Mapped Files (2/3)

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 42 / 53

Memory-Mapped Files (3/3)

I Process can explicitly request memory mapping a file via mmap()

system call: map the file into kernel address space

I Simplifies and speeds file access by driving file I/O through memoryrather than read() and write() system calls.

I Also allows several processes to map the same file allowing the pagesin memory to be shared.

I When does written data make it to disk?

• Periodically and/or at file close() time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53

Memory-Mapped Files (3/3)

I Process can explicitly request memory mapping a file via mmap()

system call: map the file into kernel address space

I Simplifies and speeds file access by driving file I/O through memoryrather than read() and write() system calls.

I Also allows several processes to map the same file allowing the pagesin memory to be shared.

I When does written data make it to disk?

• Periodically and/or at file close() time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53

Memory-Mapped Files (3/3)

I Process can explicitly request memory mapping a file via mmap()

system call: map the file into kernel address space

I Simplifies and speeds file access by driving file I/O through memoryrather than read() and write() system calls.

I Also allows several processes to map the same file allowing the pagesin memory to be shared.

I When does written data make it to disk?

• Periodically and/or at file close() time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53

Memory-Mapped Files (3/3)

I Process can explicitly request memory mapping a file via mmap()

system call: map the file into kernel address space

I Simplifies and speeds file access by driving file I/O through memoryrather than read() and write() system calls.

I Also allows several processes to map the same file allowing the pagesin memory to be shared.

I When does written data make it to disk?

• Periodically and/or at file close() time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53

Memory-Mapped Files (3/3)

I Process can explicitly request memory mapping a file via mmap()

system call: map the file into kernel address space

I Simplifies and speeds file access by driving file I/O through memoryrather than read() and write() system calls.

I Also allows several processes to map the same file allowing the pagesin memory to be shared.

I When does written data make it to disk?• Periodically and/or at file close() time.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53

Memory-Mapped I/O

I Many computer architectures provide memory-mapped I/O.

I Ranges of memory addresses are mapped to the device registers.

I Reads and writes to these memory addresses cause the data to betransferred to and from the device registers.

• Called, I/O port

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53

Memory-Mapped I/O

I Many computer architectures provide memory-mapped I/O.

I Ranges of memory addresses are mapped to the device registers.

I Reads and writes to these memory addresses cause the data to betransferred to and from the device registers.

• Called, I/O port

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53

Memory-Mapped I/O

I Many computer architectures provide memory-mapped I/O.

I Ranges of memory addresses are mapped to the device registers.

I Reads and writes to these memory addresses cause the data to betransferred to and from the device registers.

• Called, I/O port

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53

Other Considerations

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 45 / 53

Prepaging

I To reduce the large number of page faults that occurs at processstartup.

I Prepage all or some of the pages a process will need, before theyare referenced.

I If prepaged pages are unused, I/O and memory was wasted.

I Assume s pages are prepaged and a fraction 0 ≤ α ≤ 1 of the pagesare used.

• Cost of s × α > or < than the cost of prepaging s × (1− α)unnecessary pages?

• If α close to 0: prepaging loses; if α close to 1, prepaging wins

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53

Prepaging

I To reduce the large number of page faults that occurs at processstartup.

I Prepage all or some of the pages a process will need, before theyare referenced.

I If prepaged pages are unused, I/O and memory was wasted.

I Assume s pages are prepaged and a fraction 0 ≤ α ≤ 1 of the pagesare used.

• Cost of s × α > or < than the cost of prepaging s × (1− α)unnecessary pages?

• If α close to 0: prepaging loses; if α close to 1, prepaging wins

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53

Prepaging

I To reduce the large number of page faults that occurs at processstartup.

I Prepage all or some of the pages a process will need, before theyare referenced.

I If prepaged pages are unused, I/O and memory was wasted.

I Assume s pages are prepaged and a fraction 0 ≤ α ≤ 1 of the pagesare used.

• Cost of s × α > or < than the cost of prepaging s × (1− α)unnecessary pages?

• If α close to 0: prepaging loses; if α close to 1, prepaging wins

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53

Prepaging

I To reduce the large number of page faults that occurs at processstartup.

I Prepage all or some of the pages a process will need, before theyare referenced.

I If prepaged pages are unused, I/O and memory was wasted.

I Assume s pages are prepaged and a fraction 0 ≤ α ≤ 1 of the pagesare used.

• Cost of s × α > or < than the cost of prepaging s × (1− α)unnecessary pages?

• If α close to 0: prepaging loses; if α close to 1, prepaging wins

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53

Page Size

I Page size selection must take into consideration:• Fragmentation• Page table size• Resolution• I/O overhead• Number of page faults• Locality• TLB size and effectiveness

I Always power of 2

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 47 / 53

TLB Search

I TLB reach: the amount of memory accessible from the TLB

I TLB reach = (TLB Size) × (Page Size)

I Ideally, the working set of each process is stored in the TLB• Otherwise there is a high degree of page faults.

I Increase the page size• This may lead to an increase in fragmentation

I Provide multiple page sizes• This allows applications that require larger page sizes the

opportunity to use them without an increase in fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53

TLB Search

I TLB reach: the amount of memory accessible from the TLB

I TLB reach = (TLB Size) × (Page Size)

I Ideally, the working set of each process is stored in the TLB• Otherwise there is a high degree of page faults.

I Increase the page size• This may lead to an increase in fragmentation

I Provide multiple page sizes• This allows applications that require larger page sizes the

opportunity to use them without an increase in fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53

TLB Search

I TLB reach: the amount of memory accessible from the TLB

I TLB reach = (TLB Size) × (Page Size)

I Ideally, the working set of each process is stored in the TLB• Otherwise there is a high degree of page faults.

I Increase the page size• This may lead to an increase in fragmentation

I Provide multiple page sizes• This allows applications that require larger page sizes the

opportunity to use them without an increase in fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53

TLB Search

I TLB reach: the amount of memory accessible from the TLB

I TLB reach = (TLB Size) × (Page Size)

I Ideally, the working set of each process is stored in the TLB• Otherwise there is a high degree of page faults.

I Increase the page size• This may lead to an increase in fragmentation

I Provide multiple page sizes• This allows applications that require larger page sizes the

opportunity to use them without an increase in fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53

TLB Search

I TLB reach: the amount of memory accessible from the TLB

I TLB reach = (TLB Size) × (Page Size)

I Ideally, the working set of each process is stored in the TLB• Otherwise there is a high degree of page faults.

I Increase the page size• This may lead to an increase in fragmentation

I Provide multiple page sizes• This allows applications that require larger page sizes the

opportunity to use them without an increase in fragmentation

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53

Program Structure

I int[128,128] data: each row is stored in one page

I Program 1

for (j = 0; j <128; j++)

for (i = 0; i < 128; i++)

data[i, j] = 0;

128× 128 = 16, 384 page faults

I Program 2

for (i = 0; i <128; j++)

for (j = 0; j < 128; i++)

data[i, j] = 0;

128 page faults

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53

Program Structure

I int[128,128] data: each row is stored in one page

I Program 1

for (j = 0; j <128; j++)

for (i = 0; i < 128; i++)

data[i, j] = 0;

128× 128 = 16, 384 page faults

I Program 2

for (i = 0; i <128; j++)

for (j = 0; j < 128; i++)

data[i, j] = 0;

128 page faults

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53

Program Structure

I int[128,128] data: each row is stored in one page

I Program 1

for (j = 0; j <128; j++)

for (i = 0; i < 128; i++)

data[i, j] = 0;

128× 128 = 16, 384 page faults

I Program 2

for (i = 0; i <128; j++)

for (j = 0; j < 128; i++)

data[i, j] = 0;

128 page faults

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53

Program Structure

I int[128,128] data: each row is stored in one page

I Program 1

for (j = 0; j <128; j++)

for (i = 0; i < 128; i++)

data[i, j] = 0;

128× 128 = 16, 384 page faults

I Program 2

for (i = 0; i <128; j++)

for (j = 0; j < 128; i++)

data[i, j] = 0;

128 page faults

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53

I/O Interlock

I I/O interlock: pages must sometimes be locked into memory.

I Consider I/O: pages that are used for copying a file from a devicemust be locked from being selected for eviction by a page replace-ment algorithm.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 50 / 53

Summary

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 51 / 53

Summary

I Frame allocation: fixed and priority allocations

I Global and local allocation

I Thrashing: total demand frames > total num. of frames

I Prevent trashing: working set model and page fault frequency

I Allocating kernel memory: buddy system and slab allocation

I Memory-mapped files and I/O

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53

Summary

I Frame allocation: fixed and priority allocations

I Global and local allocation

I Thrashing: total demand frames > total num. of frames

I Prevent trashing: working set model and page fault frequency

I Allocating kernel memory: buddy system and slab allocation

I Memory-mapped files and I/O

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53

Summary

I Frame allocation: fixed and priority allocations

I Global and local allocation

I Thrashing: total demand frames > total num. of frames

I Prevent trashing: working set model and page fault frequency

I Allocating kernel memory: buddy system and slab allocation

I Memory-mapped files and I/O

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53

Summary

I Frame allocation: fixed and priority allocations

I Global and local allocation

I Thrashing: total demand frames > total num. of frames

I Prevent trashing: working set model and page fault frequency

I Allocating kernel memory: buddy system and slab allocation

I Memory-mapped files and I/O

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53

Summary

I Frame allocation: fixed and priority allocations

I Global and local allocation

I Thrashing: total demand frames > total num. of frames

I Prevent trashing: working set model and page fault frequency

I Allocating kernel memory: buddy system and slab allocation

I Memory-mapped files and I/O

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53

Summary

I Frame allocation: fixed and priority allocations

I Global and local allocation

I Thrashing: total demand frames > total num. of frames

I Prevent trashing: working set model and page fault frequency

I Allocating kernel memory: buddy system and slab allocation

I Memory-mapped files and I/O

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53

Questions?

Acknowledgements

Some slides were derived from Avi Silberschatz slides.

Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 53 / 53

Recommended