简体   繁体   English

是否可以检查变量是否位于 L1/L2/L3 缓存

[英]Is it possible to check if a variable is located at L1/L2/L3 cache

As all we know, in a modern architecture of computer, there are L1/L2/L3 cache, which can improve the performance of execution of a program.众所周知,在现代计算机体系结构中,存在L1/L2/L3缓存,可以提高程序的执行性能。

My question is if we can know if a variable is loaded into L1/L2/L3 cache while executing?我的问题是我们是否可以知道一个变量在执行时是否被加载到 L1/L2/L3 缓存中?

We know that we could print the address of a variable in C++ like this: int i; cout << &i;我们知道我们可以像这样在 C++ 中打印变量的地址: int i; cout << &i; int i; cout << &i; . . So if the i is loaded into the L1 cache, which kind of address is shown by the &i ?那么如果i被加载到 L1 缓存中, &i显示哪种地址? the address of L1 cache or the address of the RAM? L1缓存的地址还是RAM的地址?

In a typical PC, &i is always the address of a RAM location.在典型的 PC 中, &i始终是 RAM 位置的地址。 This is because the cache is essentially transparent to the programmer.这是因为缓存对程序员来说本质上是透明的。 It simply stores a copy of the same information contained in a region of memory, at a place that can be more quickly accessed by the CPU.它只是将包含在内存区域中的相同信息的副本存储在 CPU 可以更快访问的位置。 It is not addressable as a memory location directly.它不能直接作为内存位置寻址。

As for whether the variable is 'located' in the cache while the code is executing, it most certainly is.至于在代码执行时变量是否“位于”缓存中,它肯定是。 When you try to access data in a memory location, if it is not already in the cache, it will be brought into the cache first before your request is served.当您尝试访问内存位置中的数据时,如果它不在缓存中,它将在您的请求被服务之前首先被带入缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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