[OS] Virtual Memory

seunghyunΒ·2023λ…„ 5μ›” 12일
0

πŸ’»

λͺ©λ‘ 보기
7/15

Introduction

πŸ’‘ Virtual Memory

  • Programmer's view of memory
  • Each process has its own virtual memory
  • A linear array of bytes addressed by the virtual address

βœ”οΈ 가상 λ©”λͺ¨λ¦¬λ₯Ό μ™œ μ‚¬μš©ν•˜λŠ”κ°€?

  • νŒŒν‹°μ…˜ 크기에 따라 overlay μˆ˜μ •μ„ ν•  ν•„μš”μ—†μ΄ OSκ°€ μ±…μž„μ Έμ€˜μ„œ ν”„λ‘œκ·Έλž˜λ°μ„ ν•˜κΈ° 쉽닀.
  • λ‹€ 올라올 ν•„μš”μ—†μ΄ ν”„λ‘œμ„ΈμŠ€ μ΄λ―Έμ§€μ˜ μΌλΆ€λ§Œ μ˜¬λΌμ™€μ„œ μ‹€ν–‰ν•œλ‹€.
  • 가상 λ©”λͺ¨λ¦¬λŠ” μΆ©λΆ„νžˆ 크게 μž‘ν˜€μ Έμžˆμ–΄μ„œ ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” λ©”λͺ¨λ¦¬κ°€ (거의) λ¬΄ν•œν•˜λ‹€κ³  생각할 수 μžˆλ‹€.

πŸ’‘ Physical Memory

  • Machine's physical memory (DRAM)
    • Caches are parts of physical memory
  • Also called main memory

βœ”οΈ Virtual address : The address of a program λͺ…λ Ήμ–΄λ‚˜ λ°μ΄ν„°μ˜ μ£Όμ†Œ

βœ”οΈ Physical address : The address of a DRAM

βœ”οΈ Page table : OS κ°€ κ΄€λ¦¬ν•˜λŠ” 데이터 ꡬ쑰

βœ”οΈ TLB (MMU) : Virtual page number λ₯Ό Physical Page Number 둜 λ°”κΎΈλŠ” 것이 Translate 이라고 ν•œλ‹€. Page table 의 entry 듀을 Caching ν•œ ν•˜λ“œμ›¨μ–΄. νŠΉμˆ˜ν•œ entry λ₯Ό μ œμ™Έν•˜κ³ λŠ” λŒ€κ°œ μ»¨ν…μŠ€νŠΈ μŠ€μœ„μΉ­λ  λ•Œλ§ˆλ‹€ flush λ˜μ–΄μ•Ό ν•œλ‹€. 이것이 μ—†λ‹€λ©΄ 맀번 CPU κ°€ exception 이 κ±Έλ €μ„œ OS κ°€ μ°Ύμ•„μ™€μ•Όν•œλ‹€.. λ„ˆλ¬΄ λŠλ €μ§„λ‹€!


Virtual Memory

Functions

  • Large address space
    • Easy to program
    • Provide the illusion of infinite amount of memory
    • Program (including both code and data) can exceed the main memory capacity
    • Processes partially resident in memory
  • Protection
    • Access rights: read modify/ execute permission
      • Each segment/page has its own access rights
    • Privilege level
  • Sharing
  • Portability
  • Increased CPU utilization
    • More programs can run at the same time

Require the following functions

  • Memory allocation (Placement)
    • Any virtual page can be placed in any page frame
  • Memory deallocation (Replacement)
    • LRU, Clock algorithm
  • Memory mapping (Translation)
    • Virtual address must be translated to physical address to access main memory and caches

memory management

  • Automatic movement of data between main memory and secondary storage
    • Done by OS with the help of processor HW (TLB)
    • Main memory contains only the most frequently used portions of a process's address space
  • Illusion of infinite memory(size of secondary storage) but access time is equal to main memory
  • Usually use demand paging (cache λŠ” 눈치빠λ₯΄κ²Œ PreFetching)
    • Bring a page on demand

