简体   繁体   English

我可以检查是否有一块内存(例如,使用malloc分配的内存)保留在缓存中?

[英]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.

相关问题 如何检查是否使用 malloc 或 new 分配了内存 - how to check if memory is allocated using malloc or new 如何加速此代码(MWE!),例如使用限制 - How can I accelerate this code (MWE!), e.g. using restrict malloc分配的内存可以使用多少时间? - The amount of time malloc allocated memory can be used for? 我如何在Linux性能下获得libc6符号(例如_int_malloc)的致电父母? - How do I get call parents for libc6 symbols (e.g. _int_malloc) with linux perf? 如何确保枚举枚举类中定义的每个案例,例如使用static_assert? - How can I ensure that each case defined in an enum class is treated, e.g. using static_assert? 使用C中的结构的malloc遍历内存块 - Index through chunk of memory using malloc of structures in c 在malloc分配的内存之外使用指针的预期行为 - Expected behavior for using pointer beyond malloc allocated memory 使用 malloc 调用 free() 来释放动态分配的 memory 的析构函数 - Destructor to call free() to free dynamically allocated memory using malloc 标准是否要求自动存储中的对象具有任何类型的正确对齐方式(例如,malloc确实如此)? - Does the standard require that objects in automatic storage have the correct alignment for any type (e.g. as malloc does)? 命令模式:如何执行连续动作? (例如,移动物体) - Command pattern: how can I do continuous actions? (e.g. moving an object)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM