Virtual Memory - hochan222/Everything-in-JavaScript GitHub Wiki

๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ฌผ๋ฆฌ์ ์ธ ์ฃผ์†Œ๋ณ€ํ™˜์€ ์šด์˜์ฒด์ œ๊ฐ€ ๊ด€์—ฌํ•˜์ง€ ์•Š๋Š”๋‹ค.
ํ•˜์ง€๋งŒ, Virtual Memory๋Š” ์šด์˜์ฒด์ œ๊ฐ€ ๊ด€์—ฌํ•œ๋‹ค.

Demand Paging

  • ์‹ค์ œ๋กœ ํ•„์š”ํ•  ๋•Œ page๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ
    • I/O ์–‘์˜ ๊ฐ์†Œ
    • ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๊ฐ์†Œ
    • ๋น ๋ฅธ ์‘๋‹ต ์‹œ๊ฐ„
    • ๋” ๋งŽ์€ ์‚ฌ์šฉ์ž ์ˆ˜์šฉ
  • Valid / Invalid bit์˜ ์‚ฌ์šฉ
    • Invalid์˜ ์˜๋ฏธ
      • ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์ฃผ์†Œ ์˜์—ญ์ธ ๊ฒฝ์šฐ
      • ํŽ˜์ด์ง€๊ฐ€ ๋ฌผ๋ฆฌ์  ๋ฉ”๋ชจ๋ฆฌ์— ์—†๋Š” ๊ฒฝ์šฐ
    • ์ฒ˜์Œ์—๋Š” ๋ชจ๋“  page entry๊ฐ€ invalid๋กœ ์ดˆ๊ธฐํ™”
    • address translation ์‹œ์— invalid bit๊ฐ€ set๋˜์–ด์žˆ์œผ๋ฉด page fault

Memory์— ์—†๋Š” Page์˜ Page Table

Page Fault

  • invalid page๋ฅผ ์ ‘๊ทผํ•˜๋ฉด MMU๊ฐ€ trap์„ ๋ฐœ์ƒ ์‹œํ‚ด (page fault trap)
  • Kernel mode๋กœ ๋“ค์–ด๊ฐ€์„œ page fault handler๊ฐ€ invoke๋จ
  • ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ page fault๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ๋Š” ํŽ˜์ด์ง€ ํดํŠธ๋Š” ๋‚˜์ง€ ์•Š๋Š”๋‹ค 0.9xxxx

Free frame์ด ์—†๋Š” ๊ฒฝ์šฐ

OS๊ฐ€ ํ•˜๋Š” ์—ญํ• , ๋นˆํŽ˜์ด์ง€๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์–ด๋–ค ํŽ˜์ด์ง€๋ฅผ ์ซ“์•„๋‚ด๊ณ  ๋Œ€์ฒดํ• ์ง€

  • Page replacement
    • ์–ด๋–ค frame์„ ๋นผ์•—์•„์˜ฌ์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•จ
    • ๊ณง๋ฐ”๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ page๋ฅผ ์ซ“์•„๋‚ด๋Š” ๊ฒƒ์ด ์ข‹์Œ
    • ๋™์ผํ•œ ํŽ˜์ด์ง€๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ซ“๊ฒจ๋‚ฌ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์Œ
  • Replacement Algorithm
    • page-fault rate์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ‰๊ฐ€
      • ์ฃผ์–ด์ง„ page reference string์— ๋Œ€ํ•ด page fault๋ฅผ ์–ผ๋งˆ๋‚˜ ๋‚ด๋Š”์ง€ ์กฐ์‚ฌ

Optimal Algorithm

  • Min(OPT): ๊ฐ€์žฅ ๋จผ ๋ฏธ๋ž˜์— ์ฐธ์กฐ๋˜๋Š” page๋ฅผ replace
  • Page Fault๋ฅผ ๊ฐ€์žฅ ์ ๊ฒŒ ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • Offline algorithm

