APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Invalidating the Linux buffer cache


Invalidating the Linux buffer cache




When you write data, it doesn't necessarily get written to disk right then. The kernel maintains caches of many things, and disk data is something where a lot of work is done to keep everything fast and efficient. That's great for performance, but sometimes you want to know that data really has gotten to the disk drive. This could be because you want to test the performance of the drive, but could also be when you suspect a drive is malfunctioning: if you just write and read back, you'll be reading from cache, not from actual disk platters.

So how can you be sure you are reading data from the disk? The answer actually gets a little complicated, particularly if you are testing for integrity, so bear with me.

Obviously the first thing you need to do is get the data in the cache sent on its way to the disk. That's "sync", which tells the kernel that you want the data written. But that doesn't mean that a subsequent read comes from disk: if the requested data is still in cache, that's where it will be fetched from. It also doesn't necessarily mean that the kernel actually has sent the data along to the disk controller: a "sync" is a request, not a command that says "stop everything else you are doing and write your whole buffer cache to disk right now!". No, "sync" just means that the cache will be written, as and when the kernel has time to do so.

Traditonally, the only way to be sure you were not reading back from the cache was to overwrite the cache with other data. That required two things: knowing how big the cache is at this moment, and having unrelated data of sufficient size to overwrite with. On older Unixes with fixed sized buffer caches, the first part was easy enough, and since memory was often expensive and in shorter supply than it is now, the cache wasn't apt to be all that large anyway. That's changed radically: modern systems allocate cache memory dynamically and while the total cache is still small compared to disk drives, it can now be gigabytes of data that you need to overwrite.

Well, that's not always so hard: for a large filesystem and relatively small memory, a simple "ls -lR" might be enough. If not, a "dd" redirected to /dev/null can fill it up. Just make sure that you are looking at different disk blocks than what you first wrote. Note that you really didn't even need the "sync" if this is what you are doing: the overwrite forces the sync itself.

Modern Linux kernels make this a bit easier: in /proc/sys/vm/ you'll find "drop_caches". You simply echo a number to that to free caches.

From http://linux.inet.hr/proc_sys_vm_drop_caches.html:

To free pagecache:

  • echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

  • echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

  • echo 3 > /proc/sys/vm/drop_caches



You absolutely need to call "sync" before doing that. I haven't looked at how this is implemented; I assume that the pending syncs would be done before the cache is actually thrown away, and that in the meantime the cache is now seen as invalid so subsequent reads would have to wait for the sync write before returning. It would be simple enough to test this.

Actually, maybe not. I tried testing this on a Suse instance in a virtual machine, and couldn't do it. The script I used looked like this:


cat /tmp/t
date
date > /tmp/t
echo 3 > /proc/sys/vm/drop_caches
# this sets ctrl-alt-del not to call sync
echo 1 > /proc/sys/kernel/ctrl-alt-del
echo "ctrl-alt del now"


What I expected was for /tmp/t not to have the latest date. However, it always did, probably because the Reiserfs would fix up partial transactions. You'd need a system without a journaled file system to test this.

But even that didn't seem to work: I created an ext2 fs on another virtual hard drive and tried this:



cat /hdc/t
date
date > /hdc/t
echo 3 > /proc/sys/vm/drop_caches
cat /proc/sys/vm/drop_caches
umount /dev/hdc1
dd if=/dev/hda3 of=/dev/null
mount /dev/hdc1 /hdc
cat /hdc/t

But that didn't behave as I thought it would either. Possibly VM caching is throwing this off? Nope: I tried the same thing on a real system; the file doesn't lose its updates. So I'm not sure you can trust drop_caches.

However, if testing for integrity, and perhaps even if doing serious performance testing, this isn't enough: disk drives almost always do their own caching. If we really need to be certain that our reads came directly from the platters and not from ram on the controller, we still need to go back to the idea of knowing how big that cache is and writing enough data to force it to be flushed. So, we are still going to do "dd"'s or "ls -lR"'s or something like that.

If you are examining integrity and suspect corruption, keep in mind that aging can affect your results: you might need data to sit in cache (kernel or disk hardware) for some period before the problem occurs. Quick overwrites might mask it. Tracking down this kind of problem can be very difficult.

See also Caches and cache data corruption

By the way, if your aim is simply to bypass cache buffering, you can do that: Raw Disk I/O is what you want. And (as some databases do) you could simply write data to a raw partition (no filesystem).




If this page was useful to you, please help others find it:  





10 comments




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!




Fri Feb 2 15:30:42 2007: 2847   anonymous


Do you know anything about the opposite problem: how to force the kernel to give priority to a file when caching ?



Fri Feb 2 15:49:09 2007: 2848   TonyLawrence

gravatar
Don't know what you are trying to accomplish.. maybe put it in a ram disk..






Wed Feb 21 16:29:13 2007: 2869   axelilly


/proc/sys/vm/drop_caches does not exist in my Linux 2.6.9 kernel



Wed Feb 21 16:33:32 2007: 2870   TonyLawrence

gravatar
Ayup - that's the fun side of Linux: here today, changed tomorrow..

Have patience.. someone will probably reinvent it again :-)






Wed Mar 28 21:54:03 2007: 2929   anonymous


I believe it's new in 2.6.16



Thu Nov 22 09:16:14 2007: 3266   anonymous


/proc/sys/vm/drop_caches is not there in 2.6.9



Fri Feb 24 11:32:41 2012: 10633   Sina

gravatar


I wrote 3 in drop-cache file in my Linux. but, I don't know when it started to drop my cache? can everybody say me when dropping occurs?



Fri Feb 24 12:44:34 2012: 10634   TonyLawrence

gravatar


I would expect it to be instantaneous, but as I explained above, I never saw it work correctly.



Tue Apr 3 04:26:09 2012: 10815   anonymous

gravatar


A neat trick, but remember:
drop_caches will *NOT* drop a dirty cache. Perhaps that explains why your experiment failed.



Wed Aug 1 09:59:27 2012: 11223   SimonL

gravatar


The never ending problem of drop cache excessiveness verses the OOM killer. There should be a method to instruct the kernel to act conservatively, yet I did not find one. Other than running sync && echo 3 > /proc/sys/vm/drop_caches in the cron every minute. I wonder why anyone bothers to run Oracle on Linux instead of UNIX.

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Disks/Filesystems
       - Kernel
       - Linux
       - Troubleshooting
       - Unix















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)