510 likes | 709 Views
제 11 장. Virtual Memory. 11.1 Comparison of Cache and Virtual Memory. Hit. CPU. CPU address = Memory address. Cache. Cache address. Memory. Memory address. CPU address 는 Cache address 로 변환된 후 , Cache address 내의 해당 정보가 CPU 에게 제공된다. Memory. Cache. C block F (F0-FF).
E N D
제 11 장 Virtual Memory
11.1 Comparison of Cache and Virtual Memory Hit CPU CPU address = Memory address Cache Cache address Memory Memory address CPU address는 Cache address로 변환된 후, Cache address내의 해당 정보가 CPU에게 제공된다.
Memory Cache C block F (F0-FF) M block FFF (FFF0-FFFF) C block E (E0-EF) M block FFE (FFE0-FFEF) C block D (D0-DF) … C block C (C0-CF) … CPU C block B (B0-BF) … C block A (A0-AF) … 0x01F8 C block 9 (90-9F) … C block 8 (80-8F) … C block 7 (70-7F) M block 01F (01F0-01FF) C block 6 (60-6F) … C block 5 (50-5F) … C block 4 (40-4F) … C block 3 (30-3F) … C block 2 (20-2F) M block 002 (0020-002F) C block 1 (10-1F) M block 001 (0010-001F) C block 0 (00-0F) M block 000 (0000-000F) CPU address 0x01F8 Memory address 범위 0x0000 – 0xFFFF Cache address 범위 0x00 – 0xFF
Full Associative Method Tag Valid bit 1 0x234 C block F (F0-FF) 0 0x000 C block E (E0-EF) 0 0x000 C block D (D0-DF) 1 0x01C C block C (C0-CF) CPU 1 0x01E C block B (B0-BF) 1 0x553 C block A (A0-AF) 0 0x000 C block 9 (90-9F) 0x01F8 0 0x000 C block 8 (80-8F) 1 0x766 C block 7 (70-7F) 1 0x023 C block 6 (60-6F) 1 0x011 C block 5 (50-5F) 1 0x126 C block 4 (40-4F) 1 0x123 C block 3 (30-3F) 1 0x222 C block 2 (20-2F) 1 0x111 C block 1 (10-1F) 1 0x01F C block 0 (00-0F) CPU address : 0x01F8 -> Cache address : 0x08
Full Associative Method Tag Valid bit 1 0x234 C block F (F0-FF) 0 0x000 C block E (E0-EF) 0 0x000 C block D (D0-DF) 1 0x01C C block C (C0-CF) CPU 1 0x01E C block B (B0-BF) 1 0x01F C block A (A0-AF) 0 0x000 C block 9 (90-9F) 0x01F8 0 0x000 C block 8 (80-8F) 1 0x766 C block 7 (70-7F) 1 0x023 C block 6 (60-6F) 1 0x011 C block 5 (50-5F) 1 0x126 C block 4 (40-4F) 1 0x123 C block 3 (30-3F) 1 0x222 C block 2 (20-2F) 1 0x111 C block 1 (10-1F) 1 0x554 C block 0 (00-0F) CPU address : 0x01F8 -> Cache address : 0xA8
miss CPU CPU Cache Cache Memory Memory
Full Associative Method Tag Valid bit 1 0x234 C block F (F0-FF) 0 0x000 C block E (E0-EF) 0 0x000 C block D (D0-DF) 1 0x01C C block C (C0-CF) CPU 1 0x01E C block B (B0-BF) 1 0x553 C block A (A0-AF) 0 0x000 C block 9 (90-9F) 0x01F8 0 0x000 C block 8 (80-8F) 1 0x766 C block 7 (70-7F) 1 0x023 C block 6 (60-6F) 1 0x011 C block 5 (50-5F) 1 0x126 C block 4 (40-4F) 1 0x123 C block 3 (30-3F) 1 0x222 C block 2 (20-2F) 1 0x111 C block 1 (10-1F) 1 0x010 C block 0 (00-0F) CPU address : 0x01F8 -> Cache address :
C block F (F0-FF) C block E (E0-EF) C block D (D0-DF) C block C (C0-CF) CPU C block B (B0-BF) miss C block A (A0-AF) C block 9 (90-9F) 0x01F8 Memory C block 8 (80-8F) C block 7 (70-7F) C block 6 (60-6F) C block 5 (50-5F) C block 4 (40-4F) C block 3 (30-3F) C block 2 (20-2F) C block 1 (10-1F) C block 0 (00-0F) Parallel Reading of Cache and Memory
Full Associative Method Tag Valid bit 1 0x234 C block F (F0-FF) 0 0x000 C block E (E0-EF) 0 0x000 C block D (D0-DF) 1 0x01C C block C (C0-CF) CPU 1 0x01E C block B (B0-BF) 1 0x553 C block A (A0-AF) 0 0x000 C block 9 (90-9F) 0x01F8 1 0x01F C block 8 (80-8F) 1 0x766 C block 7 (70-7F) 1 0x023 C block 6 (60-6F) 1 0x011 C block 5 (50-5F) 1 0x126 C block 4 (40-4F) 1 0x123 C block 3 (30-3F) 1 0x222 C block 2 (20-2F) 1 0x111 C block 1 (10-1F) 1 0x010 C block 0 (00-0F) CPU address : 0x01F8 -> Cache address : 0x88
Virtual Memory Hit CPU CPU address = Virtual address Memory Memory address = Physical address Virtual Memory Swap Space in Hard Disk Virtual address CPU가 제공하는 virtual address는 physical address로 변환된 후, physical address내의 해당 정보가 CPU에게 제공된다.
miss CPU CPU Memory Memory Virtual Memory Virtual Memory Block : miss가 발생 시 memory에서 cache로 복사하는 정보 단위 Page : miss가 발생 시 virtual memory에서 memory로 복사하는 정보 단위
Cache와 Virtual Memory를 사용하는 경우 구성도 CPU CPU address = Virtual address Cache Cache address Memory Physical address Virtual Memory Virtual address
-> Virtual Address : The address used by program before mapping into physical memory Hit CPU CPU address = Virtual address Memory Memory address = Physical address program Swap Space in Hard Disk Virtual Memory Virtual address
Program1 … MOVE R1, $7000 ADD R1, $7010 MOVE $7020, R1 … 1000 1004 1008 … 7000 … 7010 … 7020 Program2 Program3
-> Computer can execute the program which size is bigger than main memory. Hit CPU Memory ex) Memory Size = 64Kbyte program Swap Space in Hard Disk Virtual Memory ex) Program Size = 1M byte
-> Computer are running multiple processes with its own address space (Divide physical memory into blocks(pages) and allocates them to different processes) CPU Memory program program Virtual Memory Virtual Memory
-> Protection is need CPU Memory Program 1 Program 2 Virtual Memory Virtual Memory
-> Sharing a smaller amount of physical memory between many processes -> program : code, data, stack j stack -> read/write i, k data -> read/write int i; main(){ int j; static k; k = i + j; . . } code -> read only 여러 프로그램에서 동일 코드 공유 가능
CPU Memory Program 1 Program 2 Virtual Memory Virtual Memory -> code
11.2 Page Placement => Full Associative CPU CPU Memory Memory Virtual Memory Virtual Memory miss가 발생시 virtual memory에서 해당되는 page를 memory로 가지고 와야 하며, 이때, 어디에 둘 것인가 ?
Fully Associative : A page can be placed anywhere in the memory Memory Memory Memory Virtual Memory Virtual Memory Memory Memory Virtual Memory Virtual Memory Virtual Memory
11.3 Page Identification CPU가 원하는 virtual address 정보가 memory에 있는지를 어떠한 방법으로 확인할 것인가 ? Miss Hit CPU CPU Memory Memory Virtual Memory Virtual Memory
Page Identification => Virtual Address Mapping by use of page table Virtual Address Main Memory Virtual Page Number Page Offset Page M … Page Table Physical Address Page N + … Physical Page Number Page 0
example) main memory : 64Kbyte page size : 4Kbyte M Page F (F000-FFFF) M Page E (E000-EFFF) M Page D (D000-DFFF) M Page C (C000-CFFF) M Page B (B000-BFFF) M Page A (A000-AFFF) M Page 9 (9000-9FFF) M Page 8 (8000-8FFF) M Page 7 (7000-7FFF) M Page 6 (6000-6FFF) M Page 5 (5000-5FFF) M Page 4 (4000-4FFF) M Page 3 (3000-3FFF) M Page 2 (2000-2FFF) M Page 1 (1000-1FFF) M Page 0 (0000-0FFF)
example) virtual memory : 1Mbyte page size : 4Kbyte VM Page FF (FF000-FFFFF) VM Page FE (FE000-FEFFF) … VM Page 79 (79000-79FFF) VM Page 78 (78000-78FFF) … VM Page 38 (38000-38FFF) VM Page 37 (37000-37FFF) VM Page 36 (36000-36FFF) … VM Page 17 (17000-17FFF) VM Page 16 (16000-16FFF) VM Page 15 (15000-15FFF) … VM Page 01 (01000-01FFF) VM Page 00 (00000-00FFF)
Page Table Main Memory Table Valid M Page F (F000-FFFF) FF 0 0 M Page E (E000-EFFF) FE 1 1 M Page D (D000-DFFF) … … … M Page C (C000-CFFF) 79 1 2 M Page B (B000-BFFF) 78 1 A M Page A (A000-AFFF) … … … M Page 9 (9000-9FFF) 38 1 7 M Page 8 (8000-8FFF) 37 0 0 M Page 7 (7000-7FFF) 36 1 6 M Page 6 (6000-6FFF) … … … M Page 5 (5000-5FFF) 17 0 0 M Page 4 (4000-4FFF) 16 0 0 M Page 3 (3000-3FFF) 15 1 3 M Page 2 (2000-2FFF) … … … M Page 1 (1000-1FFF) 01 0 0 M Page 0 (0000-0FFF) 00 0 0 Virtual Page 0x15 (0x15000 – 0x15FFF) 는 Physical Page 0x3 (0x3000 – 0x3FFF)에 있음을 의미
Main Memory Table Valid M Page F (F000-FFFF) FF 0 0 M Page E (E000-EFFF) FE 1 1 M Page D (D000-DFFF) … … … M Page C (C000-CFFF) 79 1 2 M Page B (B000-BFFF) 78 1 A M Page A (A000-AFFF) … … … M Page 9 (9000-9FFF) 38 1 7 M Page 8 (8000-8FFF) 37 0 0 M Page 7 (7000-7FFF) 36 1 6 M Page 6 (6000-6FFF) … … … M Page 5 (5000-5FFF) 17 0 0 M Page 4 (4000-4FFF) 16 0 0 M Page 3 (3000-3FFF) 15 1 3 M Page 2 (2000-2FFF) … … … M Page 1 (1000-1FFF) 01 0 0 M Page 0 (0000-0FFF) 00 0 0 Virtual Page 0x79 (0x79000 – 0x79FFF) 는 Physical Page 0x2 (0x2000 – 0x2FFF)에 있음을 의미
Virtual address : 0x79123 Virtual Page : 0x79, Page Offset : 0x123 Virtual Address Main Memory Virtual Page Number Page Offset Page M … Page Table Physical Address Page N + … Physical Page Number Page 0 Virtual Page (0x79) -> Physical Page (0x2) Physical address = Physical Page + Page Offset = 0x2123
Main Memory Table Valid M Page F (F000-FFFF) FF 0 0 M Page E (E000-EFFF) FE 1 1 M Page D (D000-DFFF) … … … M Page C (C000-CFFF) 79 1 2 M Page B (B000-BFFF) 78 1 A M Page A (A000-AFFF) … … … M Page 9 (9000-9FFF) 38 1 7 CPU M Page 8 (8000-8FFF) 37 0 0 0x79123 0x2123 M Page 7 (7000-7FFF) 36 1 6 M Page 6 (6000-6FFF) … … … M Page 5 (5000-5FFF) 17 0 0 M Page 4 (4000-4FFF) 16 0 0 M Page 3 (3000-3FFF) 15 1 3 M Page 2 (2000-2FFF) … … … M Page 1 (1000-1FFF) 01 0 0 M Page 0 (0000-0FFF) 00 0 0 Virtual Page (0x79) -> Physical Page (0x2) Physical address = Physical Page + Page Offset = 0x2123
Main Memory Table Valid M Page F (F000-FFFF) FF 0 0 M Page E (E000-EFFF) FE 1 1 M Page D (D000-DFFF) … … … M Page C (C000-CFFF) 79 1 2 M Page B (B000-BFFF) 78 1 A M Page A (A000-AFFF) … … … M Page 9 (9000-9FFF) 38 1 7 CPU M Page 8 (8000-8FFF) 37 0 0 0x78CDE 0xACDE M Page 7 (7000-7FFF) 36 1 6 M Page 6 (6000-6FFF) … … … M Page 5 (5000-5FFF) 17 0 0 M Page 4 (4000-4FFF) 16 0 0 M Page 3 (3000-3FFF) 15 1 3 M Page 2 (2000-2FFF) … … … M Page 1 (1000-1FFF) 01 0 0 M Page 0 (0000-0FFF) 00 0 0 Virtual Page (0x78) -> Physical Page (0xA) Physical address = Physical Page + Page Offset = 0xACDE
11.4 Page Replacement CPU Memory Virtual Memory
LRU (Least Recently Used) Algorithm Use bit : MMU Set bit whenever a page is accessed OS Clears the bit periodically Dirty bit : MMU Set bit whenever a page is modified
Dirty bit = 1 인 경우 Miss CPU Memory swap out swap in Virtual Memory Swap Space in Hard Disk
CPU Memory Virtual Memory Swap Space in Hard Disk
Dirty bit = 0 인 경우 Miss CPU Memory swap in Virtual Memory Swap Space in Hard Disk
11.5 Write Strategy => Write Back -> The information is written only to the page in the memory. The modified page block is written to virtual memory only when it is replaced. -> Multiple write in a page => One write to virtual memory -> Virtual Memory는 Hard Disc에 있으며 access time이 memory의 access time에 비하여 매우 느리다. 따라서, write through를 사용하면 매우 비 효율적이다.
Replacement Write hit Write hit CPU CPU CPU CPU Memory Memory Memory Memory Virtual Memory Virtual Memory Virtual Memory Virtual Memory
Write Allocate CPU CPU CPU miss write Memory Memory Memory read Virtual Memory Virtual Memory
11.6 Virtual Address Translation Page Table은 Main Memory내에 있으며 이 중 사용되는 부분들이 MMU (Memory Management Unit) 내의 TLB (Translation Lookaside Buffer)내에 저장되어 있다.
CPU Virtual address MMU Physical address Memory Virtual Memory
Use of TLB (Translation Lookaside Buffer) in MMU (Memory Management Unit) - The on-chip cache for page table - Stem from locality - Virtual address tag , Physical page-frame number Protection field, Use bit, Dirty bit
Virtual Address Main Memory Virtual Page Number Page Offset Page M … TLB in MMU Physical Address Page N + … Physical Page Number Page 0 Page Table in Main Memory Virtual Address Translation with TLB
Cache와 Virtual Memory를 사용하는 경우 구성도 1 CPU Virtual address MMU Physical address Cache Cache address Memory Virtual Memory
Tag Valid bit Table Valid FF 0 0 1 0x234 C block F (F0-FF) FE 1 1 0 0x000 C block E (E0-EF) … … … 0 0x000 C block D (D0-DF) 79 1 2 1 0x01C C block C (C0-CF) 78 1 A 1 0x01E C block B (B0-BF) Cache Hit … … … 1 0x553 C block A (A0-AF) 38 1 7 0 0x000 C block 9 (90-9F) CPU 37 0 0 0 0x000 C block 8 (80-8F) 0x79123 0x2123 36 1 6 1 0x766 C block 7 (70-7F) … … … 1 0x023 C block 6 (60-6F) 17 0 0 1 0x011 C block 5 (50-5F) 16 0 0 1 0x126 C block 4 (40-4F) 15 1 3 1 0x123 C block 3 (30-3F) … … … 1 0x222 C block 2 (20-2F) 01 0 0 1 0x111 C block 1 (10-1F) 00 0 0 1 0x212 C block 0 (00-0F) Virtual Page (0x79) -> Physical Page (0x2) Physical address = Physical Page + Page Offset = 0x2123 Physical address : 0x2123 -> Cache address : 0x03
Tag Valid bit Table Valid FF 0 0 1 0x234 C block F (F0-FF) FE 1 1 0 0x000 C block E (E0-EF) … … … 0 0x000 C block D (D0-DF) 79 1 2 1 0x01C C block C (C0-CF) 78 1 A 1 0x01E C block B (B0-BF) Cache Miss … … … 1 0x553 C block A (A0-AF) 38 1 7 0 0x000 C block 9 (90-9F) CPU 37 0 0 0 0x000 C block 8 (80-8F) 0x79123 0x2123 36 1 6 1 0x766 C block 7 (70-7F) … … … 1 0x023 C block 6 (60-6F) 17 0 0 1 0x011 C block 5 (50-5F) 16 0 0 1 0x126 C block 4 (40-4F) 15 1 3 1 0x123 C block 3 (30-3F) … … … 1 0x222 C block 2 (20-2F) 01 0 0 1 0x111 C block 1 (10-1F) 00 0 0 1 0x01A C block 0 (00-0F) Virtual Page (0x79) -> Physical Page (0x2) Physical address = Physical Page + Page Offset = 0x2123 Physical address : 0x2123 -> Cache address : ???
Table Valid 0x234 FF 0 0 C block F (F0-FF) 0x987 FE 1 1 C block E (E0-EF) 0xABC … … … C block D (D0-DF) 0x01C 79 1 2 C block C (C0-CF) 0x01E 78 1 A C block B (B0-BF) Cache Miss 0x553 … … … C block A (A0-AF) 0x922 38 1 7 C block 9 (90-9F) CPU 0x812 37 0 0 C block 8 (80-8F) 0x79123 0x2123 Memory 0x01F 36 1 6 C block 7 (70-7F) 0x023 … … … C block 6 (60-6F) 0x011 17 0 0 C block 5 (50-5F) 0x126 16 0 0 C block 4 (40-4F) 0x212 15 1 3 C block 3 (30-3F) 0x222 … … … C block 2 (20-2F) 0x111 01 0 0 C block 1 (10-1F) 0x01A 00 0 0 C block 0 (00-0F)
총정리 왜, Cache를 이용하는가 ? 첫 번째, CPU의 속도가 Memory의 속도보다 빠르기 때문에 CPU가 wait상태에 있는 비율이 높아지게 되며 따라서 CPU의 사용 효율이 낮아지게 된다. Cache를 두어 CPU의 사용 효율을 높이는 것이 목적 CPU의 사용 효율이 높아지게 되면 프로그램의 수행속도가 빨라진다. CPU CPU Fast Fast Cache Fast Memory Memory Slow Slow
왜, Virtual Memory를 이용하는가 ? -> Computer can execute the program which size is bigger than main memory. Hit CPU Memory ex) Memory Size = 64Kbyte program Swap Space in Hard Disk Virtual Memory ex) Program Size = 1M byte
-> Computer are running multiple processes with its own address space (Divide physical memory into blocks(pages) and allocates them to different processes) CPU Memory program program Virtual Memory Virtual Memory