ARM如何利用PMU(Performance Monitor Unit)量化L3 cache的cache eviction(估算cache eviction的概率)

本文将介绍如何利用DSU(DynamIQ™ Shared Unit)中的PMU,来监控L3 cache的 一些事件,通过对事件的分析,估算出L3 cache中的cache line conflict(eviction)概率。

一,PMU 都位于哪里 ?

在一个 DynamIQ™ cluster 里,集成了一个甚至多个core,以及一个DSU。如下图所示,为一个标准的DynamIQ微架构,里面实现了四个小核以及两个大核,每个核里实现了L1和L2两级cache。并且它们共用一个DSU,DSU里有L3 cache、SCU、内存接口、Debug接口、ACP以及外设接口等等。
在这里插入图片描述
在这里插入图片描述
回到我们的主题,PMU位于哪里?从下图可以看出,每个Core有自己独立的PMU,可以用于监控每个core私有的L1以及L2 cache的事件。
不仅如此,在DSU内部,也有一个PMU,可以用来监控L3 cache以及cluster内存操作的相关事件。
在这里插入图片描述
下面我将介绍一些与L3 cache相关的PMU event。

二,PMU events- 0x0029,L3D_CACHE_ALLOCATE

L3D_CACHE_ALLOCATE事件的event number为0x29,它会把除了L3D_CACHE_REFILL事件以外的,所有写入一整个cache line到L3 cache的事件发生的次数记录下来。它不会记录从L3 cache 以外的地方(下一级内存)拿数据写入L3的操作,比如,L3D_CACHE_ALLOCATE事件包括如下操作:

  • 从L2 cache 将整个cache line 写回到L3 cache的动作。
  • 从write buffer对整个cache line写入到L3 cache。
  • 使用 DC ZVA操作对L3 cache 清零的操作。

三,PMU events-0x002A, L3D_CACHE_REFILL

L3D_CACHE_REFILL事件主要包括cache line 填充事件,不管是填充的L1 cache 、L2 cache还是L3 cache,只要发生了从这些cache 外部获取数据,填充到cache line中,就包含在L3D_CACHE_REFILL事件中。
换句话说,只要从L1、L2以及L3以外的地方拿数据,并发生了cache line的填充,就算该数据没有被分配到L3cache 中,也属于L3D_CACHE_REFILL事件。
比如从cache外部获取的数据直接被放在了buffer中,而不是被分配到cache中,就算buffer里的数据随即被丢弃,这也算在L3D_CACHE_REFILL事件中,因为处理器里的各种buffer也被PMU当作是cache的一部分。

再举些例子,以下操作都算在L3D_CACHE_REFILL事件中:

  • 访问L3 cache数据时,该操作导致从外部内存、下一级cache或者其他处理器的L3中获取数据进行cache line填充。
  • 由于对L1或者L2cache的填充或者由L1、L2写回到L3的操作,导致发生了从当前处理器的L1、L2或者L3 之外的地方获取数据,填充到当前处理器的cache。
  • 访问L3 cache的数据时,导致了L2 cache的cache 填充,该数据来自当前处理器L1、L2以及L3之外的地方,就算没有填充到L3,也会被记录。

四,PMU events- 0x002C,L3D_CACHE_WB

L3D_CACHE_WB事件主要就是L3 cache上的数据写回到下一级内存中的事件。比如:

  • 将dirty的cache line写回到下一级内存。
  • 就算cache line没有被分配到L3 cache中,该cache line的write back也会被记录。

就算需要多次的数据访问才能完成一个写回操作,每个写回操作也只会被记录一次。
还有一些写回事件是否会算在L3D_CACHE_WB里,这取决于具体的架构实现:

  • 由于 cache coherency的请求,将一个L3上的dirty cache line 转移L1至L3 cache 以外的地方。
  • 由于缓存维护指令(clean & invalidate)导致cache line被写回的操作。

并且,有以下事件不算在L3D_CACHE_WB中:

  • 仅仅是cache liine的invalidation,没有造成任何的写回操作到L1至L3以外的内存。
  • 通过写通(write through )的方式,将L3的数据写到L1至L3以外的内存。
  • 将L3的数据转移到L1或者L2 cache中。

关于L3D_CACHE_WB事件,还有如下注意事项:

A write-back is attributable to the agent that generated the request
that caused the write-back. This might not be the same agent that
caused the data being written back to be allocated into the cache. An
Unattributable write-back event occurs when a requestor outside of the
PE makes a coherency request that results in write-back. If the cache
is shared, then an Unattributable write-back event is not counted. If
the cache is not shared, then the event is counted. It is
IMPLEMENTATION DEFINED whether a write of a whole cache line that is
not the result of the eviction of a line from the cache, is counted.
For example, this applies when the PE determines software is
streaming writes to memory and does not allocate lines to the cache,
or by a DC ZVA operation.

五,如何量化L3 cache的cache eviction(估算cache eviction的概率)

从上文中我们可以知道,L3D_CACHE_REFILL事件里基本包含了写入cache(L1、L2或者L3)的操作,而L3D_CACHE_WB事件则是将cache line从cache 中写回到下一级内存中的操作。换句话说,通过L3D_CACHE_REFILL事件,PMU可以统计出从cache获取数据,进行cache line填充的次数。通过L3D_CACHE_WB可以知道从cache(L1、L2或者L3)中的dirty cache line写回到L3的下一级内存的次数。
L3 cache的cache eviction(cache conflict)会导致被驱逐的cache line写回到下一级缓存。所以L3 cache的cache eviction概率可以用PMU的L3D_CACHE_WB事件发生的次数(出)与L3D_CACHE_REFILL事件发生的次数(进)的比值来量化。:
L 3 D − C A C H E − W B L 3 D − C A C H E − R E F I L L \frac{L3D-CACHE-WB}{L3D-CACHE-REFILL} L3DCACHEREFILLL3DCACHEWB
比值越小,L3 cache内发生conflict的概率越小,此时L3 cache的利用率越高。