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:ref:`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 region.
436
437The directories will be removed when another special keyword,
438``clear_schemes_tried_regions``, is written to the relevant
439``kdamonds/<N>/state`` file.
440
441tried_regions/<N>/
442------------------
443
444In each region directory, you will find four files (``start``, ``end``,
445``nr_accesses``, and ``age``).  Reading the files will show the start and end
446addresses, ``nr_accesses``, and ``age`` of the region that corresponding
447DAMON-based operation scheme ``action`` has tried to be applied.
448
449Example
450~~~~~~~
451
452Below commands applies a scheme saying "If a memory region of size in [4KiB,
4538KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
454interval in [10, 20], page out the region.  For the paging out, use only up to
45510ms per second, and also don't page out more than 1GiB per second.  Under the
456limitation, page out memory regions having longer age first.  Also, check the
457free memory rate of the system every 5 seconds, start the monitoring and paging
458out when the free memory rate becomes lower than 50%, but stop it if the free
459memory rate becomes larger than 60%, or lower than 30%". ::
460
461    # cd <sysfs>/kernel/mm/damon/admin
462    # # populate directories
463    # echo 1 > kdamonds/nr_kdamonds; echo 1 > kdamonds/0/contexts/nr_contexts;
464    # echo 1 > kdamonds/0/contexts/0/schemes/nr_schemes
465    # cd kdamonds/0/contexts/0/schemes/0
466    # # set the basic access pattern and the action
467    # echo 4096 > access_pattern/sz/min
468    # echo 8192 > access_pattern/sz/max
469    # echo 0 > access_pattern/nr_accesses/min
470    # echo 5 > access_pattern/nr_accesses/max
471    # echo 10 > access_pattern/age/min
472    # echo 20 > access_pattern/age/max
473    # echo pageout > action
474    # # set quotas
475    # echo 10 > quotas/ms
476    # echo $((1024*1024*1024)) > quotas/bytes
477    # echo 1000 > quotas/reset_interval_ms
478    # # set watermark
479    # echo free_mem_rate > watermarks/metric
480    # echo 5000000 > watermarks/interval_us
481    # echo 600 > watermarks/high
482    # echo 500 > watermarks/mid
483    # echo 300 > watermarks/low
484
485Please note that it's highly recommended to use user space tools like `damo
486<https://github.com/awslabs/damo>`_ rather than manually reading and writing
487the files as above.  Above is only for an example.
488
489.. _debugfs_interface:
490
491debugfs Interface (DEPRECATED!)
492===============================
493
494.. note::
495
496  THIS IS DEPRECATED!
497
498  DAMON debugfs interface is deprecated, so users should move to the
499  :ref:`sysfs interface <sysfs_interface>`.  If you depend on this and cannot
500  move, please report your usecase to damon@lists.linux.dev and
501  linux-mm@kvack.org.
502
503DAMON exports eight files, ``attrs``, ``target_ids``, ``init_regions``,
504``schemes``, ``monitor_on``, ``kdamond_pid``, ``mk_contexts`` and
505``rm_contexts`` under its debugfs directory, ``<debugfs>/damon/``.
506
507
508Attributes
509----------
510
511Users can get and set the ``sampling interval``, ``aggregation interval``,
512``update interval``, and min/max number of monitoring target regions by
513reading from and writing to the ``attrs`` file.  To know about the monitoring
514attributes in detail, please refer to the :doc:`/mm/damon/design`.  For
515example, below commands set those values to 5 ms, 100 ms, 1,000 ms, 10 and
5161000, and then check it again::
517
518    # cd <debugfs>/damon
519    # echo 5000 100000 1000000 10 1000 > attrs
520    # cat attrs
521    5000 100000 1000000 10 1000
522
523
524Target IDs
525----------
526
527Some types of address spaces supports multiple monitoring target.  For example,
528the virtual memory address spaces monitoring can have multiple processes as the
529monitoring targets.  Users can set the targets by writing relevant id values of
530the targets to, and get the ids of the current targets by reading from the
531``target_ids`` file.  In case of the virtual address spaces monitoring, the
532values should be pids of the monitoring target processes.  For example, below
533commands set processes having pids 42 and 4242 as the monitoring targets and
534check it again::
535
536    # cd <debugfs>/damon
537    # echo 42 4242 > target_ids
538    # cat target_ids
539    42 4242
540
541Users can also monitor the physical memory address space of the system by
542writing a special keyword, "``paddr\n``" to the file.  Because physical address
543space monitoring doesn't support multiple targets, reading the file will show a
544fake value, ``42``, as below::
545
546    # cd <debugfs>/damon
547    # echo paddr > target_ids
548    # cat target_ids
549    42
550
551Note that setting the target ids doesn't start the monitoring.
552
553
554Initial Monitoring Target Regions
555---------------------------------
556
557In case of the virtual address space monitoring, DAMON automatically sets and
558updates the monitoring target regions so that entire memory mappings of target
559processes can be covered.  However, users can want to limit the monitoring
560region to specific address ranges, such as the heap, the stack, or specific
561file-mapped area.  Or, some users can know the initial access pattern of their
562workloads and therefore want to set optimal initial regions for the 'adaptive
563regions adjustment'.
564
565In contrast, DAMON do not automatically sets and updates the monitoring target
566regions in case of physical memory monitoring.  Therefore, users should set the
567monitoring target regions by themselves.
568
569In such cases, users can explicitly set the initial monitoring target regions
570as they want, by writing proper values to the ``init_regions`` file.  The input
571should be a sequence of three integers separated by white spaces that represent
572one region in below form.::
573
574    <target idx> <start address> <end address>
575
576The ``target idx`` should be the index of the target in ``target_ids`` file,
577starting from ``0``, and the regions should be passed in address order.  For
578example, below commands will set a couple of address ranges, ``1-100`` and
579``100-200`` as the initial monitoring target region of pid 42, which is the
580first one (index ``0``) in ``target_ids``, and another couple of address
581ranges, ``20-40`` and ``50-100`` as that of pid 4242, which is the second one
582(index ``1``) in ``target_ids``.::
583
584    # cd <debugfs>/damon
585    # cat target_ids
586    42 4242
587    # echo "0   1       100 \
588            0   100     200 \
589            1   20      40  \
590            1   50      100" > init_regions
591
592Note that this sets the initial monitoring target regions only.  In case of
593virtual memory monitoring, DAMON will automatically updates the boundary of the
594regions after one ``update interval``.  Therefore, users should set the
595``update interval`` large enough in this case, if they don't want the
596update.
597
598
599Schemes
600-------
601
602For usual DAMON-based data access aware memory management optimizations, users
603would simply want the system to apply a memory management action to a memory
604region of a specific access pattern.  DAMON receives such formalized operation
605schemes from the user and applies those to the target processes.
606
607Users can get and set the schemes by reading from and writing to ``schemes``
608debugfs file.  Reading the file also shows the statistics of each scheme.  To
609the file, each of the schemes should be represented in each line in below
610form::
611
612    <target access pattern> <action> <quota> <watermarks>
613
614You can disable schemes by simply writing an empty string to the file.
615
616Target Access Pattern
617~~~~~~~~~~~~~~~~~~~~~
618
619The ``<target access pattern>`` is constructed with three ranges in below
620form::
621
622    min-size max-size min-acc max-acc min-age max-age
623
624Specifically, bytes for the size of regions (``min-size`` and ``max-size``),
625number of monitored accesses per aggregate interval for access frequency
626(``min-acc`` and ``max-acc``), number of aggregate intervals for the age of
627regions (``min-age`` and ``max-age``) are specified.  Note that the ranges are
628closed interval.
629
630Action
631~~~~~~
632
633The ``<action>`` is a predefined integer for memory management actions, which
634DAMON will apply to the regions having the target access pattern.  The
635supported numbers and their meanings are as below.
636
637 - 0: Call ``madvise()`` for the region with ``MADV_WILLNEED``.  Ignored if
638   ``target`` is ``paddr``.
639 - 1: Call ``madvise()`` for the region with ``MADV_COLD``.  Ignored if
640   ``target`` is ``paddr``.
641 - 2: Call ``madvise()`` for the region with ``MADV_PAGEOUT``.
642 - 3: Call ``madvise()`` for the region with ``MADV_HUGEPAGE``.  Ignored if
643   ``target`` is ``paddr``.
644 - 4: Call ``madvise()`` for the region with ``MADV_NOHUGEPAGE``.  Ignored if
645   ``target`` is ``paddr``.
646 - 5: Do nothing but count the statistics
647
648Quota
649~~~~~
650
651Optimal ``target access pattern`` for each ``action`` is workload dependent, so
652not easy to find.  Worse yet, setting a scheme of some action too aggressive
653can cause severe overhead.  To avoid such overhead, users can limit time and
654size quota for the scheme via the ``<quota>`` in below form::
655
656    <ms> <sz> <reset interval> <priority weights>
657
658This makes DAMON to try to use only up to ``<ms>`` milliseconds for applying
659the action to memory regions of the ``target access pattern`` within the
660``<reset interval>`` milliseconds, and to apply the action to only up to
661``<sz>`` bytes of memory regions within the ``<reset interval>``.  Setting both
662``<ms>`` and ``<sz>`` zero disables the quota limits.
663
664When the quota limit is expected to be exceeded, DAMON prioritizes found memory
665regions of the ``target access pattern`` based on their size, access frequency,
666and age.  For personalized prioritization, users can set the weights for the
667three properties in ``<priority weights>`` in below form::
668
669    <size weight> <access frequency weight> <age weight>
670
671Watermarks
672~~~~~~~~~~
673
674Some schemes would need to run based on current value of the system's specific
675metrics like free memory ratio.  For such cases, users can specify watermarks
676for the condition.::
677
678    <metric> <check interval> <high mark> <middle mark> <low mark>
679
680``<metric>`` is a predefined integer for the metric to be checked.  The
681supported numbers and their meanings are as below.
682
683 - 0: Ignore the watermarks
684 - 1: System's free memory rate (per thousand)
685
686The value of the metric is checked every ``<check interval>`` microseconds.
687
688If the value is higher than ``<high mark>`` or lower than ``<low mark>``, the
689scheme is deactivated.  If the value is lower than ``<mid mark>``, the scheme
690is activated.
691
692.. _damos_stats:
693
694Statistics
695~~~~~~~~~~
696
697It also counts the total number and bytes of regions that each scheme is tried
698to be applied, the two numbers for the regions that each scheme is successfully
699applied, and the total number of the quota limit exceeds.  This statistics can
700be used for online analysis or tuning of the schemes.
701
702The statistics can be shown by reading the ``schemes`` file.  Reading the file
703will show each scheme you entered in each line, and the five numbers for the
704statistics will be added at the end of each line.
705
706Example
707~~~~~~~
708
709Below commands applies a scheme saying "If a memory region of size in [4KiB,
7108KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
711interval in [10, 20], page out the region.  For the paging out, use only up to
71210ms per second, and also don't page out more than 1GiB per second.  Under the
713limitation, page out memory regions having longer age first.  Also, check the
714free memory rate of the system every 5 seconds, start the monitoring and paging
715out when the free memory rate becomes lower than 50%, but stop it if the free
716memory rate becomes larger than 60%, or lower than 30%".::
717
718    # cd <debugfs>/damon
719    # scheme="4096 8192  0 5    10 20    2"  # target access pattern and action
720    # scheme+=" 10 $((1024*1024*1024)) 1000" # quotas
721    # scheme+=" 0 0 100"                     # prioritization weights
722    # scheme+=" 1 5000000 600 500 300"       # watermarks
723    # echo "$scheme" > schemes
724
725
726Turning On/Off
727--------------
728
729Setting the files as described above doesn't incur effect unless you explicitly
730start the monitoring.  You can start, stop, and check the current status of the
731monitoring by writing to and reading from the ``monitor_on`` file.  Writing
732``on`` to the file starts the monitoring of the targets with the attributes.
733Writing ``off`` to the file stops those.  DAMON also stops if every target
734process is terminated.  Below example commands turn on, off, and check the
735status of DAMON::
736
737    # cd <debugfs>/damon
738    # echo on > monitor_on
739    # echo off > monitor_on
740    # cat monitor_on
741    off
742
743Please note that you cannot write to the above-mentioned debugfs files while
744the monitoring is turned on.  If you write to the files while DAMON is running,
745an error code such as ``-EBUSY`` will be returned.
746
747
748Monitoring Thread PID
749---------------------
750
751DAMON does requested monitoring with a kernel thread called ``kdamond``.  You
752can get the pid of the thread by reading the ``kdamond_pid`` file.  When the
753monitoring is turned off, reading the file returns ``none``. ::
754
755    # cd <debugfs>/damon
756    # cat monitor_on
757    off
758    # cat kdamond_pid
759    none
760    # echo on > monitor_on
761    # cat kdamond_pid
762    18594
763
764
765Using Multiple Monitoring Threads
766---------------------------------
767
768One ``kdamond`` thread is created for each monitoring context.  You can create
769and remove monitoring contexts for multiple ``kdamond`` required use case using
770the ``mk_contexts`` and ``rm_contexts`` files.
771
772Writing the name of the new context to the ``mk_contexts`` file creates a
773directory of the name on the DAMON debugfs directory.  The directory will have
774DAMON debugfs files for the context. ::
775
776    # cd <debugfs>/damon
777    # ls foo
778    # ls: cannot access 'foo': No such file or directory
779    # echo foo > mk_contexts
780    # ls foo
781    # attrs  init_regions  kdamond_pid  schemes  target_ids
782
783If the context is not needed anymore, you can remove it and the corresponding
784directory by putting the name of the context to the ``rm_contexts`` file. ::
785
786    # echo foo > rm_contexts
787    # ls foo
788    # ls: cannot access 'foo': No such file or directory
789
790Note that ``mk_contexts``, ``rm_contexts``, and ``monitor_on`` files are in the
791root directory only.
792
793
794.. _tracepoint:
795
796Tracepoint for Monitoring Results
797=================================
798
799DAMON provides the monitoring results via a tracepoint,
800``damon:damon_aggregated``.  While the monitoring is turned on, you could
801record the tracepoint events and show results using tracepoint supporting tools
802like ``perf``.  For example::
803
804    # echo on > monitor_on
805    # perf record -e damon:damon_aggregated &
806    # sleep 5
807    # kill 9 $(pidof perf)
808    # echo off > monitor_on
809    # perf script
810