简体   繁体   中英

Can i check if a chunk of memory (e.g., allocated using malloc) stays in the cache?

Assume I allocate some space using 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 ?

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.

Determining the content of the CPU cache is very low level and beyond what C can do. 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. 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 . Obviously, this will be vendor specific. AMD will probably have specific tools.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM