In a performance test, I want to bypass the influence of cache of linux system (including page cache/inode cache and so on).

I have tried O_DIRECT flag, but it's turned out that direct I/O is still "enjoy" the effect of some cache.

Is there a thorough way to close the effect of system cache?

migrated from stackoverflow.com Aug 5 '11 at 1:47

This question came from our site for professional and enthusiast programmers.

  • 2
    Whats the point of such an "unreal" performance test? You are trying to test something no modern OS would never allow to happen. – James Anderson Aug 4 '11 at 8:16
  • Unluckily, it is not unreal at all, it is still how kernel aio works today. I sooooooo wish that Mr.T would realize that asynchronous I/O and cache are not mutually exclusive terms. It's something the guys at Microsoft realized 15 years ago already (though admittedly performance prior to Vista can be disappointing, especially under concurrency). Some Indian guy wrote a patch years ago too, but was turned down. – Damon Aug 4 '11 at 9:54

(it actually drops so..) run sync first, then

free pagecache:

echo 1 > /proc/sys/vm/drop_caches

free dentries and inodes:

echo 2 > /proc/sys/vm/drop_caches

free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

Then you can start with a clean cache, but of course caching will still work.

  • i have heard about this method somewhere. but there is no such files on my ubuntu 10.04. so i failed to execute the above commands. – tom_cat Aug 4 '11 at 8:44
  • @tom_cat: You need a kernel newer than 2.6.16, and Ubuntu 10.04 came with 2.6.32 if I am not mistaken, so this should work. I've just checked my 11.04 install for what it's worth, which does have this proc entry (unluckily no 10.04 here any more) – Damon Aug 4 '11 at 9:19
  • @Damon: Yeah,you are right. thanks so much. I have found that I had logged on a remote machine of older kernel. – tom_cat Aug 4 '11 at 11:00

no, you cannot bypass the cache. But, however, you can force a file system sync (which means, clear the cache and write the data to disk) with the additional system call fsync(3).

Greetings, CK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy