memory management virtual memory - institute for …ths/a3/vm/virtual_memory.pdf ·  ·...

49
April 4 2005 Part of A3 course (by Theo Schouten) Biniam Gebremichael http://www.cs.ru.nl/~biniam/ Office: A6004 Memory Management Virtual Memory

Upload: dangnhan

Post on 30-Mar-2018

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

April 4 2005

Part of A3 course (by Theo Schouten)

Biniam Gebremichaelhttp://www.cs.ru.nl/~biniam/

Office: A6004

Memory ManagementVirtual Memory

Page 2: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 3: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 4: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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!!

Page 5: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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.

Page 6: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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.

Page 7: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 8: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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!!

Page 9: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 10: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 11: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 12: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������12/49

Page Table Entries

P: page in memory?

M: Modify bit

Other control bit (OS specific) eg. Ageing ...

Page 13: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������13/49

Page Translation

• Virtual address – Page number– Offset

• Page table• Physical address

– Frame number– Offset

OffsetPage #

F10P3F11P4

F5P2F4P1

Frame numberPage number

OffsetFrame #

Page 14: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������14/49

Page Starting bit

Page 15: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 16: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 17: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 18: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������18/49

Page 19: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������19/49

Page 20: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 21: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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.

Page 22: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������22/49

Example Page Sizes

Page 23: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 24: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������24/49

Segment Table Entries

Page 25: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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)

Page 26: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������26/49

Combined Segmentation and Paging

Page 27: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������27/49

Page 28: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������28/49

Operating System Software

• Fetch Policy � Demand and Prepaging

• Placement Policy• Replacement Policy

� Algorithms

• Resident Set Management• Cleaning Policy• Load Control

Page 29: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 30: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 31: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 32: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 33: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 34: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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)

Page 35: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 36: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 37: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������37/49

Replace Page 556

It is thenext pagewith 0use-bit

Page 38: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������38/49

Page 39: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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.

Page 40: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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.

Page 41: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 42: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 43: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 44: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������44/49

UNIX and Solaris Memory Management (Data Structures)

• Paging System� Page table� Disk block descriptor� Page frame data table� Swap-use table

����������� � ����

��������������

� �����������

����������������

��������������

Page 45: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������45/49

Data Structures

�����������������

��

�����������������

�������

��������������

�����������

���������������

�����!����

�������������

��

Page 46: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������46/49

Data Structures

Page 47: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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

Page 48: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������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.

Page 49: Memory Management Virtual Memory - Institute for …ths/a3/vm/virtual_memory.pdf ·  · 2006-04-06Memory Management Virtual Memory 2/49 Content ... Transitional Lookaside Buffer

����� �������� ����������� ��������������49/49