Operating Systems CMPSC 473
Operating Systems CMPSC 473
Last class:
Memory Management
Today:
Paging
Memory Allocation
Allocation
Previously
Allocate arbitrary-sized chunks (e.g., in old days, a process)
Challenges
Fragmentation and performance
Swapping
Need to use the disk as a backing store for limited physical memory Problems
Complex to manage backing of arbitrary-sized objects May want to work with subset of process (later)
Programs are provided with a virtual address space (say 1 MB). Role of the OS to fetch data from either physical memory or disk.
Done by a mechanism called (demand) paging.
Divide the virtual address space into units called virtual pages each of which is of a fixed size (usually 4K or 8K).
For example, 1M virtual address space has 256 4K pages.
Role of the OS to keep track of which virtual page is in physical memory and if so where?
Maintained in a data structure called page-table that the OS builds. Page-tables map Virtual-to-Physical addresses.
Page Tables
Virtual Address Virtual Page # Offset in Page
VP # vp1 vpn
PP # pp1
Present
ppn
Physical Address
Physical Page #
Offset in Page
Paging Example
Free Frames
Before allocation
After allocation
Fragmentation
External Fragmentation total memory space exists to satisfy a request, but it is not contiguous Internal Fragmentation allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Reduce external fragmentation by compaction
Shuffle memory contents to place all free memory together in one large block Compaction is possible only if relocation is dynamic, and is done at execution time
Internal Fragmentation
Partitioned allocation may result in very small fragments
Assume allocation of 126 bytes Use 128 byte block, but 2 bytes left over
Maintaining a 2-byte fragment is not worth it, so just allocate all 128 bytes
But, 2 bytes are unusable Called internal fragmentation
Non-contiguous Allocation
Internal Fragmentation
Wasted
Disable caching.
Useful for I/O devices that are memory-mapped.
Issues to Address
Size of page-tables would be very large! For example, 32-bit virtual address spaces (4 GB) and a 4 KB page size would have ~1 M pages/entries in page-tables. What about 64-bit virtual address spaces?! A process does not access all of its address space at once! Exploit this locality factor. Use multi-level page-tables. Equivalent to paging the pagetables. Inverted page-tables.
Page Tables
Page dir
Page table
Page offset
10
10
12
Code Data
Stack
Page Directory
For example on SPARC, we have the following 3level scheme, 8-bit index1, 6 bit index2, 6 bit index3, 12 bit offset. Note that only the starting location of the 1st-level indexing table needs to be fixed. Why?
The MMU hardware needs to lookup the mapping from the page-table.
Exercise: Find out the paging configuration of your favorite hardware platform!
Page-table lookup needs to be done on every memory-reference for both code & data!
Can be very expensive if this is done by software.
Requirements
Address translation/mapping must be very fast! Why?
Because it is done on every instruction fetch, memory reference instruction (loads/stores). Hence, it is in the critical path.
Previous mechanisms access memory to lookup the page-tables. Hence it is very slow!
CPU-Memory gap is ever widening!
Spatial locality
Likelihood of neighboring locations being accessed in the near future.
Typically, TLB is a cache for a few (8/16/32) Pagetable entries. Given a virtual address, check this cache to see if the mapping is present, and if so we return the physical address. If not present, the MMU attempts the usual address translation. TLB is usually designed as a fully-associative cache. TLB entry has
Used/unused bits, virtual page number, Modified bit, Protection bits, physical page number.
Fraction of references that can be satisfied by TLB is called hit-ratio(h). For example, if it takes 100 nsec to access page-table entry and 20 nsec to access TLB,
average lookup time = 20 * h + 100 * ( 1 h).
Inverted Page-tables
Page-tables could become quite large! Above mechanisms pages the page-tables and uses TLBs to take advantage of locality. Inverted page-tables organize the translation mechanism around physical memory. Each entry associates a physical page with the virtual page stored there!
Size of Inverted Page-table = Physical Memory size / Page size.
Offset in Page
If VP# is present, then PP# is available.
Segment Ptr leads you to a page table, which you then index using the offset in segment. This gives you physical frame #. You then use page offset to index this page. Virtual address = (Segment #, Page #, Page Offset)
Summary
Paging
Non-contiguous allocation Pages and frames Fragmentation Page tables Hardware support Segmentation