[英]Can i check if a chunk of memory (e.g., allocated using malloc) stays in the cache?
Assume I allocate some space using malloc. 假设我使用malloc分配了一些空间。 Can I check whether this continuous memory remains within the CPU's cache (or even better within which cache levels L1, L2, L3 etc.) in run-time ?
我是否可以在运行时检查此连续内存是否保留在CPU的缓存中(甚至更好地保留在缓存级别L1,L2,L3等中)?
No, but you can use prefetch instructions to move it from memory to cache in advance of use. 否,但是您可以在使用前使用预取指令将其从内存移至缓存。 If the data is already in cache, these instructions should be fast (there are some architecture-specific caveats, but I don't know which platform you are using).
如果数据已经在缓存中,则这些指令应该很快(有一些特定于体系结构的警告,但我不知道您使用的是哪个平台)。
The reason you can query whether memory is in cache is because the hardware manages this below the level you can easily observe. 您可以查询内存是否在缓存中的原因是因为硬件将其管理在您容易观察到的级别以下。 And on architectures with hardware-managed TLB, memory will be tracked by virtual addresses everywhere.
在具有硬件管理的TLB的体系结构上,将通过虚拟地址随处跟踪内存。
Determining the content of the CPU cache is very low level and beyond what C can do. 确定CPU缓存的内容非常低级,超出了C的能力范围。 In fact, caching is completely transparent to the code you may be writing as the CPU pretty much decides what to cache and cannot afford to waste timein convoluted logic on how to do so.
实际上,缓存对于您正在编写的代码是完全透明的,因为CPU几乎决定了要缓存的内容,并且不能浪费时间在如何做的复杂逻辑上。 A quick Googling on specific tools to that effect came up with Intel Tuning Guide and Performance Analysis Papers: https://software.intel.com/en-us/articles/processor-specific-performance-analysis-papers .
为此,《英特尔调试指南》和《性能分析论文》中快速搜索了专用工具, 网址为 : https : //software.intel.com/zh-cn/articles/processor-specific-performance-analysis-papers 。 Obviously, this will be vendor specific.
显然,这将是特定于供应商的。 AMD will probably have specific tools.
AMD可能会有特定的工具。
Generally speaking, at least for most practical purposes, no (and even if you could, by the time you tried to use the information, it might well be stale). 一般而言,至少出于大多数实际目的,不是(即使您尝试使用这些信息,即使可以,它也很陈旧)。
If you're reading the data repeatedly, you can measure long-term average access time fairly easily, and this gives a pretty decent indication of where the data was coming from as a rule. 如果您要重复读取数据,则可以很容易地测量长期平均访问时间,这通常可以很好地指示数据的来源。
There are a few (mostly embedded) processors that will let you lock some data in the cache or set aside part of the cache as addressable memory that you manage manually though. 有一些(主要是嵌入式的)处理器可以让您锁定缓存中的某些数据,或者将缓存的一部分留作您可手动管理的可寻址内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.