在CPU Hit/MIss時,Cache和Main Memory之間交互策略


 關鍵詞:Hit、Miss、Read Through、Read Allocate、Write Through、Write Back、Write Allocate、No Write Allocate等。

對Cache和Main Memory之間數據維護設計到性能問題,也涉及到一致性問題。

了解下面各種策略,有利於理解Cache和Main Memory行為和配置Cache屬性。

1. 各種策略行為和優缺點

Read Through:CPU Read data Through(not Allocate) Cache to Main Memory.

Read Allocate:CPU Read data from Main Memory and Allocate it in cache

Write Through:CPU Write data Through Cache to Main Memory。

Write Back:CPU Write data Back to Cache.

Write Allocate: CPU Write data to Main Memory and Allocate it in Cache.

No Write Allocate: CPU Write data to Main Memory, but No Allocate it in Cache.

 

Read/Write在Hit/Miss情況下,不同策略的表現行為:

  行為    
Read Hit CPU直接從Cache獲取數據。  

Miss

Read Through

直接從Main Memory讀取內容到CPU,不經過Cache。

 避免Cache占用,但是對統一內容連續讀效率很低。

Read Allocate

No Read Through

先把數據從Main Memory讀取到Cache中,再從Cache中讀取數據到CPU。

占用Cache, 
Write Hit Write Through 把數據同時從CPU寫到Cache和Main Memory中。 優點:
 - 未命中的讀不會導致寫到Main Memory。
 - 較易實現。
 - Main Memory中內容往往是最新的,也即一致性。
缺點:
 - 寫入Main Memory較慢。
 - 每次寫操作都會觸發訪問Main Memory。
 - 所需帶寬更大。
Write Back

只把數據從CPU寫到Cache中。

后續可以通過flush方式把數據從Cache寫入到Main Memory中。

優點:
 - 寫的速度非常快,Cache訪問速度遠高於Main Memory。
 - 多次到Cache的寫只需一次到Main Memory的寫。
 - 所需帶寬更小。
缺點:
  - 實現較難
  - Main Memory數據和Cache中不一致。
  - 導致Cache替換的讀操作會將dirty快寫入Main Memory。
Miss Write Allocate

先把要寫的數據從Main Memory載入到Cache中,CPU將數據寫入到Cache中。

后續通過flush將數據從Cache寫入到Main Memory中。

 
No Write Allocate 數據在Main Memory中被修改,直接從CPU寫入到Main Memory中。不會被加載到Cache中。  

2. Write策略組合

不同Write Hit和Write MIss策略組合下的行為: 

Write Hit Policy Write Miss Policy    
Write Through Write Allocate

Hit:將數據寫入Cache和Main Memory。

Miss:將數據從Main Memory讀到Cache中,然后CPU Hit,CPU再寫到Cache和Main Memory中。

當Miss的時候將數據帶到Cache中,然后再將數據寫入Cache和Main Memory,其中Cache的操作不太合情理

 
Write Through No Write Allocate

Hit:將數據寫入到Cache和Main Memory。

Miss:更新Main Memory中內容,並且不會在Cache中存在相關內容。

后續內容寫入將更新Main Memory,因為使用了Write Through策略。因此,節省了一些時間,不會在Miss時將內容帶進Cache中,因為它看起來毫無用處。

 
Write Back Write Allocate

Hit:將數據寫入Cache並更新dirty位,Main Memory不會被更新。

Miss:更新Main Memory中的內容並將該塊帶到Cache中;

對同一塊的后續寫入,如果該塊最初導致未命中,則下次將命中Cache,為該塊設置臟位。這將消除額外的Main Memory訪問,並比Write Through與Write Allocate組合執行更加有效。

 
Write Back No Write Allocate  

Hit:當它寫入Cache設置塊的臟位時,Main Memory不會被更新;

MIss:更新Main Memory中的塊,而不將該塊帶到Cache中;

對同一塊的后續寫入,如果該塊最初為Miss,則會一直導致Miss,並導致執行效率非常低

 

所以常見的組合是Write Through-No Write AllocateWrite Back-Write Allocate

3. 一個ARM內存實例

下圖是M33的Cache策略實例:

 

 

參考文檔:

Interaction Policies with Main Memory

Write Through and Write Back in Cache


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM