๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป CS/์šด์˜์ฒด์ œ

[์šด์˜์ฒด์ œ] 9. Virtual Memory

by nitronium102 2023. 1. 22.

๋ฌผ๋ฆฌ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๋ณ€ํ™”๋Š” OS๊ฐ€ ๊ด€์—ฌํ•˜์ง€ ์•Š์ง€๋งŒ Virtual Memory ๋ณ€ํ™”๋Š” OS๊ฐ€ ๊ด€์—ฌํ•œ๋‹ค!

 

* ๋Œ€๋ถ€๋ถ„ paging ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •

Demand Paging

์š”์ฒญ์ด ์žˆ์œผ๋ฉด ๊ทธ ๋•Œ page๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ

- I/O ์–‘์˜ ๊ฐ์†Œ (ํ•œ์ •๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋” ์˜๋ฏธ์žˆ๋Š” ์ •๋ณด๋กœ ์ฑ„์šธ ์ˆ˜ ์žˆ์Œ)

- Memory ์‚ฌ์šฉ๋Ÿ‰ ๊ฐ์†Œ

- ๋น ๋ฅธ ์‘๋‹ต ์‹œ๊ฐ„

- ๋” ๋งŽ์€ ์‚ฌ์šฉ์ž ์ˆ˜์šฉ

 

invalidํ•œ page๋Š” disk(backing store)์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š”๋‹ค

 

Valid / Invalid bit์˜ ์‚ฌ์šฉ

* invalid๋ž€?

- ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์ฃผ์†Œ ์˜์—ญ์ธ ๊ฒฝ์šฐ

- ํŽ˜์ด์ง€๊ฐ€ ๋ฌผ๋ฆฌ์  ๋ฉ”๋ชจ๋ฆฌ์— ์—†๋Š” ๊ฒฝ์šฐ

 

- ์ฒ˜์Œ์—๋Š” ๋ชจ๋“  page๊ฐ€ invalid bit๋กœ ์ดˆ๊ธฐํ™”

- address translation ์‹œ์— invalid bit์ด set๋˜์–ด ์žˆ์œผ๋ฉด page fault 

-> [trap] CPU๊ฐ€ OS๋กœ ๋„˜์–ด๊ฐ

-> [page handler] OS๊ฐ€ fault๋‚œ page๋ฅผ physical memory์— ์˜ฌ๋ฆผ

 

Page Fault

1) invalid page ์ ‘๊ทผ > MMU๊ฐ€ trap ๋ฐœ์ƒ(page fault trap)

2) kernel mode ์ง„์ž… > page fault handler ์ž‘๋™

3) page fault ์ฒ˜๋ฆฌ

(1) invalid reference ์ฒดํฌ (bad address, protection violation)

-> invalidํ•˜๋ฉด process abort

(2) empty page frame ํ• ๋‹นํ•˜๊ธฐ > ๋งŒ์•ฝ ๋นˆ ๊ณณ ์—†์œผ๋ฉด ์ซ“์•„๋ƒ„(replace)

(3) ํ•ด๋‹น page๋ฅผ disk -> memory๋กœ ์ฝ์–ด์˜ด

- disk I/O ๋๋‚  ๋•Œ๊นŒ์ง€ CPU preempt(block)

- page table entry ๊ธฐ๋ก / valid bit ํ‘œ์‹œ

- ready queue์— process ๋„ฃ๊ธฐ -> ๋‚˜์ค‘์— dispatch

(4) ํ•ด๋‹น process๊ฐ€ CPU๋ฅผ ์žก๊ณ  running

(5) ์ค‘๋‹จ๋˜์—ˆ๋˜ instruction ์žฌ๊ฐœ

 

 

Performance of Demand Paging

 

Page replacement

3-2)์—์„œ ๋น„์–ด์žˆ๋Š” frame์ด ์—†๋Š” ๊ฒฝ์šฐ ์–ด๋–ค frame์„ ๋นผ์•—์•„ ์˜ฌ ์ง€ ๊ฒฐ์ •ํ•ด์•ผ ํ•จ

- ๊ณง๋ฐ”๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ page๋ฅผ ์ซ“์•„๋‚ด๋Š” ๊ฒƒ์ด ์ข‹์Œ

- ๋™์ผํ•œ page๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ซ“๊ฒจ๋‚ฌ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ์Œ

 

Replacement Algorithm

