Linux Apache2 – How to Disable All Disk Caching

apache-2.2benchmarkdisk-cachelinux

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.

Best Answer

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.