Storing C / C ++ variables in the processor cache instead of system memory

On an Intel x86 platform running Linux, in C / C ++, how can I tell the operating system and hardware to store a value (like uint32) in the L1 / L2 cache, and not in the system memory? For example, let's say for security or performance reasons, I don’t want to store the 32-bit key (32-bit unsigned int) in DRAM, and instead I would like to store it only in the processor cache. How can i do this? I am using Fedora 16 (Linux 3.1 and gcc 4.6.2) on an Intel Xeon processor.

Thank you very much for your help!

+4
source share
5 answers

On x86 (or even on any platform that I know of) there are no cpu commands that will allow you to force the CPU to save something in the L1 / L2 cache. Not to mention exposing such an extremely low-level detail for higher-level languages ​​such as C / C ++. Saying that you need to do this for "performance" is pointless without the additional context of what kind of performance you are watching. Why is your program so heavily dependent on data access only in the cache. To say you need this for security just seems like a bad security design. In any case, you need to provide much more details about what exactly you are trying to do here.

+5
source

The short answer is, you can’t - this is not what these caches are for - they are loaded from the main memory, speed up access, or allow you to use advanced methods, such as branch prediction and pipeline processing.

There are ways to ensure that caches are used for certain data, but it will still be in ram mode and in proactive multitasking operating system, you cannot guarantee that the contents of your cache will not be deleted using the context switch between any two instructions, except for "stopping the world" or low-level atomic operations, but they are usually for very, very, very short sequences of instructions that simply cannot be interrupted, for example, increment Nia and sampling for the spindle lock, and not for the processing of cryptographic algorithms at a time.

+5
source

I don’t think you can force the variable to be stored in the processor cache, but you can use the register keyword to suggest to the compiler that the variable be allocated to the CPU register, declaring it like this:

 register int i; 
+3
source

You cannot use the cache directly, but you can use hardware registers for integers, and they are faster.

+1
source

If you really need performance, the variable is better in the CPU register.

If you cannot use case, for example, because you need to share the same value in different threads or cores (multi-core is becoming common now!), You need to store this variable in memory.

As already mentioned, you cannot force some cache memory to use a call or keyword. However, caches are not completely stupid: if you often use a block of memory, you should not have problems to save it in the cache.

Keep in mind that if you manage to write a lot of space from different cores to this memory, you will strain the cache coherency blocks in the processor, because they need to make sure that all caches and the actual memory below are stored in synchronization. Simply put, this will reduce the overall performance of the CPU.

Note that the opposite (non-caching) exists as a property that you can assign parts of your memory to heaps.

+1
source

Source: https://habr.com/ru/post/1396705/


All Articles