简体   繁体   中英

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.

My question is if we can know if a variable is loaded into L1/L2/L3 cache while executing?

We know that we could print the address of a variable in C++ like this: 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 ? the address of L1 cache or the address of the RAM?

In a typical PC, &i is always the address of a RAM location. 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. 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.

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