by
This is post #16 in myDecember 2013 series about Linux Virtual Machine PerformanceTuning. For more, please see the tag “Linux VMPerformance Tuning.”
In previous postson
There is also thechance that a lot of I/O will overwhelm the cache, too. Everwritten a lot of data to disk all at once, and seen large pauses onthe system while it tries to deal with all that data? Those pausesare a result of the cache deciding that there’s too much data to bewritten asynchronously (as a non-blocking background operation,letting the application process continue), and switches to writingsynchronously (blocking and making the process wait until the I/Ois committed to disk). Of course, a filesystem also has to preservewrite order, so when it starts writing synchronously it first hasto destage the cache. Hence the long pause.
The nice thing isthat these are controllable options, and based on your workloads& data you can decide how you want to set them up. Let’s take alook:
$ sysctl -a | grep dirty vm.dirty_background_ratio = 10 vm.dirty_background_bytes = 0 vm.dirty_ratio = 20 vm.dirty_bytes = 0 vm.dirty_writeback_centisecs = 500 vm.dirty_expire_centisecs = 3000
vm.dirty_background_ratio
vm.dirty_ratio
vm.dirty_background_bytes
vm.dirty_expire_centisecs
vm.dirty_writeback_centisecs
You can also seestatistics on the page cache in /proc/vmstat:
$ cat /proc/vmstat | egrep "dirty|writeback" nr_dirty 878 nr_writeback 0 nr_writeback_temp 0
In my case I have878 dirty pages waiting to be written to disk.
As with mostthings in the computer world, how you adjust these depends on whatyou’re trying to do. In many cases we have fast disk subsystemswith their own big, battery-backed NVRAM caches, so keeping thingsin the OS page cache is risky. Let’s try to send I/O to the arrayin a more timely fashion and reduce the chance our local OS will,to borrow a phrase from the service industry, be “in theweeds.” To do this we lower vm.dirty_background_ratio andvm.dirty_ratio by adding new numbers to /etc/sysctl.conf andreloading with “sysctl –p”:
vm.dirty_background_ratio = 5vm.dirty_ratio = 10
This is a typicalapproach on virtual machines, as well as Linux-basedhypervisors.
There arescenarios where raising the cache dramatically has positive effectson performance. These situations are where the data contained on aLinux guest isn’t critical and can be lost, and usually where anapplication is writing to the same files repeatedly or inrepeatable bursts. In theory, by allowing more dirty pages to existin memory you’ll rewrite the same blocks over and over in cache,and just need to do one write every so often to the actual disk. Todo this we raise the parameters:
vm.dirty_background_ratio = 50vm.dirty_ratio = 80
Sometimes folksalso increase the vm.dirty_expire_centisecs parameter to allow moretime in cache. Beyond the increased risk of data loss, you also runthe risk of long I/O pauses if that cache gets full and needs todestage, because on large VMs there will be a lot of data incache.
There are alsoscenarios where a system has to deal with infrequent, burstytraffic to slow disk (batch jobs at the top of the hour, midnight,writing to an SD card on a Raspberry Pi, etc.). In that case anapproach might be to allow all that write I/O to be deposited inthe cache so that the background flush operations can deal with itasynchronously over time:
vm.dirty_background_ratio = 5vm.dirty_ratio = 80
Here thebackground processes will start writing right away when it hitsthat 5% ceiling but the system won’t force synchronous I/O until itgets to 80% full. From there you just size your system RAM andvm.dirty_ratio to be able to consume all the written data. Again,there are tradeoffs with data consistency on disk, which translatesinto risk to data. Buy a UPS and make sure you can destage cachebefore the UPS runs out of power. :)
No matter theroute you choose you should always be gathering hard data tosupport your changes and help you determine if you are improvingthings or making them worse. In this case you can get data frommany different places, including the application itself,/proc/vmstat, /proc/meminfo, iostat, vmstat, and many of the thingsin /proc/sys/vm. Good luck!
联系客服