- page-fault rate๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ > ์ฃผ์–ด์ง„ page reference string์— ๋Œ€ํ•ด page fault๋ฅผ ์–ผ๋งˆ๋‚˜ ๋‚ด๋Š”์ง€ ์กฐ์‚ฌ

 

 

Offline - Optimal Algorithm

* ๋ฏธ๋ž˜์˜ ์ฐธ์กฐ๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋Š” ๊ฐ€์ • ํ•˜์— ์ง„ํ–‰(offline algo) -> ์‹ค์ œ๋กœ๋Š” ์‚ฌ์šฉ X

MIN(OPT) : ๊ฐ€์žฅ ๋จผ ๋ฏธ๋ž˜์— ์ฐธ์กฐ๋˜๋Š” page๋ฅผ replace

 

* ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์„ฑ๋Šฅ์— ๋Œ€ํ•œ upper bound ์ œ๊ณต(Belady's optimal algorithm, MIN, OPT ๋“ฑ์œผ๋กœ ๋ถˆ๋ฆผ)

 

FIFO Algorithm

FIFO anomaly(belady's anomaly) : more frames != less page faults

๋จผ์ € ๋“ค์–ด์˜จ ๊ฒƒ์„ ๋จผ์ € ๋‚ด์ซ“์Œ

 

LRU (Least Recently Used) Algorithm

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

 

LFU (Least Frequently Used) Algorithm

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

์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ 1) ๋žœ๋ค 2) ๊ฐ€์žฅ ์˜ค๋ž˜์ „์— ์ฐธ์กฐ๋œ page ์ง€์šด๋‹ค

 

* ์žฅ์ - LRU์ฒ˜๋Ÿผ ์ง์ „ ์ฐธ์กฐ ์‹œ์ ๋งŒ ๋ณด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์žฅ๊ธฐ์ ์ธ ์‹œ๊ฐ„ ๊ทœ๋ชจ๋ฅผ ๋ณด๊ธฐ ๋•Œ๋ฌธ์— page์˜ ์ธ๊ธฐ๋„๋ฅผ ์ข€ ๋” ์ •ํ™•ํžˆ ๋ฐ˜์˜

 

* ๋‹จ์ - ์ฐธ์กฐ ์‹œ์ ์˜ ์ตœ๊ทผ์„ฑ์„ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•จ

- LRU ๋ณด๋‹ค ๊ตฌํ˜„์ด ๋ณต์žกํ•จ

 

๋‹ค์–‘ํ•œ Caching ํ™˜๊ฒฝ

ํ•œ์ •๋œ ๋น ๋ฅธ ๊ณต๊ฐ„(cache)์— ์š”์ฒญ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ ํ›„์† ์š”์ฒญ ์‹œ cache๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ์„œ๋น„์Šคํ•˜๋Š” ๋ฐฉ์‹

- paging system, cache memory, buffer caching, web caching ๋“ฑ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ ์‚ฌ์šฉ

 

* replacement algorithm์˜ ์‹œ๊ฐ„ ์ œ์•ฝ

- buffer / web caching : O(1) ~ O(logn)๊นŒ์ง€ ํ—ˆ์šฉ

- paging system

1) page hit : ์ฃผ์†Œ ๋ณ€ํ™˜ ๊ณผ์ •์—์„œ OS๊ฐ€ ๊ด€์—ฌํ•˜์ง€ ์•Š์Œ (OS๊ฐ€ ์ ‘๊ทผ ์‹œ๊ฐ„ ์•Œ ์ˆ˜ ์—†์Œ -> LRU, LFU ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ)

2) page fault : I/O ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— OS์—๊ฒŒ๋กœ CPU ์ œ์–ด๊ถŒ ๋„˜์–ด์˜ด

 

Clock Algorithm

LRU์˜ ๊ทผ์‚ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Second change algorithm / NUR not used recently / NRU not recently used

1) reference bit์„ ์‚ฌ์šฉํ•ด์„œ ๊ต์ฒด ๋Œ€์ƒ ํŽ˜์ด์ง€ ์„ ์ •(circular list)

2) reference bit์ด 0์ธ ๊ฒƒ์„ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ํฌ์ธํ„ฐ ์•ž์œผ๋กœ ์ด๋™

3) ์ด๋™ ์ค‘ reference bit 1์€ ๋ชจ๋‘ 0์œผ๋กœ ๋ณ€๊ฒฝ

