แท็บ Cache

เครื่องมือจำลอง L1 cache ทั้งฝั่ง data (L1D) และ instruction (L1I) สามารถปรับแต่งโครงสร้าง cache และวิเคราะห์ผลกระทบต่อประสิทธิภาพได้

Cache animation
ภาพเคลื่อนไหวแสดงการ access cache

หลักการทำงาน

แท็บ Cache ของ Ripes ทำงานในแบบ trace-based simulation — ตัว processor model จะ ไม่ เข้าถึง cache โดยตรง แต่ตัว cache simulator จะดู memory access ของ CPU ทุก cycle แล้วนำมาจำลองการทำงานของ cache

ข้อจำกัดที่ต้องเข้าใจ
  • Ripes ไม่ จำลอง cache access latency ดังนั้นไม่สามารถบอก execution time ที่แท้จริงได้
  • บอกได้แค่ hit rate, miss rate, writeback rate
  • แม้ตั้ง write-back policy ค่าที่ "dirty" ใน cache จะปรากฏใน memory view ด้วยเสมอ (Ripes เขียนทุก write ลง main memory)
เคล็ดลับ สำหรับการเรียนเรื่อง cache แนะนำให้ใช้ Single Cycle processor เพราะ pipelined processor อาจ stall ระหว่าง memory access ซึ่งจะทำให้นับ memory access เกินจริง

การตั้งค่า Cache

Cache configuration
หน้าจอตั้งค่า cache
พารามิเตอร์ความหมายหน่วย
Ways Associativity ของ cache (เก็บเป็น power of 2)
ways=0 → 1-way (direct mapped)
ways=1 → 2-way set associative
ways=2 → 4-way set associative
เลขชี้กำลังของ 2
Sets จำนวน cache set กำหนดขนาดของ index field เลขชี้กำลังของ 2
Words per Line จำนวน word ในแต่ละ cache line กำหนดขนาดของ word offset เลขชี้กำลังของ 2
Wr. hit / Wr. miss Cache write policy เช่น write-through, write-back, write-allocate, no-write-allocate
Repl. policy Replacement policy เช่น LRU, Random

นอกจากนี้มี preset ที่กำหนดมาให้ใช้ทันที และยังสามารถบันทึก preset ของตัวเองได้

การอ่าน Cache View

Cache view
โครงสร้าง cache view

โดยปกติแบบเรียนจะวาด set-associative cache เป็นตารางแยกตาม way Ripes ใช้รูปแบบที่เทียบเท่ากันแต่รวมเป็นตารางเดียว:

Cache way explanation
การเทียบรูปแบบที่ Ripes ใช้กับรูปแบบแบบเรียนทั่วไป

คอลัมน์ใน Cache View

การอ่าน Highlight

สถิติและกราฟ Cache

Cache plot
กราฟแสดง cache statistics แบบ real-time

Ripes บันทึกข้อมูลทุก cycle:

เลือก Numerator และ Denominator เพื่อ plot อัตราส่วน เช่น เลือก Hits/Access count จะได้ hit rate ตามเวลา

ตัวอย่าง: ผลของการเลือก Cache แบบต่าง ๆ

โปรแกรมตัวอย่างที่ access memory ด้วย stride เฉพาะ:

.data
stride:          .word   512   # ก้าวกระโดดทีละกี่ word
accessesPerTurn: .word   2
turns:           .word   128
baseAddress:     .word   4096

.text
cacheLoop:
        lw      a6, turns
        lw      a7, baseAddress
        lw      a2, stride
        lw      a0, accessesPerTurn
        # ... (วน access memory ตาม stride)

ลองที่ 1: 32-entry 4-word direct mapped cache

  1. เปิด Memory tab แล้วเลือก preset "32-entry 4-word direct mapped cache"
  2. กด Run
  3. ได้ hit rate = 0.01154 (ต่ำมาก!)

เหตุผล: stride = 512 word ทำให้ทุก access map ลง set เดียวกัน เกิด conflict miss ทุกครั้ง

ลองที่ 2: 32-entry 4-word 2-way set associative

  1. เปลี่ยน preset เป็น "32-entry 4-word 2-way set associative" (ขนาด cache เท่าเดิม!)
  2. กด Run
  3. ได้ hit rate = 0.9885 (สูงมาก!)

เหตุผล: ทั้ง 2 address ที่ access สามารถอยู่ใน 2 way ของ set เดียวกันได้ ไม่มี conflict miss

บทเรียน ขนาด cache เท่ากันแต่ associativity ต่างกัน → hit rate ต่างกันมาก! นี่คือเหตุผลที่ CPU จริงเลือกใช้ set-associative cache ในระดับ L1/L2