SlideShare a Scribd company logo
1 of 128
Download to read offline
Virtual Memory (Part II)
Amir H. Payberah
amir@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
Partially-loaded programs
Virtual memory: much larger than physical memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 3 / 53
Reminder
Demand paging similar to paging + swapping
Locality
Page fault
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
How do we allocate the fixed amount of free memory among the
various processes?
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 6 / 53
Allocation of Frames
How do we allocate the fixed amount of free memory among the
various processes?
If we have 93 free frames and two processes, how many frames does
each process get?
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 6 / 53
Frame Allocation Constraints (1/2)
The maximum number of frames is the total frames in the system.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53
Frame Allocation Constraints (1/2)
The maximum number of frames is the total frames in the system.
Each process needs minimum number of frames.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53
Frame Allocation Constraints (1/2)
The maximum number of frames is the total frames in the system.
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)
Why the minimum number of frames for each process?
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
Frame Allocation Constraints (2/2)
Why the minimum number of frames for each process? performance
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
Frame Allocation Constraints (2/2)
Why the minimum number of frames for each process? performance
Decreases the number of frames:
• Increases the page-fault rate
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
Frame Allocation Constraints (2/2)
Why the minimum number of frames for each process? performance
Decreases the number of frames:
• Increases the page-fault rate
When a page fault occurs before an executing instruction is com-
plete, the instruction must be restarted.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
Frame Allocation Constraints (2/2)
Why the minimum number of frames for each process? performance
Decreases the number of frames:
• Increases the page-fault rate
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 that
any single instruction can reference.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
Allocation Schemes
Fixed allocation
Priority allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53
Allocation Schemes
Fixed allocation
• Equal allocation
• Proportional allocation
Priority allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53
Fixed Allocation (1/4)
Equal allocation
Split m frames among n processes: m
n frames to each process.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 10 / 53
Fixed Allocation (1/4)
Equal allocation
Split m frames among n processes: m
n frames to each process.
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)
Assume 62 free frames, and the frame size is 1KB
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
Fixed Allocation (2/4)
Assume 62 free frames, and the frame size is 1KB
Two processes:
• A student process: 10KB
• An interactive database: 127KB
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
Fixed Allocation (2/4)
Assume 62 free frames, and the frame size is 1KB
Two processes:
• A student process: 10KB
• An interactive database: 127KB
Equal allocation: gives each process 31 frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
Fixed Allocation (2/4)
Assume 62 free frames, and the frame size is 1KB
Two processes:
• A student process: 10KB
• An interactive database: 127KB
Equal allocation: gives each process 31 frames
Wasting 21 frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
Fixed Allocation (3/4)
Proportional allocation
Allocate according to the size of process.
Dynamic as degree of multiprogramming, process sizes change.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
Fixed Allocation (3/4)
Proportional allocation
Allocate according to the size of process.
Dynamic as degree of multiprogramming, process sizes change.
si = size of process pi
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
Fixed Allocation (3/4)
Proportional allocation
Allocate according to the size of process.
Dynamic as degree of multiprogramming, process sizes change.
si = size of process pi
S = si
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
Fixed Allocation (3/4)
Proportional allocation
Allocate according to the size of process.
Dynamic as degree of multiprogramming, process sizes change.
si = size of process pi
S = si
m = total number of frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
Fixed Allocation (3/4)
Proportional allocation
Allocate according to the size of process.
Dynamic as degree of multiprogramming, process sizes change.
si = size of process pi
S = si
m = total number of frames
ai = si
S × m: allocation for pi
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
Fixed Allocation (4/4)
Assume 62 free frames, and the frame size is 1KB
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
Fixed Allocation (4/4)
Assume 62 free frames, and the frame size is 1KB
Two processes:
• A student process: 10KB
• An interactive database: 127KB
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
Fixed Allocation (4/4)
Assume 62 free frames, and the frame size is 1KB
Two processes:
• A student process: 10KB
• An interactive database: 127KB
Equal allocation: s1 = 10, s2 = 127, S = 137, m = 62
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
Fixed Allocation (4/4)
Assume 62 free frames, and the frame size is 1KB
Two processes:
• A student process: 10KB
• An interactive database: 127KB
Equal allocation: s1 = 10, s2 = 127, S = 137, m = 62
a1 = 10
137 × 62 ≈ 4 a2 = 127
137 × 62 ≈ 57
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
Priority Allocation
Use a proportional allocation scheme using priorities rather than
size.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 14 / 53
Priority Allocation
Use a proportional allocation scheme using priorities rather than
size.
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
Global replacement: process selects a replacement frame from the
set of all frames; one process can take a frame from another
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
Global vs. Local Allocation
Global replacement: process selects a replacement frame from the
set of all frames; one process can take a frame from another
• The process execution time can vary greatly.
• The greater throughput so more common.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
Global vs. Local Allocation
Global replacement: process selects a replacement frame from the
set of all frames; one process can take a frame from another
• The process execution time can vary greatly.
• The greater throughput so more common.
Local replacement: each process selects from only its own set of
allocated frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
Global vs. Local Allocation
Global replacement: process selects a replacement frame from the
set of all frames; one process can take a frame from another
• The process execution time can vary greatly.
• The greater throughput so more common.
Local replacement: each process selects from only its own set of
allocated 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
So far all memory accessed equally.
Many systems are NUMA: speed of access to memory varies.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 16 / 53
Non-Uniform Memory Access
So far all memory accessed equally.
Many systems are NUMA: speed of access to memory varies.
Optimal performance: allocate memory close to the CPU on which
the 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)
If a process does not have enough pages, the page-fault rate is very
high.
• Page fault to get page
• Replace existing frame
• But quickly need replaced frame back
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 18 / 53
Thrashing (1/2)
If a process does not have enough pages, the page-fault rate is very
high.
• Page fault to get page
• Replace existing frame
• But quickly need replaced frame back
This leads to:
• Low CPU utilization
• 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)
Thrashing: a process is busy swapping pages in and out.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 19 / 53
Prevent Thrashing
Providing a process with as many frames as it needs.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 20 / 53
Prevent Thrashing
Providing a process with as many frames as it needs.
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)
A locality is a set of pages that are actively used together.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
Locality Model (1/2)
A locality is a set of pages that are actively used together.
A process moves from locality to locality.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
Locality Model (1/2)
A locality is a set of pages that are actively used together.
A process moves from locality to locality.
A program is generally composed of several different localities, which
may overlap.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
Locality Model (1/2)
A locality is a set of pages that are actively used together.
A process moves from locality to locality.
A program is generally composed of several different localities, which
may overlap.
For example, when a function is called, it defines a new locality:
consists of memory references to the instructions of the function
call, 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
A process will fault for the pages in its locality, until all these pages
are in memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53
Locality and Thrashing
A process will fault for the pages in its locality, until all these pages
are in memory.
After allocating all the pages of the locality, it will not fault again
until it changes localities.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53
Locality and Thrashing
A process will fault for the pages in its locality, until all these pages
are in memory.
After allocating all the pages of the locality, it will not fault again
until it changes localities.
If we do not allocate enough frames to accommodate the size of the
current locality, the process will thrash.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53
Working-Set Model (1/2)
: working-set window: a fixed number of page references
WSSi : working set of process pi : total number of pages referenced
in the most recent (varies in time).
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 24 / 53
Working-Set Model (1/2)
: working-set window: a fixed number of page references
WSSi : working set of process pi : total number of pages referenced
in the most recent (varies in time).
• If too small will not encompass entire locality
• If too large will encompass several localities
• If = ∞ will encompass entire program
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 24 / 53
Working-Set Model (2/2)
m: total number of frames
D: total demand frames: D = WSSi :
• Approximation of locality
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53
Working-Set Model (2/2)
m: total number of frames
D: total demand frames: D = WSSi :
• Approximation of locality
If D > m, then thrashing
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53
Working-Set Model (2/2)
m: total number of frames
D: total demand frames: D = WSSi :
• Approximation of locality
If D > m, then thrashing
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
Approximate with interval timer + a reference bit
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
Keeping Track of the Working Set
Approximate with interval timer + a reference bit
Example: = 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.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
Keeping Track of the Working Set
Approximate with interval timer + a reference bit
Example: = 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.
Why is this not completely accurate?
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
Keeping Track of the Working Set
Approximate with interval timer + a reference bit
Example: = 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.
Why is this not completely accurate?
Improvement = 10 bits and interrupt every 1000 time units
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
Page-Fault Frequency
More direct approach than WSS
Establish acceptable page-fault frequency (PFF) rate and use local
replacement 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
Direct relationship between working set of a process and its page-
fault rate.
Working set changes over time.
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
Treated differently from user memory
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 30 / 53
Allocating Kernel Memory
Treated differently from user memory
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
Buddy system
Slab allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 31 / 53
Buddy System (1/2)
Allocates memory from fixed-size segment consisting of physically-
contiguous pages
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
Buddy System (1/2)
Allocates memory from fixed-size segment consisting of physically-
contiguous pages
Memory allocated using power-of-2 allocator
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
Buddy System (1/2)
Allocates memory from fixed-size segment consisting of physically-
contiguous pages
Memory allocated using power-of-2 allocator
• Satisfies requests in units sized as power of 2.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
Buddy System (1/2)
Allocates memory from fixed-size segment consisting of physically-
contiguous pages
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.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
Buddy System (1/2)
Allocates memory from fixed-size segment consisting of physically-
contiguous pages
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.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
Buddy System (1/2)
Allocates memory from fixed-size segment consisting of physically-
contiguous pages
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)
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.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 33 / 53
Buddy System (2/2)
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.
Advantage: quickly coalesce unused
chunks into larger chunk
Disadvantage: fragmentation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 33 / 53
Slab Allocation (1/2)
Alternate strategy
Slab is one or more physically contiguous pages.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
Slab Allocation (1/2)
Alternate strategy
Slab is one or more physically contiguous pages.
Cache consists of one or more slabs.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
Slab Allocation (1/2)
Alternate strategy
Slab is one or more physically contiguous pages.
Cache consists of one or more slabs.
Single cache for each unique
kernel data structure, e.g.,
a separate cache for file objects,
a separate cache for semaphores,
and so forth.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
Slab Allocation (1/2)
Alternate strategy
Slab is one or more physically contiguous pages.
Cache consists of one or more slabs.
Single cache for each unique
kernel data structure, e.g.,
a separate cache for file objects,
a separate cache for semaphores,
and so forth.
Objects: instantiations of the
data structure
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
Slab Allocation (2/2)
When cache created, filled with objects marked as free.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
Slab Allocation (2/2)
When cache created, filled with objects marked as free.
When structures stored, objects marked as used.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
Slab Allocation (2/2)
When cache created, filled with objects marked as free.
When structures stored, objects marked as used.
If slab is full of used objects, next object allocated from empty slab.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
Slab Allocation (2/2)
When cache created, filled with objects marked as free.
When structures stored, objects marked as used.
If slab is full of used objects, next object allocated from empty slab.
If no empty slabs, new slab allocated.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
Slab Allocation (2/2)
When cache created, filled with objects marked as free.
When structures stored, objects marked as used.
If slab is full of used objects, next object allocated from empty slab.
If no empty slabs, new slab allocated.
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)
Process descriptor: type struct task struct
Approx 1.7KB of memory
New task → allocate new struct from cache
• Will use existing free struct task struct
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)
Upon request, slab allocator
1 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)
Linux originally used the buddy system.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53
Slab Allocator in Linux (3/3)
Linux originally used the buddy system.
Kernel 2.2 had SLAB.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53
Slab Allocator in Linux (3/3)
Linux originally used the buddy system.
Kernel 2.2 had SLAB.
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)
Memory-mapped file I/O allows file I/O to be treated as routine
memory access by mapping a disk block to a page in memory.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 41 / 53
Memory-Mapped Files (1/3)
Memory-mapped file I/O allows file I/O to be treated as routine
memory access by mapping a disk block to a page in memory.
A file is initially read using demand paging.
• A page-sized portion of the file is read from the file system into a
physical page.
• Subsequent reads/writes to/from the file are treated as ordinary
memory accesses.
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)
Process can explicitly request memory mapping a file via mmap()
system call: map the file into kernel address space
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
Memory-Mapped Files (3/3)
Process can explicitly request memory mapping a file via mmap()
system call: map the file into kernel address space
Simplifies and speeds file access by driving file I/O through memory
rather than read() and write() system calls.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
Memory-Mapped Files (3/3)
Process can explicitly request memory mapping a file via mmap()
system call: map the file into kernel address space
Simplifies and speeds file access by driving file I/O through memory
rather than read() and write() system calls.
Also allows several processes to map the same file allowing the pages
in memory to be shared.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
Memory-Mapped Files (3/3)
Process can explicitly request memory mapping a file via mmap()
system call: map the file into kernel address space
Simplifies and speeds file access by driving file I/O through memory
rather than read() and write() system calls.
Also allows several processes to map the same file allowing the pages
in memory to be shared.
When does written data make it to disk?
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
Memory-Mapped Files (3/3)
Process can explicitly request memory mapping a file via mmap()
system call: map the file into kernel address space
Simplifies and speeds file access by driving file I/O through memory
rather than read() and write() system calls.
Also allows several processes to map the same file allowing the pages
in memory to be shared.
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
Many computer architectures provide memory-mapped I/O.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53
Memory-Mapped I/O
Many computer architectures provide memory-mapped I/O.
Ranges of memory addresses are mapped to the device registers.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53
Memory-Mapped I/O
Many computer architectures provide memory-mapped I/O.
Ranges of memory addresses are mapped to the device registers.
Reads and writes to these memory addresses cause the data to be
transferred 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
To reduce the large number of page faults that occurs at process
startup.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53
Prepaging
To reduce the large number of page faults that occurs at process
startup.
Prepage all or some of the pages a process will need, before they
are referenced.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53
Prepaging
To reduce the large number of page faults that occurs at process
startup.
Prepage all or some of the pages a process will need, before they
are referenced.
If prepaged pages are unused, I/O and memory was wasted.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53
Prepaging
To reduce the large number of page faults that occurs at process
startup.
Prepage all or some of the pages a process will need, before they
are referenced.
If prepaged pages are unused, I/O and memory was wasted.
Assume s pages are prepaged and a fraction 0 ≤ α ≤ 1 of the pages
are 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
Page size selection must take into consideration:
• Fragmentation
• Page table size
• Resolution
• I/O overhead
• Number of page faults
• Locality
• TLB size and effectiveness
Always power of 2
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 47 / 53
TLB Search
TLB reach: the amount of memory accessible from the TLB
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
TLB Search
TLB reach: the amount of memory accessible from the TLB
TLB reach = (TLB Size) × (Page Size)
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
TLB Search
TLB reach: the amount of memory accessible from the TLB
TLB reach = (TLB Size) × (Page Size)
Ideally, the working set of each process is stored in the TLB
• Otherwise there is a high degree of page faults.
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
TLB Search
TLB reach: the amount of memory accessible from the TLB
TLB reach = (TLB Size) × (Page Size)
Ideally, the working set of each process is stored in the TLB
• Otherwise there is a high degree of page faults.
Increase the page size
• This may lead to an increase in fragmentation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
TLB Search
TLB reach: the amount of memory accessible from the TLB
TLB reach = (TLB Size) × (Page Size)
Ideally, the working set of each process is stored in the TLB
• Otherwise there is a high degree of page faults.
Increase the page size
• This may lead to an increase in fragmentation
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
int[128,128] data: each row is stored in one page
Program 1
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i, j] = 0;
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53
Program Structure
int[128,128] data: each row is stored in one page
Program 1
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i, j] = 0;
128 × 128 = 16, 384 page faults
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53
Program Structure
int[128,128] data: each row is stored in one page
Program 1
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i, j] = 0;
128 × 128 = 16, 384 page faults
Program 2
for (i = 0; i <128; j++)
for (j = 0; j < 128; i++)
data[i, j] = 0;
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53
Program Structure
int[128,128] data: each row is stored in one page
Program 1
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i, j] = 0;
128 × 128 = 16, 384 page faults
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/O interlock: pages must sometimes be locked into memory.
Consider I/O: pages that are used for copying a file from a device
must be locked from being selected for eviction by a page 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
Frame allocation: fixed and priority allocations
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
Summary
Frame allocation: fixed and priority allocations
Global and local allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
Summary
Frame allocation: fixed and priority allocations
Global and local allocation
Thrashing: total demand frames > total num. of frames
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
Summary
Frame allocation: fixed and priority allocations
Global and local allocation
Thrashing: total demand frames > total num. of frames
Prevent trashing: working set model and page fault frequency
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
Summary
Frame allocation: fixed and priority allocations
Global and local allocation
Thrashing: total demand frames > total num. of frames
Prevent trashing: working set model and page fault frequency
Allocating kernel memory: buddy system and slab allocation
Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
Summary
Frame allocation: fixed and priority allocations
Global and local allocation
Thrashing: total demand frames > total num. of frames
Prevent trashing: working set model and page fault frequency
Allocating kernel memory: buddy system and slab allocation
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

