linux caching vm.dirty_backgroud_ratio vm.dirty_ratio hung_task_timeout_secs

$ 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 is the percentage of system memory that can be filled with “dirty” pages — memory pages that still need to be written to disk — before the pdflush/flush/kdmflush background processes kick in to write it to disk. My example is 10%, so if my virtual server has 32 GB of memory that’s 3.2 GB of data that can be sitting in RAM before something is done.

vm.dirty_ratio is the absolute maximum amount of system memory that can be filled with dirty pages before everything must get committed to disk. When the system gets to this point all new I/O blocks until dirty pages have been written to disk. This is often the source of long I/O pauses, but is a safeguard against too much data being cached unsafely in memory.

vm.dirty_background_bytes and vm.dirty_bytes are another way to specify these parameters. If you set the _bytes version the _ratio version will become 0, and vice-versa.

vm.dirty_expire_centisecs is how long something can be in cache before it needs to be written. In this case it’s 30 seconds. When the pdflush/flush/kdmflush processes kick in they will check to see how old a dirty page is, and if it’s older than this value it’ll be written asynchronously to disk. Since holding a dirty page in memory is unsafe this is also a safeguard against data loss.

vm.dirty_writeback_centisecs is how often the pdflush/flush/kdmflush processes wake up and check to see if work needs to be done.

You can also see statistics 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 have 878 dirty pages waiting to be written to disk.

Approach 1: Decreasing the Cache

As with most things in the computer world, how you adjust these depends on what you’re trying to do. In many cases we have fast disk subsystems with their own big, battery-backed NVRAM caches, so keeping things in the OS page cache is risky. Let’s try to send I/O to the array in a more timely fashion and reduce the chance our local OS will, to borrow a phrase from the service industry, be “in the weeds.” To do this we lower vm.dirty_background_ratio and vm.dirty_ratio by adding new numbers to /etc/sysctl.conf and reloading with “sysctl –p”:

vm.dirty_background_ratio = 5
vm.dirty_ratio = 10

This is a typical approach on virtual machines, as well as Linux-based hypervisors. I wouldn’t suggest setting these parameters to zero, as some background I/O is nice to decouple application performance from short periods of higher latency on your disk array & SAN (“spikes”).

Approach 2: Increasing the Cache

There are scenarios where raising the cache dramatically has positive effects on performance. These situations are where the data contained on a Linux guest isn’t critical and can be lost, and usually where an application is writing to the same files repeatedly or in repeatable bursts. In theory, by allowing more dirty pages to exist in 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. To do this we raise the parameters:

vm.dirty_background_ratio = 50
vm.dirty_ratio = 80

Sometimes folks also increase the vm.dirty_expire_centisecs parameter to allow more time in cache. Beyond the increased risk of data loss, you also run the risk of long I/O pauses if that cache gets full and needs to destage, because on large VMs there will be a lot of data in cache.

Approach 3: Both Ways

There are also scenarios where a system has to deal with infrequent, bursty traffic 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 an approach might be to allow all that write I/O to be deposited in the cache so that the background flush operations can deal with it asynchronously over time:

vm.dirty_background_ratio = 5
vm.dirty_ratio = 80

Here the background processes will start writing right away when it hits that 5% ceiling but the system won’t force synchronous I/O until it gets to 80% full. From there you just size your system RAM and vm.dirty_ratio to be able to consume all the written data. Again, there are tradeoffs with data consistency on disk, which translates into risk to data. Buy a UPS and make sure you can destage cache before the UPS runs out of power. :)

No matter the route you choose you should always be gathering hard data to support your changes and help you determine if you are improving things or making them worse. In this case you can get data from many different places, including the application itself, /proc/vmstat, /proc/meminfo, iostat, vmstat, and many of the things in /proc/sys/vm. Good luck!


有關Cache

文件緩存是提升性能的重要手段。毋庸置疑,讀緩存(Read caching)在絕大多數情況下是有益無害的(程序可以直接從RAM中讀取數據),而寫緩存(Write caching)則相對複雜。Linux內核將寫磁盤的操作分解成了,先寫緩存,每隔一段時間再異步地將緩存寫入磁盤。這提升了IO讀寫的速度,但存在一定風險。數據沒有及時寫入磁盤,所以存在數據丟失的風險。

同樣,也存在cache被寫爆的情況。還可能出現一次性往磁盤寫入過多數據,以致使系統卡頓。之所以卡頓,是因爲系統認爲,緩存太大用異步的方式來不及把它們都寫進磁盤,於是切換到同步的方式寫入。(異步,即寫入的同時進程能正常運行;同步,即寫完之前其他進程不能工作)。

好消息是,你可以根據實際情況,對寫緩存進行配置。
可以看一下這些參數:

[root@host ~]# sysctl -a | grep dirty
vm.dirty_background_ratio = 10vm.dirty_background_bytes = 0vm.dirty_ratio = 20vm.dirty_bytes = 0vm.dirty_writeback_centisecs = 500vm.dirty_expire_centisecs = 3000

