390 likes | 502 Views
Cache Memory Systems. Chapter 6: Physical Cache Chapter 7: Efficient Cache Management Technique. นายพิสัณห์ ดวงจันทร์ รหัส 4422035. Chapter 6: Physical Cache. Physical Cache Organization Physical Cache Management Multilevel Cache. Physical Cache Organization. Virtual Address.
E N D
Cache Memory Systems Chapter 6: Physical Cache Chapter 7: Efficient Cache Management Technique นายพิสัณห์ ดวงจันทร์ รหัส 4422035
Chapter 6: Physical Cache Physical Cache Organization Physical Cache Management Multilevel Cache
Physical Cache Organization Virtual Address Physical Address Physical Address Physical Cache CPU MMU Main Memory Hit! Miss Data Data
Physical Cache Organization Physical Cache Virtual Address Physical Address Physical Address CPU MMU Main Memory • ข้อดีของ Physical Cache • ไม่มีปัญหาเรื่อง Ambiguity และ Alias • ไม่ต้องทำการ flush Data Data
Physical Cache Organization • Process ที่ไม่ได้ใช้ข้อมูลร่วมกัน อยู่กันคนละ Physical Page • Physical Tag ไม่มีทางที่จะ match กัน • Process ที่ใช้ข้อมูลร่วมกัน อยู่ใน Physical Page เดียวกัน • เพื่อจะได้ ทำ index ร่วมกัน และ match Tag กันได้ ==> Hit! • ไม่เกิด Alias และ Ambiguity ขึ้น เพราะไม่ใช้ Virtual address • โดยทั่ว ๆ ไปแล้ว จะไม่ต้อง flush • สามารถทำ Bus Watchingได้
Physical Cache Organization • ในระบบที่ไม่มี Physical cache: ข้อมูลของ Process ที่ไม่เกี่ยวข้องกัน อาจจะอยู่ใน Virtual address เดียวกัน (Virtual Line เดียวกัน) • ในระบบที่ใช้ Physical cache: ข้อมูลของ Process ที่ไม่เกี่ยวข้องกัน แม้ว่าจะอยู่ใน Virtual address เดียวกัน แต่จะอยู่ในคนละ Physical Line เพราะจะนำเอา Physical address มาจัด index • แก้ปัญหาสำคัญ ของ Virtual cache ลงได้ (Alias & Ambiguity) • มีความสามารถในการกระจายข้อมูลภายใน Cache ได้อย่างดีเยี่ยม
Managing a Physical Cache • Physical cache ไม่ต้องทำการ Flushing เพราะ • Context Switch time: • ใช้ Physical tag ==> ไม่เกิด Alias และ Ambiguity ==> ไม่ต้อง flush • Hit ratio สูง เพราะมีการกระจายข้อมูลได้ดี • Fork time (Copy-on-Write Sharing): • ทั้ง Parent และ Child process ใช้ Physical address อันเดียวกัน ==> ไม่เกิด Alias และ Ambiguity ==> ไม่ต้อง flush • Copy Page ได้ง่าย เพราะไม่ได้นำ Virtual address ที่ทำ Temporary mapping มาเกี่ยวข้อง
Managing a Physical Cache • Exec, Exit, Brk, Sbrk time: • Process ที่ไม่เกี่ยวข้องกัน อยู่คนละ Physical page • เมื่อ Process A จบการทำงาน ==> memory deallocation ==> Process อื่น ๆ ไม่สามารถเข้าถึง Physical page ของ process A ได้ ==> ข้อมูลเก่าที่ค้างอยู่ ไม่มีทางถูกเข้าถึง (access) • เมื่อเนื้อที่ส่วนนี้ถูก reallocate ==> UNIX จะมีการทำ Zero-filling เอง • ดังนั้น ไม่ต้อง flush • Shared Memory and Mapped Files: • ใช้ Physical address ในการอ้างถึงข้อมูลที่ใช้ร่วมกัน ==> ไม่เกิด Alias (เหมือนกับการใช้ Virtual address) ==> ไม่ต้อง flush
Managing a Physical Cache • User-Kernel Data Ambiguity: • เมื่อมีการ access Kernel’s Page ==> เช็ค Permission ==> User process ไม่สามารถเข้าถึง Kernel data ได้ • เนื่องจากใช้ Physical address ==> ไม่เกิด Alias และ Ambiguity ==> ไม่ต้อง flush • I/O and Bus Watching: • Virtual Cache: เมื่อเกิด I/O Operation ==> Data Inconsistency ==> Flushing • Physical Cache ใช้ “Bus Watching” ==> ข้อมูลใน Cache มีการแก้ไขอัตโนมัติ ==> ไม่ต้อง flush
Snooping Bus Watching Cache, Memory and I/O interconnection on a System Bus CPU/MMU Physical Cache System Bus I/O Memory
Bus Watching • ข้อมูลส่งไปใน bus ในรูปของ Bus Transaction • ขนาดของ Bus Transaction = 12 words, 24 words ฯลฯ • Cache จะคอยจับตาดู (monitor) กิจกรรมต่าง ๆ ใน Bus เรียกว่า“Snooping” • Cache Look-up Operation • นำ Address มาผ่าน Hash function ==> Line/Set ==> Indexing ==> ตรวจสอบ Tag ว่า … • Miss ==> ไม่ต้องทำอะไร • Hit ==> ดำเนินการกับข้อมูลที่อยู่ใน Cache (Cache-Resident)
VPN PPN Offset Offset <11..5> <11..5> Intel i860 XP Cache Architecture Virtual address from CPU Physical address from bus Virtual Tag Physical Tag Data Set 0 1 Set 0 1 Line 0 1 2 3 0 1 2 3 Line 0 1 2 3 0 1 2 3 . . .
Multilevel Cache CPU Registers 1st Level Cache 2nd Level Cache Main Memory • Primary (1st Level) Cache ==> Faster & SmallerVirtual Cache • Secondary (2nd Level) Cache ==> Lower & LargerPhysical Cache • ทำ MMU Translation พร้อม ๆ กับ 1st Level Cache Look-up ได้ • ถ้า miss ที่ 1st Level Cache ==> จะไปดูที่ 2nd Level Cache ต่อไป โดยใช้ Physical address ที่ได้จาก MMU Translation • เป็นการผสมผสานข้อดีของ Cache ทั้ง 2 แบบไว้ด้วยกัน
Intel i860 XR Cache Architecture i860 XR Bus MMU Main Memory Virtual Address External Cache Phys Addr Phys Addr Physical Cache CPU Data Cache Data I/O Data Data Inst Cache Instructions
MIPS R4000 Cache Architecture MIPS R4000 Bus Virtual Address MMU Main Memory External Cache Phys Addr Phys Addr Physical Cache CPU Data Cache I/O Data Data Data Inst Cache Instructions
Chapter 7: Efficient Cache Management Technique Address Space Layout Cache Size Bounded Flushing Delayed Cache Invalidation Cache-Aligning Data Structure
Efficient Cache Management Techniques • การตัดสินประสิทธิภาพของ Cache ขึ้นอยู่กับ 3 ปัจจัย • Locality of Reference • Physical design • Cache Management Techniques • Locality of Referenceและ Physical design of the cacheนั้นแก้ไขไม่ได้ • เป้าหมาย ==> Cache Management Techniques
Efficient Cache Management Techniques • มี 4 เทคนิค • Address Space Layout • Cache Size Bounded Flushing • Delayed Cache Invalidation • Cache-Aligning Data Structure
Text Data/bss Stack 0x0 0x80000000 0xffff0000 Address Space Layout • Virtually Indexed Cache • ปกติแล้ว จะใช้ Hash algorithmในการกระจายข้อมูลของ text, data และ stack region ของ Process (ทำเป็น index) • ทำการกำหนด Virtual address ที่เหมาะสมให้แก่ text, data และ stack region เพื่อไม่ให้เกิดเป็น index ที่ซ้ำซ้อนกัน
Text index Data index Stack index 0x0 0x10 0x20 0x30 … 0xff0 0 1 2 3 … 255 0x80000000 0x80000010 0x80000020 0x80000030 … 0x80000ff0 0xffff0000 0xffff0010 0xffff0020 0xffff0030 … 0xffff0ff0 0 1 2 3 … 255 0 1 2 3 … 255 Address Space Layout • Virtually Indexed Cache
Address Space Layout • Virtually Indexed Cache Address Space 0x0 Text Cache 0xff 0 255 60 K Unused 0x80000000 Data 0x800000fff. 0xffff0000 Stack 0xffff0ff0
Text index Data index Stack index 0x0 0x10 0x20 0x30 … 0xff0 0 1 2 3 … 255 0x80006000 0x80006010 0x80006020 0x80006030 … 0x80006ff0 0xfffff000 0xfffff010 0xfffff020 0xfffff030 … 0xfffffff0 1536 1537 1538 1539 … 1791 3840 3841 3842 3843 … 4095 Address Space Layout • Virtually Indexed Cache
Address Space Layout • Virtually Indexed Cache Address Space 0x0 Cache Text 0xff 24K 0x80006000 Data 0x800060fff. 40K 0xffff0000 Stack 0xffff0fff
Address Space Layout • ข้อจำกัดของ Virtually Indexed Cache • ใช้ได้กับระบบที่สามารถคาดหมายเนื้อที่ที่ application ต้องการได้ • ใช้ไม่ได้เลยกับระบบที่มี Cache ขนาดเล็ก โดยเฉพาะที่มีขนาดน้อยกว่า หรือเท่ากับขนาดของ Page ของ Process
Address Space B 0x0 0xfff 0x80006000 0x800006fff 0xfffff000 0xffffffff Text Text Data Data Stack Stack Address Space Layout • Dynamic Address Binding Address Space A 0x0 0xfff 0x80006000 0x800006fff 0xfffff000 0xffffffff Cache
Address Space Layout • Dynamic Address Binding • สุ่ม Starting address ตอน link time หรือ ส่วนของ stackสามารถทำได้ตอน exec time • แต่ปกติแล้ว Address binding จะเกิดขึ้นแบบ Static ณ Link time • ใช้ Position-independent code (โปรแกรมที่ไม่มีการ binding ไปยัง Virtual address ใด ๆ ในตอน compile, link time) • ยังไม่มีสถาปัตยกรรมใดที่รองรับ Position-independent code
Address Space Layout • ข้อจำกัดของ Dynamic Address Binding • ข้อตกลงต่าง ๆ รวมถึง Starting address มาตรฐาน หายไป ทำให้เสียเวลาตรวจสอบโปรแกรมมากขึ้น • ใช้ได้กับระบบที่มี Cache ขนาดใหญ่ พอที่จะทำให้เกิด Locality of Referenceเท่านั้น • เกิด Overheadในการคำนวณ Load address
Address Space Layout • Physically Indexed Cache • ทำการ allocate Physical page แบบกระจาย • ใช้ Modulo cache indexing แบ่ง Physical page ออกเป็น n กลุ่ม • โดยที่ n = Cache size / Page size • Page แรก จะอยู่ที่ตำแหน่ง 0 • Page ที่สอง จะอยู่ที่ตำแหน่ง 1 • ต่อไปเรื่อย ๆ จนถึงตำแหน่งที่ n-1 • เมื่อครบ ก็วนกลับไปที่ตำแหน่ง 0 ใหม่
Page Number 0 1 2 3 4 5 6 7 Address Space Layout • Physically Indexed Cache Physical Memory Cache
Address Space Layout • ข้อจำกัดของ Physically Indexed Cache • ใช้ได้กับระบบที่มี Cache ขนาดใหญ่มากกว่าขนาดของ Page หลาย ๆ เท่า
Cache Size Bounded Flushing • ขนาดข้อมูลที่ใหญ่ที่สุดที่จะถูก flush = ขนาดของ Cache • ถ้าต้องทำการ flush cache เมื่อ I/O buffer มีขนาด 4K • ถ้า Cache ขนาด 2K ==> Flush 2 ครั้ง • ถ้า Cache ขนาด 4K ==> Flush ครั้งเดียว • Cache ที่มีขนาดใหญ่ จะลด flushing overhead
Delayed Cache Invalidation • การทำ Invalidation บ่อย ๆ ทำให้ประสิทธิภาพของระบบลดลงเพราะ • เป็น time consuming ==> ทำทีละ Line • โดยปกติแล้ว Cache จะ flush ได้ทีละ 1 line • เกิดการ invalidate line ที่ไม่ได้บรรจุข้อมูลที่ต้องการลบขึ้นได้ • บางกรณี สามารถ delay การทำ Invalidation ออกไปได้ • 12K Virtual cache with physical tag;4K Page;10 Processes • Invalidate 1 ครั้ง: 1 Process = ดำเนินการกับข้อมูล120K • Invalidate 1 ครั้ง: 10 Processes = ดำเนินการกับข้อมูล12K
Delayed Cache Invalidation • Virtual Cache with Key • รอจนระบบใช้ Key ที่ว่างอยู่หมด และต้องการใช้ Key ของ Defunct Process ==> Invalidate • Unused Key List:ดึง Key ออกไปให้ Process ที่เกิดขึ้นใหม่ • Used Key List: เก็บ Key ของบรรดา Defunct Process ไว้ • เมื่อ Unused Key List หมด • OS ทำการ Invalidate Cache • นำ Key ทั้งหมดในUsedKeyList ไปให้แก่ Unused Key List
Delayed Cache Invalidation • Physically Tagged without Bus Watching • คล้าย ๆ กับที่ผ่านมา คือมีการใช้ Free List ของ Physical Page • Clean List: Physical Page เปล่า ๆ สำหรับ Process ใหม่ • Dirty List:Physical Page ของบรรดา Defunct Process • เมื่อ Clean List หมด • OS ทำการ Invalidate Cache • ย้ายทุก Page ใน DirtyList ไปให้แก่ Clean List
Cache-Aligning Data Structure • ทำให้ข้อมูลที่โปรแกรมต้องใช้งาน บรรจุลงใน Cache line ได้อย่างเหมาะสม เพื่อ • ลดการ Miss • เพิ่มLocality of Reference • ข้อมูลดังกล่าว อาจจะเป็น data structure ของโปรแกรม หรือตัวโปรแกรมเองก็ได้ • ใน UNIX โครงสร้างข้อมูลที่เข้าถึงบ่อยที่สุด คือ “Process table”
Cache-Aligning Data Structure Cache Line Entry 0 Entry 1 0 1 2 3 4 Entry 2 Entry 4 Entry 3 Entry 5 Entry 6 Entry 7
Padding Entry 0 Padding Entry 1 Padding Entry 2 Padding Entry 3 Padding Entry 4 Cache-Aligning Data Structure Cache Line 0 1 2 3 4 Cache
Cache-Aligning Data Structure • ข้อจำกัดของ Cache-Aligning Data Structure • Padding ขนาดใหญ่ ==> ลดประสิทธิภาพของระบบ • เสียเนื้อที่ว่างไปเปล่า ๆ • ลด Locality of Reference • ไม่ Portable เพราะต้องทำการ built-in ขนาดของ Cache line ไว้ในโปรแกรม • เหมาะกับสถานการณ์ที่ให้ความสำคัญกับ “ประสิทธิภาพของ โปรแกรม” มากกว่า “Portability” • ตัวอย่าง เช่น OS