5

What would be the programmatic steps written in "C" associated with clearing the L2 cache on a Linux OS machine?

/sys/devices/system/cpu/cpu0/cache/index2/size = 6144K x 8CPUs

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Rob Schein
  • 91
  • 1
  • 3
  • Related: http://stackoverflow.com/questions/1756825/cpu-cache-flush – quantumSoup Aug 10 '10 at 22:41
  • and another related: http://stackoverflow.com/questions/3446138/how-to-clear-cpu-l1-and-l2-cache – ennuikiller Aug 10 '10 at 22:42
  • 3
    This is CPU dependent and unportable. Please add description of your platform/corresponding tags. Adding tag "assembly" might help getting attention of people with system development experience who might have experience. Because in the end you would need to code some assembly anyway. – Dummy00001 Aug 10 '10 at 23:30
  • By clearing, do you mean setting each cache line to invalid? how can you run the code to clear the cache without having at least some cache lines being valid? :-) –  Mar 10 '11 at 16:18
  • Possible duplicate of [How to flush the CPU cache in Linux from a C program?](https://stackoverflow.com/questions/11277984/how-to-flush-the-cpu-cache-in-linux-from-a-c-program) – Ciro Santilli OurBigBook.com Aug 24 '17 at 06:28

2 Answers2

2

The closest you can get in any remotely clean/portable way:

char dummy[L2_CACHE_SIZE];
memset(dummy, 0, sizeof dummy);

Depending on your CPU, there may be privileged opcodes that can clear the cache, but I don't know anything about them or how you might access them. It's likely that if they exist, you still might need kernel-level code to use them.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • And that means we might find a syscall to perform the task in question. – Basilevs Aug 11 '10 at 05:18
  • 3
    Memset() historically uses instructions which don't bother to read the destination cache line and write line combine on the cache line to be written. There are however instructions which stream data directly to memory (they have their own dedicated cache line, outside of the cache). A smart compiler might use such instructions, to protect the L2 cache from being wiped by such a memset(). –  Mar 10 '11 at 16:09
  • I wonder how much cache associativity will get in the way of clearing the cache? imagine you had a fully associative L2 cache; LRU replacement being the only thing which is occurring. Other processes are running, keeping cache lines in general recently used - but the memset is of course not writing again to the cache lines its using to write. They will soon become the LRU lines... so the memset() would end up using and re-using a small set of cache lines for its writes. –  Mar 10 '11 at 16:12
  • Of course, L2 cache is at most say 24 way associative. But I wonder if this would mean after a few sets of 24 memory locations, the cache associativity combined with LRU replacements means you end up *re-using* the same few write lines in each associativity block in the L2 cache. –  Mar 10 '11 at 16:16
0

You cannot access low level memory from user space, you must implement your own device driver to have access to physical memory in Linux.

Amine Kerkeni
  • 914
  • 3
  • 14
  • 29