4) ํ•œ ๋ฐ”ํ€ด ๋˜๋Œ์•„์™€์„œ๋„(second chance) 0์ด๋ฉด ๊ทธ๋•Œ๋Š” replace

5) ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” page๋ผ๋ฉด second chance ์‹œ 1์ผ ๊ฒƒ (HW)

 

* ๊ฐœ์„ 

modified bit์ด 0์ธ ๊ฒƒ์„ ์šฐ์„ ํ•ด์„œ ์ซ“์•„๋ƒ„

- reference bit์™€ modified bit(dirty bit)์„ ํ•จ๊ป˜ ์‚ฌ์šฉ

- reference bit = 1 : ์ตœ๊ทผ์— ์ฐธ์กฐ๋œ ํŽ˜์ด์ง€

- modified bit = 1 : ์ตœ๊ทผ์— ๋ณ€๊ฒฝ๋œ ํŽ˜์ด์ง€(I/O ๋™๋ฐ˜) -> ์ซ“์•„๋‚ด๊ธฐ ์ „์— backing store์— ๋ฐ˜์˜ํ•ด์•ผ ํ•จ

 

Page frame allocation

๊ฐ process์— ์–ผ๋งˆ๋งŒํผ์˜ page frame์„ ํ• ๋‹นํ•  ๊ฒƒ์ธ๊ฐ€?

1) ๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ ๋ช…๋ น์–ด ์ˆ˜ํ–‰ ์‹œ ์ตœ์†Œํ•œ ํ• ๋‹น๋˜์–ด์•ผ ํ•˜๋Š” frame์˜ ์ˆ˜๊ฐ€ ์žˆ์Œ (๋ช…๋ น์–ด, ๋ฐ์ดํ„ฐ ๋“ฑ ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€ ๋™์‹œ ์ฐธ์กฐ)

2) loop๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” page๋“ค์€ ํ•œ๊บผ๋ฒˆ์— allocate๋˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•จ

 

Allocation scheme

1) Equal allocation : ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์— ๋˜‘๊ฐ™์€ ๊ฐœ์ˆ˜ ํ• ๋‹น

2) Proportional allocation : ํ”„๋กœ์„ธ์Šค ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ํ• ๋‹น

3) Priority allocation : ํ”„๋กœ์„ธ์Šค์˜ priority์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ํ• ๋‹น

 

Global vs Local replacement

1) Global replacement

- ๋‹ค๋ฅธ process์— ํ• ๋‹น๋œ frame์„ ๋นผ์•—์•„ ์˜ฌ ์ˆ˜ ์žˆ์Œ

- FIFO, LRU, LFU ๋“ฑ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ global replacement๋กœ ์‚ฌ์šฉ ์‹œ ํ•ด๋‹น

- Working set, PFF ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ ์‹œ ์•Œ์•„์„œ ํ• ๋‹น

 

2) Local replacement

- ์ž์‹ ์—๊ฒŒ ํ• ๋‹น๋œ frame ๋‚ด์—์„œ๋งŒ replacement

- FIFO, LRU, LFU ๋“ฑ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ process ๋ณ„๋กœ ์šด์˜์‹œ

 

Thrashing

ํ”„๋กœ์„ธ์Šค์˜ ์›ํ™œํ•œ ์ˆ˜ํ–‰์— ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ page frame ์ˆ˜๋ฅผ ํ• ๋‹น๋ฐ›์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ

1) page fault๊ฐ€ ๋งค์šฐ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒ

2) low CPU utilization

3) OS๋Š” MPD(multiprogramming degree)๋ฅผ ๋†’์—ฌ์•ผ ํ•œ๋‹ค๊ณ  ํŒ๋‹จ

4) ๋˜ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์Šคํ…œ์— ์ถ”๊ฐ€๋จ (higher MPD)

5) ํ”„๋กœ์„ธ์Šค ๋‹น ํ• ๋‹น๋œ frame์˜ ์ˆ˜๊ฐ€ ๋”์šฑ ๊ฐ์†Œ

6) ํ”„๋กœ์„ธ์Šค๋Š” page์˜ swap in / swap out์œผ๋กœ ๋งค์šฐ ๋ฐ”์จ

7) ๋Œ€๋ถ€๋ถ„์˜ ์‹œ๊ฐ„์— CPU๋Š” ํ•œ๊ฐ€ํ•จ

8) low throughput

