我可以检查是否有一块内存(例如,使用malloc分配的内存)保留在缓存中? [英] Can i check if a chunk of memory (e.g., allocated using malloc) stays in the cache?

查看:68
本文介绍了我可以检查是否有一块内存(例如,使用malloc分配的内存)保留在缓存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用malloc分配了一些空间. 我能否在运行时检查此连续内存是否保留在CPU的缓存中(甚至更好地保留在缓存级别L1,L2,L3等内)?

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).

之所以可以查询内存是否在缓存中,是因为硬件将其管理在您可以轻松观察到的级别以下.在具有硬件管理的TLB的体系结构上,将通过虚拟地址随处跟踪内存.

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.

这篇关于我可以检查是否有一块内存(例如,使用malloc分配的内存)保留在缓存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