Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flush the CPU cache in Linux from a C program?

I am writing a C program in which I need to flush my memory. I would like know if there is any UNIX system command to flush the CPU cache.

This is a requirement for my project which involves calculating the time taken for my logic.

I have read about the cacheflush(char *s, int a, int b) function but I am not sure as to whether it will be suitable and what to pass in the parameters.

like image 990
Rose BEck Avatar asked Jun 30 '12 21:06

Rose BEck


People also ask

How do I free up CPU cache?

If you turn off the PC and unplug the power (to know for sure it's off) all cache/RAM in the system (including that in the CPU, GPU, memory, storage drives, etc.) is cleared.

Can we clear cache memory in Linux?

In all the Linux systems we have three options to clear cache without interrupting any services or processes. Now to set run permission, to clear ram cache, you have to call the script whenever required, setting a cron to clear RAM caches every day for 3 hours.

What is Cacheflush in Linux?

cacheflush() flushes the contents of the indicated cache(s) for the user addresses in the range addr to (addr+nbytes-1). cache may be one of: ICACHE Flush the instruction cache. DCACHE Write back to memory and invalidate the affected valid cache lines.


1 Answers

  1. I take it you mean "CPU cache", not memory cache

  2. The link above is good: the suggestion "write a lot of data via CPU" is not Windows specific

  3. Here's another variation on the same theme:

    • How to clear CPU L1 and L2 cache
  4. Here's an article about Linux and CPU cache:

    • http://lwn.net/Articles/252125/

NOTE:

At this (very, very low) level, "Linux" != "Unix"

like image 99
paulsm4 Avatar answered Oct 03 '22 10:10

paulsm4