8. Demand Paging การนำเฉพาะเพจที่ต้องการใช้เข้ามาไว้ในหน่วยความ มีข้อดีคือ Less I/O needed Less memory needed Faster response More users Page is needed reference to it invalid reference abort not-in-memory bring to memory Lazy swapper – จะไม่นำเพจเข้ามาในหน่วยความจำถ้าไม่มีความจำเป็นที่จะใช้เพจนั้น swapper มักใช้กรณีจัดการสลับโปรเซส ( เข้าออกหน่วยความจำ ) แต่ในการนำเพจ เข้า - ออกหน่วยความจำ เราจะใช้ pager
9. Transfer of a Paged Memory to Contiguous Disk Space เมื่อโปรเซสถูกนำกลับเข้ามาไว้ในหน่วยความจำ ( swapped in ) pager จะเดาว่าเพจใดจะถูกใช้ก่อนที่โปรเซสจะถูกเอาออกจากหน่วยความจำ ( swapped out ) pager จะนำเฉพาะเพจที่จำเป็นเอาเข้ามาไว้ในหน่วยความจำ
10. Valid-Invalid Bit ข้อมูลแต่ละ record ใน page table มีบิท valid–invalid bit เป็นการระบุว่า เพจที่ต้องการอยู่ในหน่วยความจำหลักแล้วหรือไม่ ( v in-memory, i not-in-memory) เริ่มต้นค่าของ valid–invalid bit จะถูกตั้งเป็น i ไว้สำหรับทุก ๆ entries Example of a page table snapshot: ในช่วงของการแปลงแอดเดรส ถ้า valid–invalid bit ใน page table เป็น i อาจเกิด page fault Page fault คือ เพจที่ต้องการให้ทำงาน ไม่ได้อยู่ในหน่วยความจำหลักขณะนั้น v v v v i i i … . Frame # valid-invalid bit page table
11. Page Table When Some Pages Are Not in Main Memory การตั้งค่าเป็น invalid ไว้จะไม่มีผลอะไรถ้าโปรเซสไม่พยายาม access เพจนั้น
12. Hardware Instruction Processing Algorithm Start processing instruction Generate data address Compute page number If page is in memory Then get data and finish instruction advance to next instruction return to step 1 Else generate page interrupt call page fault handler
13. Page Fault ถ้า executes หรือ accesses เพจที่อยู่ในหน่วยความจำ ( memory resident ) การทำงานจะดำเนินอย่างปกติ ถ้าเพจที่มีการอ้างถึงไม่อยู่ในหน่วยความจำ จะมี trap ส่งไปที่ระบบปฏิบัติการ เรียกว่าเกิด page fault ขึ้น Operating system looks at another table to decide: Invalid reference abort Just not in memory Get empty frame Swap page into frame Reset tables Set validation bit = v Restart the instruction that caused the page fault block move auto increment/decrement location
14. Steps in Handling a Page Fault Page table – has the ability to mark an entry invalid through a valid-invalid bit or special value of protection bits ดูจาก internal table (kept with the PCB) ว่าโปรเซสมีการอ้างอิงแอดเดรสที่ valid หรือ invalid ในการพยายาม access หน่วยความจำ ถ้าการอ้างอิงแอดเดรสที่ invalid ให้ terminate โปรเซส แต่ถ้าเป็นแบบ valid แต่ยังไม่ถูกนำเข้ามา ให้หาเฟรมที่ว่าง ( free frame ) ทำการอ่านดึงเพจที่ต้องการจากดิสต์มาไว้ยังเฟรมที่ว่าง ปรับปรุงข้อมูลใน page table ( ระบุเพจที่เอาเข้ามาอยู่ในเฟรมไหน และเปลี่ยนค่า valid-invalid bit) Restart คำสั่งที่ทำให้เกิด interrupted จาก trap
15. Page Fault Handler Algorithm If there is no free page frame Then Select page to be swapped out using page removal algorithm Update job’s page map table If content of page had been changed then Write page to disk End if End if 2. Use page number from step 3 from the hardware instruction processing algorithm to get disk address where the requested page is stored. 3. Read page into memory. 4. Update job’s page map table. 5. Update memory map table. 6. Restart interrupted instruction.
16. Sequence occurs caused by a Page fault Trap to the operating system Save the user registers and process state Determine the interrupt was a page fault Check that the page reference was legal and determine the location of the page on the disk Issue a read from the disk to a free frame: Wait in a queue for device until the read request is serviced Wait for the device seek and/or latency time Begin the transfer of the page to a free frame While waiting , allocate the CPU to some other user (CPU scheduling optional) Receive an interrupt from the disk I/O subsystem (I/O completed) Update page table (show that the desired page is now in memory) Wait for the CPU to be allocated to this process again Restore the user registers, process state, new page table, then resume the interrupted instruction
17. Performance of Demand Paging กำหนดให้อัตราการเกิด Page Fault ( p ) มีค่าระหว่าง 0 – 1 ( 0 p 1.0) ถ้า p = 0 แสดงว่าไม่เกิด page faults Effective access time เท่ากับเวลาที่ใช้ในการ access หน่วยความจำ ถ้า p = 1 แสดงว่าทุก ๆ ครั้งที่อ้างอิงเพจ เกิด page fault Effective Access Time (EAT) EAT = (1 – p ) x memory access + p (page fault overhead + swap page out + swap page in + restart overhead) ตัวอย่าง Memory access time = 200 nanoseconds Average page-fault service time = 8 milliseconds EAT = (1 – p) x 200 + p (8 milliseconds) = (1 – p x 200 + p x 8,000,000 = 200 + p x 7,999,800 If one access out of 1,000 causes a page fault, then EAT = 8.2 microseconds. This is a slowdown by a factor of 40!!
19. Copy-on-Write Copy-on-Write (COW) เป็นวิธีที่ทั้งโปรเซส parent และ child มีการ initially share เพจเดียวกันในหน่วยความจำ ไม่ว่าโปรเซสใด modifies เพจที่ใช้ร่วมกัน เพจนั้นก็จะถูกคัดลอก ตัวอย่างเช่น ถ้าโปรเซสลูกพยายามที่จะ modify เพจที่มีการตั้งเป็น COW OS ก็จะทำการคัดลอกเพจนั้นให้ โปรเซสก็สามารถ modify เพจที่ถูกคัดลอกไป จะไม่ไปยุ่งกับเพจต้นฉบับที่โปรเซสแม่เป็นเจ้าของ เมื่อมีการคัดลอกหรือ duplicate เพจ จะต้องมีการจัดสรร free page OS หลายตัวที่มีการจัดเตรียม pool ของ free page โดยใช้เทคนิค zero-fill-on-demand. Free pages are allocated from a pool of zeroed-out pages – erasing the previous contents COW allows more efficient process creation as only modified pages are copied
23. Need For Page Replacement โปรแกรมของ user 1 เมื่อ execute เพจ 1 ซึ่งเจอคำสั่งให้ load M ( ในขณะที่ M อยู่ในเพจ 3 และไม่ได้อยู่ในหน่วยความจำในขณะนี้ ) 2. ไม่มีเฟรมว่าง
24. Basic Page Replacement Find the location of the desired page on disk Find a free frame: - If there is a free frame, use it - If there is no free frame, use a page replacement algorithm to select a victim frame Bring the desired page into the (newly) free frame; update the page and frame tables Restart the process
25. Page Replacement Algorithms ต้องการให้อัตราการเกิด page-fault ต่ำที่สุด การทดสอบอัลกอริธึมต่าง ๆ จะอาศัยชุดของสตริง (reference string) อ้างอิงแทนการเรียกใช้เพจต่างๆ และนำมาหาค่าการเกิด page faults reference string ที่นำมาใช้ได้แก่ 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 ซึ่งคาดหวังว่าอัตราการเกิด page fault จะลดลงเป็นสัดส่วนตรงกับการเพิ่มจำนวนเฟรม Graph of Page Faults Versus The Number of Frames
28. FIFO Page Replacement 3 frames are initially empty First 3 references (7,0,1) cause page faults and are brought into empty frames 15 faults altogether
29. Optimal Algorithm เป็นการแทนที่เพจที่จะยังไม่มีการเรียกใช้อีกนาน ( page that will not be used for longest period of time ) 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 How do you know this? Used for measuring how well your algorithm performs 1 2 3 4 6 page faults 4 5
30. Optimal Page Replacement The first 3 references cause faults that fill the 3 empty frames The reference to page 2 replaces page 7, because 7 will not be used until reference 18, whereas page 0 will be used at 5, and page 1 at 14. The reference to page 3 replaces page 1, as page 1 will be the last of the three pages in memory to be referenced again. Total 9 page faults
32. LRU Algorithm การแทนที่เพจ แบบ LRU สามารถพัฒนาโดยใช้หลัการของ stack algorithm หมายความว่า เพจที่มีการทำงาน จะอยู่บนสุดของ stack เสมอ เพจที่ไม่ได้ถูกทำงานมาเป็นเวลานานก็จะอยู่ด้านล่างของ stack เพจที่อยู่ด้านล่างสุดของ stack ก็จะถูกแทนที่ ( หรือถูกนำออกจากหน่วยความจำหลัก ) keep a stack of page numbers in a double link form: Page referenced: move it to the top requires 6 pointers No search for replacement Use Of A Stack to Record The Most Recent Page References
33. LRU - Clock Counter implementation Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter When a page needs to be changed, look at the counters to determine which are to change ดัดแปลงมาจาก การแทนที่เพจ แบบ LRU แต่อาศัยคิววงกลม (circular queue) ใช้ pointer ในการชี้ดูตาม reference bit ของแต่ละเพจ โดยเคลื่อนตามเข็มนาฬิกา reference bit เป็นตัวระบุว่า มีการใช้งานเพจนั้นหรือไม่ ถ้าเพจที่ถูกใช้งาน จะมี reference bit เป็น 1 เมื่อ pointer นี้เคลื่อนผ่านตามจังหวะของนาฬิกา ก็จะมีการเปลี่ยน reference bit ของเพจ จาก 1 เป็น 0 ถ้าเพจนั้นไม่ถูกใช้งาน แต่จะยังไม่มีการแทนที่เพจนั้น เพจที่มี reference bit เป็น 0 จะถูกแทนที่
37. Enhanced Second-Chance Algorithm Consider the reference and modify bit Counting Algorithms Keep a counter of the number of references that have been made to each page LFU Algorithm : replaces page with smallest count MFU Algorithm : based on the argument that the page with the smallest count was probably just brought in and has yet to be used Modified Referenced Meaning Case 1 0 0 Not modified AND not referenced Case 2 0 1 Not modified BUT was referenced Case 3 1 0 Was modified BUT not referenced (impossible?) Case 4 1 1 Was modified AND referenced
39. Allocation of Frames แต่ละโปรเซสต้องการ minimum number of pages การจัดสรรเฟรมแบ่งเป็น การจัดสรรเฟรมแบบคงที่ ( fixed allocation ) แบ่งเฟรมให้ทุก ๆ โปรเซสเท่ากัน ( Equal allocation ) เช่น จัดสรร 100 เฟรมให้กับ 5 โปรเซส แต่ละโปรเซสจะได้ 20 เฟรมเท่ากัน แบ่งตามสัดส่วน ( Proportional allocation ) – ขึ้นกับขนาดของโปรเซส เช่น การจัดสรรเฟรมแบ่งตามความสำคัญ ( priority allocation ) แบ่งตามสัดส่วน แต่ใช้ความสำคัญ ( priorities ) แทนขนาดของโปรเซส ถ้า process P i generates a page fault ให้เลือกแทนที่เพจจากเฟรมของโปรเซสนั้นเอง หรือแทนที่เฟรมจากโปรเซสที่มี priority ต่ำกว่า
40. Allocation of Frames การแทนที่เพจแบ่งเป็น Global replacement โปรเซสเลือกแทนที่เฟรมใด ๆ ก็ได้ อาจจะแทนที่เฟรมที่จัดสรรให้กับโปรเซสอื่น Local replacement แต่ละโปรเซสจะต้องแทนที่เพจ จากเฟรมที่ถูกจัดสรรให้กับโปรเซสนั้นเท่านั้น
41. Thrashing ถ้าจัดสรรเฟรมให้กับโปรเซสไม่เพียงพอ จะทำให้เกิด page-fault สูง ผลตามมาคือ low CPU utilization ซีพียูแทบไม่ได้ประมวลผล ต้องคอยนำเพจเข้า - ออก OS เข้าใจว่าไม่มีงานออกมา ก็จะนำโปรเซสเพิ่มเข้ามา ( increase the degree of multiprogramming ) โปรเซสที่ถูกนำเข้ามาต้องจัดสรรหน่วยความจำให้ ( เกิด page fault เพิ่มขึ้นอีก ) Thrashing a process is busy swapping pages in and out เป็นอาการที่มีการสลับเปลี่ยนเพจไปมาจำนวนมาก ระหว่างหน่วยความจำหลักและหน่วยความจำสำรอง ( คือเกิด page fault สูง ) ระบบทำงานไม่มีประสิทธิภาพ โดยสาเหตุมักเกิดจากมีการนำเพจหนึ่งๆ ออกจากหน่วยความจำหลักและมีการเรียกใช้เพจนั้นในเวลาต่อมา ก็ต้องนำเพจนั้นกลับเข้ามายังหน่วยความจำหลักอีกในช่วงเวลาสั้นๆ เช่น ถ้ามีการจัดสรรเฟรมจำนวนหนึ่งให้กับงานหนึ่งซึ่งถูกแบ่งแล้ว มีจำนวนเพจมาก กว่าจำนวนเฟรม ทำให้ต้องมีการนำเพจเข้าออกสลับเปลี่ยนไปมาในเฟรมที่จัดสรรให้ กรณีที่งานหรือโปรแกรมมีการทำงานแบบวนซ้ำ ( loop) แล้วมีการแบ่งในส่วนที่เป็น loop ออกเป็นคนละเพจ โอกาสที่จะเกิด thrashing เป็นไปได้สูง ถ้ามีการจัดสรรเฟรม 1 เฟรม สำหรับให้เพจ 2 เพจสลับกันเข้ามาทำงาน
42. Thrashing (Cont.) Why does demand paging work? Locality model Process migrates from one locality to another Localities may overlap Why does thrashing occur? size of locality > total memory size
43. Locality In A Memory-Reference Pattern A locality คือกลุ่มของเพจ ที่จะถูกใช้ด้วยกัน ( actively used together ) ในโปรแกรมหนึ่งๆ มักประกอบด้วยหลาย ๆ localities ซึ่งอาจจะมีการซ้อนกันอยู่ Localities มักจะกำหนดโดยโครงสร้างของโปรแกรม และโครงสร้างของข้อมูล เช่นถ้าเราจัดสรรเฟรมให้กับการทำงานในส่วนของ current locality อาจจะเกิด page fault จนกว่าทุก ๆ เพจใน locality นี้ถูกนำเข้ามาในหน่วยความจำ จากนั้นก็จะไม่เกิด page fault อีกจนกระทั่งมีการเปลี่ยนไปทำงานในอีก locality
44. Working-Set Model มีการคิดวิธีในการเพิ่มประสิทธิภาพในการทำ demand paging โดยอาศัยแนวคิดของ working set Working Set หมายถึงกลุ่ม ( set) ของเพจที่อยู่ในหน่วยความจำหลัก ( residing in memory) ซึ่งสามารถทำงานได้ทันทีโดยไม่เกิด page fault การที่จะนำทุกเพจที่ต้องการ ( working set) มาไว้ในหน่วยความจำได้นั้น มักขึ้นอยู่กับโครงสร้างของโปรแกรมที่นำไปสู่ในเรื่องของการอ้างอิงเฉพาะที่ ( locality of reference) การอ้างอิงเฉพาะที่ ( locality of reference) หมายความว่า โปรแกรมที่มีการแบ่งโครงสร้างที่ดี การอ้างอิงเพจในแต่ละเฟสในระหว่างที่โปรแกรมทำงานก็มักจะอยู่ภายในเพจ หรืออยู่เฉพาะที่ ถ้าทุกๆ เพจหรือ working set ถูกนำเข้ามาไว้ในหน่วยความจำหลักทั้งหมด ก็จะไม่เกิด page fault เป็นการเพิ่มความไวในการประมวลผลด้วย ( speed up processing) A working-set model is based on the assumption of locality
45. Working-Set Model working-set window a fixed number of page references WSS i (working set of Process P i ) = total number of pages referenced in the most recent (varies in time) if too small will not encompass entire locality if too large will encompass several localities if = will encompass entire program D = WSS i จำนวนเฟรมที่ต้องการทั้งหมด ( demand frames ) ถ้าจำนวนเฟรมที่ต้องการน้อยกว่าจำนวนเฟรมที่ว่าง if D > m Thrashing Policy if D > m, then suspend one of the processes
46. Page-Fault Frequency Scheme แทนที่จะดูจาก ( หรือหาขนาดที่เหมาะสมของ ) working set ให้ดูจากอัตราการเกิด page fault แทน Establish “acceptable” page-fault rate If actual rate too low, process loses frame If actual rate too high, process gains frame
47. Allocating Kernel Memory Treated differently from user memory Often allocated from a free-memory pool Kernel requests memory for structures of varying sizes The kernel must use memory conservatively and attempt to minimize waste due to fragmentation. Some kernel memory needs to be contiguous Some hardware devices interact directly with physical memory – without the benefit of a virtual memory interface – and consequently may require memory residing in physically contiguous pages. Two strategies for managing free memory that is assigned to kernel processes: Buddy System Slab Allocation
48. Buddy System Allocates memory from fixed-size segment consisting of physically-contiguous pages Memory allocated using power-of-2 allocator Satisfies requests in units sized as power of 2 Request rounded up to next highest power of 2 When smaller allocation needed than is available, current chunk split into two buddies of next-lower power of 2 Continue until appropriate sized chunk available
49. Slab Allocator Alternate strategy Slab is one or more physically contiguous pages Cache consists of one or more slabs Single cache for each unique kernel data structure Each cache filled with objects – instantiations of the data structure When cache created, filled with objects marked as free When structures stored, objects marked as used If slab is full of used objects, next object allocated from empty slab If no empty slabs, new slab allocated Benefits include no fragmentation, fast memory request satisfaction
51. Other Issues – Prepaging & Page Size การเตรียมเพจล่วงหน้า ( Prepaging ) ลดจำนวนการเกิด page faults เมื่อโปรเซสเริ่มทำงาน เตรียมก่อนที่จะมีการ reference เพจ แต่ถ้าเตรียมเพจมาล่วงหน้าแล้วเพจนั้นไม่ได้ใช้ ( unused ) จะเป็นการเสียพื้นที่หน่วยความจำ และเสียเวลาในการอ่านจาก I/O Assume s pages are prepaged and α of the pages is used Is cost of s * α save pages faults > or < than the cost of prepaging s * (1- α ) unnecessary pages ? α near zero prepaging loses การเลือกขนาดของเพจ ( Page size ) ต้องพิจารณาจาก fragmentation table size I/O overhead locality
52. Other Issues – TLB Reach Increase the Page Size This may lead to an increase in fragmentation as not all applications require a large page size Provide Multiple Page Sizes This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation TLB Reach - The amount of memory accessible from the TLB TLB Reach = (TLB Size) X (Page Size) Ideally, the working set of each process is stored in the TLB Otherwise there is a high degree of page faults
53. Other Issues – Program Structure Program structure Int[128,128] data; Each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i,j] = 0; 128 x 128 = 16,384 page faults Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0; 128 page faults
54. Other Issues – I/O interlock I/O Interlock – Pages must sometimes be locked into memory Consider I/O - Pages that are used for copying a file from a device must be locked from being selected for eviction by a page replacement algorithm Reason Why Frames Used For I/O Must Be In Memory
55. Other Issues – Real-Time Processing ทั้งหมดที่กล่าวมาคำนึงถึงการใช้พื้นที่หน่วยความจำให้มีประโยชน์มากที่สุด เป็นการเพิ่ม throughput ของระบบโดยรวม แต่บางโปรเซสอาจได้รับผลกระทบ อาจเกิด page fault ในระหว่างที่มันประมวลผล โปรเซสประเภท real-time เป็นโปรเซสที่ต้องการประมวลผล ( ครอบครอง cpu) เพื่อทำงานให้เสร็จโดยให้เกิด delay ที่น้อยที่สุด Virtual Memory is the antithesis of real-time computing, because it can introduce unexpected long-term delays in the execution of a process while pages are brought into memory.
56. Summary Job no longer constrained by the size of physical memory in demand paging scheme Virtual memory allows programs to be executed even though they are not stored entirely in memory Virtual memory also allows the degree of multiprogramming to be raised, increasing CPU utilization. Pure demand paging never brings in a page util that page is referenced. The first reference causes a page fault to OS resident monitor If total memory requirements exceed the physical memory, then it may be necessary to replace pages from memory to free frames for new pages. FIFO page replacement is easy but suffers from Belady’s anomaly LRU scheme results in slightly better efficiency as compared to FIFO scheme If a process does not have enough memory for its working set, it will thrash.