Modeling Page Replacement Algorithms Reference text: Tanenbaum ch.3.4.5
Paging in Practice Newer UNIX OS is based on swapping and demand paging. The kernel and the page daemon process performs paging Main memory is divided into kernel, core map and page frames. 4M page frame 1KB
Two-Handed Clock Algorithm For the (i – HANDSPREADPAGES) th page , check the usuage bit. If it is not set, add the page to the free list. Every 250ms, page replacement algorithm wakes up to see if the free page frame is equal to a set value(~1/4 of memory). If less, transfer pages from memory to disk. 1st hand 2nd hand clear the usuage bit of the i th page Page daemon maintains 2 pointers into the core map The first hand clears the usage bit at the front end The second hand checks the usage bit at the back end. Pages with the usage bit=0 will be put on the free list
UNIX Commands: vmstat and uptime uptime : shows how long the system has been up vmstat -s : shows virtual memory stat
Swapping in UNIX If the paging rate is too high and the number of free pages is always way below the the threshold, the swapper is used to remove one or more processes from memory Processes that have been idled for > 20 sec will be swapped out first Processes that are the largest and have been idled the longest will be swapped out second
Paging Implementation Issues Operating System Involvement 1. When a new process is created: determine how large the program and data and create a page table for it page table not in memory when process is swapped out allocate space from disk for swap area 2. When the process is scheduled for execution: reset the MMU for the new process (make current page table current) and flush TLB bring the process’ pages in memory to reduce initial page faults 3. When a page faults occurs: read register to determine where page fault occurs compute the page needed and find the page frame to accommodate the new page back up PC to execute the instruction again 4. When a process exits OS releases the page table, pages, and disk space CR2 in x86 processors
Page Fault Handling Hardware traps to kernel - save PC on stack 2. General registers saved - using an assembly program 3. OS determines which virtual page needed - reading a hardware register (for x86, CR2) 4. OS checks validity of address, seeks page frame - address is valid and no protection fault - no free page frame, invoke page replacement algorithm 5. If selected frame is dirty, write it to disk - context switch takes place, suspend the faulting process and start the disk transfer process - mark the frame busy
Page Fault Handling (cont’d) 6. OS schedules new page in from disk - looks up the disk address where the needed page is and schedule a disk read 7. Page tables updated - mark the page frame to be in normal state 8. Faulting instruction backed up to when it began 9. Faulting process scheduled 10. Reload registers and continues program
Page Fault Handling on x86 CPU register CR3 contains the physical address of the page directory. Each process has its own page directory plus page tables and pages they point to At process switch, CR3 is loaded with the physical address of the scheduled process’ page directory and this causes a whole process image to be mapped in Loading of CR3 flushes data/instruction cache and TLBs
Page Fault Handling on x86(cont’d) 1. MMU sees V=0 in a needed PTE, causes trap via vector number 0x14. IDT[0x14] has the fault handling routine. MMU puts faulting linear address in CR2 2. Assembly routine saves registers (this is part of the OS) 3. PF handler (in C) figures out which page not present 4. OS checks if this page is a valid page of this process (else usually kill process), get page from free list. 5. Drop this step (the free list handling gets dirty pages written out earlier) 6. OS locates needed data, often in executable file or swap space, schedules disk i/o, blocks this process
Page Fault Handling on x86(cont’d) 7. Disk interrupt signals page in, PTE updated, wakeup process. 8. Faulting instruction needs re-executing—process is still in kernel after scheduling, back in PF handler, with user PC on bottom of stack where it can be adjusted (backed up) (this is done in the PF handler, not the disk interrupt). 9. The PF handler returns to the assembly routine 10. The as routine does the iret, using the backed-up PC, and resumes user execution. 11. (added) The user code re-executes the instruction that caused the PF, more successfully this time
Example of Page Faults 1. First reference to data page—page contents are in executable file. PF handler blocks. 2. First reference to BSS page (uninitialized data)—no blocking, just assign page from free list. 3. Reference that extends the user stack—same as 2. 4. First reference to text page (code)—as in 1, or if this program is in use by another process, arrange sharing of code page already in memory. 5. Re-reference after pageout to swap space—block while read in from swap space. 6. Reference to address outside of program image: fails validity test in step 4 above, causes “segmentation violation” in Solaris, usually kills process. 7. Ref to malloc’d memory (heap): malloc itself only allocates swap space, not real memory, so the memory is added by PFs, like #2.