162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci=======================
462306a36Sopenharmony_ciDAMON-based Reclamation
562306a36Sopenharmony_ci=======================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciDAMON-based Reclamation (DAMON_RECLAIM) is a static kernel module that aimed to
862306a36Sopenharmony_cibe used for proactive and lightweight reclamation under light memory pressure.
962306a36Sopenharmony_ciIt doesn't aim to replace the LRU-list based page_granularity reclamation, but
1062306a36Sopenharmony_cito be selectively used for different level of memory pressure and requirements.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciWhere Proactive Reclamation is Required?
1362306a36Sopenharmony_ci========================================
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciOn general memory over-committed systems, proactively reclaiming cold pages
1662306a36Sopenharmony_cihelps saving memory and reducing latency spikes that incurred by the direct
1762306a36Sopenharmony_cireclaim of the process or CPU consumption of kswapd, while incurring only
1862306a36Sopenharmony_ciminimal performance degradation [1]_ [2]_ .
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciFree Pages Reporting [3]_ based memory over-commit virtualization systems are
2162306a36Sopenharmony_cigood example of the cases.  In such systems, the guest VMs reports their free
2262306a36Sopenharmony_cimemory to host, and the host reallocates the reported memory to other guests.
2362306a36Sopenharmony_ciAs a result, the memory of the systems are fully utilized.  However, the
2462306a36Sopenharmony_ciguests could be not so memory-frugal, mainly because some kernel subsystems and
2562306a36Sopenharmony_ciuser-space applications are designed to use as much memory as available.  Then,
2662306a36Sopenharmony_ciguests could report only small amount of memory as free to host, results in
2762306a36Sopenharmony_cimemory utilization drop of the systems.  Running the proactive reclamation in
2862306a36Sopenharmony_ciguests could mitigate this problem.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciHow It Works?
3162306a36Sopenharmony_ci=============
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciDAMON_RECLAIM finds memory regions that didn't accessed for specific time
3462306a36Sopenharmony_ciduration and page out.  To avoid it consuming too much CPU for the paging out
3562306a36Sopenharmony_cioperation, a speed limit can be configured.  Under the speed limit, it pages
3662306a36Sopenharmony_ciout memory regions that didn't accessed longer time first.  System
3762306a36Sopenharmony_ciadministrators can also configure under what situation this scheme should
3862306a36Sopenharmony_ciautomatically activated and deactivated with three memory pressure watermarks.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciInterface: Module Parameters
4162306a36Sopenharmony_ci============================
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ciTo use this feature, you should first ensure your system is running on a kernel
4462306a36Sopenharmony_cithat is built with ``CONFIG_DAMON_RECLAIM=y``.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ciTo let sysadmins enable or disable it and tune for the given system,
4762306a36Sopenharmony_ciDAMON_RECLAIM utilizes module parameters.  That is, you can put
4862306a36Sopenharmony_ci``damon_reclaim.<parameter>=<value>`` on the kernel boot command line or write
4962306a36Sopenharmony_ciproper values to ``/sys/module/damon_reclaim/parameters/<parameter>`` files.
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciBelow are the description of each parameter.
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cienabled
5462306a36Sopenharmony_ci-------
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciEnable or disable DAMON_RECLAIM.
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciYou can enable DAMON_RCLAIM by setting the value of this parameter as ``Y``.
5962306a36Sopenharmony_ciSetting it as ``N`` disables DAMON_RECLAIM.  Note that DAMON_RECLAIM could do
6062306a36Sopenharmony_cino real monitoring and reclamation due to the watermarks-based activation
6162306a36Sopenharmony_cicondition.  Refer to below descriptions for the watermarks parameter for this.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cicommit_inputs
6462306a36Sopenharmony_ci-------------
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciMake DAMON_RECLAIM reads the input parameters again, except ``enabled``.
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciInput parameters that updated while DAMON_RECLAIM is running are not applied
6962306a36Sopenharmony_ciby default.  Once this parameter is set as ``Y``, DAMON_RECLAIM reads values
7062306a36Sopenharmony_ciof parametrs except ``enabled`` again.  Once the re-reading is done, this
7162306a36Sopenharmony_ciparameter is set as ``N``.  If invalid parameters are found while the
7262306a36Sopenharmony_cire-reading, DAMON_RECLAIM will be disabled.
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cimin_age
7562306a36Sopenharmony_ci-------
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciTime threshold for cold memory regions identification in microseconds.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciIf a memory region is not accessed for this or longer time, DAMON_RECLAIM
8062306a36Sopenharmony_ciidentifies the region as cold, and reclaims it.
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci120 seconds by default.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciquota_ms
8562306a36Sopenharmony_ci--------
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciLimit of time for the reclamation in milliseconds.
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciDAMON_RECLAIM tries to use only up to this time within a time window
9062306a36Sopenharmony_ci(quota_reset_interval_ms) for trying reclamation of cold pages.  This can be
9162306a36Sopenharmony_ciused for limiting CPU consumption of DAMON_RECLAIM.  If the value is zero, the
9262306a36Sopenharmony_cilimit is disabled.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci10 ms by default.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciquota_sz
9762306a36Sopenharmony_ci--------
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciLimit of size of memory for the reclamation in bytes.
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciDAMON_RECLAIM charges amount of memory which it tried to reclaim within a time
10262306a36Sopenharmony_ciwindow (quota_reset_interval_ms) and makes no more than this limit is tried.
10362306a36Sopenharmony_ciThis can be used for limiting consumption of CPU and IO.  If this value is
10462306a36Sopenharmony_cizero, the limit is disabled.
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci128 MiB by default.
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciquota_reset_interval_ms
10962306a36Sopenharmony_ci-----------------------
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciThe time/size quota charge reset interval in milliseconds.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciThe charget reset interval for the quota of time (quota_ms) and size
11462306a36Sopenharmony_ci(quota_sz).  That is, DAMON_RECLAIM does not try reclamation for more than
11562306a36Sopenharmony_ciquota_ms milliseconds or quota_sz bytes within quota_reset_interval_ms
11662306a36Sopenharmony_cimilliseconds.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci1 second by default.
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciwmarks_interval
12162306a36Sopenharmony_ci---------------
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciMinimal time to wait before checking the watermarks, when DAMON_RECLAIM is
12462306a36Sopenharmony_cienabled but inactive due to its watermarks rule.
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciwmarks_high
12762306a36Sopenharmony_ci-----------
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ciFree memory rate (per thousand) for the high watermark.
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ciIf free memory of the system in bytes per thousand bytes is higher than this,
13262306a36Sopenharmony_ciDAMON_RECLAIM becomes inactive, so it does nothing but only periodically checks
13362306a36Sopenharmony_cithe watermarks.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciwmarks_mid
13662306a36Sopenharmony_ci----------
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciFree memory rate (per thousand) for the middle watermark.
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ciIf free memory of the system in bytes per thousand bytes is between this and
14162306a36Sopenharmony_cithe low watermark, DAMON_RECLAIM becomes active, so starts the monitoring and
14262306a36Sopenharmony_cithe reclaiming.
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ciwmarks_low
14562306a36Sopenharmony_ci----------
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciFree memory rate (per thousand) for the low watermark.
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciIf free memory of the system in bytes per thousand bytes is lower than this,
15062306a36Sopenharmony_ciDAMON_RECLAIM becomes inactive, so it does nothing but periodically checks the
15162306a36Sopenharmony_ciwatermarks.  In the case, the system falls back to the LRU-list based page
15262306a36Sopenharmony_cigranularity reclamation logic.
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cisample_interval
15562306a36Sopenharmony_ci---------------
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciSampling interval for the monitoring in microseconds.
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciThe sampling interval of DAMON for the cold memory monitoring.  Please refer to
16062306a36Sopenharmony_cithe DAMON documentation (:doc:`usage`) for more detail.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciaggr_interval
16362306a36Sopenharmony_ci-------------
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciAggregation interval for the monitoring in microseconds.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciThe aggregation interval of DAMON for the cold memory monitoring.  Please
16862306a36Sopenharmony_cirefer to the DAMON documentation (:doc:`usage`) for more detail.
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cimin_nr_regions
17162306a36Sopenharmony_ci--------------
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciMinimum number of monitoring regions.
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciThe minimal number of monitoring regions of DAMON for the cold memory
17662306a36Sopenharmony_cimonitoring.  This can be used to set lower-bound of the monitoring quality.
17762306a36Sopenharmony_ciBut, setting this too high could result in increased monitoring overhead.
17862306a36Sopenharmony_ciPlease refer to the DAMON documentation (:doc:`usage`) for more detail.
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cimax_nr_regions
18162306a36Sopenharmony_ci--------------
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ciMaximum number of monitoring regions.
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciThe maximum number of monitoring regions of DAMON for the cold memory
18662306a36Sopenharmony_cimonitoring.  This can be used to set upper-bound of the monitoring overhead.
18762306a36Sopenharmony_ciHowever, setting this too low could result in bad monitoring quality.  Please
18862306a36Sopenharmony_cirefer to the DAMON documentation (:doc:`usage`) for more detail.
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cimonitor_region_start
19162306a36Sopenharmony_ci--------------------
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ciStart of target memory region in physical address.
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciThe start physical address of memory region that DAMON_RECLAIM will do work
19662306a36Sopenharmony_ciagainst.  That is, DAMON_RECLAIM will find cold memory regions in this region
19762306a36Sopenharmony_ciand reclaims.  By default, biggest System RAM is used as the region.
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cimonitor_region_end
20062306a36Sopenharmony_ci------------------
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ciEnd of target memory region in physical address.
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciThe end physical address of memory region that DAMON_RECLAIM will do work
20562306a36Sopenharmony_ciagainst.  That is, DAMON_RECLAIM will find cold memory regions in this region
20662306a36Sopenharmony_ciand reclaims.  By default, biggest System RAM is used as the region.
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciskip_anon
20962306a36Sopenharmony_ci---------
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ciSkip anonymous pages reclamation.
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciIf this parameter is set as ``Y``, DAMON_RECLAIM does not reclaim anonymous
21462306a36Sopenharmony_cipages.  By default, ``N``.
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cikdamond_pid
21862306a36Sopenharmony_ci-----------
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ciPID of the DAMON thread.
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ciIf DAMON_RECLAIM is enabled, this becomes the PID of the worker thread.  Else,
22362306a36Sopenharmony_ci-1.
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cinr_reclaim_tried_regions
22662306a36Sopenharmony_ci------------------------
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ciNumber of memory regions that tried to be reclaimed by DAMON_RECLAIM.
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cibytes_reclaim_tried_regions
23162306a36Sopenharmony_ci---------------------------
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ciTotal bytes of memory regions that tried to be reclaimed by DAMON_RECLAIM.
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cinr_reclaimed_regions
23662306a36Sopenharmony_ci--------------------
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ciNumber of memory regions that successfully be reclaimed by DAMON_RECLAIM.
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cibytes_reclaimed_regions
24162306a36Sopenharmony_ci-----------------------
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ciTotal bytes of memory regions that successfully be reclaimed by DAMON_RECLAIM.
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_cinr_quota_exceeds
24662306a36Sopenharmony_ci----------------
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ciNumber of times that the time/space quota limits have exceeded.
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ciExample
25162306a36Sopenharmony_ci=======
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciBelow runtime example commands make DAMON_RECLAIM to find memory regions that
25462306a36Sopenharmony_cinot accessed for 30 seconds or more and pages out.  The reclamation is limited
25562306a36Sopenharmony_cito be done only up to 1 GiB per second to avoid DAMON_RECLAIM consuming too
25662306a36Sopenharmony_cimuch CPU time for the paging out operation.  It also asks DAMON_RECLAIM to do
25762306a36Sopenharmony_cinothing if the system's free memory rate is more than 50%, but start the real
25862306a36Sopenharmony_ciworks if it becomes lower than 40%.  If DAMON_RECLAIM doesn't make progress and
25962306a36Sopenharmony_citherefore the free memory rate becomes lower than 20%, it asks DAMON_RECLAIM to
26062306a36Sopenharmony_cido nothing again, so that we can fall back to the LRU-list based page
26162306a36Sopenharmony_cigranularity reclamation. ::
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci    # cd /sys/module/damon_reclaim/parameters
26462306a36Sopenharmony_ci    # echo 30000000 > min_age
26562306a36Sopenharmony_ci    # echo $((1 * 1024 * 1024 * 1024)) > quota_sz
26662306a36Sopenharmony_ci    # echo 1000 > quota_reset_interval_ms
26762306a36Sopenharmony_ci    # echo 500 > wmarks_high
26862306a36Sopenharmony_ci    # echo 400 > wmarks_mid
26962306a36Sopenharmony_ci    # echo 200 > wmarks_low
27062306a36Sopenharmony_ci    # echo Y > enabled
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci.. [1] https://research.google/pubs/pub48551/
27362306a36Sopenharmony_ci.. [2] https://lwn.net/Articles/787611/
27462306a36Sopenharmony_ci.. [3] https://www.kernel.org/doc/html/latest/mm/free_page_reporting.html
275