简体   繁体   中英

What would be the most correct way to clear L1,L2 and L3 caches in C++?

I want to run some experiments on an algorithm in order to see how cache efficient it is.

I run the main code of the algorithm on one input several times (iterations), get the values of different counters (branch mispredictions, L2, L3 cache accesses, misses etc) and then after all the iterations are finished I find the average of each counter and return it as an output.

In order for the experiment to be precise, I need to clear the cache before every new iteration.

So the code is looking something like this:

main()
   for (it = 0; it < iterations; it++)
        clear_cache();
        run algorithm
        update counters
   return average of all counters

Everything is working as expected but I am not very sure about how to do the clearing of the cache properly.

I have found the following method online:

void clear_cache(){
   sync();
   std::ofstream ofs("/proc/sys/vm/drop_caches");
   ofs << "3" << std::endl;
   sync();
}

However if the total amount of iterations is large, this method takes a lot of time to execute. On the other hand if I completely remove sync(); the clearing process becomes much faster.

But I have no idea what sync(); does in practice. Why does everything becomes faster without sync(); ? Do I need this call in order to be sure that all L1,L2 and L3 caches will be clear before each new iteration?

thank you in advance

sync flushes outstanding file writes. It doesn't affect cached file reads, though. The drop_caches method you found online does clear the disk cache, so any future read will also hit the disk.

All this is unrelated to L1/2/3 CPU caches. Couldn't be, even. The sync function itself will be in L1 cache!

You need assembly code to flush the cache, but you forgot to state which CPU you have.

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