Operating System Lecture 9 : Virtual Memory (462271) วัตถุประสงค์ นักศึกษาสามารถอธิบายข้อดีในการใช้หน่วยความจำเสมือน นักศึกษาสามารถเรียนรู้และอธิบาย  concepts  ของ   demand paging   อัลกอริธึมในการทำ   page-replacement  และการจัดสรร   page frames นักศึกษาสามารถอธิบายหลัการของ  working-set model
Contents Background Demand Paging Copy-on-Write Memory-Mapped Files Page Replacement Allocation of Frames  Thrashing Allocating Kernel Memory Other Considerations
Background ข้อเสียของการจัดการหน่วยความจำในยุคแรก ๆ ทุกส่วนของโปรแกรมจำเป็นต้องอยู่ติดกันในหน่วยความจำ พื้นที่หน่วยความจำบางส่วนไม่สามารถใช้ได้ เนื่องจากเป็นชิ้นเล็กชิ้นน้อยอยู่อย่างกระจัดกระจาย  ( เกิด  Fragmentation ) มี  Overhead  ในการทำ   relocation การใช้หน่วยความจำเสมือน สามารถช่วยแก้ปัญหาได้โดย กำจัดข้อจำกัดของการนำทุกส่วนของโปรแกรมมาอยู่ติดกัน ในหน่วยความจำ กำจัดข้อจำกัดของการนำทุกส่วนของโปรแกรมมาไว้ในหน่วยความจำ   โปรแกรมสามารถมีขนาดใหญ่กว่าขนาดของหน่วยความจำได้ การสร้างหน่วยความจำเสมือนสามารถ   implemented  โดยวิธี : Demand paging  Demand segmentation
Virtual Memory That is Larger Than Physical Memory 
Virtual-address Space Virtual address space  ของโปรเซสหนึ่งๆ หมายถึง  logical (virtual) view  ในการจัดเก็บโปรเซสลงในหน่วยความจำ Heap  โตขึ้นด้านบน  ( grows upward )  ในหน่วยความจำ ใช้สำหรับจัดสรรหน่วยความจำแบบ   dynamic  Stack  โตจากบนลงล่าง  ( grows downward )   ในหน่วยความจำ   เก็บค่าแอดเดรสและตัวแปร  local  เมื่อมีการเรียกใช้ฟังก์ชัน ช่องว่าง  ( hole )   ที่อยู่ระหว่าง   heap  และ   stack  เป็นส่วนหนึ่งของ   virtual address space  Virtual address spaces  ที่มีอยู่   holes  เรียกว่า  sparse address spaces
Shared Library Using Virtual Memory หลาย ๆ โปรเซสสามารถใช้  System libraries  ร่วมกันโดยการ   mapping of the shared object  ไปใน  virtual address space.
Demand Paging เพจจะถูกนำมาไว้ในหน่วยความจำ ก็ต่อเมื่อต้องการ ทำให้สามารถทำงานได้แม้นว่าจะเหลือพื้นที่หน่วยความจำไม่มาก โดยใช้แนวคิดที่ว่า โปรแกรมหรืองาน จะมีการทำงานตามลำดับ เพราะฉะนั้นจึงไม่จำเป็นต้องนำทุกๆ เพจของงานมาไว้ในหน่วยความจำในทีเดียว ตัวอย่างเช่น  ส่วนของโมดูลที่ไว้มีไว้ดักจับถ้าเกิดข้อผิดพลาดขึ้น  ( User-written error handling modules)  ซึ่งโมดูลนี้จะทำงานก็ต่อเมื่อเกิดข้อผิดพลาดตามที่ผู้ใช้ตั้งหรือกำหนดเงื่อนไขไว้ โมดูลที่เป็นลักษณะเลือกทำงานเพียงอย่างใดอย่างหนึ่ง  (Mutually exclusive module s) ทางเลือกบางอย่างที่ไม่จำเป็นต้องทำงานเสมอไป  (Certain program options are not always accessible)
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
Transfer of a Paged Memory to Contiguous Disk Space เมื่อโปรเซสถูกนำกลับเข้ามาไว้ในหน่วยความจำ  ( swapped in )  pager  จะเดาว่าเพจใดจะถูกใช้ก่อนที่โปรเซสจะถูกเอาออกจากหน่วยความจำ  ( swapped out ) pager  จะนำเฉพาะเพจที่จำเป็นเอาเข้ามาไว้ในหน่วยความจำ
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
Page Table When Some Pages Are Not in Main Memory การตั้งค่าเป็น  invalid  ไว้จะไม่มีผลอะไรถ้าโปรเซสไม่พยายาม  access  เพจนั้น
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
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
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
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.
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
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!!
Process Creation Virtual memory  มีข้อดีในขณะที่มีการสร้างโปรเซสใหม่  ( process creation ) : Copy-on-Write โปรเซสที่ถูกสร้างโดย  fork()  สามารถ  by pass  การทำ  demand paging  เพราะสามารถใช้เพจร่วมกับ  parent  ของมันได้ การที่โปรเซส  parent  และ  child  จะใช้เพจร่วมกัน จะใช้วิธี  copy-on-write วิธีนี้ช่วยลดจำนวนเพจที่จะต้องจัดสรรให้กับโปรเซสที่เกิดใหม่ Memory-Mapped Files การ  access  ไฟล์  (open, read, write)  ที่เรียกใช้   system call  และ   disk access   สามารถใช้   virtual memory technique  ในการ  treat file I/O  ให้เหมือนกับ   routine  ในการ   access   หน่วยความจำ โดยการทำ  memory mapping
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
Memory-Mapped Files Memory-mapped file I/O  เป็นการใช้งาน  ( treat) file I/O  ดังเช่นการ  access  หน่วยความจำโดยการ  mapping  a disk block  ไปยังเพจในหน่วยความจำ การอ่านไฟล์ ก็ทำในลักษณะ  demand paging.  บางส่วนของไฟล์ถูกอ่านจาก  file system  มาลงใน  physical page  ( ในหน่วยความจำ )  การอ่าน / เขียนต่อจากนั้นก็จะเหมือนการ  access  หน่วยความจำปกติ การ  access  ไฟล์ผ่านทางหน่วยความจำง่ายกว่าการใช้  read()   write()  system calls หลาย ๆ โปรเซสสามารถ   map  ไฟล์เดียวกัน สามารถใช้เพจร่วมกันได้  ( shared page)
What happens if there is no free frame? Page fault handler   เป็นการจัดการที่ระบบปฏิบัติจัดการเวลาเกิด  page fault  โดยดูว่า มีเฟรมว่างในหน่วยความจำหลักหรือไม่  ถ้ามี ก็จะนำเพจที่ต้องการโดยคัดลอกจากหน่วยความจำสำรองมาไว้ในเฟรมที่ว่างอยู่ ถ้าไม่มีเฟรมว่างก็ต้องเลือกเพจใดเพจหนึ่งสลับออกไป โดยขึ้นอยู่กับนโยบายที่กำหนด Page replacement –  หาเพจหนึ่งในหน่วยความจำ ที่ไม่ได้ใช้  swap  ออกไป มีหลาย ๆ  algorithm   ในการแทนที่เพจ ดู  performance –  เลือกอัลกอริธึมที่ทำให้เกิดจำนวน  page faults   น้อยที่สุด ถ้าเพจใดถูกนำกลับเข้ามาในหน่วยความจำหลาย ๆ ครั้ง  ( ถูกนำออกไปหลายครั้ง )  ถือว่ามี  system performance   ไม่ดี   ( unacceptable ) Locality of reference  ใช้ช่วยแก้ปัญหา   page fault
Page Replacement เพื่อป้องกันการจัดสรรหน่วยความจำให้กับแต่ละโปรเซสเกินความจำเป็น จะต้องมีการปรับปรุงรูทีนในการจัดการ  page-fault  โดยเพิ่มการทำ   page replacement   ลงไป โดยการใช้  modify (dirty) bit   เพื่อลด  overhead  ในการถ่ายโอนเพจ  –  เฉพาะเพจที่มีการ   modified  ที่มีจะมีการเขียนลง  disk การทำ  Page replacement  ทำให้แยกการใช้งาน  logical memory  จาก   physical memory –   หมายความว่า   large virtual memory  สามารถที่จะถูกจัดสรรลงบน  smaller physical memory   ขนาดของงานไม่มีปัญหากับขนาดของหน่วยความจำหลัก  ( โดยอาศัยหลักการของการใช้หน่วยความจำเสมือน )  ในการสลับเปลี่ยนเพจออกจากเฟรมหน่วยความจำหลัก จะต้องมีนโยบายหรือวิธี ในการคัดเลือกเพจออกจากเฟรม โดยต้องคำนึงถึงประสิทธิภาพโดยรวมของระบบ
Need For Page Replacement โปรแกรมของ  user 1  เมื่อ  execute  เพจ   1  ซึ่งเจอคำสั่งให้  load M  ( ในขณะที่  M  อยู่ในเพจ  3   และไม่ได้อยู่ในหน่วยความจำในขณะนี้ )  2.  ไม่มีเฟรมว่าง
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
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
First-In-First-Out (FIFO) Algorithm เป็นการแทนที่เพจที่อยู่ในหน่วยความจำมานานที่สุด  ( คือเข้ามาก่อน  ( อยู่นานที่สุด )  ก็ต้องถูกนำออกไปก่อน  Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 อัลกอริธึมนี้ทำให้เกิด  Belady’s Anomaly: more frames    more page faults
FIFO Illustrating Belady’s Anomaly การแทนที่เพจ แบบ  FIFO  มีข้อเสียคือ ไม่สามารถรับประกันได้ว่า การเพิ่มจำนวนเฟรม จะทำให้การเกิด  page fault  ลดลง
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
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
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
Least Recently Used (LRU) Algorithm การแทนที่เพจ แบบ  LRU  จะนำเพจที่ไม่มีการใช้งานมาเป็นเวลานานออกจากหน่วยความจำก่อน โดยคำนึงว่าเพจที่ไม่มีการถูกใช้งานมาเป็นเวลานานแล้วนั้น จะไม่ถูกใช้งานอีกในเร็วๆ นี้  ในทางกลับกัน เพจที่เพิ่งจะมีการทำงาน ก็น่าจะถูกใช้งานอีกในเร็วๆ นี้  ( ตามทฤษฎีของ  locality)  Reference string:  1, 2, 3, 4, 1, 2,  5 , 1, 2,  3 ,  4 ,  5 12 faults 5 2 4 3 1 2 3 4 1 2 5 4 1 2 5 3 1 2 4 3
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
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  จะถูกแทนที่
LRU- Second Chance (clock) Page Replacement
LRU- Second Chance (clock) Page Replacement (2)
LRU- Second Chance (clock) Page Replacement (3)
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
Page Replacement Policy - Bit-shifting ดัดแปลงมาจาก การแทนที่เพจ แบบ  LRU  ใช้  8-bit reference  และอาศัยหลักการ  shift bit  ( เลื่อนไปด้านขวา ทีละ  1  บิท ) เมื่อเพจแรกเริ่มเข้ามาอยู่ในหน่วยความจำหลัก บิทซ้ายสุดของ  reference bit  จะถูกตั้งให้เป็น   1  อีก  7  บิทที่เหลือจะเป็น  0 ตามจังหวะของนาฬิกา ตัวจัดการหน่วยความจำจะเลื่อนบิทไปทางขวา  1  บิท ในขณะที่ ถ้าเพจไหนถูกใช้งานอีก บิทซ้ายสุดก็จะถูกตั้งให้เป็น  1  ส่วนเพจที่ไม่มีการใช้งาน จะเป็น  0 ถ้าผ่านไป  8  จังหวะนาฬิกา เพจที่ไม่ได้ถูกใช้งานเลยจะมี  reference  เป็น  00000000  ในขณะที่เพจที่มีการใช้งานทุกครั้งจะมี  reference byte  เป็น  11111111 เพจที่มีค่าใน  reference byte  น้อยที่สุด จะถูกแทนที่ เพราะไม่ถูกอ้างอิงหรือทำงาน มาเป็นเวลานานที่สุด
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  ต่ำกว่า
Allocation   of Frames การแทนที่เพจแบ่งเป็น Global replacement   โปรเซสเลือกแทนที่เฟรมใด ๆ ก็ได้ อาจจะแทนที่เฟรมที่จัดสรรให้กับโปรเซสอื่น Local replacement   แต่ละโปรเซสจะต้องแทนที่เพจ จากเฟรมที่ถูกจัดสรรให้กับโปรเซสนั้นเท่านั้น
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  เพจสลับกันเข้ามาทำงาน
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
Locality In A Memory-Reference Pattern A locality  คือกลุ่มของเพจ ที่จะถูกใช้ด้วยกัน  ( actively used together ) ในโปรแกรมหนึ่งๆ มักประกอบด้วยหลาย ๆ  localities   ซึ่งอาจจะมีการซ้อนกันอยู่ Localities  มักจะกำหนดโดยโครงสร้างของโปรแกรม และโครงสร้างของข้อมูล เช่นถ้าเราจัดสรรเฟรมให้กับการทำงานในส่วนของ  current locality   อาจจะเกิด  page fault  จนกว่าทุก ๆ เพจใน   locality  นี้ถูกนำเข้ามาในหน่วยความจำ จากนั้นก็จะไม่เกิด  page fault  อีกจนกระทั่งมีการเปลี่ยนไปทำงานในอีก  locality
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
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
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
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
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
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
Slab Allocation
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
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
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
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
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.
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.

More Related Content

PPT
PPTX
Andrew File System
PPT
Chapter 9 - Virtual Memory
PPTX
Scaling MongoDB to a Million Collections
PDF
Build an High-Performance and High-Durable Block Storage Service Based on Ceph
PPTX
File System in Operating System
PPT
PPT
Virtual memory
Andrew File System
Chapter 9 - Virtual Memory
Scaling MongoDB to a Million Collections
Build an High-Performance and High-Durable Block Storage Service Based on Ceph
File System in Operating System
Virtual memory

What's hot (20)

PDF
Disk Management
PDF
Storage Virtualization
PDF
Improving Apache Spark's Reliability with DataSourceV2
PDF
バックアップと障害復旧から考えるOracle Database, MySQL, PostgreSQLの違い - Database Lounge Tokyo #2
PPT
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
PPT
Chapter 10 - File System Interface
PPTX
Distributed Shared Memory
DOCX
Storage Area Networks Unit 1 Notes
DOCX
Storage Area Networks Unit 3 Notes
PPTX
Virtualization support by intel
PPT
Virtual memory
PPTX
Alfresco勉強会#26 alfresco community 5.0でssoを設定する
PPT
40 demand paging
PDF
Geopaparazzi & SMASH state of art 2019
PDF
Cephfs架构解读和测试分析
PPT
Set model and page fault.44
PPT
Virtual memory
PPTX
Virtual Memory in Operating System
PPTX
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
PDF
QGIS初級編2018
Disk Management
Storage Virtualization
Improving Apache Spark's Reliability with DataSourceV2
バックアップと障害復旧から考えるOracle Database, MySQL, PostgreSQLの違い - Database Lounge Tokyo #2
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
Chapter 10 - File System Interface
Distributed Shared Memory
Storage Area Networks Unit 1 Notes
Storage Area Networks Unit 3 Notes
Virtualization support by intel
Virtual memory
Alfresco勉強会#26 alfresco community 5.0でssoを設定する
40 demand paging
Geopaparazzi & SMASH state of art 2019
Cephfs架构解读和测试分析
Set model and page fault.44
Virtual memory
Virtual Memory in Operating System
Q1 Memory Fabric Forum: Memory expansion with CXL-Ready Systems and Devices
QGIS初級編2018
Ad

Similar to Lecture9 (20)

PPT
PPT
Ch9 2003
PPT
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
PDF
3 instruction design
PPTX
PDF
การเพิ่มประสิทธิภาพ Instance memory - Oracle 11G
 
PPT
4 ca-process structure
PPT
10 ca-mass-storage system structure (wichet p.'s conflicted copy 2012-12-17)
PDF
อุปกรณ์รับและแสดงผล
PDF
Com sys 5
PPT
Information Technology for Business
PDF
Computer past 1
PDF
Computer past 1
PDF
PDF
PDF
PDF
Lesson
Ch9 2003
6 ca-virtual memory structure (wichet p.'s conflicted copy 2012-12-17)
3 instruction design
การเพิ่มประสิทธิภาพ Instance memory - Oracle 11G
 
4 ca-process structure
10 ca-mass-storage system structure (wichet p.'s conflicted copy 2012-12-17)
อุปกรณ์รับและแสดงผล
Com sys 5
Information Technology for Business
Computer past 1
Computer past 1
Lesson
Ad

Lecture9

  • 1. Operating System Lecture 9 : Virtual Memory (462271) วัตถุประสงค์ นักศึกษาสามารถอธิบายข้อดีในการใช้หน่วยความจำเสมือน นักศึกษาสามารถเรียนรู้และอธิบาย concepts ของ demand paging อัลกอริธึมในการทำ page-replacement และการจัดสรร page frames นักศึกษาสามารถอธิบายหลัการของ working-set model
  • 2. Contents Background Demand Paging Copy-on-Write Memory-Mapped Files Page Replacement Allocation of Frames Thrashing Allocating Kernel Memory Other Considerations
  • 3. Background ข้อเสียของการจัดการหน่วยความจำในยุคแรก ๆ ทุกส่วนของโปรแกรมจำเป็นต้องอยู่ติดกันในหน่วยความจำ พื้นที่หน่วยความจำบางส่วนไม่สามารถใช้ได้ เนื่องจากเป็นชิ้นเล็กชิ้นน้อยอยู่อย่างกระจัดกระจาย ( เกิด Fragmentation ) มี Overhead ในการทำ relocation การใช้หน่วยความจำเสมือน สามารถช่วยแก้ปัญหาได้โดย กำจัดข้อจำกัดของการนำทุกส่วนของโปรแกรมมาอยู่ติดกัน ในหน่วยความจำ กำจัดข้อจำกัดของการนำทุกส่วนของโปรแกรมมาไว้ในหน่วยความจำ โปรแกรมสามารถมีขนาดใหญ่กว่าขนาดของหน่วยความจำได้ การสร้างหน่วยความจำเสมือนสามารถ implemented โดยวิธี : Demand paging Demand segmentation
  • 4. Virtual Memory That is Larger Than Physical Memory 
  • 5. Virtual-address Space Virtual address space ของโปรเซสหนึ่งๆ หมายถึง logical (virtual) view ในการจัดเก็บโปรเซสลงในหน่วยความจำ Heap โตขึ้นด้านบน ( grows upward ) ในหน่วยความจำ ใช้สำหรับจัดสรรหน่วยความจำแบบ dynamic Stack โตจากบนลงล่าง ( grows downward ) ในหน่วยความจำ เก็บค่าแอดเดรสและตัวแปร local เมื่อมีการเรียกใช้ฟังก์ชัน ช่องว่าง ( hole ) ที่อยู่ระหว่าง heap และ stack เป็นส่วนหนึ่งของ virtual address space Virtual address spaces ที่มีอยู่ holes เรียกว่า sparse address spaces
  • 6. Shared Library Using Virtual Memory หลาย ๆ โปรเซสสามารถใช้ System libraries ร่วมกันโดยการ mapping of the shared object ไปใน virtual address space.
  • 7. Demand Paging เพจจะถูกนำมาไว้ในหน่วยความจำ ก็ต่อเมื่อต้องการ ทำให้สามารถทำงานได้แม้นว่าจะเหลือพื้นที่หน่วยความจำไม่มาก โดยใช้แนวคิดที่ว่า โปรแกรมหรืองาน จะมีการทำงานตามลำดับ เพราะฉะนั้นจึงไม่จำเป็นต้องนำทุกๆ เพจของงานมาไว้ในหน่วยความจำในทีเดียว ตัวอย่างเช่น ส่วนของโมดูลที่ไว้มีไว้ดักจับถ้าเกิดข้อผิดพลาดขึ้น ( User-written error handling modules) ซึ่งโมดูลนี้จะทำงานก็ต่อเมื่อเกิดข้อผิดพลาดตามที่ผู้ใช้ตั้งหรือกำหนดเงื่อนไขไว้ โมดูลที่เป็นลักษณะเลือกทำงานเพียงอย่างใดอย่างหนึ่ง (Mutually exclusive module s) ทางเลือกบางอย่างที่ไม่จำเป็นต้องทำงานเสมอไป (Certain program options are not always accessible)
  • 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!!
  • 18. Process Creation Virtual memory มีข้อดีในขณะที่มีการสร้างโปรเซสใหม่ ( process creation ) : Copy-on-Write โปรเซสที่ถูกสร้างโดย fork() สามารถ by pass การทำ demand paging เพราะสามารถใช้เพจร่วมกับ parent ของมันได้ การที่โปรเซส parent และ child จะใช้เพจร่วมกัน จะใช้วิธี copy-on-write วิธีนี้ช่วยลดจำนวนเพจที่จะต้องจัดสรรให้กับโปรเซสที่เกิดใหม่ Memory-Mapped Files การ access ไฟล์ (open, read, write) ที่เรียกใช้ system call และ disk access สามารถใช้ virtual memory technique ในการ treat file I/O ให้เหมือนกับ routine ในการ access หน่วยความจำ โดยการทำ memory mapping
  • 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
  • 20. Memory-Mapped Files Memory-mapped file I/O เป็นการใช้งาน ( treat) file I/O ดังเช่นการ access หน่วยความจำโดยการ mapping a disk block ไปยังเพจในหน่วยความจำ การอ่านไฟล์ ก็ทำในลักษณะ demand paging. บางส่วนของไฟล์ถูกอ่านจาก file system มาลงใน physical page ( ในหน่วยความจำ ) การอ่าน / เขียนต่อจากนั้นก็จะเหมือนการ access หน่วยความจำปกติ การ access ไฟล์ผ่านทางหน่วยความจำง่ายกว่าการใช้ read() write() system calls หลาย ๆ โปรเซสสามารถ map ไฟล์เดียวกัน สามารถใช้เพจร่วมกันได้ ( shared page)
  • 21. What happens if there is no free frame? Page fault handler เป็นการจัดการที่ระบบปฏิบัติจัดการเวลาเกิด page fault โดยดูว่า มีเฟรมว่างในหน่วยความจำหลักหรือไม่ ถ้ามี ก็จะนำเพจที่ต้องการโดยคัดลอกจากหน่วยความจำสำรองมาไว้ในเฟรมที่ว่างอยู่ ถ้าไม่มีเฟรมว่างก็ต้องเลือกเพจใดเพจหนึ่งสลับออกไป โดยขึ้นอยู่กับนโยบายที่กำหนด Page replacement – หาเพจหนึ่งในหน่วยความจำ ที่ไม่ได้ใช้ swap ออกไป มีหลาย ๆ algorithm ในการแทนที่เพจ ดู performance – เลือกอัลกอริธึมที่ทำให้เกิดจำนวน page faults น้อยที่สุด ถ้าเพจใดถูกนำกลับเข้ามาในหน่วยความจำหลาย ๆ ครั้ง ( ถูกนำออกไปหลายครั้ง ) ถือว่ามี system performance ไม่ดี ( unacceptable ) Locality of reference ใช้ช่วยแก้ปัญหา page fault
  • 22. Page Replacement เพื่อป้องกันการจัดสรรหน่วยความจำให้กับแต่ละโปรเซสเกินความจำเป็น จะต้องมีการปรับปรุงรูทีนในการจัดการ page-fault โดยเพิ่มการทำ page replacement ลงไป โดยการใช้ modify (dirty) bit เพื่อลด overhead ในการถ่ายโอนเพจ – เฉพาะเพจที่มีการ modified ที่มีจะมีการเขียนลง disk การทำ Page replacement ทำให้แยกการใช้งาน logical memory จาก physical memory – หมายความว่า large virtual memory สามารถที่จะถูกจัดสรรลงบน smaller physical memory ขนาดของงานไม่มีปัญหากับขนาดของหน่วยความจำหลัก ( โดยอาศัยหลักการของการใช้หน่วยความจำเสมือน ) ในการสลับเปลี่ยนเพจออกจากเฟรมหน่วยความจำหลัก จะต้องมีนโยบายหรือวิธี ในการคัดเลือกเพจออกจากเฟรม โดยต้องคำนึงถึงประสิทธิภาพโดยรวมของระบบ
  • 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
  • 26. First-In-First-Out (FIFO) Algorithm เป็นการแทนที่เพจที่อยู่ในหน่วยความจำมานานที่สุด ( คือเข้ามาก่อน ( อยู่นานที่สุด ) ก็ต้องถูกนำออกไปก่อน Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 อัลกอริธึมนี้ทำให้เกิด Belady’s Anomaly: more frames  more page faults
  • 27. FIFO Illustrating Belady’s Anomaly การแทนที่เพจ แบบ FIFO มีข้อเสียคือ ไม่สามารถรับประกันได้ว่า การเพิ่มจำนวนเฟรม จะทำให้การเกิด page fault ลดลง
  • 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
  • 31. Least Recently Used (LRU) Algorithm การแทนที่เพจ แบบ LRU จะนำเพจที่ไม่มีการใช้งานมาเป็นเวลานานออกจากหน่วยความจำก่อน โดยคำนึงว่าเพจที่ไม่มีการถูกใช้งานมาเป็นเวลานานแล้วนั้น จะไม่ถูกใช้งานอีกในเร็วๆ นี้ ในทางกลับกัน เพจที่เพิ่งจะมีการทำงาน ก็น่าจะถูกใช้งานอีกในเร็วๆ นี้ ( ตามทฤษฎีของ locality) Reference string: 1, 2, 3, 4, 1, 2, 5 , 1, 2, 3 , 4 , 5 12 faults 5 2 4 3 1 2 3 4 1 2 5 4 1 2 5 3 1 2 4 3
  • 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 จะถูกแทนที่
  • 34. LRU- Second Chance (clock) Page Replacement
  • 35. LRU- Second Chance (clock) Page Replacement (2)
  • 36. LRU- Second Chance (clock) Page Replacement (3)
  • 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
  • 38. Page Replacement Policy - Bit-shifting ดัดแปลงมาจาก การแทนที่เพจ แบบ LRU ใช้ 8-bit reference และอาศัยหลักการ shift bit ( เลื่อนไปด้านขวา ทีละ 1 บิท ) เมื่อเพจแรกเริ่มเข้ามาอยู่ในหน่วยความจำหลัก บิทซ้ายสุดของ reference bit จะถูกตั้งให้เป็น 1 อีก 7 บิทที่เหลือจะเป็น 0 ตามจังหวะของนาฬิกา ตัวจัดการหน่วยความจำจะเลื่อนบิทไปทางขวา 1 บิท ในขณะที่ ถ้าเพจไหนถูกใช้งานอีก บิทซ้ายสุดก็จะถูกตั้งให้เป็น 1 ส่วนเพจที่ไม่มีการใช้งาน จะเป็น 0 ถ้าผ่านไป 8 จังหวะนาฬิกา เพจที่ไม่ได้ถูกใช้งานเลยจะมี reference เป็น 00000000 ในขณะที่เพจที่มีการใช้งานทุกครั้งจะมี reference byte เป็น 11111111 เพจที่มีค่าใน reference byte น้อยที่สุด จะถูกแทนที่ เพราะไม่ถูกอ้างอิงหรือทำงาน มาเป็นเวลานานที่สุด
  • 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.