深度学习armv8/armv9 cache的原理
快速鏈接:
.
👉👉👉 個人博客筆記導讀目錄(全部) 👈👈👈
目錄
- 1、為什么要用cache?
- 2、背景:架構的變化?
- 2、cache的層級關系 –--big.LITTLE架構(A53為例)
- 3、cache的層級關系 –-- DynamIQ架構(A76為例)
- 4、DSU / L3 cache
- 5、L1/L2/L3 cache都是多大呢
- 6、cache相關的術語介紹
- 7、cache的分配策略(alocation,write-through, write-back)
- 8、架構中內存的類型
- 9、架構中定義的cache的范圍(inner, outer)
- 10、架構中內存的類型 (mair_elx寄存器)
- 11、cache的種類(VIVT,PIPT,VIPT)
- 12、Inclusive and exclusive caches
- 13、cache的查詢過程(非官方,白話)
- 14、cache的組織形式(index, way, set)
- 15、cache line里都有什么
- 16、cache查詢示例
- 17、cache查詢原理
- 18、cache maintenance
- 19、軟件中維護內存一致性 – invalid cache
- 20、軟件中維護內存一致性 – flush cache
- 21、cache一致性指令介紹
- 22、PoC/PoU point介紹
- 23、cache一致性指令的總結
- 24、Kernel中使用cache一致性指令的示例
- 25、Linux Kernel Cache API
- 26、A76的cache介紹
- 27、A78的cache介紹
- 28、armv8/armv9中的cache相關的系統寄存器
- 29、多核之間的cache一致性
- 30、MESI/MOESI的介紹
1、為什么要用cache?
ARM 架構剛開始開發時,處理器的時鐘速度和內存的訪問速度大致相似。今天的處理器內核要復雜得多,并且時鐘頻率可以快幾個數量級。然而,外部總線和存儲設備的頻率并沒有達到同樣的程度。可以實現可以與內核以相同速度運行的小片上 SRAM塊,但與標準 DRAM 塊相比,這種 RAM 非常昂貴,標準 DRAM 塊的容量可能高出數千倍。在許多基于 ARM 處理器的系統中,訪問外部存儲器需要數十甚至數百個內核周期。
緩存是位于核心和主內存之間的小而快速的內存塊。它在主內存中保存項目的副本。對高速緩沖存儲器的訪問比對主存儲器的訪問快得多。每當內核讀取或寫入特定地址時,它首先會在緩存中查找。如果它在高速緩存中找到地址,它就使用高速緩存中的數據,而不是執行對主存儲器的訪問。通過減少緩慢的外部存儲器訪問時間的影響,這顯著提高了系統的潛在性能。通過避免驅動外部信號的需要,它還降低了系統的功耗
2、背景:架構的變化?
- DynamIQ是Arm公司2017年發表的新一代多核心微架構(microarchitecture)技術,正式名稱為DynamIQ big.LITTLE(以下簡稱為DynamIQ),取代使用多年的big.LITTLE技術
- big.LITTLE技術將多核心處理器IP分為兩個clusters,每個cluster最多4個核,兩個cluster最多4+4=8核,而DynamIQ的一個cluster,最多支持8個核
- big.LITTLE大核和小核必須放在不同的cluster,例如4+4(4大核+4小核),DynamIQ的一個cluster中,可同時包含大核和小核,達到cluster內的異構(heterogeneous cluster),而且大核和小核可以隨意排列組合,例如1+3、1+7等以前無法做到的彈性配置。
- big.LITTLE每個cluster只能用一種電壓,也因此同一個cluster內的各核心CPU只有一種頻率,DynamIQ內的每個CPU核心都可以有不同的電壓和不同的頻率
- big.LITTLE每個cluster內的CPU核,共享同一塊L2 Cache,DynamIQ內的每個CPU核心,都有專屬的L2 Cache,再共享同一塊L3 Cache,L2 Cache和L3 Cache的容量大小都是可以選擇的,各核專屬L2 Cache可以從256KB~512KB,各核共享L3 Cahce可以從1MB~4MB。這樣的設計大幅提升了跨核數據交換的速度。 L3 Cache是DynamIQ Shared Unit(DSU)的一部分
2、cache的層級關系 ––big.LITTLE架構(A53為例)
3、cache的層級關系 –-- DynamIQ架構(A76為例)
4、DSU / L3 cache
DSU-AE 實現了系統控制寄存器,這些寄存器對cluster中的所有core都是通用的。 可以從cluster中的任何core訪問這些寄存器。 這些寄存器提供:
- 控制cluster的電源管理。
- L3 cache控制。
- CHI QoS 總線控制和scheme ID分配。
- 有關DSU?AE 硬件配置的信息,包括指定的Split?Lock 集群執行模式。
- L3 緩存命中和未命中計數信息
L3 cache
- cache size可選 : 512KB, 1MB, 1.5MB, 2MB, or 4MB. cache line = 64bytes
- 1.5MB的cache 12路組相連
- 512KB, 1MB, 2MB, and 4MB的caches 16路組相連
5、L1/L2/L3 cache都是多大呢
需要參考ARM文檔,其實每一個core的cache大小都是固定的或可配置的。
6、cache相關的術語介紹
思考 :什么是Set、way、TAG 、index、cache line、entry?
7、cache的分配策略(alocation,write-through, write-back)
-
讀分配(read allocation)
當CPU讀數據時,發生cache缺失,這種情況下都會分配一個cache line緩存從主存讀取的數據。默認情況下,cache都支持讀分配。 -
讀分配(read allocation)寫分配(write allocation)
當CPU寫數據發生cache缺失時,才會考慮寫分配策略。當我們不支持寫分配的情況下,寫指令只會更新主存數據,然后就結束了。當支持寫分配的時候,我們首先從主存中加載數據到cache line中(相當于先做個讀分配動作),然后會更新cache line中的數據。 -
寫直通(write through)
當CPU執行store指令并在cache命中時,我們更新cache中的數據并且更新主存中的數據。cache和主存的數據始終保持一致。 -
讀分配(read allocation)寫回(write back)
當CPU執行store指令并在cache命中時,我們只更新cache中的數據。并且每個cache line中會有一個bit位記錄數據是否被修改過,稱之為dirty bit(翻翻前面的圖片,cache line旁邊有一個D就是dirty bit)。我們會將dirty bit置位。主存中的數據只會在cache line被替換或者顯示的clean操作時更新。因此,主存中的數據可能是未修改的數據,而修改的數據躺在cache中。cache和主存的數據可能不一致
8、架構中內存的類型
9、架構中定義的cache的范圍(inner, outer)
對于cacheable屬性,inner和outer描述的是cache的定義或分類。比如把L1/L1看做是inner,把L3看做是outer
通常,內部集成的cache屬于inner cache,外部總線AMBA上的cache屬于outer cache。例如:
- 對于上述的big.LITTLE架構(A53為例)中,L1/L2屬于inner cache,如果SOC上掛了L3的話,則其屬于outer cache
- 對于上述的DynamIQ架構(A76為例)中,L1/L2/L3屬于inner cache,如果SOC上掛了System cache(或其它名稱)的話,則其屬于outer cache
然后我們可以對每類cache進行單獨是屬性配置,例如:
- 配置 inner Non-cacheable 、配置 inner Write-Through Cacheable 、配置 inner Write-back Cacheable
- 配置 outer Non-cacheable 、配置 outer Write-Through Cacheable 、配置 outer Write-back Cacheable
對于shareable屬性,inner和outer描述的是cache的范圍。比如inner是指L1/L2范圍內的cache,outer是指L1/L2/L3范圍內的cache
以下再次對Inner/Outer屬性做了一個小小的總結:
-
如果將block的內存屬性配置成Non-cacheable,那么數據就不會被緩存到cache,那么所有observer看到的內存是一致的,也就說此時也相當于Outer Shareable。
其實官方文檔,也有這一句的描述:
在B2.7.2章節 “Data accesses to memory locations are coherent for all observers in the system, and correspondingly are treated as being Outer Shareable” -
如果將block的內存屬性配置成write-through cacheable 或 write-back cacheable,那么數據會被緩存cache中。write-through和write-back是緩存策略。
-
如果將block的內存屬性配置成 non-shareable, 那么core0訪問該內存時,數據緩存的到Core0的L1 d-cache 和 cluster0的L2 cache,不會緩存到其它cache中
-
如果將block的內存屬性配置成 inner-shareable, 那么core0訪問該內存時,數據只會緩存到core 0和core 1的L1 d-cache中, 也會緩存到clustor0的L2 cache,不會緩存到clustor1中的任何cache里。
-
如果將block的內存屬性配置成 outer-shareable, 那么core0訪問該內存時,數據會緩存到所有cache中
| non-shareable | 數據不會緩存到cache (對于觀察則而言,又相當于outer-shareable) | Core0讀取時,數據緩存的到Core0的L1 d-cache 和 cluster0的L2 cache, 如果core0和core1都讀寫過該內存,且在core0 core1的L1 d-cache中都緩存了該內存。那么core0在讀取數據的時候,core0的L1 Dcache會更新,但core 1的L1 Dcache不會更新 | 同左側 |
| inner-shareable | 數據不會緩存到cache (對于觀察則而言,又相當于outer-shareable) | Core0讀取時,數據緩存的到Cluster0中所有cache | 同左側 |
| outer-shareable | 數據不會緩存到cache (對于觀察則而言,又相當于outer-shareable) | Core0讀取時,數據緩存的到所有cache | 同左側 |
10、架構中內存的類型 (mair_elx寄存器)
11、cache的種類(VIVT,PIPT,VIPT)
MMU由TLB和Address Translation 組成:
- Translation Lookaside Buffer
- TAddress Translation
cache又分為;
- PIPT
- VIVT
- VIPT
12、Inclusive and exclusive caches
先討論一個簡單的內存讀取,單核的. 如LDR X0, [X1], 假設X1指向main memory,且是cacheable.
(1)、Core先去L1 cache讀取,hit了,直接返回數據給Core
(2)、Core先去L1 cache讀取,miss了,然后會查詢L2 cache,hit了,L2的cache數據會返回Core,還會導致這個cache line替換到L1中的某一行cache line
(3)、如果L1 L2都是miss,那么data將會從內存中讀取,緩存到L1和L2,并返回給Core
接著我們再看一個復雜的系統,不考慮L3,多核的.
(1)、如果是inclusive cache,那么數據將會被同時緩存到L1和L2
(2)、如果是exclusive cache,那么數據只緩存到L1,不會緩存到L2
- Strictly inclusive: Any cache line present in an L1 cache will also be present in the L2
- Weakly inclusive: Cache line will be allocated in L1 and L2 on a miss, but can later be evicted from L2
- Fully exclusive: Any cache line present in an L1 cache will not be present in the L2
13、cache的查詢過程(非官方,白話)
假設一個4路相連的cache,大小64KB, cache line = 64bytes,那么 1 way = 16KB,indexs = 16KB / 64bytes = 256 (注: 0x4000 = 16KB、0x40 = 64 bytes)
0x4000 – index 0
0x4040 – index 1
0x4080 – index 2
…
0x7fc0 – index 255
0x8000 – index 0
0x8040 – index 1
0x8080 – index 2
…
0xbfc0 – index 255
14、cache的組織形式(index, way, set)
- 全相連
- 直接相連
- 4路組相連
例如 A76
-
L1 i-cache :64KB,4路256組相連,cache line為64bytes
-
TLB i-cache :全相連,支持4KB, 16KB, 64KB, 2MB,32M的頁
-
L1 d-cache :64KB,4路256組相連,cache line位64bytes
-
TLB d-cache :全相連,支持4KB, 16KB, 64KB, 2MB,512MB的頁
-
L2 cache :8路相連的cache,大小可選128KB, 256KB, or 512KB
15、cache line里都有什么
Each line in the cache includes:
? A tag value from the associated Physical Address.
? Valid bits to indicate whether the line exists in the cache, that is whether the tag is valid.
Valid bits can also be state bits for MESI state if the cache is coherent across multiple cores.
? Dirty data bits to indicate whether the data in the cache line is not coherent with external memory
? data
那么TAG里又都有什么呢??(S13 才會說這里的TAG等于物理地址里的TAG)
如下以A78為例,展示了TAG里都有什么
補充一點:TLB里都有什么? 同樣以為A78為例;
16、cache查詢示例
17、cache查詢原理
先使用index去查詢cache,然后再比較TAG,比較tag的時候還會檢查valid標志位
18、cache maintenance
軟件維護操作cache的指令有三類:
- Invalidation:其實就是修改valid bit,讓cache無效,主要用于讀
- Cleaning: 其實就是我們所說的flush cache,這里會將cache數據回寫到內存,并清楚dirty標志
- Zero:將cache中的數據清0, 這里其實是我們所說的clean cache.
什么時候需要軟件維護cache:
(1)、當有其它的Master改變的external memory,如DMA操作
(2)、MMU的enable或disable的整個區間的內存訪問,如REE enable了mmu,TEE disable了mmu.
針對第(2)點,cache怎么和mmu扯上關系了呢?那是因為:
mmu的開啟和關閉,影響了內存的permissions, cache policies
19、軟件中維護內存一致性 – invalid cache
20、軟件中維護內存一致性 – flush cache
21、cache一致性指令介紹
<cache> <operation>{, <Xt>}
22、PoC/PoU point介紹
- PoC is the point at which all observers, for example, cores, DSPs, or DMA engines, that can access memory, are guaranteed to see the same copy of a memory location
- PoU for a core is the point at which the instruction and data caches and translation table walks of the core are guaranteed to see the same copy of a memory location
23、cache一致性指令的總結
24、Kernel中使用cache一致性指令的示例
25、Linux Kernel Cache API
linux/arch/arm64/mm/cache.S linux/arch/arm64/include/asm/cacheflush.hvoid __flush_icache_range(unsigned long start, unsigned long end); int invalidate_icache_range(unsigned long start, unsigned long end); void __flush_dcache_area(void *addr, size_t len); void __inval_dcache_area(void *addr, size_t len); void __clean_dcache_area_poc(void *addr, size_t len); void __clean_dcache_area_pop(void *addr, size_t len); void __clean_dcache_area_pou(void *addr, size_t len); long __flush_cache_user_range(unsigned long start, unsigned long end); void sync_icache_aliases(void *kaddr, unsigned long len); void flush_icache_range(unsigned long start, unsigned long end) void __flush_icache_all(void)26、A76的cache介紹
A76
-
L1 i-cache :64KB,4路256組相連,cache line為64bytes
-
L1 d-cache :64KB,4路256組相連,cache line為64bytes
-
L2 cache :8路相連的cache,大小可選128KB, 256KB, or 512KB
-
L1 TLB i-cache :48 entries, 全相連,支持4KB, 16KB, 64KB, 2MB,32M的頁
-
L1 TLB d-cache : 48 entries,全相連,支持4KB, 16KB, 64KB, 2MB,512MB的頁
-
L2 TLB cache : 1280 entries, 5路組相連
-
L3 cache
cache size可選 : 512KB, 1MB, 1.5MB, 2MB, or 4MB. cache line = 64bytes
1.5MB的cache 12路組相連
512KB, 1MB, 2MB, and 4MB的caches 16路組相連
27、A78的cache介紹
A78
-
L1 i-cache :32或64KB,4路組相連,cache line為64bytes , VIPT
-
L1 d-cache : 32或64KB,4路組相連,cache line為64bytes, VIPT
-
L1 TLB i-cache :32 entries, 全相連,支持4KB, 16KB, 64KB, 2MB,32M的頁
-
L1 TLB d-cache : 32 entries,全相連,支持4KB, 16KB, 64KB, 2MB,512MB的頁
-
L2 TLB cache : 1024 entries, 4路組相連
-
L3 cache
cache size可選 : 512KB, 1MB, 1.5MB, 2MB, or 4MB. cache line = 64bytes
1.5MB的cache 12路組相連
512KB, 1MB, 2MB, and 4MB的caches 16路組相連
28、armv8/armv9中的cache相關的系統寄存器
ID Register
CTR_EL0, Cache Type Register
- IminLine, bits [3:0]
Log2 of the number of words in the smallest cache line of all the instruction caches that are controlled by the PE. - DminLine, bits [19:16]
Log2 of the number of words in the smallest cache line of all the data caches and unified caches that are controlled by the PE
29、多核之間的cache一致性
對于 Big.LITTLE架構
對于 DynamIQ架構
30、MESI/MOESI的介紹
Events:
- RH = Read Hit
- RMS = Read miss, shared
- RME = Read miss, exclusive
- WH = Write hit
- WM = Write miss
- SHR = Snoop hit on read
- SHI = Snoop hit on invalidate
- LRU = LRU replacement
Bus Transactions:
- Push = Write cache line back to memory
- Invalidate = Broadcast invalidate
- Read = Read cache line from memory
歡迎添加微信、微信群,多多交流
總結
以上是生活随笔為你收集整理的深度学习armv8/armv9 cache的原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: armv8/armv9的简介-学习这一篇
- 下一篇: ARM Cortex-M学习简介