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