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