FIFO (First In First Out) Algorithm

  • ๋จผ์ € ๋“ค์–ด์˜จ ๊ฒƒ์„ ๋จผ์ € ๋‚ด์ซ“์Œ
  • FIFO Anomaly (Belady's Anomaly)
    • more frames => more page faults

LRU (Least Recently Used) Algorithm

  • LRU: ๊ฐ€์žฅ ์˜ค๋ž˜ ์ „์— ์ฐธ์กฐ๋œ ๊ฒƒ์„ ์ง€์›€.

LFU (Least Frequently Used) Algorithm

  • LFU: ์ฐธ์กฐ ํšŸ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ํŽ˜์ด์ง€๋ฅผ ์ง€์›€

๋‹ค์–‘ํ•œ ์บ์‰ฌ ํ™˜๊ฒฝ

  • ์บ์Š ๊ธฐ๋ฒ•
    • ํ•œ์ •๋œ ๋น ๋ฅธ ๊ณต๊ฐ„(=์บ์‰ฌ)์— ์š”์ฒญ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ ํ›„์† ์š”์ฒญ์‹œ ์บ์‰ฌ๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ์„œ๋น„์Šค ํ•˜๋Š” ๋ฐฉ์‹
    • paging system ์™ธ์—๋„ cache memory, buffer caching, Web caching๋“ฑ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ

Pageing System์—์„œ LRU, LFU ๊ฐ€๋Šฅํ•œ๊ฐ€?

์ด๋ฏธ ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์šด์˜์ฒด์ œ๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ, ์ž์ฃผ ์ฐธ์กฐ๋œ๊ฒƒ์„ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
trap(page fault)์ด ๋ฐœ์ƒํ•  ๋•Œ๋งŒ CPU ์ ์œ ๊ถŒ์ด ์šด์˜์ฒด์ œ์—๊ฒŒ ๋„˜์–ด์˜จ๋‹ค.

Clock Algorithm

  • NRU (Not Recently Used)

Page Frame์˜ Allocation

  • Allocation Problem: ๊ฐ process์— ์–ผ๋งˆ๋งŒํผ์˜ page frame์„ ํ• ๋‹นํ•ด ์ค„ ๊ฒƒ ์ธ๊ฐ€?
  • Allocation์˜ ํ•„์š”์„ฑ
    • ๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ ๋ช…๋ น์–ด ์ˆ˜ํ–‰์‹œ ๋ช…๋ น์–ด, ๋ฐ์ดํ„ฐ ๋“ฑ ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€ ๋™์‹œ ์ฐธ์กฐ
      • ๋ช…๋ น์–ด ์ˆ˜ํ–‰์„ ์œ„ํ•ด ์ตœ์†Œํ•œ ํ• ๋‹น๋˜์–ด์•ผ ํ•˜๋Š” frame์˜ ์ˆ˜๊ฐ€ ์žˆ์Œ
    • Loop๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” page๋“ค์„ ํ•œ๊บผ๋ฒˆ์— allocate ๋˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•จ
      • ์ตœ์†Œํ•œ์˜ allocation์ด ์—†์œผ๋ฉด ๋งค loop ๋งˆ๋‹ค page fault
  • Allocation Scheme
    • Equal allocation: ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์— ๋˜‘๊ฐ™์€ ๊ฐฏ์ˆ˜ ํ• ๋‹น
    • Proportional allocation: ํ”„๋กœ์„ธ์Šค ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ํ• ๋‹น
    • Priority allocation: ํ”„๋กœ์„ธ์Šค์˜ priority์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ํ• ๋‹น

Global vs Local Replacement

  • Global replacement
    • replace ์‹œ ๋‹ค๋ฅธ process์— ํ• ๋‹น๋œ frame์„ ๋นผ์•—์•„ ์˜ฌ ์ˆ˜ ์ด์‹ฟ
  • Local replacement
    • ์ž์‹ ์—๊ฒŒ ํ• ๋‹น๋œ frame ๋‚ด์—์„œ๋งŒ replacement

Trashing

  • ํ”„๋กœ์„ธ์Šค์˜ ์›ํ™œํ•œ ์ˆ˜ํ–‰์— ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ page frame ์ˆ˜๋ฅผ ํ• ๋‹น ๋ฐ›์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ๋ฐœ์ƒ
  • Page fault rate์ด ๋งค์šฐ ๋†’์•„์ง
  • CPU utilization์ด ๋‚ฎ์•„์ง
  • OS๋Š” MPD(Multiprogramming degree)๋ฅผ ๋†’์—ฌ์•ผ ํ•œ๋‹ค๊ณ  ํŒ๋‹จ
  • ๋˜ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์Šคํ…œ์— ์ถ”๊ฐ€๋จ
  • ํ”„๋กœ์„ธ์Šค ๋‹น ํ• ๋‹น๋œ frame์˜ ์ˆ˜๊ฐ€ ๋”์šฑ ๊ฐ์†Œ
  • ํ”„๋กœ์„ธ์Šค๋Š” page์˜ swap in / swap out์œผ๋กœ ๋งค์šฐ ๋ฐ”์จ
  • ๋Œ€๋ถ€๋ถ„์˜ ์‹œ๊ฐ„์— CPU๋Š” ํ•œ๊ฐ€ํ•จ
  • low throughtput

Working-Set Model

  • Locality of reference

    • ํ”„๋กœ์„ธ์Šค๋Š” ํŠน์ • ์‹œ๊ฐ„ ๋™์•ˆ ์ผ์ • ์ž์†Œ๋งŒ์„ ์ง‘์ค‘์ ์œผ๋กœ ์ฐธ์กฐํ•œ๋‹ค
    • ์ง‘์ค‘์ ์œผ๋กœ ์ฐธ์กฐ๋˜๋Š” ํ•ด๋‹น page๋“ค์˜ ์ง‘ํ•ฉ์„ locality set์ด๋ผ ํ•จ
  • Working-set Model

    • Locality์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ผ์ • ์‹œ๊ฐ„ ๋™์•ˆ ์›ํ™œํ•˜๊ฒŒ ์ˆ˜ํ–‰๋˜๊ธฐ ์œ„ํ•ด ํ•œ๊บผ๋ฒˆ์— ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ์–ด์•ผ ํ•˜๋Š” page๋“ค์˜ ์ง‘ํ•ฉ์„ Working Set์ด๋ผ ์ •์˜ํ•จ
    • Working Set ๋ชจ๋ธ์—์„œ๋Š” process์˜ working set ์ „์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ์–ด์•ผ ์ˆ˜ํ–‰๋˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ชจ๋“  frame์„ ๋ฐ˜๋‚ฉํ•œ ํ›„ swap out
    • Thrashing์„ ๋ฐฉ์ง€ํ•จ
    • Multiprogramming degree๋ฅผ ๊ฒฐ์ •ํ•จ

PFF (Page-Fault Frequency) Scheme

Page Size์˜ ๊ฒฐ์ •

  • Page size๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋ฉด

    • ํŽ˜์ด์ง€ ์ˆ˜ ์ฆ๊ฐ€
    • ํŽ˜์ด์ง€ ํ…Œ์ด๋ธ” ํฌ๊ธฐ ์ฆ๊ฐ€
    • Internal fragmentation ๊ฐ์†Œ
    • Disk transfer์˜ ํšจ์œจ์„ฑ ๊ฐ์†Œ
    • ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ๋ฉ”๋ชจ๋ฆฌ ์ด์šฉ์ด ํšจ์œจ์ 
      • Locality์˜ ํ™œ์šฉ ์ธก๋ฉด์—์„œ๋Š” ์ข‹์ง€ ์•Š์Œ
  • Trend

    • Larger page size