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