Thrashing Diagram

 

Working-Set Algorithm

* Locality of reference

- ํ”„๋กœ์„ธ์Šค๋Š” ํŠน์ • ์‹œ๊ฐ„ ๋™์•ˆ ์ผ์ • ์žฅ์†Œ๋งŒ์„ ์ง‘์ค‘์ ์œผ๋กœ ์ฐธ์กฐํ•œ๋‹ค

- ์ง‘์ค‘์ ์œผ๋กœ ์ฐธ์กฐ๋˜๋Š” ํ•ด๋‹น page๋“ค์˜ ์ง‘ํ•ฉ์„ locality set์ด๋ผ ํ•จ

 

* Working-set model

locality์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ผ์ • ์‹œ๊ฐ„ ๋™์•ˆ ์›ํ™œํ•˜๊ฒŒ ์ˆ˜ํ–‰๋˜๊ธฐ ์œ„ํ•ด ํ•œ๊บผ๋ฒˆ์— ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ์–ด์•ผ ํ•˜๋Š” page๋“ค์˜ ์ง‘ํ•ฉ

- process์˜ working set ์ „์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ์–ด์•ผ ์ˆ˜ํ–‰๋˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ชจ๋“  frame์„ ๋ฐ˜๋‚ฉํ•œ ํ›„ swap out(suspend)

- thrashing์„ ๋ฐฉ์ง€

- multiprogramming degree๋ฅผ ๊ฒฐ์ •

 

* Working-set์˜ ๊ฒฐ์ •

time interval ์‚ฌ์ด์— ์ฐธ์กฐ๋œ ์„œ๋กœ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋“ค์˜ ์ง‘ํ•ฉ

- working set window๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ƒ„ (๊ณผ๊ฑฐ delta ์‹œ๊ฐ„ ๋™์•ˆ ์ฐธ์กฐ๋œ page = working set window)

-> working set์— ๋ชจ๋‘ ์†ํ•œ page๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์œ ์ง€ํ•ด์ฃผ๊ณ , ์†ํ•˜์ง€ ์•Š์€ ์• ๋“ค์€ ๋ชจ๋“  frame ๋ฐ˜๋‚ฉ ํ›„ suspend

(์ฐธ์กฐ๋œ page๋ฅผ delta ์‹œ๊ฐ„๋งŒํผ๋งŒ ์œ ์ง€ํ•˜๊ณ  ๋ฒ„๋ฆผ)

 

* algorithm

1) process์˜ working set size ํ•ฉ > page frame์˜ ์ˆ˜

- ์ผ๋ถ€ process๋ฅผ swap out ์‹œ์ผœ(MPD ์ค„์ž„) ๋‚จ์€ process์˜ working set์„ ์šฐ์„ ์ ์œผ๋กœ ์ถฉ์กฑ์‹œ์ผœ์ค€๋‹ค

2) working set์„ ๋‹ค ํ• ๋‹นํ•˜๊ณ ๋„ page frame์ด ๋‚จ๋Š” ๊ฒฝ์šฐ

- swap out๋˜์—ˆ๋˜ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ working set์„ ํ• ๋‹น(MPD ํ‚ค์›€)

 

PFF(Page-Fault Frequency) Scheme

์ง์ ‘ page-fault rate๋ฅผ ๋ณด๊ณ  frame ํ• ๋‹น

๋นˆ frame์ด ์—†์œผ๋ฉด ์ผ๋ถ€ ํ”„๋กœ์„ธ์Šค๋ฅผ swap out

 

Page Size์˜ ๊ฒฐ์ •

larger page size๊ฐ€ ์š”์ฆ˜์˜ ์ถ”์„ธ (low page fault -> seek ์‹œ๊ฐ„ ๊ฐ์†Œ)

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

- page ์ˆ˜ ์ฆ๊ฐ€

- page table ํฌ๊ธฐ ์ฆ๊ฐ€

- internal fragmentation ๊ฐ์†Œ

- disk transfer์˜ ํšจ์œจ์„ฑ ๊ฐ์†Œ(seek/rotation vs transfer)

- ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ๋ฉ”๋ชจ๋ฆฌ ์ด์šฉ์ด ํšจ์œจ์  -> locality์˜ ํ™œ์šฉ ์ธก๋ฉด์—์„œ๋Š” ์ข‹์ง€ ์•Š์Œ

๋Œ“๊ธ€