1 / 28

Caching and Virtual Memory

Caching and Virtual Memory. 20 조 OX 20010110 김정현 20010362 이영재. Assignment #1 software-management of the TLB. TLB 는 가장 최근에 메모리에 기억된 가상페이지들이 실제 메모리 주소를 빠르게 변환하기 위해 만든 것으로 , OS 에서는 Kernel 에서 하나의 TLB 를 제공하며 , 나쵸스에서의 TLB 는 4 개의 Entry 를 가지고 있다 .

natara
Download Presentation

Caching and Virtual Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Caching and Virtual Memory 20조 OX 20010110 김정현 20010362 이영재

  2. Assignment #1 software-management of the TLB • TLB는 가장 최근에 메모리에 기억된 가상페이지들이 실제 메모리 주소를 빠르게 변환하기 위해 만든 것으로, OS에서는 Kernel에서 하나의 TLB를 제공하며, 나쵸스에서의 TLB는 4개의 Entry를 가지고 있다. • TranslationEntry class 에 TLB가 정의되어 있다. • 우리가 해야 할 일은 nachos가 제공하는 hardware적인 simulated TLB 를 software적으로 관리하는 일이다. 이러한 상황은, nachos에서 TLB를 사용하여 주소 변환을 하다가 TLB에 원하는 entry가 없을 때, exception이 발생하고, 현재 실행중인 process가 이를 감지하여 exception을 해결해 주는 것이다.

  3. TLB를 통한 주소 변환translate() method in Processor.java for (int i=0; i<tlbSize; i++) { if (translations[i].valid && translations[i].vpn == vpn) { entry = translations[i]; break; } } if (entry == null) { privilege.stats.numTLBMisses++; Lib.debug(dbgProcessor, "\t\tTLB miss"); throw new MipsException(exceptionTLBMiss, vaddr); }

  4. exceptionTLBMiss의 처리 handleException() method in VMProcess.java public void handleException(int cause) { Processor processor = Machine.processor(); switch (cause) { case Processor.exceptionTLBMiss: /** TLB miss handling **/ default: super.handleException(cause); break; } }

  5. TLBMissing handling • 1. TLB에는 없으나 Kernel의 page table에는 변환정보가 있는 경우.- 메모리에 기억되어 있지만   그 페이지가 최근에 접근되지 않은 경우 - 이 페이지의 관한 정보가 TLB에 기록된다. • 2. TLB에도 없고 Kernel의 page table에도 변환정보가 없는 경우.- page fault!- Assignement #2에서의 구현으로 원하는 page를 메모리로 가져온다. (demand page)- 이 페이지의 translation 정보를 kernel의 page table에 기록한다.- 이 translation의 정보 역시 TLB에 기록한다.

  6. Pseudo code case Processor.exceptionTLBMiss: int vaddr = processor.readRegister(Processor.regBadVAddr); /* TLB Miss를 낸 virtual address를 읽는다. */ /* Kernel을 invoke시켜서, 현재 동작중인 프로세스의, address vaddr을 포함하는 Entry를 얻는다. */ Page = VMKernel.findPage (getId (), Processor.pageFromAddress (vaddr)); Processor.writeTLBEntry (TLBcount, page); /* 저장할 값을 가져와서 TLB에 쓴다. */ TLBcount = (TLBcount + 1) % processor.getTLBSize (); /* TLB count조정 - FIFO */ break;

  7. TLB invalidate • TLB에는 process id에 대한 정보가 없다.-> process가 다른 process의 정보를 access할 수 도 있음! • Context Switching이 일어날 때 현재 쓰던 페이지 정보에 대해서는 invalidate시켜주어야 한다. • Implement saveState() method in VMProcess class

  8. Pseudo code public void saveState() { for( all TLB ){ TLBEntry,valid = false // invalidate if( TLBEntry.dirty bit or TLBEntry.used bit is true ){ VMKernel.findPage( getId() , dummyEntry.vpn ).dirty = dummyEntry.dirty; VMKernel.findPage( getId() , dummyEntry.vpn ).used = dummyEntry.used; } Machine.processor().writeTLBEntry( i , TLBEntry); } return; }

  9. Inverted Page Table • 프로젝트3에서는 2번과는 달리 page table을 각 프로세스 마다 갖지 않고, 커널에 하나만이 존재한다. (Inverted Page Table) • 이를 위해서는 Page Table에 PID,VPN,PPN 이 함께 들어가야 한다 • 커널의 Page Table을 검사하는 시간을 줄이기 위해 Hash table을 이용한다. Hash table은 java.util을 이용하며 Key값은 PID가 된다.

  10. Load, Unload Section • 프로젝트2 에서는 UserProcess class 의 생성자에서 page table을 새로 만들고, vpn-ppn의 정보를 이 page table 에 넣었다 • 이번 프로젝트3 에서는 이 대신 Kernel에서 만든 하나의 Inverted page table에 pid-vpn-ppn의 정보를 저장한다. • 각 process의 vpn에 ppn을 대응시키는 작업은 Kernel에서 해준다 • Unload 역시 마찬가지로 inverted page table에서 vpn에 맞는 ppn을 찾아서 없애주고, page table내에서도 그 정보를 지워준다. • 이 작업들은 커널 안의 한 data(inverted page table)을 다루므로 atomic하게 수행하기 위해 lock을 사용한다.

  11. Assignment #2Demand paging of virtual memory • Process 실행에 필요한 page만을 physical memory에 올려서 실제 메모리보다 더 큰 메모리를 필요로 하는 프로그램을 실행 가능하게 한다. • Page fault 가 일어났을 때, physical memory가 full 이라면, 다음의 알고리즘들을 이용하여, momory 상의 한 페이지를 disk로 back-up 하고, 그 자리에 새 page를 load한다. FIFO, RANDOM, LRU 등 • TLB와 Kernel의 inverted page table을 update 한다.

  12. loadSection() , findPage() • loadSection() • findPage()1. 원하는 page가 inverted page table에 있으면 반환한다. 2. 원하는 page가 없을 경우 - 메모리가 꽉 차지 않았을 경우 swap file 에 있을 경우 : swap file에서 page로 copy 후 page return swap file 에 없을 경우 : assignment #3 “lazy loading” - 메모리가 꽉 찼을 경우 handlePageFault()를 이용해서 physical memory 하나를 비운 후 page를 load 하고 return

  13. findPage() - Pseudo code public static TranslationEntry findPage( int pid, int vpn) { //찾으려는 페이지가 메모리에 있으면, inverted page Table에서 entry를 찾아서 반환한다. TranslationEntry temp = invfindPage( pid , vpn ); if ( temp != null ) return temp; //메모리가 꽉 차 있지 않은 상태에서 page fault가 발생했을때 int j = virMem.getNewVirpage(); //메모리가 꽉 차 있는 상태에서 page fault 가 발생했을때 handlePageFault(); int j = virMem.getNewVirPage(); “Read from Swap file” or “Lazy Loading” to physical Memory return dumEntry= new TranslationEntry( vpn, j, , , , );

  14. handlePageFault - Pseudo code public static void handlePageFault() { ppn = getFP(); if (ppn != -1){ // 그 사이 free physical page가 생겼다면 return; } Replacement Algorithm 을 이용, (ex : Clock algorithm) physical memroy에서 내려질 page를 찾는다. read only 일 경우 : page를 지우고 return 한다. dirty bit이 set 된 경우 : swap file에 저장한 후, page를 지우고 return 한다. }

  15. Swap file • Memory 가 부족할 때, page를 disk에 back up하게 되는데, 이때 Swap file을 사용한다. • Swap file은 모든 process들이 공유하는 하나의 file 이다. • Swap file에 씌여있는 page들의 정보들은 SwapList라는 linked list를 이용하여 저장한다. • SwapList는 SwapNode라는 Data Structure 들의 linked list이며, pid, vpn, offset, NextNode 로 이루어져 있다. • pid와 vpn을 이용하여, SwapList를 검색하게 되고 offset을 이용하여 Swap file의 어디에 이 page가 back up 되어 있는지 알 수 있다. • SwapList 역시 kernel에 만들어지는 단 하나의 DS이며, atomic한 수행을 위해 lock을 이용한다.

  16. Swap list

  17. Swap list (cont’) • public staticvoid addSwap( ) • public staticswapNode findLastNodeSwap( ) • public staticboolean isInThisListSwap( ) • public staticint findOffsetSwap( ) • public staticswapNode findNodeSwap( ) • public staticvoid deleteSwap( )

  18. Initialize swap file and list - Pseudo code public void initialize(String[] args) { super.initialize(args); //사용할 swap file의 이름을 얻어서, swap file을 열어 본다 swapFileName = new String("VM_SWAP_FILE"); swapFile = fileSystem.open( swapFileName , false ); //만약 swapFile이 존재하면 nachos가 비정상적으로 종료된 것이므로 swapfile을 지운다. if ( swapFile != null ) fileSystem.remove(swapFileName); //swap file을 열고, SwapList를 하나 생성한다. swapFile = fileSyste.open( swapFileName, true); swapList = new SwatList(); }

  19. Assignment #3 Lazy loading • 시작 시에 모든 code와 data를 읽어 들이지 않고, page table entry들만을 설정해서, page fault가 일어날 때마다 executable에서 page를 읽어서 memory에 loading • page fault 가 일어나면 • set up page table entries • read the pages from the executable into memory on demand • Modify loadSection(), findPage()

  20. SectionNum Offset loadSection() • Process가 initialize될 때의 loadSection() method를 수정하여, 시작시에는 아무 페이지도 load 되지 않도록 한다. • 대신 시작 시, Field 라는 형태의 data structure 배열에서 테이블의 정보를 기억해두고, Page Fault가 났을 때, 이 정보를 이용하여 excutable 에서 page를 읽어서 memory에 load 한다. • static class sectionField{ public int secNum; public int offset; }

  21. loadSection()(cont’) protected boolean loadSections() { secfield = new sectionField[pageSize]; int vpn = -1; Don’t load Section, Instead… for ( int s=0; s<coff.getNumSections(); s++){ CoffSection section = coff.getSection(s); for (int i=0; i<section.getLength(); i++) { vpn = section.getFirstVPN()+i; secfield[vpn] = new sectionField(); secfield[vpn].secNum = s; secfield[vpn].offset = i; } } return true; }

  22. findPage() • Project #2에서는 findPage() 메소드를 호출하면, 그 Page만 반환하면 되었으나, lazy loading에서는 그 Page가 Coff section의 page이면, 첨부터 페이지 테이블을 안 만들어 놓았으므로, 이를 load한 다음 return해야 한다. • 현재 프로세스가 실행중일 때, vpn을 찾으려는 요청이 들어오고, 그것이 참조하는 페이지가 coff Section안에 들어있는 경우 : 그 section을 physical memory에 load 한다. • section이 Read-Only일 경우 : : Entry에 Read-Only임을 Check해 준다.

  23. findPage() (cont’) public static TranslationEntry findPage( int pid, int vpn) { …… if(swap file에도 없다면) j=현재 비어있는 ppn pro = (VMProcess)currentProcess(); if ( pro != null && pro.secfield[vpn] != null){ CoffSection section = pro.coff.getSection(pro.secfield[vpn].secNum); section.loadPage( pro.secfield[vpn].offset , j ); if ( section.isReadOnly() ){ 위 페이지의 readOnly = true; } } …… }

  24. Assignment #4 mmap syscall • In syscall.h • Map the file referenced by fileDescriptor into memory at address. • To maintain consistency, further calls to read() and write() on this file descriptor will fail (returning -1) until the file descriptor is closed. -> file descriptor에 mmap이라는 flag를 추가해서 read나 write할때 flag를 검사한다. • When the file descriptor is closed, all remaining dirty pages of the map will be flushed to disk and the map will be removed -> mmap된 file이름과 address를 같이 갖고 있는 list를 만든다. • Returns the length of the file on success, or -1 if an error occurred.

  25. Pseudo code SyscallMmap = 10; Int handleMmap(int fdNum, int address){ mmapfilelist.add(fds[fdNum].name, address) fds[fdNum].Mmap=true; filesize=fds[fdNum].OpenFile.length; byte[] temp = new byte[filesize]; fds[fdNum].OpenFile.read(temp, 0, filesize); writeVirtualMemory(address, temp, 0, filesize); return 1; }

  26. Pseudo code int handleRead(int fdNum, int bf, int count){ … if(fds[fdNum].mmap==true) return -1; … } int handleWrite(int fdNum, int bf, int count){ … if(fds[fdNum].mmap==true) return -1; … }

  27. Pseudo code Int handleClose(int fdNum){ … if(fds[fdNum].mmap){ mmapfilelist에서 해당되는 file이 mmap되있는 address를 찾는다. String temp=readVirtualMemoryString(address, MaxLength); length=temp.Length(); while(length!=0){ address+length범위의 vpn들에 해당되는 ppn들을 찾고 수정 된 page들이 있으면 실제 file에 기록해준다. } } … }

  28. Pseudo code - In cp.c int main(int argc, char *argv[]){ char orig[MaxLength]; char des[MaxLength]; int fd1=fopen(argv[1]); int fd2=fopen(argv[2]); mmap(fd1, orig); mmap(fd2, des); strcpy(orig, des); close(fd1); close(fd2); }

More Related