Washington WASHINGTON UNIVERSITY IN ST LOUIS Memory Management and Virtual Memory Fred Kuhns ([email protected], http://www.arl.wustl.edu/~fredk) Applied Research Laboratory Department of Computer Science and Engineering Washington University in St. Louis
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
– Applications should not have to manage memory resources
– Write machine independent code – program should not depend on memory architecture.
• Permit sharing of memory segments or regions. For example, read-only code segments should be shared between program instances.
Fred Kuhns (04/28/23) CS523 – Operating Systems 7
Virtual Memory Costs• Space: Translation tables and other data used by
VM system reduce available memory to programs• Time: Address translation time is added to the cost
(execution time) of each instruction. • Async: Also page fault handling may result in page
I/O operations increasing latency and possibly affecting unrelated processes.
• Overhead: Memory management operations have been measured to consume up to 10% of the CPU time on a busy system.
• Efficiency: Allocating memory in pages may result in fragmentation
Fred Kuhns (04/28/23) CS523 – Operating Systems 8
Processes and Memory• Process runs on a virtual machine as defined
by the underlying hardware.• Focus is on Hardware support for a virtual
address spacevirtual addresses independent of physical
memory• Key hardware component is the Memory
Management Unit (MMU)address translation: virtual to physical memorysimplifies context switchingensures virtual address space protection
Fred Kuhns (04/28/23) CS523 – Operating Systems 9
Memory allocation
Page-levelallocator
physical page
Kernel memoryAllocator
Pagingsystem
Networkbuffers
Datastructures
tempstorage process Buffer cache
Fred Kuhns (04/28/23) CS523 – Operating Systems 10
Page-level Allocation• Kernel maintains a list of free page
frames (physical memory)• Since kernel and user space programs
use virtual memory addresses, the physical location of a page is not important
• Pages are allocated from the free list• Two principal clients:
paging systemkernel memory allocator
Fred Kuhns (04/28/23) CS523 – Operating Systems 11
The Paging System• Responsible for allocating and
managing the address space of processes
• Primary goal is to allow processes to run in a virtual address space and to perform address translations transparently
• In demand-paged system the page is the basic unit of memory allocation, protection and address translation. Virtual address is converted to physical page (frame) number and offset.
Fred Kuhns (04/28/23) CS523 – Operating Systems 12
The Paging System• Requirements:
– Address space management – Address Translation – translation maps
used by MMU, may result in a page fault exception
– Physical memory management – physical memory used as a cache for useful data.
– Memory Protection – HW support exploited– Memory Sharing– Monitoring system load– Other facilities – for example memory
mapped files or shared libraries
Fred Kuhns (04/28/23) CS523 – Operating Systems 13
pagen
Paged Virtual Memory
page0page1page2page3
pagen
P1 virtualaddress space
page0page1page2page3
P2 virtualaddress spacepage
0page1page2page3page4page5page6page7
Physicaladdress space
AddressTranslation
residentNon-resident Page
frames
Working set
pagen
Fred Kuhns (04/28/23) CS523 – Operating Systems 14
The Virtual Address Space• Address space along with processes
register context reflects the current state• exec causes kernel to build new process
– These regions may differ in protection, initialization and sharing. Protections usually set at page level when allocated.
• Process may start running before any of its pages are resident in memory.
Fred Kuhns (04/28/23) CS523 – Operating Systems 15
Initial Access to Pages
• Text and initialized data are read in from executable file.
• Uninitialized data are zero-filled pages• Shared libraries from library file• The u area and stacks are setup during
process creation (copied from parent).
Fred Kuhns (04/28/23) CS523 – Operating Systems 16
Swap Area: VM Backing Store• Swap Area: Pages are copied to the swap
device to free up space for running programs.
• Swapping plus paging for two-tiered scheme
• Requires a swap map to locate swapped out pages
• MMU set dirty bit for page if it has been modified
• Text pages need not be backed by swap
Fred Kuhns (04/28/23) CS523 – Operating Systems 17
Translation Maps• Hardware Translation Tables – each access to
memory must have the virtual address translated to a physical memory location– Page tables provide the MMU with this mapping– MMU uses TLB to cache recent translations
• Other maps used by the OS:– Address space map – describes a virtual address
space for a process or kernel– Physical memory map – kernel uses to perform
reverse maps and to describe a pages ownership, references and protections.
– Backing store map – used to locate non-resident pages
Fred Kuhns (04/28/23) CS523 – Operating Systems 18
Replacement Algorithms• Deciding when to reclaim a page: Defined in
terms of criteria used for selecting pages to reclaim
• Reference string: pages referenced over time• fault rate: page faults for some length of a
reference string (i.e. over a period of time)• Algorithms evaluated based on effectiveness
on collected (real) reference strings• Implementations usually require sample
reference strings• Local versus global policies:
– Most UNIX implementation use a global replacement policy but guarantee a minimum
Fred Kuhns (04/28/23) CS523 – Operating Systems 19
Working set Model• Assumes a slowing changing locality of reference
– processes tend to localize references to a small set of pages– implies that if a page was recently accessed then it will be
accessed again in the “near” future– if working set is in memory then few page faults
• A simple model is a least recently used (LRU) policy:– if a page has been accessed “recently” then assume it will be
need again else assume it will not be needed– else free pages not accessed “recently”
• Implement using an approximate set: – number of pages held versus fault rate. – Set high and low water marks
• Most kernels implement a scheme whereby pages are periodically freed and placed on a free pool.
• Prepaging: working set resident before scheduling process
Fred Kuhns (04/28/23) CS523 – Operating Systems 20
Working set model• Reference pattern nonuniform but clustered• w(k,t) = pages (set) corresponding to the last k
memory references, at time t.– w(1,t) w(2,t) … w(n,t)– |w(k,t)| = size of w is monotonically increasing with k
• Practical considerations lead to using a processes virtual time rather than k recent references– consider page references in past virtual time units.– clear R bit every clock tick, it is set when a page is
referenced– on page fault if R is set then update virtual time of last use
else set age = current_vt – last_use_vt. If age > then reclaim page else it is in the working set.
continue to scan all entriesif no entries found then reclaim oldest. If all had R bit set then randomly select page to reclaim.
Fred Kuhns (04/28/23) CS523 – Operating Systems 21
WSClock Algorithm• Similar to clock algorithm with circular list
and clock hand.• scans list and if R = 1 then it is cleared and
the current virtual time is written.• advance hand• if R = 0 then check timestamp > T then
replace (if dirty schedule for write else put on free list)
• Alternatively, can use two clock hands
Fred Kuhns (04/28/23) CS523 – Operating Systems 22
Example Paging System
SwapUFS
app1
Disk
DRAM
CPUUnitialized data
Allocated virtual pages
Text and initialized data
Stack and heap
app1Address space
Low Address(0x00000000)
High Address(0x7fffffff)
Initialized Data
Text (shared)
Unitialized DataHeap (Dynamic)
stack (dynamic)Environment
Fred Kuhns (04/28/23) CS523 – Operating Systems 23
Hardware Requirements• Protection: Prevent process from changing
own memory maps• Residency: CPU distinguishes between
resident and non-resident pages• Loading: Load pages and restart interrupted
program instructions• Dirty: Determine if pages have been
modified
Fred Kuhns (04/28/23) CS523 – Operating Systems 24
Fred Kuhns (04/28/23) CS523 – Operating Systems 39
VM Architecture
Proc A virtual address
AddressTranslation
physicaladdress
Physical page& offset
AS layer
vnode layeranon layer
swap layer page layer...
fault
HAT
page tables
Persistent storage
fork/exec
Fred Kuhns (04/28/23) CS523 – Operating Systems 40
Physical Memory• Divided into paged and non-paged regions• Paged region described by an array of
page structures, each describing one logical page (cluster of hardware pages)
• Each physical page (page frame):– described by struct page– mapped to some memory object, with the
memory object represented by a vnode– page identity or name = <vnode, offset>
Fred Kuhns (04/28/23) CS523 – Operating Systems 41
Page Struct• page struct stores offset and pointer to
corresponding vnode• may sit on several linked lists, has 3 sets of pointers
– hash table of vnode and offset– vnode contains list of all object pages currently in memory– free page list or list of pages waiting to be written to
backing store• Reference count• synchronization flags (lock, wanted, in-transit)• Copies of modified and referenced bits• HAT field used to locate all translations for this page
Fred Kuhns (04/28/23) CS523 – Operating Systems 42
AS Layer• High-level abstraction describing the
virtual address space.• References a linked list of seg (segment)
structs that represent non-overlapping page-aligned address regions
• Contains the hat structure and a hint to the last segment that had a page fault
• Supports two set of operations: those operating on the entire address space and those that affect ranges within the space.
Fred Kuhns (04/28/23) CS523 – Operating Systems 43
Segment Drivers• Segments represent mappings between backing
store and address regions• Segment represents an abstract base class with
specific drivers being derived classes.• seg struct contains pointer to
– a seg_ops vector, these represent the virtual functions. i.e. the type dependent interface to the class.