memory management virtual memory - institute for …ths/a3/vm/virtual_memory.pdf · ·...
Post on 30-Mar-2018
229 Views
Preview:
TRANSCRIPT
April 4 2005
Part of A3 course (by Theo Schouten)
Biniam Gebremichaelhttp://www.cs.ru.nl/~biniam/
Office: A6004
Memory ManagementVirtual Memory
����� �������� ����������� ��������������2/49
Content• Virtual memory
� Definition� Advantage and challenges
• Virtual memory Paging� Page table� Transitional Lookaside Buffer (TLB)
• Virtual memory Segmentation• Fetch policy• Replacement policy• Unix/Linux/Windows memory management
����� �������� ����������� ��������������3/49
From Last Week
• Fixed and dynamic partition• Paging and Segmentation• Internal and External fragmentation
SegmentationDynamic PartitonDynamic
PagingFixed PartitionFixed
Also Process partition
Memory partition
����� �������� ����������� ��������������4/49
From last week• Logical vs Physical address.
– Relocation allowed: A process may be swapped in and out of main memory such that it occupies different regions.
• A process can be broken up into pieces • Contiguously not necessary
– All pieces of a process do not need to beloaded in main memory during execution.
All pieces of a process do not have to be in the main memory during execution!!
����� �������� ����������� ��������������5/49
Virtual Memory
• Keep only the active pieces (pages or segments) of the process in main memory.
• Inactive processes are kept in the secondary memory (hard disk).
• Advantages� More processes can be maintained in the main
memory, increases effective use of CPU.� The main memory can execute a process bigger
than its own capacity.
����� �������� ����������� ��������������6/49
Challenges• Who is active and who is inactive.
� Bad swapping results in Trashing.� The heuristic “principle of locality”
• Expensive interrupt operation to retrieve inactive page/segment.
• Management complexity• Extra hardware support needed
The improvement gained by virtual memory is worth the difficulty introduced by it.
����� �������� ����������� ��������������7/49
Thrashing
• Swapping out a piece of a process just before that piece is needed
• The processor spends most of its time swapping pieces rather than executing user instructions
P3Q3Q1P2Q2P1
P1 P2 P3 P4 P1 P2 P3 P4 P1P1 P2 P3 P4 P1 P2
P1 P2 P3 P4 P1 P2 P3 P4 P1P3 P2
����� �������� ����������� ��������������8/49
Principle of Locality
• Program and data references within a process tend to cluster
• Only a few pieces of a process will be needed over a short period of time
• Possible to make intelligent guesses about which pieces will be needed in the future
• In practice virual memory works!!
����� �������� ����������� ��������������9/49
Support Needed forVirtual Memory• Hardware must support paging and
segmentation • Operating system must be able to
management the movement of pages and/or segments between secondary memory and main memory
����� �������� ����������� ��������������10/49
Paging and Page Tables
• Each process has its own page table• Each page table entry contains the
frame number of the corresponding page in main memory
• A bit is needed to indicate whether the page is in main memory or not
����� �������� ����������� ��������������11/49
Modify Bit in Page Table
• Another modify bit is needed to indicate if the page has been altered since it was last loaded into main memory
• If no change has been made, the page does not have to be written to the diskwhen it needs to be swapped out
����� �������� ����������� ��������������12/49
Page Table Entries
P: page in memory?
M: Modify bit
Other control bit (OS specific) eg. Ageing ...
����� �������� ����������� ��������������13/49
Page Translation
• Virtual address – Page number– Offset
• Page table• Physical address
– Frame number– Offset
OffsetPage #
F10P3F11P4
F5P2F4P1
Frame numberPage number
OffsetFrame #
����� �������� ����������� ��������������14/49
Page Starting bit
����� �������� ����������� ��������������15/49
Page Tables
• The entire page table may take too much main memory space. Specially:– With small size pages– in 32-bit or higher addressing.– 8000 entries in Windows2000
• Solution: Store Page tables also invirtual memory
• When a process is running, part of its page table is in main memory
����� �������� ����������� ��������������16/49
Translation Lookaside Buffer• Each virtual memory reference
can cause two physical memory accesses– one to fetch the page table– one to fetch the data
• To overcome this problem a high-speed cache is set up for page table entries– called the TLB - Translation
Lookaside Buffer
TLB
Main memory
Secondary memory
����� �������� ����������� ��������������17/49
Translation Lookaside Buffer
• Contains page table entries that have been most recently used
• TLB hit: page number in TLB• TLB miss: page number in memory• TLB page fault: page number in secondary
memory. First load table and do like TLB miss
• Example: memory cache, proxy
����� �������� ����������� ��������������18/49
����� �������� ����������� ��������������19/49
����� �������� ����������� ��������������20/49
Page Size
moreFewPages in VM
smallLargePage table
Page importing
Internal fragmentation
Importing a page costs almost the same regardless of its size
largeSmall
Large page size
Small page size
����� �������� ����������� ��������������21/49
Page Size• Memory size and CPU speed grows, but TLB
can not grow at the same rate. Complicate OS operations.
• Multiple (unequal) page sizes provide the flexibility needed to effectively use a TLB
• Example:� Large pages can be used for program instructions� Small pages can be used for threads
• Most operating system support only one page size.
����� �������� ����������� ��������������22/49
Example Page Sizes
����� �������� ����������� ��������������23/49
BREAKSegmentation• Dynamic size• Simplifies handling of growing data
structures• Process partitioning is easier.
� Allows programs to be altered and recompiled independently
� Sharing data among processes is easier� Efficient protection
����� �������� ����������� ��������������24/49
Segment Table Entries
����� �������� ����������� ��������������25/49
Combined Paging and Segmentation• Paging is transparent to the programmer• Paging eliminates external fragmentation• Equal page size, good for replacement policy• Segmentation is visible to the programmer• Segmentation allows for growing data
structures, modularity, and support for sharing and protection
• Each segment is broken into fixed-size pages (paging after segmentation)
����� �������� ����������� ��������������26/49
Combined Segmentation and Paging
����� �������� ����������� ��������������27/49
����� �������� ����������� ��������������28/49
Operating System Software
• Fetch Policy � Demand and Prepaging
• Placement Policy• Replacement Policy
� Algorithms
• Resident Set Management• Cleaning Policy• Load Control
����� �������� ����������� ��������������29/49
Fetch PolicyFetch Policy: Determines when a page
should be brought into memory� Demand paging only brings pages into main
memory when a reference is made to a location on the page
• Many page faults when process first started
� Prepaging brings in more pages than needed• More efficient to bring in pages that reside
contiguously on the disk. • Exploit disk rotation latency…
� Swapping follows the same
����� �������� ����������� ��������������30/49
Placement Policy
• Where is a page placed?
• First-, worst-, best-fit algorithm.
• Distributed or Parallel computing with several processors accessing the same data. Placement is a serious design issue
����� �������� ����������� ��������������31/49
Replacement Policy
• Issues to consider� Avoid trashing� How many should be replaced (prepaging?)� Should a process replace its own page or not?� Which one should be served first
• Page removed should be the page least likely to be referenced in the near future
• Most policies predict the future behavior on the basis of past behavior
����� �������� ����������� ��������������32/49
Replacement Policy
• Frame Locking� If frame is locked, it may not be replaced� Example
• Kernel of the operating system• Control structures• I/O buffers
� Associate a lock bit with each frame
����� �������� ����������� ��������������33/49
Basic Replacement Algorithms• Optimal policy
� Selects for replacement that page for which the time to the next reference is the longest
� Impossible to have perfect knowledge of future events
P3Q3Q1P2Q2P1
P1 P2 P3 P4 P1 P2 P3 P4 P1P1 P2 P3 P4 P1 P2
P1 P2 P3 P4 P1 P2 P3 P4 P1P3 P2
����� �������� ����������� ��������������34/49
Basic Replacement Algorithms• Least Recently Used (LRU)
� Replaces the page that has not been referenced for the longest time
� By the principle of locality, this should be the page least likely to be referenced in the near future
� Each page could be tagged with the time of last reference (aging). This would require a great deal of overhead. (aging is not a bit)
����� �������� ����������� ��������������35/49
Basic Replacement Algorithms• First-in, first-out (FIFO)
� Treats page frames allocated to a process as a circular buffer
� Pages are removed in round-robin style� Simplest replacement policy to implement� Page that has been in memory the longest is
replaced� These pages may be needed again very soon
����� �������� ����������� ��������������36/49
Basic Replacement Algorithms• Clock Policy
� Additional bit called a use bit � When a page is first loaded in memory, the use bit
is set to 0� When the page is referenced, the use bit is set to 1� When it is time to replace a page, the first frame
encountered with the use bit set to 0 is replaced.� During the search for replacement, each use bit set
to 1 is changed to 0
����� �������� ����������� ��������������37/49
Replace Page 556
It is thenext pagewith 0use-bit
����� �������� ����������� ��������������38/49
����� �������� ����������� ��������������39/49
Resident Set Size
• Fixed-allocation� gives a process a fixed number of pages
within which to execute� when a page fault occurs, one of the pages
of that process must be replaced
• Variable-allocation� number of pages allocated to a process
varies over the lifetime of the process� Local and Global scope.
����� �������� ����������� ��������������40/49
Variable Allocation,Global Scope• Easiest to implement• Adopted by many operating systems• Operating system keeps list of free
frames• Free frame is added to resident set of
process when a page fault occurs• If no free frame, replaces one from
another process depending on page faults.
����� �������� ����������� ��������������41/49
Variable Allocation,Local Scope• When new process added, allocate
number of page frames based on � application type, � program request, or � other criteria
• When page fault occurs, select page from among the resident set of the process that suffers the fault
• Reevaluate allocation from time to time
����� �������� ����������� ��������������42/49
Cleaning Policy
• Demand cleaning� a page is written out only when it has been
selected for replacement
• Precleaning� pages are written out in batches
• Compare with fetch policy� Demand paging, and� Prepaging
����� �������� ����������� ��������������43/49
Cleaning Policy
• Best approach uses page buffering� Replaced pages are placed in two lists
• Modified and unmodified
� Pages in the modified list are periodically written out in batches later, to save time
• writing to disk is time consuming.
� Pages in the unmodified list are either reclaimed if referenced again or lost when its frame is assigned to another page
����� �������� ����������� ��������������44/49
UNIX and Solaris Memory Management (Data Structures)
• Paging System� Page table� Disk block descriptor� Page frame data table� Swap-use table
����������� � ����
�
��������������
� �����������
����������������
��������������
����� �������� ����������� ��������������45/49
Data Structures
�����������������
��
�����������������
�������
��������������
�����������
���������������
�����!����
�������������
��
����� �������� ����������� ��������������46/49
Data Structures
����� �������� ����������� ��������������47/49
UNIX and Solaris Memory Management• Page Replacement
� refinement of the clock policy
• Kernel Memory Allocator� Uses different replacement policy for kernel� Refinement of buddy system� most blocks are smaller than a typical page
size
����� �������� ����������� ��������������48/49
Windows 2000Memory Management
• 4 GB of virtual memory� 2 OS 2 user space� 1 OS 3 user space for servers
• W2K Paging� Available: page not used. Available for use� Reserved: page reserved for future use, but
not yet used.� Committed: page set aside for OS.
����� �������� ����������� ��������������49/49
top related