vm.dirty_background_ratio 是內存可以填充“髒數據”的百分比。這些“髒數據”在稍後是會寫入磁盤的,pdflush/flush/kdmflush這些後臺進程會稍後清理髒數據。舉一個例子,我有32G內存,那麼有3.2G的內存可以待着內存裏,超過3.2G的話就會有後來進程來清理它。

vm.dirty_ratio 是絕對的髒數據限制,內存裏的髒數據百分比不能超過這個值。如果髒數據超過這個數量,新的IO請求將會被阻擋,直到髒數據被寫進磁盤。這是造成IO卡頓的重要原因,但這也是保證內存中不會存在過量髒數據的保護機制。

vm.dirty_expire_centisecs 指定髒數據能存活的時間。在這裏它的值是30秒。當 pdflush/flush/kdmflush 進行起來時,它會檢查是否有數據超過這個時限,如果有則會把它異步地寫到磁盤中。畢竟數據在內存裏待太久也會有丟失風險。

vm.dirty_writeback_centisecs 指定多長時間 pdflush/flush/kdmflush 這些進程會起來一次。

可以通過下面方式看內存中有多少髒數據:

[root@host ~]# cat /proc/vmstat | egrep "dirty|writeback"nr_dirty 69nr_writeback 0nr_writeback_temp 0

這說明了,我有69頁的髒數據要寫到磁盤裏。


情景1:減少Cache

你可以針對要做的事情,來制定一個合適的值。
在一些情況下,我們有快速的磁盤子系統,它們有自帶的帶備用電池的NVRAM caches,這時候把數據放在操作系統層面就顯得相對高風險了。所以我們希望系統更及時地往磁盤寫數據。
可以在/etc/sysctl.conf中加入下面兩行,並執行"sysctl -p"

vm.dirty_background_ratio = 5vm.dirty_ratio = 10

這是虛擬機的典型應用。不建議將它設置成0,畢竟有點後臺IO可以提升一些程序的性能。


情景2:增加Cache

在一些場景中增加Cache是有好處的。例如,數據不重要丟了也沒關係,而且有程序重複地讀寫一個文件。允許更多的cache,你可以更多地在內存上進行讀寫,提高速度。

vm.dirty_background_ratio = 50vm.dirty_ratio = 80

有時候還會提高vm.dirty_expire_centisecs 這個參數的值,來允許髒數據更長時間地停留。


情景3:增減兼有

有時候系統需要應對突如其來的高峯數據,它可能會拖慢磁盤。(比如說,每個小時開始時進行的批量操作等)
這個時候需要容許更多的髒數據存到內存,讓後臺進程慢慢地通過異步方式將數據寫到磁盤當中。

vm.dirty_background_ratio = 5vm.dirty_ratio = 80

這個時候,後臺進行在髒數據達到5%時就開始異步清理,但在80%之前系統不會強制同步寫磁盤。這樣可以使IO變得更加平滑。






這是一個在客戶現場碰到的問題,問題很簡單,但是之前沒有碰到過,大概是在readhat上裝數據庫較少吧,記錄一下:
客戶有一臺服務器,安裝了VMW軟件做了虛擬化,在其上搭建了一臺readhat虛擬機,起初給的內存爲16G,在添加了12G的內存後,將虛擬機的內存調整到了20G
調整完後主機這邊就一直報錯:
Nov  5 13:05:41 RedHat5 kernel: INFO: task oracle:22439 blocked for more than 120 seconds.
Nov  5 13:05:41 RedHat5 kernel: “echo 0 > /proc/sys/kernel/hung_task_timeout_secs” disables this message.
查詢了資料後對於該參數的瞭解爲後臺對進行的任務由於超時而掛起
從以上的報錯信息也給出了簡單的解決方案,就是禁止該120秒的超時:echo 0 > /proc/sys/kernel/hung_task_timeout_secs
隨後詢問了主機工程師:給出方案是按照告警裏的提示將該提醒disable

後續詢問後給出如下解釋:
This is a know bug. By default Linux uses up to 40% of the available memory for file system caching.
After this mark has been reached the file system flushes all outstanding data to disk causing all following IOs going synchronous.
For flushing out this data to disk this there is a time limit of 120 seconds by default.
In the case here the IO subsystem is not fast enough to flush the data withing 120 seconds.
This especially happens on systems with a lof of memory.

The problem is solved in later kernels and there is not “fix” from Oracle.
I fixed this by lowering the mark for flushing the cache from 40% to 10% by setting “vm.dirty_ratio=10″ in /etc/sysctl.conf.
This setting does not influence overall database performance since you hopefully use Direct IO and bypass the file system cache completely.
告知是linux會設置40%的可用內存用來做系統cache,當flush數據時這40%內存中的數據由於和IO同步問題導致超時(120s),所將40%減小到10%,避免超時。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章