Paging

  • Divide address space into fixed size pages

    • VA consists of VPN, offset
    • PA consists of PPN, offset
  • Map a virtual page to a physical page frame at runtime

  • Each process has its own page table

    • The page table contains mapping between VPN and PPN
    • VPN is used as an index into the page table
  • Page Table Entry(PTN) contains

    • PPN (Physical Page Number or Frame Number)
    • Presence bit : 1 if this page is in main memory
    • Modified bit : 1 if this page has been modified
    • Reference bits : reference statistics into used for page replacement
    • Access control : read/write/execute permissions
    • Privilege level : user-level page versus system-level page
    • Disk Address

Page table organization

  • Linear : one PTE per virtual page

  • Hierarchical : tree structured page table

    • Page Directory πŸ‘‰ Page Table πŸ‘‰ Virtual Memory
  • Inverted : PTEs for only pages in main memory ν”„λ‘œμ„ΈμŠ€ μˆ˜μ— 상관없이 λ©”λͺ¨λ¦¬μ— μ˜¬λΌμ™€μžˆλŠ” νŽ˜μ΄μ§€μ— λŒ€ν•΄μ„œλ§Œ κ΄€λ¦¬ν•˜κ² λ‹€. frame 에 따라 κ΄€λ¦¬ν•˜κ² λ‹€.

    • Hashing 으둜 νŽ˜μ΄μ§€μ™€ ν”„λ ˆμž„μ„ 짝지어쀀닀. Hash collision 이 λ‚˜λ©΄ chain 으둜 κ·Έ λ‹€μŒ 것을 μ—°κ²°ν•œλ‹€
  • Page table entries are dynamically allocated as needed

TLB (MMU; Memory Management Unit)

TLB 도 μΊμ‹œμ΄λ―€λ‘œ TLB miss, hit 이 μžˆλ‹€
이것이 CPU 내에 μžˆμ–΄μ•Όλ§Œ 가상메λͺ¨λ¦¬λ₯Ό 지원할 수 μžˆλ‹€

βœ”οΈ Translation Lookaside Buffer

  • Cache of page table entries (PTEs)
  • On TLB hit, can do virtual to physical translation without accessing the page table
  • On TLB miss (Exception), must search the page table for the missing entry

Page fault

πŸ’‘ But what happens if the process tries to access a page that was not brought into memory? Access to a page marked invalid causes a page fault.

The paging hardware, in translating the address through the page table, will notice that the invalid bit is set, causing a trap to the operating system. This trap is the result of the operating system’s failure to bring the desired page into memory. The procedure for handling this page fault is straightforward (Figure 10.5):

  1. We check an internal table (usually kept with the process control block) for this process to determine whether the reference was a valid or an invalid memory access.
  2. If the reference was invalid, we terminate the process. If it was valid but we have not yet brought in that page, we now page it in.
  3. We find a free frame (by taking one from the free-frame list, for example)
  4. We schedule a secondary storage operation to read the desired page into
    the newly allocated frame.
  5. When the storage read is complete, we modify the internal table kept with the process and the page table to indicate that the page is now in memory.
  6. We restart the instruction that was interrupted by the trap. The process can now access the page as though it had always been in memory.

Page Size

μ»€μ§€λŠ” μΆ”μ„Έλ‹€. κ°€μ Έμ˜€λŠ” μ˜€λ²„ν—€λ“œλ₯Ό 쀄이기 μœ„ν•΄μ„œ.

  • The smaller the page size, the lesser the amount of internal fragmnentation
    • However, more pages are required per process
    • More pages per process means larger page tables
      • For large programs, portion of the page tables of active processes must be in secondary storage instead of main memory

Segmentation

βœ”οΈ Segmentation allows a programmer to view a virtual memory as a collection of sgments

βœ”οΈ Advantages

  • Simplify the handling of growing data structures
  • Facilitate sharing among processes

βœ”οΈ Segment Table Entry Contains

  • Base address of the corresponding segment in main memory (DRAM μ—μ„œ μ‹œμž‘ν•˜λŠ” μ£Όμ†Œ)
  • Length of the segment (λ²”μœ„)
  • Presence bit - 1 if this segment is in main memory
  • Modified bit - 1 if this segment has been modified

Virtual Memory Policies

πŸ’‘ Key issues: Performance

  • Minimize page faults

Fetch Policy

βœ”οΈ Demand paging

  • Bring a page into main memory only on a page miss
  • Generate many page faults when process is first started
    • Principle of locality suggests that as more and more pages are brought in, most future references will be to pages that have recently been brought in. and page fault should drop to a very low level