More Related Content

What's hot

Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1Amir Payberah
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Amir Payberah
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Amir Payberah
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1Amir Payberah
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2Amir Payberah
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3Amir Payberah
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Amir Payberah
 
Process management os concept
Process management os conceptProcess management os concept
Process management os conceptpriyadeosarkar91
 
Cse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solutionCse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solutionShobha Kumar
 
Chapter 1 - Introduction
Chapter 1 - IntroductionChapter 1 - Introduction
Chapter 1 - IntroductionWayne Jones Jnr
 

What's hot (20)

Storage
StorageStorage
Storage
 
Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1
 
G143
G143G143
G143
 
Chapter 2 pc
Chapter 2 pcChapter 2 pc
Chapter 2 pc
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Sucet os module_4_notes
Sucet os module_4_notesSucet os module_4_notes
Sucet os module_4_notes
 
Cs8493 unit 3
Cs8493 unit 3Cs8493 unit 3
Cs8493 unit 3
 
Sucet os module_5_notes
Sucet os module_5_notesSucet os module_5_notes
Sucet os module_5_notes
 
Cse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solutionCse viii-advanced-computer-architectures-06cs81-solution
Cse viii-advanced-computer-architectures-06cs81-solution
 
Chapter 1 - Introduction
Chapter 1 - IntroductionChapter 1 - Introduction
Chapter 1 - Introduction
 
