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