βœ”οΈ Prepaging

  • Pages other than the one demanded by a page fault are brought in
  • If pages of a process are stored contiquously in secondary memory it is more efficient to bring in a number of pages at one time
  • Ineffective if extra pages are not referenced

Frame Locking

βœ”οΈ Frame Locking

  • When a frame is locked, the page currently stored in that frame should not be replaced
    • OS kernel and key control structures are locked
    • I/O buffers and time-critical areas may be locked
    • Locking is achieved by associating a lock bit with each frame

Replacement Algorithms

βœ”οΈ Optimal

  • Select the page for which the time to the next reference is the longest

βœ”οΈ LRU

  • Select the page that has not been referenced for the longest time

βœ”οΈ FIFO

  • Page that has been in memory the longest is replaced

βœ”οΈ Clock

  • Associate a use bit with each frames
  • When a page is first loaded or referenced, the use bit is set to
  • Any frame with a use bit of 1 is passed over by the algorithm
  • Page frames visualized as laid out in a circle

Page Buffering

βœ”οΈ When a page is replaced, the page is not physically moved

  • Instead, the PTE for this page is removed and placed in either the free or modified page list
  • Free page list is a list of page frames available for reading in pages
    • When a page is to be replaced, it remains in memory and its page frame is added to the tail of the free page list
    • Thus, if the process reference the page, it is returned to the resident set of that process at little cost
    • When a page is to be read in, the page frame at the head of the list is used. destroying the page that was there
  • Modified page list is a list of page frames that have been modified
    • When a modified is replaced, the page frame is added to the tail of the modified page list
    • Modified pages are written out in clusters rather than one at a timer significantly reducing the number of I/O operation

Page Fault Frequency (PFF)

PF κ°€ λ„ˆλ¬΄ 자주 λ°œμƒν•˜λ©΄ working set 을 늘렀주고 PF κ°€ λ„ˆλ¬΄ 가끔 λ°œμƒν•˜λ©΄ working set 을 쀄여쀀닀.

βœ”οΈ Requires a use bit to be associated with each page in memory

  • Bit is set to 1 when that page is accessed
  • When a page fault occurs, the OS notes the virtual time since the last page fault for that process
  • If the amount of time since the last page fault is less than a threshold (μž„κ³„μ ), then a page is added to the working set of the process
  • Otherwise, discard all pages with a use bit of 0, and shrink the working set accordingly. At the same time reset the use bit on the remaining pages
  • The strategy can be refined by using 2 thresholds: An upper threshold is used to trigger a growth in the working size while a lower threshold is used to trigger a shrink in the working set size

βœ”οΈ Does not perform well during the transient periods when there is a shift to a new locality


Multiprogramming

process μˆ˜κ°€ λ„ˆλ¬΄ λ§Žκ±°λ‚˜ 적으면 λ¬Έμ œλ‹€!

ν•˜λ‚˜μ˜ process μˆ˜κ°€ λ„ˆλ¬΄ 많으면 working set 이 쀄어든닀. πŸ‘‰ ν•œ process μ•ˆμ—μ„œ page fault κ°€ λ°˜λ³΅λœλ‹€ πŸ‘‰ thrashing

βœ”οΈ Load Control

  • Determine the number of processes that will be resident in main memory
  • This number is called multiprogramming level

βœ”οΈ Too few processes lead to swapping

  • Since all the processes can be blocked

βœ”οΈ Too many processes, lead to insufficient working set size, resulting in thrashing (frequent faults)


Process Suspension

βœ”οΈ If the degree of multiprogramming is to be reduced,

  • One or more of the currently resident processes must be swapped out

βœ”οΈ Six Possibilities

  • Lowest-priority process
  • Faulting process
  • Last process activated
  • Process with the smallest working set
  • Largest process
  • Process with the largest remaining execution window

πŸ”— Reference

[KUOCW] 졜린 κ΅μˆ˜λ‹˜μ˜ 운영체제 κ°•μ˜λ₯Ό μˆ˜κ°•ν•˜κ³  μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€. 잘λͺ»λœ λ‚΄μš©μ΄ μžˆλ‹€λ©΄ λŒ“κΈ€λ‘œ μ•Œλ €μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€ 😊

profile
dododo

0개의 λŒ“κΈ€