Cs8493 unit 2
Cs8493 unit 2Cs8493 unit 2
Cs8493 unit 2
 
os mod1 notes
 os mod1 notes os mod1 notes
os mod1 notes
 
Operating System
Operating SystemOperating System
Operating System
 
Main Memory
Main MemoryMain Memory
Main Memory
 

Viewers also liked

Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2Amir Payberah
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformAmir Payberah
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing FrameworksAmir Payberah
 
File System Interface
File System InterfaceFile System Interface
File System InterfaceAmir Payberah
 
P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution NetworkAmir Payberah
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1Amir Payberah
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and PagingEmery Berger
 
Virtual memory
Virtual memoryVirtual memory
Virtual memoryAnuj Modi
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1Amir Payberah
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2Amir Payberah
 

Viewers also liked (14)

Security
SecuritySecurity
Security
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2
 
IO Systems
IO SystemsIO Systems
IO Systems
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics Platform
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing Frameworks
 
File System Interface
File System InterfaceFile System Interface
File System Interface
 
P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution Network
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Virtual memory ppt
Virtual memory pptVirtual memory ppt
Virtual memory ppt
 
Threads
ThreadsThreads
Threads
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2
 

Similar to Virtual Memory - Part2

Similar to Virtual Memory - Part2 (20)

