cache

发布时间 2023-12-11 18:17:48作者: freedom-dalin

  cache - 硬件控制

  CPU <--->  L1 L2  ...... <---> main memory

  两种工作方式:inclusive cache / exclusive cache,代表数据能不能同时存在于各级cache,比如L1 L2

  hit / miss

  cortex A53架构中,L1 cache分为单独的instruction cache + data cache,即ICache + DCache,L1 cache是cpu私有,一个cpu cluster共享一个L2 cache,L3 cache所有cpu共享

  cache line,cache与main memory间数据传输最小单位

  cache中有tag array + data array,tag中有一位表示V - valid,data中有一位表示D - dirty,tag array在硬件cache中,没有计算size

  一个地址寻址使用cache过程(以cache line size = 8, cache line num = 8,共64bytes cache为例):

    最后3bit索引cache line内部位置,接近3个bit索引哪个cache line,剩余的为tag;先使用中间3个bit索引哪个cache line,查看对应tag是否有效,有效则对比tag,对比成功则使用最后3个bit索引cache line中具体位置;如果过程中tag无效,或者tag对比不成功,则为miss,会重新加载对应内存从main memory到cache line,如果有旧数据(Dirty),需要先把旧数据从cache line中swap到main memory。

  直接映射缓存 - cache颠簸

  组相联缓存 - 提升硬件复杂度,降低cache颠簸频率

  全相联缓存 - 硬件成本高,降低cache颠簸频率

  cache 分配策略:

    读分配,cpu读,cache miss,分配cache line,从main memory加载数据,默认支持

    写分配,cpu写,cache miss,分配cache line,从main memory加载数据,然后写cache,不支持写分配时,cpu直接更新到main memory

  cache更新策略:

    写直通 - write through(写时保持cache和main memory数据一致) / 写回 write back(cache line被替换或者显示clean操作时更新main memory)