This topic describes how to modify the parameter files in the /proc/sys/vm/ directory of Alibaba Cloud Linux 2 or 3 to fine-tune the system dirty page thresholds and writeback control.
Background information
In Linux, page cache is used to cache file system read and write operations (buffer I/Os). Data is first written to page cache and then to non-volatile storage through a writeback operation at a specific time. The page cache that has not yet been written back is referred to as dirty pages. To maintain an optimal level of dirty pages, the system periodically performs writeback operations or initiates writeback operations when the level of dirty pages reaches the upper limit.
Supported operating systems
Alibaba Cloud Linux 2
Alibaba Cloud Linux 3
Dirty page thresholds
Description
Different thresholds apply when the level of dirty pages fall into one of the ranges illustrated below:
```
(background writeback)
free run | soft throttle | hard throttle
-------------+---------------+---------------+------------>
^ ^ ^ dirty pages
dirty_background_thresh free_run dirty_thresh
```Free run
When the level of dirty pages in the system is lower than the
free runthreshold (free run = (dirty_background_thresh + dirty_thresh)/2)), the speed of writing dirty pages to the cache is not throttled. In this case, the system periodically executes the background writeback task (kupdate) to control the level of dirty pages.Soft throttle
When the level of dirty pages in the system exceeds the
free runthreshold, the speed of writing dirty pages to the cache is throttled to curb the accumulation of new dirty pages and control the level of dirty pages. The soft-throttle range is separated by thedirty_threshparameter from the hard-throttle range.Soft throttling occurs when the level of dirty pages exceeds the
free runthreshold but remains below thedirty_threshthreshold. In this case, the speed of writing dirty pages to the cache is throttled only if the number of dirty pages being written by a file system exceeds the dirty page quota for the file system. The dirty page quota for each file system is calculated by taking the average writeback bandwidth of the file system as a percentage of the total writeback bandwidth in the system over a certain period of time and multiplying the percentage by the threshold defined by thethreshparameter.Hard throttle
Hard throttling occurs when the level of dirty pages in the system exceeds the threshold defined by the
dirty_threshparameter. In this case, the speed of writing data to the significantly restricted.
Configure dirty page thresholds
The level of dirty pages in the system is jointly controlled by the thresholds defined by the thresh and dirty_background_thresh parameters.
dirty_background_thresh
You can use the /proc/sys/vm/dirty_background_bytes or /proc/sys/vm/dirty_background_ratio parameter to configure a value for the dirty_background_thresh parameter as a threshold.
/proc/sys/vm/dirty_background_bytes: specifies the value of thedirty_background_threshparameter in bytes./proc/sys/vm/dirty_background_ratio: specifies a percentage of the total available memory in the system as the value of thedirty_background_threshparameter.
Only one of the /proc/sys/vm/dirty_background_bytes and /proc/sys/vm/dirty_background_ratio parameters takes effect at a time, with the other set to 0. When you use sysctl to set one of the preceding parameters, the other parameter is automatically set to 0. The default value of the dirty_background_thresh parameter is 10% of the total available memory in the system.
Use the
dirty_background_bytesparameter to specify a threshold.Query the
dirty_background_bytesparameter.sysctl -n vm.dirty_background_bytesSet the
dirty_background_bytesparameter.Replace
<value>with a desired value. Unit: bytes.sudo sysctl -w vm.dirty_background_bytes=<value>
Use the
dirty_background_ratioparameter to specify a percentage of the total available memory in the system as a threshold.Query the
dirty_background_ratioparameter.sysctl -n vm.dirty_background_ratioSet the
dirty_background_ratioparameter.Replace
<percent>with a desired value. Unit: percentage (%). Example: 20.sudo sysctl -w vm.dirty_background_ratio=<percent>
dirty_thresh
You can use the /proc/sys/vm/dirty_bytes or /proc/sys/vm/dirty_ratio parameter to configure a value for the thresh parameter as a threshold.
/proc/sys/vm/dirty_bytes: specifies the value of thethreshparameter in bytes./proc/sys/vm/dirty_ratio: specifies a percentage of the total available memory in the system as the value ofthreshparameter.
Only one of the /proc/sys/vm/dirty_bytes and /proc/sys/vm/dirty_ratio parameters takes effect at a time, with the other set to 0. When you use sysctl to set one of the preceding parameters, the other parameter is automatically set to 0. The default value of the thresh parameter is 20% of the total available memory in the system.
Use the
/proc/sys/vm/dirty_bytesparameter to specify a threshold.Query the
dirty_bytesparameter.sysctl -n vm.dirty_bytesSet the
dirty_bytesparameter.Replace
<value>with a desired value. Unit: bytes.sudo sysctl -w vm.dirty_bytes=<value>
Use the
/proc/sys/vm/dirty_ratioparameter to specify a percentage of the total available memory in the system as a threshold.Query the
dirty_ratioparameter.sysctl -n vm.dirty_ratioSet
dirty_ratioparameter.Replace
<percent>with a desired value. Unit: percentage (%). Example: 20.sudo sysctl -w vm.dirty_ratio=<percent>
Periodic writeback in the background
When the level of dirty pages in the system is lower than the free run threshold (free run = (dirty_background_thresh + dirty_thresh)/2)), the speed of writing dirty pages to the cache is not throttled. In this case, the system periodically executes the background writeback task (kupdate) to control the level of dirty pages.
dirty_writeback_centisecs
The /proc/sys/vm/dirty_writeback_centisecs parameter specifies the interval at which the periodic writeback task is executed. Unit: centiseconds. A value of 0 indicates that the periodic writeback task is disabled. Default value: 500, which indicates that the periodic writeback task is executed at an interval of 5 seconds.
Query the
dirty_writeback_centisecsparameter.sysctl -n vm.dirty_writeback_centisecsSet the
dirty_writeback_centisecsparameter.Replace
<value>with a desired value.sudo sysctl -w vm.dirty_writeback_centisecs=<value>
dirty_expire_centisecs
The periodic writeback task selectively performs writeback operations and targets only timed-out dirty pages. If dirty pages are still available in a file a specific period of time after the first dirty page is written to the file, the file is deemed to have timed out. The periodic writeback task performs writeback on these timed-out dirty pages.
The /proc/sys/vm/dirty_writeback_centisecs parameter specifies the timeout period. Unit: centiseconds. Default value: 3,000, which is equal to 30 seconds.
Query the
dirty_expire_centisecsparameter.sysctl -n vm.dirty_expire_centisecsSet the
dirty_expire_centisecsparameter.sudo sysctl -w vm.dirty_expire_centisecs=<value>