1.. SPDX-License-Identifier: GPL-2.0
2
3===============
4Detailed Usages
5===============
6
7DAMON provides below interfaces for different users.
8
9- *DAMON user space tool.*
10  `This <https://github.com/awslabs/damo>`_ is for privileged people such as
11  system administrators who want a just-working human-friendly interface.
12  Using this, users can use the DAMON’s major features in a human-friendly way.
13  It may not be highly tuned for special cases, though.  For more detail,
14  please refer to its `usage document
15  <https://github.com/awslabs/damo/blob/next/USAGE.md>`_.
16- *sysfs interface.*
17  :ref:`This <sysfs_interface>` is for privileged user space programmers who
18  want more optimized use of DAMON.  Using this, users can use DAMON’s major
19  features by reading from and writing to special sysfs files.  Therefore,
20  you can write and use your personalized DAMON sysfs wrapper programs that
21  reads/writes the sysfs files instead of you.  The `DAMON user space tool
22  <https://github.com/awslabs/damo>`_ is one example of such programs.
23- *debugfs interface. (DEPRECATED!)*
24  :ref:`This <debugfs_interface>` is almost identical to :ref:`sysfs interface
25  <sysfs_interface>`.  This is deprecated, so users should move to the
26  :ref:`sysfs interface <sysfs_interface>`.  If you depend on this and cannot
27  move, please report your usecase to damon@lists.linux.dev and
28  linux-mm@kvack.org.
29- *Kernel Space Programming Interface.*
30  :doc:`This </mm/damon/api>` is for kernel space programmers.  Using this,
31  users can utilize every feature of DAMON most flexibly and efficiently by
32  writing kernel space DAMON application programs for you.  You can even extend
33  DAMON for various address spaces.  For detail, please refer to the interface
34  :doc:`document </mm/damon/api>`.
35
36.. _sysfs_interface:
37
38sysfs Interface
39===============
40
41DAMON sysfs interface is built when ``CONFIG_DAMON_SYSFS`` is defined.  It
42creates multiple directories and files under its sysfs directory,
43``<sysfs>/kernel/mm/damon/``.  You can control DAMON by writing to and reading
44from the files under the directory.
45
46For a short example, users can monitor the virtual address space of a given
47workload as below. ::
48
49    # cd /sys/kernel/mm/damon/admin/
50    # echo 1 > kdamonds/nr_kdamonds && echo 1 > kdamonds/0/contexts/nr_contexts
51    # echo vaddr > kdamonds/0/contexts/0/operations
52    # echo 1 > kdamonds/0/contexts/0/targets/nr_targets
53    # echo $(pidof <workload>) > kdamonds/0/contexts/0/targets/0/pid_target
54    # echo on > kdamonds/0/state
55
56Files Hierarchy
57---------------
58
59The files hierarchy of DAMON sysfs interface is shown below.  In the below
60figure, parents-children relations are represented with indentations, each
61directory is having ``/`` suffix, and files in each directory are separated by
62comma (","). ::
63
64    /sys/kernel/mm/damon/admin
65kdamonds/nr_kdamonds
66    │ │ 0/state,pid
67    │ │ │ contexts/nr_contexts
68    │ │ │ │ 0/avail_operations,operations
69    │ │ │ │ │ monitoring_attrs/
70    │ │ │ │ │ │ intervals/sample_us,aggr_us,update_us
71    │ │ │ │ │ │ nr_regions/min,max
72    │ │ │ │ │ targets/nr_targets
73    │ │ │ │ │ │ 0/pid_target
74    │ │ │ │ │ │ │ regions/nr_regions
75    │ │ │ │ │ │ │ │ 0/start,end
76    │ │ │ │ │ │ │ │ ...
77    │ │ │ │ │ │ ...
78    │ │ │ │ │ schemes/nr_schemes
79    │ │ │ │ │ │ 0/action
80    │ │ │ │ │ │ │ access_pattern/
81    │ │ │ │ │ │ │ │ sz/min,max
82    │ │ │ │ │ │ │ │ nr_accesses/min,max
83    │ │ │ │ │ │ │ │ age/min,max
84    │ │ │ │ │ │ │ quotas/ms,bytes,reset_interval_ms
85    │ │ │ │ │ │ │ │ weights/sz_permil,nr_accesses_permil,age_permil
86    │ │ │ │ │ │ │ watermarks/metric,interval_us,high,mid,low
87    │ │ │ │ │ │ │ filters/nr_filters
88    │ │ │ │ │ │ │ │ 0/type,matching,memcg_id
89    │ │ │ │ │ │ │ stats/nr_tried,sz_tried,nr_applied,sz_applied,qt_exceeds
90    │ │ │ │ │ │ │ tried_regions/
91    │ │ │ │ │ │ │ │ 0/start,end,nr_accesses,age
92    │ │ │ │ │ │ │ │ ...
93    │ │ │ │ │ │ ...
94    │ │ │ │ ...
95    │ │ ...
96
97Root
98----
99
100The root of the DAMON sysfs interface is ``<sysfs>/kernel/mm/damon/``, and it
101has one directory named ``admin``.  The directory contains the files for
102privileged user space programs' control of DAMON.  User space tools or deamons
103having the root permission could use this directory.
104
105kdamonds/
106---------
107
108The monitoring-related information including request specifications and results
109are called DAMON context.  DAMON executes each context with a kernel thread
110called kdamond, and multiple kdamonds could run in parallel.
111
112Under the ``admin`` directory, one directory, ``kdamonds``, which has files for
113controlling the kdamonds exist.  In the beginning, this directory has only one
114file, ``nr_kdamonds``.  Writing a number (``N``) to the file creates the number
115of child directories named ``0`` to ``N-1``.  Each directory represents each
116kdamond.
117
118kdamonds/<N>/
119-------------
120
121In each kdamond directory, two files (``state`` and ``pid``) and one directory
122(``contexts``) exist.
123
124Reading ``state`` returns ``on`` if the kdamond is currently running, or
125``off`` if it is not running.  Writing ``on`` or ``off`` makes the kdamond be
126in the state.  Writing ``commit`` to the ``state`` file makes kdamond reads the
127user inputs in the sysfs files except ``state`` file again.  Writing
128``update_schemes_stats`` to ``state`` file updates the contents of stats files
129for each DAMON-based operation scheme of the kdamond.  For details of the
130stats, please refer to :ref:`stats section <sysfs_schemes_stats>`.  Writing
131``update_schemes_tried_regions`` to ``state`` file updates the DAMON-based
132operation scheme action tried regions directory for each DAMON-based operation
133scheme of the kdamond.  Writing ``clear_schemes_tried_regions`` to ``state``
134file clears the DAMON-based operating scheme action tried regions directory for
135each DAMON-based operation scheme of the kdamond.  For details of the
136DAMON-based operation scheme action tried regions directory, please refer to
137:ref:`tried_regions section <sysfs_schemes_tried_regions>`.
138
139If the state is ``on``, reading ``pid`` shows the pid of the kdamond thread.
140
141``contexts`` directory contains files for controlling the monitoring contexts
142that this kdamond will execute.
143
144kdamonds/<N>/contexts/
145----------------------
146
147In the beginning, this directory has only one file, ``nr_contexts``.  Writing a
148number (``N``) to the file creates the number of child directories named as
149``0`` to ``N-1``.  Each directory represents each monitoring context.  At the
150moment, only one context per kdamond is supported, so only ``0`` or ``1`` can
151be written to the file.
152
153.. _sysfs_contexts:
154
155contexts/<N>/
156-------------
157
158In each context directory, two files (``avail_operations`` and ``operations``)
159and three directories (``monitoring_attrs``, ``targets``, and ``schemes``)
160exist.
161
162DAMON supports multiple types of monitoring operations, including those for
163virtual address space and the physical address space.  You can get the list of
164available monitoring operations set on the currently running kernel by reading
165``avail_operations`` file.  Based on the kernel configuration, the file will
166list some or all of below keywords.
167
168 - vaddr: Monitor virtual address spaces of specific processes
169 - fvaddr: Monitor fixed virtual address ranges
170 - paddr: Monitor the physical address space of the system
171
172Please refer to :ref:`regions sysfs directory <sysfs_regions>` for detailed
173differences between the operations sets in terms of the monitoring target
174regions.
175
176You can set and get what type of monitoring operations DAMON will use for the
177context by writing one of the keywords listed in ``avail_operations`` file and
178reading from the ``operations`` file.
179
180.. _sysfs_monitoring_attrs:
181
182contexts/<N>/monitoring_attrs/
183------------------------------
184
185Files for specifying attributes of the monitoring including required quality
186and efficiency of the monitoring are in ``monitoring_attrs`` directory.
187Specifically, two directories, ``intervals`` and ``nr_regions`` exist in this
188directory.
189
190Under ``intervals`` directory, three files for DAMON's sampling interval
191(``sample_us``), aggregation interval (``aggr_us``), and update interval
192(``update_us``) exist.  You can set and get the values in micro-seconds by
193writing to and reading from the files.
194
195Under ``nr_regions`` directory, two files for the lower-bound and upper-bound
196of DAMON's monitoring regions (``min`` and ``max``, respectively), which
197controls the monitoring overhead, exist.  You can set and get the values by
198writing to and rading from the files.
199
200For more details about the intervals and monitoring regions range, please refer
201to the Design document (:doc:`/mm/damon/design`).
202
203contexts/<N>/targets/
204---------------------
205
206In the beginning, this directory has only one file, ``nr_targets``.  Writing a
207number (``N``) to the file creates the number of child directories named ``0``
208to ``N-1``.  Each directory represents each monitoring target.
209
210targets/<N>/
211------------
212
213In each target directory, one file (``pid_target``) and one directory
214(``regions``) exist.
215
216If you wrote ``vaddr`` to the ``contexts/<N>/operations``, each target should
217be a process.  You can specify the process to DAMON by writing the pid of the
218process to the ``pid_target`` file.
219
220.. _sysfs_regions:
221
222targets/<N>/regions
223-------------------
224
225When ``vaddr`` monitoring operations set is being used (``vaddr`` is written to
226the ``contexts/<N>/operations`` file), DAMON automatically sets and updates the
227monitoring target regions so that entire memory mappings of target processes
228can be covered.  However, users could want to set the initial monitoring region
229to specific address ranges.
230
231In contrast, DAMON do not automatically sets and updates the monitoring target
232regions when ``fvaddr`` or ``paddr`` monitoring operations sets are being used
233(``fvaddr`` or ``paddr`` have written to the ``contexts/<N>/operations``).
234Therefore, users should set the monitoring target regions by themselves in the
235cases.
236
237For such cases, users can explicitly set the initial monitoring target regions
238as they want, by writing proper values to the files under this directory.
239
240In the beginning, this directory has only one file, ``nr_regions``.  Writing a
241number (``N``) to the file creates the number of child directories named ``0``
242to ``N-1``.  Each directory represents each initial monitoring target region.
243
244regions/<N>/
245------------
246
247In each region directory, you will find two files (``start`` and ``end``).  You
248can set and get the start and end addresses of the initial monitoring target
249region by writing to and reading from the files, respectively.
250
251Each region should not overlap with others.  ``end`` of directory ``N`` should
252be equal or smaller than ``start`` of directory ``N+1``.
253
254contexts/<N>/schemes/
255---------------------
256
257The directory for DAMON-based Operation Schemes (:ref:`DAMOS
258<damon_design_damos>`).  Users can get and set the schemes by reading from and
259writing to files under this directory.
260
261In the beginning, this directory has only one file, ``nr_schemes``.  Writing a
262number (``N``) to the file creates the number of child directories named ``0``
263to ``N-1``.  Each directory represents each DAMON-based operation scheme.
264
265schemes/<N>/
266------------
267
268In each scheme directory, five directories (``access_pattern``, ``quotas``,
269``watermarks``, ``filters``, ``stats``, and ``tried_regions``) and one file
270(``action``) exist.
271
272The ``action`` file is for setting and getting the scheme's :ref:`action
273<damon_design_damos_action>`.  The keywords that can be written to and read
274from the file and their meaning are as below.
275
276Note that support of each action depends on the running DAMON operations set
277:ref:`implementation <sysfs_contexts>`.
278
279 - ``willneed``: Call ``madvise()`` for the region with ``MADV_WILLNEED``.
280   Supported by ``vaddr`` and ``fvaddr`` operations set.
281 - ``cold``: Call ``madvise()`` for the region with ``MADV_COLD``.
282   Supported by ``vaddr`` and ``fvaddr`` operations set.
283 - ``pageout``: Call ``madvise()`` for the region with ``MADV_PAGEOUT``.
284   Supported by ``vaddr``, ``fvaddr`` and ``paddr`` operations set.
285 - ``hugepage``: Call ``madvise()`` for the region with ``MADV_HUGEPAGE``.
286   Supported by ``vaddr`` and ``fvaddr`` operations set.
287 - ``nohugepage``: Call ``madvise()`` for the region with ``MADV_NOHUGEPAGE``.
288   Supported by ``vaddr`` and ``fvaddr`` operations set.
289 - ``lru_prio``: Prioritize the region on its LRU lists.
290   Supported by ``paddr`` operations set.
291 - ``lru_deprio``: Deprioritize the region on its LRU lists.
292   Supported by ``paddr`` operations set.
293 - ``stat``: Do nothing but count the statistics.
294   Supported by all operations sets.
295
296schemes/<N>/access_pattern/
297---------------------------
298
299The directory for the target access :ref:`pattern
300<damon_design_damos_access_pattern>` of the given DAMON-based operation scheme.
301
302Under the ``access_pattern`` directory, three directories (``sz``,
303``nr_accesses``, and ``age``) each having two files (``min`` and ``max``)
304exist.  You can set and get the access pattern for the given scheme by writing
305to and reading from the ``min`` and ``max`` files under ``sz``,
306``nr_accesses``, and ``age`` directories, respectively.  Note that the ``min``
307and the ``max`` form a closed interval.
308
309schemes/<N>/quotas/
310-------------------
311
312The directory for the :ref:`quotas <damon_design_damos_quotas>` of the given
313DAMON-based operation scheme.
314
315Under ``quotas`` directory, three files (``ms``, ``bytes``,
316``reset_interval_ms``) and one directory (``weights``) having three files
317(``sz_permil``, ``nr_accesses_permil``, and ``age_permil``) in it exist.
318
319You can set the ``time quota`` in milliseconds, ``size quota`` in bytes, and
320``reset interval`` in milliseconds by writing the values to the three files,
321respectively.  Then, DAMON tries to use only up to ``time quota`` milliseconds
322for applying the ``action`` to memory regions of the ``access_pattern``, and to
323apply the action to only up to ``bytes`` bytes of memory regions within the
324``reset_interval_ms``.  Setting both ``ms`` and ``bytes`` zero disables the
325quota limits.
326
327You can also set the :ref:`prioritization weights
328<damon_design_damos_quotas_prioritization>` for size, access frequency, and age
329in per-thousand unit by writing the values to the three files under the
330``weights`` directory.
331
332schemes/<N>/watermarks/
333-----------------------
334
335The directory for the :ref:`watermarks <damon_design_damos_watermarks>` of the
336given DAMON-based operation scheme.
337
338Under the watermarks directory, five files (``metric``, ``interval_us``,
339``high``, ``mid``, and ``low``) for setting the metric, the time interval
340between check of the metric, and the three watermarks exist.  You can set and
341get the five values by writing to the files, respectively.
342
343Keywords and meanings of those that can be written to the ``metric`` file are
344as below.
345
346 - none: Ignore the watermarks
347 - free_mem_rate: System's free memory rate (per thousand)
348
349The ``interval`` should written in microseconds unit.
350
351schemes/<N>/filters/
352--------------------
353
354The directory for the :ref:`filters <damon_design_damos_filters>` of the given
355DAMON-based operation scheme.
356
357In the beginning, this directory has only one file, ``nr_filters``.  Writing a
358number (``N``) to the file creates the number of child directories named ``0``
359to ``N-1``.  Each directory represents each filter.  The filters are evaluated
360in the numeric order.
361
362Each filter directory contains three files, namely ``type``, ``matcing``, and
363``memcg_path``.  You can write one of two special keywords, ``anon`` for
364anonymous pages, or ``memcg`` for specific memory cgroup filtering.  In case of
365the memory cgroup filtering, you can specify the memory cgroup of the interest
366by writing the path of the memory cgroup from the cgroups mount point to
367``memcg_path`` file.  You can write ``Y`` or ``N`` to ``matching`` file to
368filter out pages that does or does not match to the type, respectively.  Then,
369the scheme's action will not be applied to the pages that specified to be
370filtered out.
371
372For example, below restricts a DAMOS action to be applied to only non-anonymous
373pages of all memory cgroups except ``/having_care_already``.::
374
375    # echo 2 > nr_filters
376    # # filter out anonymous pages
377    echo anon > 0/type
378    echo Y > 0/matching
379    # # further filter out all cgroups except one at '/having_care_already'
380    echo memcg > 1/type
381    echo /having_care_already > 1/memcg_path
382    echo N > 1/matching
383
384Note that filters are currently supported only when ``paddr``
385`implementation <sysfs_contexts>` is being used.
386
387.. _sysfs_schemes_stats:
388
389schemes/<N>/stats/
390------------------
391
392DAMON counts the total number and bytes of regions that each scheme is tried to
393be applied, the two numbers for the regions that each scheme is successfully
394applied, and the total number of the quota limit exceeds.  This statistics can
395be used for online analysis or tuning of the schemes.
396
397The statistics can be retrieved by reading the files under ``stats`` directory
398(``nr_tried``, ``sz_tried``, ``nr_applied``, ``sz_applied``, and
399``qt_exceeds``), respectively.  The files are not updated in real time, so you
400should ask DAMON sysfs interface to updte the content of the files for the
401stats by writing a special keyword, ``update_schemes_stats`` to the relevant
402``kdamonds/<N>/state`` file.
403
404.. _sysfs_schemes_tried_regions:
405
406schemes/<N>/tried_regions/
407--------------------------
408
409When a special keyword, ``update_schemes_tried_regions``, is written to the
410relevant ``kdamonds/<N>/state`` file, DAMON creates directories named integer
411starting from ``0`` under this directory.  Each directory contains files
412exposing detailed information about each of the memory region that the
413corresponding scheme's ``action`` has tried to be applied under this directory,
414during next :ref:`aggregation interval <sysfs_monitoring_attrs>`.  The
415information includes address range, ``nr_accesses``, and ``age`` of the region.
416
417The directories will be removed when another special keyword,
418``clear_schemes_tried_regions``, is written to the relevant
419``kdamonds/<N>/state`` file.
420
421The expected usage of this directory is investigations of schemes' behaviors,
422and query-like efficient data access monitoring results retrievals.  For the
423latter use case, in particular, users can set the ``action`` as ``stat`` and
424set the ``access pattern`` as their interested pattern that they want to query.
425
426tried_regions/<N>/
427------------------
428
429In each region directory, you will find four files (``start``, ``end``,
430``nr_accesses``, and ``age``).  Reading the files will show the start and end
431addresses, ``nr_accesses``, and ``age`` of the region that corresponding
432DAMON-based operation scheme ``action`` has tried to be applied.
433
434Example
435~~~~~~~
436
437Below commands applies a scheme saying "If a memory region of size in [4KiB,
4388KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
439interval in [10, 20], page out the region.  For the paging out, use only up to
44010ms per second, and also don't page out more than 1GiB per second.  Under the
441limitation, page out memory regions having longer age first.  Also, check the
442free memory rate of the system every 5 seconds, start the monitoring and paging
443out when the free memory rate becomes lower than 50%, but stop it if the free
444memory rate becomes larger than 60%, or lower than 30%". ::
445
446    # cd <sysfs>/kernel/mm/damon/admin
447    # # populate directories
448    # echo 1 > kdamonds/nr_kdamonds; echo 1 > kdamonds/0/contexts/nr_contexts;
449    # echo 1 > kdamonds/0/contexts/0/schemes/nr_schemes
450    # cd kdamonds/0/contexts/0/schemes/0
451    # # set the basic access pattern and the action
452    # echo 4096 > access_pattern/sz/min
453    # echo 8192 > access_pattern/sz/max
454    # echo 0 > access_pattern/nr_accesses/min
455    # echo 5 > access_pattern/nr_accesses/max
456    # echo 10 > access_pattern/age/min
457    # echo 20 > access_pattern/age/max
458    # echo pageout > action
459    # # set quotas
460    # echo 10 > quotas/ms
461    # echo $((1024*1024*1024)) > quotas/bytes
462    # echo 1000 > quotas/reset_interval_ms
463    # # set watermark
464    # echo free_mem_rate > watermarks/metric
465    # echo 5000000 > watermarks/interval_us
466    # echo 600 > watermarks/high
467    # echo 500 > watermarks/mid
468    # echo 300 > watermarks/low
469
470Please note that it's highly recommended to use user space tools like `damo
471<https://github.com/awslabs/damo>`_ rather than manually reading and writing
472the files as above.  Above is only for an example.
473
474.. _debugfs_interface:
475
476debugfs Interface (DEPRECATED!)
477===============================
478
479.. note::
480
481  THIS IS DEPRECATED!
482
483  DAMON debugfs interface is deprecated, so users should move to the
484  :ref:`sysfs interface <sysfs_interface>`.  If you depend on this and cannot
485  move, please report your usecase to damon@lists.linux.dev and
486  linux-mm@kvack.org.
487
488DAMON exports eight files, ``attrs``, ``target_ids``, ``init_regions``,
489``schemes``, ``monitor_on``, ``kdamond_pid``, ``mk_contexts`` and
490``rm_contexts`` under its debugfs directory, ``<debugfs>/damon/``.
491
492
493Attributes
494----------
495
496Users can get and set the ``sampling interval``, ``aggregation interval``,
497``update interval``, and min/max number of monitoring target regions by
498reading from and writing to the ``attrs`` file.  To know about the monitoring
499attributes in detail, please refer to the :doc:`/mm/damon/design`.  For
500example, below commands set those values to 5 ms, 100 ms, 1,000 ms, 10 and
5011000, and then check it again::
502
503    # cd <debugfs>/damon
504    # echo 5000 100000 1000000 10 1000 > attrs
505    # cat attrs
506    5000 100000 1000000 10 1000
507
508
509Target IDs
510----------
511
512Some types of address spaces supports multiple monitoring target.  For example,
513the virtual memory address spaces monitoring can have multiple processes as the
514monitoring targets.  Users can set the targets by writing relevant id values of
515the targets to, and get the ids of the current targets by reading from the
516``target_ids`` file.  In case of the virtual address spaces monitoring, the
517values should be pids of the monitoring target processes.  For example, below
518commands set processes having pids 42 and 4242 as the monitoring targets and
519check it again::
520
521    # cd <debugfs>/damon
522    # echo 42 4242 > target_ids
523    # cat target_ids
524    42 4242
525
526Users can also monitor the physical memory address space of the system by
527writing a special keyword, "``paddr\n``" to the file.  Because physical address
528space monitoring doesn't support multiple targets, reading the file will show a
529fake value, ``42``, as below::
530
531    # cd <debugfs>/damon
532    # echo paddr > target_ids
533    # cat target_ids
534    42
535
536Note that setting the target ids doesn't start the monitoring.
537
538
539Initial Monitoring Target Regions
540---------------------------------
541
542In case of the virtual address space monitoring, DAMON automatically sets and
543updates the monitoring target regions so that entire memory mappings of target
544processes can be covered.  However, users can want to limit the monitoring
545region to specific address ranges, such as the heap, the stack, or specific
546file-mapped area.  Or, some users can know the initial access pattern of their
547workloads and therefore want to set optimal initial regions for the 'adaptive
548regions adjustment'.
549
550In contrast, DAMON do not automatically sets and updates the monitoring target
551regions in case of physical memory monitoring.  Therefore, users should set the
552monitoring target regions by themselves.
553
554In such cases, users can explicitly set the initial monitoring target regions
555as they want, by writing proper values to the ``init_regions`` file.  The input
556should be a sequence of three integers separated by white spaces that represent
557one region in below form.::
558
559    <target idx> <start address> <end address>
560
561The ``target idx`` should be the index of the target in ``target_ids`` file,
562starting from ``0``, and the regions should be passed in address order.  For
563example, below commands will set a couple of address ranges, ``1-100`` and
564``100-200`` as the initial monitoring target region of pid 42, which is the
565first one (index ``0``) in ``target_ids``, and another couple of address
566ranges, ``20-40`` and ``50-100`` as that of pid 4242, which is the second one
567(index ``1``) in ``target_ids``.::
568
569    # cd <debugfs>/damon
570    # cat target_ids
571    42 4242
572    # echo "0   1       100 \
573            0   100     200 \
574            1   20      40  \
575            1   50      100" > init_regions
576
577Note that this sets the initial monitoring target regions only.  In case of
578virtual memory monitoring, DAMON will automatically updates the boundary of the
579regions after one ``update interval``.  Therefore, users should set the
580``update interval`` large enough in this case, if they don't want the
581update.
582
583
584Schemes
585-------
586
587Users can get and set the DAMON-based operation :ref:`schemes
588<damon_design_damos>` by reading from and writing to ``schemes`` debugfs file.
589Reading the file also shows the statistics of each scheme.  To the file, each
590of the schemes should be represented in each line in below form::
591
592    <target access pattern> <action> <quota> <watermarks>
593
594You can disable schemes by simply writing an empty string to the file.
595
596Target Access Pattern
597~~~~~~~~~~~~~~~~~~~~~
598
599The target access :ref:`pattern <damon_design_damos_access_pattern>` of the
600scheme.  The ``<target access pattern>`` is constructed with three ranges in
601below form::
602
603    min-size max-size min-acc max-acc min-age max-age
604
605Specifically, bytes for the size of regions (``min-size`` and ``max-size``),
606number of monitored accesses per aggregate interval for access frequency
607(``min-acc`` and ``max-acc``), number of aggregate intervals for the age of
608regions (``min-age`` and ``max-age``) are specified.  Note that the ranges are
609closed interval.
610
611Action
612~~~~~~
613
614The ``<action>`` is a predefined integer for memory management :ref:`actions
615<damon_design_damos_action>`.  The supported numbers and their meanings are as
616below.
617
618 - 0: Call ``madvise()`` for the region with ``MADV_WILLNEED``.  Ignored if
619   ``target`` is ``paddr``.
620 - 1: Call ``madvise()`` for the region with ``MADV_COLD``.  Ignored if
621   ``target`` is ``paddr``.
622 - 2: Call ``madvise()`` for the region with ``MADV_PAGEOUT``.
623 - 3: Call ``madvise()`` for the region with ``MADV_HUGEPAGE``.  Ignored if
624   ``target`` is ``paddr``.
625 - 4: Call ``madvise()`` for the region with ``MADV_NOHUGEPAGE``.  Ignored if
626   ``target`` is ``paddr``.
627 - 5: Do nothing but count the statistics
628
629Quota
630~~~~~
631
632Users can set the :ref:`quotas <damon_design_damos_quotas>` of the given scheme
633via the ``<quota>`` in below form::
634
635    <ms> <sz> <reset interval> <priority weights>
636
637This makes DAMON to try to use only up to ``<ms>`` milliseconds for applying
638the action to memory regions of the ``target access pattern`` within the
639``<reset interval>`` milliseconds, and to apply the action to only up to
640``<sz>`` bytes of memory regions within the ``<reset interval>``.  Setting both
641``<ms>`` and ``<sz>`` zero disables the quota limits.
642
643For the :ref:`prioritization <damon_design_damos_quotas_prioritization>`, users
644can set the weights for the three properties in ``<priority weights>`` in below
645form::
646
647    <size weight> <access frequency weight> <age weight>
648
649Watermarks
650~~~~~~~~~~
651
652Users can specify :ref:`watermarks <damon_design_damos_watermarks>` of the
653given scheme via ``<watermarks>`` in below form::
654
655    <metric> <check interval> <high mark> <middle mark> <low mark>
656
657``<metric>`` is a predefined integer for the metric to be checked.  The
658supported numbers and their meanings are as below.
659
660 - 0: Ignore the watermarks
661 - 1: System's free memory rate (per thousand)
662
663The value of the metric is checked every ``<check interval>`` microseconds.
664
665If the value is higher than ``<high mark>`` or lower than ``<low mark>``, the
666scheme is deactivated.  If the value is lower than ``<mid mark>``, the scheme
667is activated.
668
669.. _damos_stats:
670
671Statistics
672~~~~~~~~~~
673
674It also counts the total number and bytes of regions that each scheme is tried
675to be applied, the two numbers for the regions that each scheme is successfully
676applied, and the total number of the quota limit exceeds.  This statistics can
677be used for online analysis or tuning of the schemes.
678
679The statistics can be shown by reading the ``schemes`` file.  Reading the file
680will show each scheme you entered in each line, and the five numbers for the
681statistics will be added at the end of each line.
682
683Example
684~~~~~~~
685
686Below commands applies a scheme saying "If a memory region of size in [4KiB,
6878KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
688interval in [10, 20], page out the region.  For the paging out, use only up to
68910ms per second, and also don't page out more than 1GiB per second.  Under the
690limitation, page out memory regions having longer age first.  Also, check the
691free memory rate of the system every 5 seconds, start the monitoring and paging
692out when the free memory rate becomes lower than 50%, but stop it if the free
693memory rate becomes larger than 60%, or lower than 30%".::
694
695    # cd <debugfs>/damon
696    # scheme="4096 8192  0 5    10 20    2"  # target access pattern and action
697    # scheme+=" 10 $((1024*1024*1024)) 1000" # quotas
698    # scheme+=" 0 0 100"                     # prioritization weights
699    # scheme+=" 1 5000000 600 500 300"       # watermarks
700    # echo "$scheme" > schemes
701
702
703Turning On/Off
704--------------
705
706Setting the files as described above doesn't incur effect unless you explicitly
707start the monitoring.  You can start, stop, and check the current status of the
708monitoring by writing to and reading from the ``monitor_on`` file.  Writing
709``on`` to the file starts the monitoring of the targets with the attributes.
710Writing ``off`` to the file stops those.  DAMON also stops if every target
711process is terminated.  Below example commands turn on, off, and check the
712status of DAMON::
713
714    # cd <debugfs>/damon
715    # echo on > monitor_on
716    # echo off > monitor_on
717    # cat monitor_on
718    off
719
720Please note that you cannot write to the above-mentioned debugfs files while
721the monitoring is turned on.  If you write to the files while DAMON is running,
722an error code such as ``-EBUSY`` will be returned.
723
724
725Monitoring Thread PID
726---------------------
727
728DAMON does requested monitoring with a kernel thread called ``kdamond``.  You
729can get the pid of the thread by reading the ``kdamond_pid`` file.  When the
730monitoring is turned off, reading the file returns ``none``. ::
731
732    # cd <debugfs>/damon
733    # cat monitor_on
734    off
735    # cat kdamond_pid
736    none
737    # echo on > monitor_on
738    # cat kdamond_pid
739    18594
740
741
742Using Multiple Monitoring Threads
743---------------------------------
744
745One ``kdamond`` thread is created for each monitoring context.  You can create
746and remove monitoring contexts for multiple ``kdamond`` required use case using
747the ``mk_contexts`` and ``rm_contexts`` files.
748
749Writing the name of the new context to the ``mk_contexts`` file creates a
750directory of the name on the DAMON debugfs directory.  The directory will have
751DAMON debugfs files for the context. ::
752
753    # cd <debugfs>/damon
754    # ls foo
755    # ls: cannot access 'foo': No such file or directory
756    # echo foo > mk_contexts
757    # ls foo
758    # attrs  init_regions  kdamond_pid  schemes  target_ids
759
760If the context is not needed anymore, you can remove it and the corresponding
761directory by putting the name of the context to the ``rm_contexts`` file. ::
762
763    # echo foo > rm_contexts
764    # ls foo
765    # ls: cannot access 'foo': No such file or directory
766
767Note that ``mk_contexts``, ``rm_contexts``, and ``monitor_on`` files are in the
768root directory only.
769
770
771.. _tracepoint:
772
773Tracepoint for Monitoring Results
774=================================
775
776Users can get the monitoring results via the :ref:`tried_regions
777<sysfs_schemes_tried_regions>` or a tracepoint, ``damon:damon_aggregated``.
778While the tried regions directory is useful for getting a snapshot, the
779tracepoint is useful for getting a full record of the results.  While the
780monitoring is turned on, you could record the tracepoint events and show
781results using tracepoint supporting tools like ``perf``.  For example::
782
783    # echo on > monitor_on
784    # perf record -e damon:damon_aggregated &
785    # sleep 5
786    # kill 9 $(pidof perf)
787    # echo off > monitor_on
788    # perf script
789