7

For benchmarking purposes I want to force Apache 2 to load each requested file from disk instead of loading it from a cache in memory. From what I have read doing a sync followed by

echo 3 > /proc/sys/vm/drop_caches

lets me drop linux's cache. A subsequent request for a particular file will then not be served from linux's cache, but further requests for the same file will be served from linux's cache again. That is so because /proc/sys/vm/drop_caches doesn't disable caching, it only discards what has been cached up to that moment. I could probably drop the cache before each and every request, but I'd prefer another solution. Is there anything else I can do to ensure that apache loads each requested file from disk?

Why I want to do this: I know that in normal operation caching is enabled. But the server is not serving small and frequently accessed files such as html pages, small images, etc. Instead it is serving files which are mostly a couple of megabytes in size from a very large set of files. These files are accessed pretty uniformly and therefore each individual file is accessed very rarely. Thus in normal operation I expect that most accesses will not cause a cache hit but require the file to be loaded from disk. I have several sample files which I want to access using apache's ab benchmark to measure how many transactions per second the server is able to serve. Unfortunately I believe the results I am getting are way too optimistic because of caching. Therefore I want to disable Linux's disk caching and any caching Apache might do itself.

Update: the answer given so far tells me how to disable Apache's own caching, but I am still wondering if there is a way to disable the caching done by the linux kernel.

davitenio
  • 375
  • 2
  • 5
  • 10
  • 4
    What are you trying to test? Disk Speed: bonnie is IMHO much better to do that. Apache? **Use** caches, normal operation will use caches anyway... – Martin M. Jun 23 '09 at 11:38
  • You should also consider the Cache-Control: no-cache HTTP header field. – zero_r Jun 23 '09 at 16:24
  • I posted the solution for your problem at superuser.com: http://superuser.com/questions/242928/disable-linux-read-and-write-file-cache-on-partition/464382#464382 – Robert Metzger Aug 21 '12 at 21:59
  • Please set expires to 0, http header with no-cache and it will do. Disabling filesystem cache in linux is afaik impossible because there are too many caches. You should consider **pagecache**, **dentries** and **inodes** and if we are talking about os cache you should somehow remove SLAB caching. Think about mounting the apache2 content with "sync". Why would You do that again ? – wojciechz Aug 21 '12 at 23:20
  • please refer to this answer: http://superuser.com/a/464382/106740 It explains how you can start a linux process so that it "fadvices" the kernel to aviod caching the files! – Robert Metzger Aug 21 '12 at 22:01

4 Answers4

7

I don't think you can disable all disk caching in Linux.

As a hack, you could keep running "sync; echo 3 > /proc/sys/vm/drop_caches" to flush almost anything that is cached in memory. From the console

watch -n 1 `sync; echo 3 > /proc/sys/vm/drop_caches`

would do the trick. In the above example nothing will remain cached by the kernel for more than a second, though it will have no effect on data held in memory by Apache or other processes. It may also not flush stuff from any memory-mapped files that are still open with portions locked.

If you only want nothing cached at the start of a test run, and don't care if it caches stuff during the tests, then you could just add a single call to "sync" and "echo 3 > /proc/sys/vm/drop_caches" at the start of your test run.

If your test involves scripts that access a database you will need to be able to tell your database back-end to not cache stuff in RAM between tests also.

David Spillett
  • 22,534
  • 42
  • 66
  • Don't forget that even with the OS cache disabled, the drive itself will have an on-board cache. – David Jun 28 '09 at 00:44
  • Good point, thought the physical disk cache will be smaller (between 2Mb and 32Mb these days) than the OS cache so it may be practical to write a cache size block to somewhere you don't care about to "flush" this cache. Most drives will respect "disable write cache" instructions from hdparm/sdparm but I don't think you can disable the physical cache completely. – David Spillett Jun 28 '09 at 08:26
2

You can disable the majority of caching in Apache by disabling the mod_cache module, commenting out the following lines in your configuration should do the trick:

  • LoadModule cache_module
  • LoadModule disk_cache_module
  • LoadModule mem_cache_module
David
  • 3,519
  • 21
  • 17
  • I think he menas he wants to turn off OS level disk caching. – Jason Tan Jun 23 '09 at 16:08
  • Actually I want to turn off caching both on the OS level and in Apache. So this answer is helpful as it probably disables caching by Apache. But I am wondering if there is a way to also disable caching by the OS (/proc/sys/vm/drop_caches lets me only drop the cache but not disable it) – davitenio Jun 23 '09 at 16:38
1

No, it is not possible to disable the use of the buffer cache. There are ways to do this programatically, (opening the file in O_DIRECT for instance), but apache would have to be rewritten to do this.

At the core, it looks like you are trying to benchmark your IO subsystem for multiple streaming megabyte sized files. There are much better ways to do this without mixing the overhead of apache into the picture.

If you are determined to attempt this you could try writing a small program to malloc most of the physical memory in the machine to reduce the amount of disk cache available, however this would probably cause the server to page ruining your benchmark results (apaches multi process model will interact badly with this unless you set the StartServers, MinServers and MaxServers to the same value to avoid process creation during your benchmark run)

Dave Cheney
  • 18,307
  • 7
  • 48
  • 56
0

I might be terribly off-kilter here (so let me know if I'm making an incorrect assumption) but disk caching in Linux is known as swap, and uses a swapfile or swap partition.

swapoff -a 

Should disable disk caching.

Matt Simmons
  • 20,218
  • 10
  • 67
  • 114
  • I think he is referring to the keeping of disk based data in RAM for quick access (measured more-or-less by the cache and buffers readings in "free" or "top") not data from memory stored on disk because there was a RAM shortage (which is the virtual memory facility you are turning off with the swappoff command). – David Spillett Jun 23 '09 at 17:37
  • With disk caching I am referring to caching disk accesses in RAM, i.e. to cache pages in RAM to avoid unnecessary accesses to disk. I am not referring to swapping, which is a technique to free RAM by saving pages on disk. – davitenio Jun 23 '09 at 17:43