Google File System
Google File SystemGoogle File System
Google File System
 
Mesos and YARN
Mesos and YARNMesos and YARN
Mesos and YARN
 
Bigtable
BigtableBigtable
Bigtable
 
MegaStore and Spanner
MegaStore and SpannerMegaStore and Spanner
MegaStore and Spanner
 
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvkVirtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
 
Unit 2chapter 2 memory mgmt complete
Unit 2chapter 2  memory mgmt completeUnit 2chapter 2  memory mgmt complete
Unit 2chapter 2 memory mgmt complete
 
Virtual memory and page replacement algorithm
Virtual memory and page replacement algorithmVirtual memory and page replacement algorithm
Virtual memory and page replacement algorithm
 
Chubby
ChubbyChubby
Chubby
 
Allocation of Frames & Thrashing
Allocation of Frames & ThrashingAllocation of Frames & Thrashing
Allocation of Frames & Thrashing
 
Chap2 slides
Chap2 slidesChap2 slides
Chap2 slides
 
Limitations of memory system performance
Limitations of memory system performanceLimitations of memory system performance
Limitations of memory system performance
 
Chap2 slides
Chap2 slidesChap2 slides
Chap2 slides
 
A survey on exploring memory optimizations in smartphones
A survey on exploring memory optimizations in smartphonesA survey on exploring memory optimizations in smartphones
A survey on exploring memory optimizations in smartphones
 
