A72 Core如何将数据放入二级缓存?

How to put data in L2 cache with A72 Core?

我有一组数据,如下所示:

uint32_t data[128]; //Could be more than L1D Cache size

为了对其进行计算,我想将数据尽可能靠近我的计算单元放在二级缓存中。

我的目标使用 linux 内核和一些额外的应用程序运行

我知道我可以使用 mmap 访问内存的某个区域,并且我已经在内核之间共享的可用内存的某些部分成功完成了它。

如何在二级缓存区做同样的事情?

我已经阅读了部分 gcc 文档和 AArch64 汇编指令集,但无法找到实现此目的的方法。

缓存 - 不是应该存储数据的地方,它只是...缓存? :)

我的意思是,您的处理器决定应缓存哪些数据以及缓存在何处 (L1/L2/L3),逻辑取决于 CPU 实现。

如果你愿意,你可以尝试找到在缓存中放置和替换数据的算法并使用它(当然没有保证)通过使用专用指令预取你的数据然后用[维护缓存=22=] 其他程序的说明。

也许对于现代 ARM 有更简单的方法,我是从 x86/x64 的角度讲的,但我的全部观点是“你真的确定你需要这个”吗? CPU 足够聪明,可以缓存他们需要的数据,而且他们一年比一年做得更好。

我建议您使用任何可以显示缓存未命中情况的分析器,以确保您的数据尚未出现在缓存中。 如果没有,首先要优化的是算法。尝试找出缓存未命中的原因 - 例如,也许您应该使用临时变量在循环中加载较少的数据,或者甚至手动展开循环以控制访问的位置和内容。

How to do the same thing but in L2 Cache area ?

您的硬件不支持。

一般来说,ARMv8 体系结构不对缓存的内容做出任何保证,也不提供任何显式操作或查询它们的方法 - 它仅提供保证并提供处理 的工具一致性.

具体来说,来自 the spec 的 D4.4.1 节“缓存的一般行为”:

[...] the architecture cannot guarantee whether:

• A memory location present in the cache remains in the cache.
• A memory location not present in the cache is brought into the cache.

Instead, the following principles apply to the behavior of caches:

• The architecture has a concept of an entry locked down in the cache.
  How lockdown is achieved is IMPLEMENTATION DEFINED, and lockdown might
  not be supported by:

  — A particular implementation.
  — Some memory attributes.

• An unlocked entry in a cache might not remain in that cache. The
  architecture does not guarantee that an unlocked cache entry remains in
  the cache or remains incoherent with the rest of memory. Software must
  not assume that an unlocked item that remains in the cache remains dirty.

• A locked entry in a cache is guaranteed to remain in that cache. The
  architecture does not guarantee that a locked cache entry remains
  incoherent with the rest of memory, that is, it might not remain dirty.

[...]

• Any memory location is not guaranteed to remain incoherent with the rest of memory.

所以基本上你想要缓存锁定。咨询 the manual of your CPU 虽然:

• The Cortex-A72 processor does not support TLB or cache lockdown.

所以你不能故意把东西放在缓存里。现在,您可以通过尝试观察副作用来告诉 是否缓存了某些内容。缓存的两个常见副作用是延迟和一致性。因此,您可以尝试计算访问时间或修改 DRAM 的内容并检查您是否在缓存映射中看到该更改...但这仍然是一个糟糕的想法。
首先,这两个都是破坏性操作,这意味着它们将通过测量来改变您正在测量的 属性。另一方面,仅仅因为你观察到它们一次并不意味着你可以依赖发生的事情。

底线:您无法保证在您使用某项内容时它已保存在任何特定缓存中。