Virtual Memory - Part2
-
Upload
amir-payberah -
Category
Software
-
view
66 -
download
6
Transcript of Virtual Memory - Part2
![Page 1: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/1.jpg)
Virtual Memory (Part II)
Amir H. [email protected]
Amirkabir University of Technology(Tehran Polytechnic)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 1 / 53
![Page 2: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/2.jpg)
Reminder
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 2 / 53
![Page 3: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/3.jpg)
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
![Page 4: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/4.jpg)
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
![Page 5: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/5.jpg)
Allocation of Frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 5 / 53
![Page 6: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/6.jpg)
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
![Page 7: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/7.jpg)
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
![Page 8: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/8.jpg)
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
![Page 9: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/9.jpg)
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
![Page 10: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/10.jpg)
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
![Page 11: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/11.jpg)
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
![Page 12: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/12.jpg)
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
![Page 13: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/13.jpg)
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
![Page 14: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/14.jpg)
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
![Page 15: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/15.jpg)
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
![Page 16: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/16.jpg)
Allocation Schemes
I Fixed allocation
• Equal allocation• Proportional allocation
I Priority allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53
![Page 17: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/17.jpg)
Allocation Schemes
I Fixed allocation• Equal allocation• Proportional allocation
I Priority allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53
![Page 18: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/18.jpg)
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
![Page 19: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/19.jpg)
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
![Page 20: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/20.jpg)
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
![Page 21: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/21.jpg)
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
![Page 22: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/22.jpg)
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
![Page 23: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/23.jpg)
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
![Page 24: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/24.jpg)
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
![Page 25: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/25.jpg)
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
![Page 26: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/26.jpg)
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
![Page 27: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/27.jpg)
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
![Page 28: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/28.jpg)
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
![Page 29: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/29.jpg)
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
![Page 30: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/30.jpg)
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
![Page 31: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/31.jpg)
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
![Page 32: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/32.jpg)
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
![Page 33: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/33.jpg)
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
![Page 34: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/34.jpg)
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
![Page 35: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/35.jpg)
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
![Page 36: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/36.jpg)
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
![Page 37: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/37.jpg)
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
![Page 38: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/38.jpg)
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
![Page 39: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/39.jpg)
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
![Page 40: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/40.jpg)
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
![Page 41: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/41.jpg)
Thrashing
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 17 / 53
![Page 42: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/42.jpg)
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
![Page 43: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/43.jpg)
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
![Page 44: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/44.jpg)
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
![Page 45: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/45.jpg)
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
![Page 46: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/46.jpg)
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
![Page 47: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/47.jpg)
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
![Page 48: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/48.jpg)
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
![Page 49: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/49.jpg)
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
![Page 50: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/50.jpg)
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
![Page 51: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/51.jpg)
Locality Model (2/2)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 22 / 53
![Page 52: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/52.jpg)
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
![Page 53: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/53.jpg)
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
![Page 54: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/54.jpg)
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
![Page 55: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/55.jpg)
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
![Page 56: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/56.jpg)
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
![Page 57: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/57.jpg)
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
![Page 58: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/58.jpg)
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
![Page 59: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/59.jpg)
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
![Page 60: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/60.jpg)
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 61: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/61.jpg)
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 62: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/62.jpg)
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 63: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/63.jpg)
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 64: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/64.jpg)
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
![Page 65: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/65.jpg)
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
![Page 66: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/66.jpg)
Allocating Kernel Memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 29 / 53
![Page 67: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/67.jpg)
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
![Page 68: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/68.jpg)
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
![Page 69: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/69.jpg)
Managing Free Memory Strategies
I Buddy system
I Slab allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 31 / 53
![Page 70: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/70.jpg)
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
![Page 71: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/71.jpg)
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
![Page 72: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/72.jpg)
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
![Page 73: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/73.jpg)
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
![Page 74: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/74.jpg)
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
![Page 75: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/75.jpg)
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
![Page 76: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/76.jpg)
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
![Page 77: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/77.jpg)
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
![Page 78: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/78.jpg)
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
![Page 79: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/79.jpg)
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
![Page 80: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/80.jpg)
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
![Page 81: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/81.jpg)
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
![Page 82: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/82.jpg)
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
![Page 83: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/83.jpg)
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
![Page 84: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/84.jpg)
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
![Page 85: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/85.jpg)
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
![Page 86: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/86.jpg)
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
![Page 87: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/87.jpg)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 36 / 53
![Page 88: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/88.jpg)
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
![Page 89: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/89.jpg)
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
![Page 90: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/90.jpg)
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
![Page 91: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/91.jpg)
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
![Page 92: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/92.jpg)
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
![Page 93: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/93.jpg)
Memory-Mapped Files
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 40 / 53
![Page 94: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/94.jpg)
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
![Page 95: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/95.jpg)
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
![Page 96: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/96.jpg)
Memory-Mapped Files (2/3)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 42 / 53
![Page 97: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/97.jpg)
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
![Page 98: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/98.jpg)
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
![Page 99: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/99.jpg)
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
![Page 100: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/100.jpg)
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
![Page 101: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/101.jpg)
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
![Page 102: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/102.jpg)
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
![Page 103: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/103.jpg)
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
![Page 104: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/104.jpg)
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
![Page 105: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/105.jpg)
Other Considerations
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 45 / 53
![Page 106: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/106.jpg)
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 107: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/107.jpg)
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 108: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/108.jpg)
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 109: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/109.jpg)
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 110: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/110.jpg)
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
![Page 111: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/111.jpg)
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
![Page 112: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/112.jpg)
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
![Page 113: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/113.jpg)
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
![Page 114: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/114.jpg)
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
![Page 115: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/115.jpg)
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
![Page 116: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/116.jpg)
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
![Page 117: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/117.jpg)
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
![Page 118: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/118.jpg)
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
![Page 119: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/119.jpg)
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
![Page 120: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/120.jpg)
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
![Page 121: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/121.jpg)
Summary
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 51 / 53
![Page 122: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/122.jpg)
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
![Page 123: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/123.jpg)
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
![Page 124: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/124.jpg)
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
![Page 125: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/125.jpg)
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
![Page 126: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/126.jpg)
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
![Page 127: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/127.jpg)
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
![Page 128: Virtual Memory - Part2](https://reader034.fdocuments.us/reader034/viewer/2022051414/55ab61161a28ab702f8b46db/html5/thumbnails/128.jpg)
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 53 / 53