Operating system 38 page replacement
Operating system 38 page replacementOperating system 38 page replacement
Operating system 38 page replacement
 
Co question 2010
Co question 2010Co question 2010
Co question 2010
 
Real time operating systems
Real time operating systemsReal time operating systems
Real time operating systems
 
Matrix multiplication
Matrix multiplicationMatrix multiplication
Matrix multiplication
 
O ssvv82014
O ssvv82014O ssvv82014
O ssvv82014
 
Pharos
PharosPharos
Pharos
 
thesis-shai
thesis-shaithesis-shai
thesis-shai
 

Recently uploaded

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Recently uploaded (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

Virtual Memory - Part2

  • 1. Virtual Memory (Part II) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 1 / 53
  • 2. Reminder Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 2 / 53
  • 3. Reminder Partially-loaded programs Virtual memory: much larger than physical memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 3 / 53
  • 4. Reminder Demand paging similar to paging + swapping Locality Page fault Page replacement algorithms: • FIFO, optimal, LRU, LRU-approximate, counting-based Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 4 / 53
  • 5. Allocation of Frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 5 / 53
  • 6. Allocation of Frames How do we allocate the fixed amount of free memory among the various processes? Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 6 / 53
  • 7. Allocation of Frames How do we allocate the fixed amount of free memory among the various processes? If we have 93 free frames and two processes, how many frames does each process get? Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 6 / 53
  • 8. Frame Allocation Constraints (1/2) The maximum number of frames is the total frames in the system. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53
  • 9. Frame Allocation Constraints (1/2) The maximum number of frames is the total frames in the system. Each process needs minimum number of frames. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 7 / 53
  • 10. Frame Allocation Constraints (1/2) The maximum number of frames is the total frames in the system. 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
  • 11. Frame Allocation Constraints (2/2) Why the minimum number of frames for each process? Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
  • 12. Frame Allocation Constraints (2/2) Why the minimum number of frames for each process? performance Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
  • 13. Frame Allocation Constraints (2/2) Why the minimum number of frames for each process? performance Decreases the number of frames: • Increases the page-fault rate Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
  • 14. Frame Allocation Constraints (2/2) Why the minimum number of frames for each process? performance Decreases the number of frames: • Increases the page-fault rate When a page fault occurs before an executing instruction is com- plete, the instruction must be restarted. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
  • 15. Frame Allocation Constraints (2/2) Why the minimum number of frames for each process? performance Decreases the number of frames: • Increases the page-fault rate 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 that any single instruction can reference. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 8 / 53
  • 16. Allocation Schemes Fixed allocation Priority allocation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53
  • 17. Allocation Schemes Fixed allocation • Equal allocation • Proportional allocation Priority allocation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 9 / 53
  • 18. Fixed Allocation (1/4) Equal allocation Split m frames among n processes: m n frames to each process. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 10 / 53
  • 19. Fixed Allocation (1/4) Equal allocation Split m frames among n processes: m n frames to each process. 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
  • 20. Fixed Allocation (2/4) Assume 62 free frames, and the frame size is 1KB Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
  • 21. Fixed Allocation (2/4) Assume 62 free frames, and the frame size is 1KB Two processes: • A student process: 10KB • An interactive database: 127KB Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
  • 22. Fixed Allocation (2/4) Assume 62 free frames, and the frame size is 1KB Two processes: • A student process: 10KB • An interactive database: 127KB Equal allocation: gives each process 31 frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
  • 23. Fixed Allocation (2/4) Assume 62 free frames, and the frame size is 1KB Two processes: • A student process: 10KB • An interactive database: 127KB Equal allocation: gives each process 31 frames Wasting 21 frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 11 / 53
  • 24. Fixed Allocation (3/4) Proportional allocation Allocate according to the size of process. Dynamic as degree of multiprogramming, process sizes change. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
  • 25. Fixed Allocation (3/4) Proportional allocation Allocate according to the size of process. Dynamic as degree of multiprogramming, process sizes change. si = size of process pi Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
  • 26. Fixed Allocation (3/4) Proportional allocation Allocate according to the size of process. Dynamic as degree of multiprogramming, process sizes change. si = size of process pi S = si Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
  • 27. Fixed Allocation (3/4) Proportional allocation Allocate according to the size of process. Dynamic as degree of multiprogramming, process sizes change. si = size of process pi S = si m = total number of frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
  • 28. Fixed Allocation (3/4) Proportional allocation Allocate according to the size of process. Dynamic as degree of multiprogramming, process sizes change. si = size of process pi S = si m = total number of frames ai = si S × m: allocation for pi Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 12 / 53
  • 29. Fixed Allocation (4/4) Assume 62 free frames, and the frame size is 1KB Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
  • 30. Fixed Allocation (4/4) Assume 62 free frames, and the frame size is 1KB Two processes: • A student process: 10KB • An interactive database: 127KB Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
  • 31. Fixed Allocation (4/4) Assume 62 free frames, and the frame size is 1KB Two processes: • A student process: 10KB • An interactive database: 127KB Equal allocation: s1 = 10, s2 = 127, S = 137, m = 62 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
  • 32. Fixed Allocation (4/4) Assume 62 free frames, and the frame size is 1KB Two processes: • A student process: 10KB • An interactive database: 127KB Equal allocation: s1 = 10, s2 = 127, S = 137, m = 62 a1 = 10 137 × 62 ≈ 4 a2 = 127 137 × 62 ≈ 57 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 13 / 53
  • 33. Priority Allocation Use a proportional allocation scheme using priorities rather than size. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 14 / 53
  • 34. Priority Allocation Use a proportional allocation scheme using priorities rather than size. 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
  • 35. Global vs. Local Allocation Global replacement: process selects a replacement frame from the set of all frames; one process can take a frame from another Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
  • 36. Global vs. Local Allocation Global replacement: process selects a replacement frame from the set of all frames; one process can take a frame from another • The process execution time can vary greatly. • The greater throughput so more common. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
  • 37. Global vs. Local Allocation Global replacement: process selects a replacement frame from the set of all frames; one process can take a frame from another • The process execution time can vary greatly. • The greater throughput so more common. Local replacement: each process selects from only its own set of allocated frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
  • 38. Global vs. Local Allocation Global replacement: process selects a replacement frame from the set of all frames; one process can take a frame from another • The process execution time can vary greatly. • The greater throughput so more common. Local replacement: each process selects from only its own set of allocated frames • More consistent per-process performance • Possibly underutilized memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 15 / 53
  • 39. Non-Uniform Memory Access So far all memory accessed equally. Many systems are NUMA: speed of access to memory varies. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 16 / 53
  • 40. Non-Uniform Memory Access So far all memory accessed equally. Many systems are NUMA: speed of access to memory varies. Optimal performance: allocate memory close to the CPU on which the thread is scheduled. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 16 / 53
  • 41. Thrashing Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 17 / 53
  • 42. Thrashing (1/2) If a process does not have enough pages, the page-fault rate is very high. • Page fault to get page • Replace existing frame • But quickly need replaced frame back Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 18 / 53
  • 43. Thrashing (1/2) If a process does not have enough pages, the page-fault rate is very high. • Page fault to get page • Replace existing frame • But quickly need replaced frame back This leads to: • Low CPU utilization • 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
  • 44. Thrashing (2/2) Thrashing: a process is busy swapping pages in and out. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 19 / 53
  • 45. Prevent Thrashing Providing a process with as many frames as it needs. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 20 / 53
  • 46. Prevent Thrashing Providing a process with as many frames as it needs. How do we know how many frames it needs? Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 20 / 53
  • 47. Locality Model (1/2) A locality is a set of pages that are actively used together. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
  • 48. Locality Model (1/2) A locality is a set of pages that are actively used together. A process moves from locality to locality. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
  • 49. Locality Model (1/2) A locality is a set of pages that are actively used together. A process moves from locality to locality. A program is generally composed of several different localities, which may overlap. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
  • 50. Locality Model (1/2) A locality is a set of pages that are actively used together. A process moves from locality to locality. A program is generally composed of several different localities, which may overlap. For example, when a function is called, it defines a new locality: consists of memory references to the instructions of the function call, its local variables, and a subset of the global variables. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 21 / 53
  • 51. Locality Model (2/2) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 22 / 53
  • 52. Locality and Thrashing A process will fault for the pages in its locality, until all these pages are in memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53
  • 53. Locality and Thrashing A process will fault for the pages in its locality, until all these pages are in memory. After allocating all the pages of the locality, it will not fault again until it changes localities. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53
  • 54. Locality and Thrashing A process will fault for the pages in its locality, until all these pages are in memory. After allocating all the pages of the locality, it will not fault again until it changes localities. If we do not allocate enough frames to accommodate the size of the current locality, the process will thrash. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 23 / 53
  • 55. Working-Set Model (1/2) : working-set window: a fixed number of page references WSSi : working set of process pi : total number of pages referenced in the most recent (varies in time). Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 24 / 53
  • 56. Working-Set Model (1/2) : working-set window: a fixed number of page references WSSi : working set of process pi : total number of pages referenced in the most recent (varies in time). • If too small will not encompass entire locality • If too large will encompass several localities • If = ∞ will encompass entire program Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 24 / 53
  • 57. Working-Set Model (2/2) m: total number of frames D: total demand frames: D = WSSi : • Approximation of locality Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53
  • 58. Working-Set Model (2/2) m: total number of frames D: total demand frames: D = WSSi : • Approximation of locality If D > m, then thrashing Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 25 / 53
  • 59. Working-Set Model (2/2) m: total number of frames D: total demand frames: D = WSSi : • Approximation of locality If D > m, then thrashing 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
  • 60. Keeping Track of the Working Set Approximate with interval timer + a reference bit Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
  • 61. Keeping Track of the Working Set Approximate with interval timer + a reference bit Example: = 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. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
  • 62. Keeping Track of the Working Set Approximate with interval timer + a reference bit Example: = 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. Why is this not completely accurate? Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
  • 63. Keeping Track of the Working Set Approximate with interval timer + a reference bit Example: = 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. Why is this not completely accurate? Improvement = 10 bits and interrupt every 1000 time units Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 26 / 53
  • 64. Page-Fault Frequency More direct approach than WSS Establish acceptable page-fault frequency (PFF) rate and use local replacement 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
  • 65. Working Sets and Page-Fault Rates Direct relationship between working set of a process and its page- fault rate. Working set changes over time. Peaks and valleys over time. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 28 / 53
  • 66. Allocating Kernel Memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 29 / 53
  • 67. Allocating Kernel Memory Treated differently from user memory Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 30 / 53
  • 68. Allocating Kernel Memory Treated differently from user memory 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
  • 69. Managing Free Memory Strategies Buddy system Slab allocation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 31 / 53
  • 70. Buddy System (1/2) Allocates memory from fixed-size segment consisting of physically- contiguous pages Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
  • 71. Buddy System (1/2) Allocates memory from fixed-size segment consisting of physically- contiguous pages Memory allocated using power-of-2 allocator Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
  • 72. Buddy System (1/2) Allocates memory from fixed-size segment consisting of physically- contiguous pages Memory allocated using power-of-2 allocator • Satisfies requests in units sized as power of 2. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
  • 73. Buddy System (1/2) Allocates memory from fixed-size segment consisting of physically- contiguous pages 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. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
  • 74. Buddy System (1/2) Allocates memory from fixed-size segment consisting of physically- contiguous pages 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. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 32 / 53
  • 75. Buddy System (1/2) Allocates memory from fixed-size segment consisting of physically- contiguous pages 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
  • 76. Buddy System (2/2) 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. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 33 / 53
  • 77. Buddy System (2/2) 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. Advantage: quickly coalesce unused chunks into larger chunk Disadvantage: fragmentation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 33 / 53
  • 78. Slab Allocation (1/2) Alternate strategy Slab is one or more physically contiguous pages. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
  • 79. Slab Allocation (1/2) Alternate strategy Slab is one or more physically contiguous pages. Cache consists of one or more slabs. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
  • 80. Slab Allocation (1/2) Alternate strategy Slab is one or more physically contiguous pages. Cache consists of one or more slabs. Single cache for each unique kernel data structure, e.g., a separate cache for file objects, a separate cache for semaphores, and so forth. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
  • 81. Slab Allocation (1/2) Alternate strategy Slab is one or more physically contiguous pages. Cache consists of one or more slabs. Single cache for each unique kernel data structure, e.g., a separate cache for file objects, a separate cache for semaphores, and so forth. Objects: instantiations of the data structure Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 34 / 53
  • 82. Slab Allocation (2/2) When cache created, filled with objects marked as free. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
  • 83. Slab Allocation (2/2) When cache created, filled with objects marked as free. When structures stored, objects marked as used. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
  • 84. Slab Allocation (2/2) When cache created, filled with objects marked as free. When structures stored, objects marked as used. If slab is full of used objects, next object allocated from empty slab. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
  • 85. Slab Allocation (2/2) When cache created, filled with objects marked as free. When structures stored, objects marked as used. If slab is full of used objects, next object allocated from empty slab. If no empty slabs, new slab allocated. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
  • 86. Slab Allocation (2/2) When cache created, filled with objects marked as free. When structures stored, objects marked as used. If slab is full of used objects, next object allocated from empty slab. If no empty slabs, new slab allocated. Benefits include no fragmentation and fast memory request satis- faction. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 35 / 53
  • 87. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 36 / 53
  • 88. Slab Allocator in Linux (1/3) Process descriptor: type struct task struct Approx 1.7KB of memory New task → allocate new struct from cache • Will use existing free struct task struct 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
  • 89. Slab Allocator in Linux (2/3) Upon request, slab allocator 1 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
  • 90. Slab Allocator in Linux (3/3) Linux originally used the buddy system. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53
  • 91. Slab Allocator in Linux (3/3) Linux originally used the buddy system. Kernel 2.2 had SLAB. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 39 / 53
  • 92. Slab Allocator in Linux (3/3) Linux originally used the buddy system. Kernel 2.2 had SLAB. 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
  • 93. Memory-Mapped Files Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 40 / 53
  • 94. Memory-Mapped Files (1/3) Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 41 / 53
  • 95. Memory-Mapped Files (1/3) Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory. A file is initially read using demand paging. • A page-sized portion of the file is read from the file system into a physical page. • Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 41 / 53
  • 96. Memory-Mapped Files (2/3) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 42 / 53
  • 97. Memory-Mapped Files (3/3) Process can explicitly request memory mapping a file via mmap() system call: map the file into kernel address space Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
  • 98. Memory-Mapped Files (3/3) Process can explicitly request memory mapping a file via mmap() system call: map the file into kernel address space Simplifies and speeds file access by driving file I/O through memory rather than read() and write() system calls. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
  • 99. Memory-Mapped Files (3/3) Process can explicitly request memory mapping a file via mmap() system call: map the file into kernel address space Simplifies and speeds file access by driving file I/O through memory rather than read() and write() system calls. Also allows several processes to map the same file allowing the pages in memory to be shared. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
  • 100. Memory-Mapped Files (3/3) Process can explicitly request memory mapping a file via mmap() system call: map the file into kernel address space Simplifies and speeds file access by driving file I/O through memory rather than read() and write() system calls. Also allows several processes to map the same file allowing the pages in memory to be shared. When does written data make it to disk? Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 43 / 53
  • 101. Memory-Mapped Files (3/3) Process can explicitly request memory mapping a file via mmap() system call: map the file into kernel address space Simplifies and speeds file access by driving file I/O through memory rather than read() and write() system calls. Also allows several processes to map the same file allowing the pages in memory to be shared. 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
  • 102. Memory-Mapped I/O Many computer architectures provide memory-mapped I/O. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53
  • 103. Memory-Mapped I/O Many computer architectures provide memory-mapped I/O. Ranges of memory addresses are mapped to the device registers. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53
  • 104. Memory-Mapped I/O Many computer architectures provide memory-mapped I/O. Ranges of memory addresses are mapped to the device registers. Reads and writes to these memory addresses cause the data to be transferred to and from the device registers. • Called, I/O port Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 44 / 53
  • 105. Other Considerations Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 45 / 53
  • 106. Prepaging To reduce the large number of page faults that occurs at process startup. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53
  • 107. Prepaging To reduce the large number of page faults that occurs at process startup. Prepage all or some of the pages a process will need, before they are referenced. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53
  • 108. Prepaging To reduce the large number of page faults that occurs at process startup. Prepage all or some of the pages a process will need, before they are referenced. If prepaged pages are unused, I/O and memory was wasted. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 46 / 53
  • 109. Prepaging To reduce the large number of page faults that occurs at process startup. Prepage all or some of the pages a process will need, before they are referenced. If prepaged pages are unused, I/O and memory was wasted. Assume s pages are prepaged and a fraction 0 ≤ α ≤ 1 of the pages are 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
  • 110. Page Size Page size selection must take into consideration: • Fragmentation • Page table size • Resolution • I/O overhead • Number of page faults • Locality • TLB size and effectiveness Always power of 2 Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 47 / 53
  • 111. TLB Search TLB reach: the amount of memory accessible from the TLB Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
  • 112. TLB Search TLB reach: the amount of memory accessible from the TLB TLB reach = (TLB Size) × (Page Size) Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
  • 113. TLB Search TLB reach: the amount of memory accessible from the TLB TLB reach = (TLB Size) × (Page Size) Ideally, the working set of each process is stored in the TLB • Otherwise there is a high degree of page faults. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
  • 114. TLB Search TLB reach: the amount of memory accessible from the TLB TLB reach = (TLB Size) × (Page Size) Ideally, the working set of each process is stored in the TLB • Otherwise there is a high degree of page faults. Increase the page size • This may lead to an increase in fragmentation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 48 / 53
  • 115. TLB Search TLB reach: the amount of memory accessible from the TLB TLB reach = (TLB Size) × (Page Size) Ideally, the working set of each process is stored in the TLB • Otherwise there is a high degree of page faults. Increase the page size • This may lead to an increase in fragmentation 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
  • 116. Program Structure int[128,128] data: each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i, j] = 0; Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53
  • 117. Program Structure int[128,128] data: each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i, j] = 0; 128 × 128 = 16, 384 page faults Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53
  • 118. Program Structure int[128,128] data: each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i, j] = 0; 128 × 128 = 16, 384 page faults Program 2 for (i = 0; i <128; j++) for (j = 0; j < 128; i++) data[i, j] = 0; Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 49 / 53
  • 119. Program Structure int[128,128] data: each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i, j] = 0; 128 × 128 = 16, 384 page faults 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
  • 120. I/O Interlock I/O interlock: pages must sometimes be locked into memory. Consider I/O: pages that are used for copying a file from a device must be locked from being selected for eviction by a page replace- ment algorithm. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 50 / 53
  • 121. Summary Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 51 / 53
  • 122. Summary Frame allocation: fixed and priority allocations Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
  • 123. Summary Frame allocation: fixed and priority allocations Global and local allocation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
  • 124. Summary Frame allocation: fixed and priority allocations Global and local allocation Thrashing: total demand frames > total num. of frames Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
  • 125. Summary Frame allocation: fixed and priority allocations Global and local allocation Thrashing: total demand frames > total num. of frames Prevent trashing: working set model and page fault frequency Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
  • 126. Summary Frame allocation: fixed and priority allocations Global and local allocation Thrashing: total demand frames > total num. of frames Prevent trashing: working set model and page fault frequency Allocating kernel memory: buddy system and slab allocation Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
  • 127. Summary Frame allocation: fixed and priority allocations Global and local allocation Thrashing: total demand frames > total num. of frames Prevent trashing: working set model and page fault frequency Allocating kernel memory: buddy system and slab allocation Memory-mapped files and I/O Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 52 / 53
  • 128. Questions? Acknowledgements Some slides were derived from Avi Silberschatz slides. Amir H. Payberah (Tehran Polytechnic) Virtual Memory 1393/8/26 53 / 53