xref: /linux/Documentation/admin-guide/mm/damon/usage.rst (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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/damonitor/damo>`_ is for privileged people such as
11  system administrators who want a just-working human-friendly interface.
12  Using this, users can use the DAMON’s major features in a human-friendly way.
13  It may not be highly tuned for special cases, though.  For more detail,
14  please refer to its `usage document
15  <https://github.com/damonitor/damo/blob/next/USAGE.md>`_.
16- *sysfs interface.*
17  :ref:`This <sysfs_interface>` is for privileged user space programmers who
18  want more optimized use of DAMON.  Using this, users can use DAMON’s major
19  features by reading from and writing to special sysfs files.  Therefore,
20  you can write and use your personalized DAMON sysfs wrapper programs that
21  reads/writes the sysfs files instead of you.  The `DAMON user space tool
22  <https://github.com/damonitor/damo>`_ is one example of such programs.
23- *Kernel Space Programming Interface.*
24  :doc:`This </mm/damon/api>` is for kernel space programmers.  Using this,
25  users can utilize every feature of DAMON most flexibly and efficiently by
26  writing kernel space DAMON application programs for you.  You can even extend
27  DAMON for various address spaces.  For detail, please refer to the interface
28  :doc:`document </mm/damon/api>`.
29- *debugfs interface. (DEPRECATED!)*
30  :ref:`This <debugfs_interface>` is almost identical to :ref:`sysfs interface
31  <sysfs_interface>`.  This is deprecated, so users should move to the
32  :ref:`sysfs interface <sysfs_interface>`.  If you depend on this and cannot
33  move, please report your usecase to damon@lists.linux.dev and
34  linux-mm@kvack.org.
35
36.. _sysfs_interface:
37
38sysfs Interface
39===============
40
41DAMON sysfs interface is built when ``CONFIG_DAMON_SYSFS`` is defined.  It
42creates multiple directories and files under its sysfs directory,
43``<sysfs>/kernel/mm/damon/``.  You can control DAMON by writing to and reading
44from the files under the directory.
45
46For a short example, users can monitor the virtual address space of a given
47workload as below. ::
48
49    # cd /sys/kernel/mm/damon/admin/
50    # echo 1 > kdamonds/nr_kdamonds && echo 1 > kdamonds/0/contexts/nr_contexts
51    # echo vaddr > kdamonds/0/contexts/0/operations
52    # echo 1 > kdamonds/0/contexts/0/targets/nr_targets
53    # echo $(pidof <workload>) > kdamonds/0/contexts/0/targets/0/pid_target
54    # echo on > kdamonds/0/state
55
56Files Hierarchy
57---------------
58
59The files hierarchy of DAMON sysfs interface is shown below.  In the below
60figure, parents-children relations are represented with indentations, each
61directory is having ``/`` suffix, and files in each directory are separated by
62comma (",").
63
64.. parsed-literal::
65
66    :ref:`/sys/kernel/mm/damon <sysfs_root>`/admin
67    │ :ref:`kdamonds <sysfs_kdamonds>`/nr_kdamonds
68    │ │ :ref:`0 <sysfs_kdamond>`/state,pid
69    │ │ │ :ref:`contexts <sysfs_contexts>`/nr_contexts
70    │ │ │ │ :ref:`0 <sysfs_context>`/avail_operations,operations
71    │ │ │ │ │ :ref:`monitoring_attrs <sysfs_monitoring_attrs>`/
72    │ │ │ │ │ │ intervals/sample_us,aggr_us,update_us
73    │ │ │ │ │ │ nr_regions/min,max
74    │ │ │ │ │ :ref:`targets <sysfs_targets>`/nr_targets
75    │ │ │ │ │ │ :ref:`0 <sysfs_target>`/pid_target
76    │ │ │ │ │ │ │ :ref:`regions <sysfs_regions>`/nr_regions
77    │ │ │ │ │ │ │ │ :ref:`0 <sysfs_region>`/start,end
78    │ │ │ │ │ │ │ │ ...
79    │ │ │ │ │ │ ...
80    │ │ │ │ │ :ref:`schemes <sysfs_schemes>`/nr_schemes
81    │ │ │ │ │ │ :ref:`0 <sysfs_scheme>`/action,target_nid,apply_interval_us
82    │ │ │ │ │ │ │ :ref:`access_pattern <sysfs_access_pattern>`/
83    │ │ │ │ │ │ │ │ sz/min,max
84    │ │ │ │ │ │ │ │ nr_accesses/min,max
85    │ │ │ │ │ │ │ │ age/min,max
86    │ │ │ │ │ │ │ :ref:`quotas <sysfs_quotas>`/ms,bytes,reset_interval_ms,effective_bytes
87    │ │ │ │ │ │ │ │ weights/sz_permil,nr_accesses_permil,age_permil
88    │ │ │ │ │ │ │ │ :ref:`goals <sysfs_schemes_quota_goals>`/nr_goals
89    │ │ │ │ │ │ │ │ │ 0/target_metric,target_value,current_value
90    │ │ │ │ │ │ │ :ref:`watermarks <sysfs_watermarks>`/metric,interval_us,high,mid,low
91    │ │ │ │ │ │ │ :ref:`filters <sysfs_filters>`/nr_filters
92    │ │ │ │ │ │ │ │ 0/type,matching,memcg_id
93    │ │ │ │ │ │ │ :ref:`stats <sysfs_schemes_stats>`/nr_tried,sz_tried,nr_applied,sz_applied,qt_exceeds
94    │ │ │ │ │ │ │ :ref:`tried_regions <sysfs_schemes_tried_regions>`/total_bytes
95    │ │ │ │ │ │ │ │ 0/start,end,nr_accesses,age
96    │ │ │ │ │ │ │ │ ...
97    │ │ │ │ │ │ ...
98    │ │ │ │ ...
99    │ │ ...
100
101.. _sysfs_root:
102
103Root
104----
105
106The root of the DAMON sysfs interface is ``<sysfs>/kernel/mm/damon/``, and it
107has one directory named ``admin``.  The directory contains the files for
108privileged user space programs' control of DAMON.  User space tools or daemons
109having the root permission could use this directory.
110
111.. _sysfs_kdamonds:
112
113kdamonds/
114---------
115
116Under the ``admin`` directory, one directory, ``kdamonds``, which has files for
117controlling the kdamonds (refer to
118:ref:`design <damon_design_execution_model_and_data_structures>` for more
119details) exists.  In the beginning, this directory has only one file,
120``nr_kdamonds``.  Writing a number (``N``) to the file creates the number of
121child directories named ``0`` to ``N-1``.  Each directory represents each
122kdamond.
123
124.. _sysfs_kdamond:
125
126kdamonds/<N>/
127-------------
128
129In each kdamond directory, two files (``state`` and ``pid``) and one directory
130(``contexts``) exist.
131
132Reading ``state`` returns ``on`` if the kdamond is currently running, or
133``off`` if it is not running.
134
135Users can write below commands for the kdamond to the ``state`` file.
136
137- ``on``: Start running.
138- ``off``: Stop running.
139- ``commit``: Read the user inputs in the sysfs files except ``state`` file
140  again.
141- ``commit_schemes_quota_goals``: Read the DAMON-based operation schemes'
142  :ref:`quota goals <sysfs_schemes_quota_goals>`.
143- ``update_schemes_stats``: Update the contents of stats files for each
144  DAMON-based operation scheme of the kdamond.  For details of the stats,
145  please refer to :ref:`stats section <sysfs_schemes_stats>`.
146- ``update_schemes_tried_regions``: Update the DAMON-based operation scheme
147  action tried regions directory for each DAMON-based operation scheme of the
148  kdamond.  For details of the DAMON-based operation scheme action tried
149  regions directory, please refer to
150  :ref:`tried_regions section <sysfs_schemes_tried_regions>`.
151- ``update_schemes_tried_bytes``: Update only ``.../tried_regions/total_bytes``
152  files.
153- ``clear_schemes_tried_regions``: Clear the DAMON-based operating scheme
154  action tried regions directory for each DAMON-based operation scheme of the
155  kdamond.
156- ``update_schemes_effective_quotas``: Update the contents of
157  ``effective_bytes`` files for each DAMON-based operation scheme of the
158  kdamond.  For more details, refer to :ref:`quotas directory <sysfs_quotas>`.
159
160If the state is ``on``, reading ``pid`` shows the pid of the kdamond thread.
161
162``contexts`` directory contains files for controlling the monitoring contexts
163that this kdamond will execute.
164
165.. _sysfs_contexts:
166
167kdamonds/<N>/contexts/
168----------------------
169
170In the beginning, this directory has only one file, ``nr_contexts``.  Writing a
171number (``N``) to the file creates the number of child directories named as
172``0`` to ``N-1``.  Each directory represents each monitoring context (refer to
173:ref:`design <damon_design_execution_model_and_data_structures>` for more
174details).  At the moment, only one context per kdamond is supported, so only
175``0`` or ``1`` can be written to the file.
176
177.. _sysfs_context:
178
179contexts/<N>/
180-------------
181
182In each context directory, two files (``avail_operations`` and ``operations``)
183and three directories (``monitoring_attrs``, ``targets``, and ``schemes``)
184exist.
185
186DAMON supports multiple types of :ref:`monitoring operations
187<damon_design_configurable_operations_set>`, including those for virtual address
188space and the physical address space.  You can get the list of available
189monitoring operations set on the currently running kernel by reading
190``avail_operations`` file.  Based on the kernel configuration, the file will
191list different available operation sets.  Please refer to the :ref:`design
192<damon_operations_set>` for the list of all available operation sets and their
193brief explanations.
194
195You can set and get what type of monitoring operations DAMON will use for the
196context by writing one of the keywords listed in ``avail_operations`` file and
197reading from the ``operations`` file.
198
199.. _sysfs_monitoring_attrs:
200
201contexts/<N>/monitoring_attrs/
202------------------------------
203
204Files for specifying attributes of the monitoring including required quality
205and efficiency of the monitoring are in ``monitoring_attrs`` directory.
206Specifically, two directories, ``intervals`` and ``nr_regions`` exist in this
207directory.
208
209Under ``intervals`` directory, three files for DAMON's sampling interval
210(``sample_us``), aggregation interval (``aggr_us``), and update interval
211(``update_us``) exist.  You can set and get the values in micro-seconds by
212writing to and reading from the files.
213
214Under ``nr_regions`` directory, two files for the lower-bound and upper-bound
215of DAMON's monitoring regions (``min`` and ``max``, respectively), which
216controls the monitoring overhead, exist.  You can set and get the values by
217writing to and rading from the files.
218
219For more details about the intervals and monitoring regions range, please refer
220to the Design document (:doc:`/mm/damon/design`).
221
222.. _sysfs_targets:
223
224contexts/<N>/targets/
225---------------------
226
227In the beginning, this directory has only one file, ``nr_targets``.  Writing a
228number (``N``) to the file creates the number of child directories named ``0``
229to ``N-1``.  Each directory represents each monitoring target.
230
231.. _sysfs_target:
232
233targets/<N>/
234------------
235
236In each target directory, one file (``pid_target``) and one directory
237(``regions``) exist.
238
239If you wrote ``vaddr`` to the ``contexts/<N>/operations``, each target should
240be a process.  You can specify the process to DAMON by writing the pid of the
241process to the ``pid_target`` file.
242
243.. _sysfs_regions:
244
245targets/<N>/regions
246-------------------
247
248In case of ``fvaddr`` or ``paddr`` monitoring operations sets, users are
249required to set the monitoring target address ranges.  In case of ``vaddr``
250operations set, it is not mandatory, but users can optionally set the initial
251monitoring region to specific address ranges.  Please refer to the :ref:`design
252<damon_design_vaddr_target_regions_construction>` for more details.
253
254For such cases, users can explicitly set the initial monitoring target regions
255as they want, by writing proper values to the files under this directory.
256
257In the beginning, this directory has only one file, ``nr_regions``.  Writing a
258number (``N``) to the file creates the number of child directories named ``0``
259to ``N-1``.  Each directory represents each initial monitoring target region.
260
261.. _sysfs_region:
262
263regions/<N>/
264------------
265
266In each region directory, you will find two files (``start`` and ``end``).  You
267can set and get the start and end addresses of the initial monitoring target
268region by writing to and reading from the files, respectively.
269
270Each region should not overlap with others.  ``end`` of directory ``N`` should
271be equal or smaller than ``start`` of directory ``N+1``.
272
273.. _sysfs_schemes:
274
275contexts/<N>/schemes/
276---------------------
277
278The directory for DAMON-based Operation Schemes (:ref:`DAMOS
279<damon_design_damos>`).  Users can get and set the schemes by reading from and
280writing to files under this directory.
281
282In the beginning, this directory has only one file, ``nr_schemes``.  Writing a
283number (``N``) to the file creates the number of child directories named ``0``
284to ``N-1``.  Each directory represents each DAMON-based operation scheme.
285
286.. _sysfs_scheme:
287
288schemes/<N>/
289------------
290
291In each scheme directory, five directories (``access_pattern``, ``quotas``,
292``watermarks``, ``filters``, ``stats``, and ``tried_regions``) and three files
293(``action``, ``target_nid`` and ``apply_interval``) exist.
294
295The ``action`` file is for setting and getting the scheme's :ref:`action
296<damon_design_damos_action>`.  The keywords that can be written to and read
297from the file and their meaning are same to those of the list on
298:ref:`design doc <damon_design_damos_action>`.
299
300The ``target_nid`` file is for setting the migration target node, which is
301only meaningful when the ``action`` is either ``migrate_hot`` or
302``migrate_cold``.
303
304The ``apply_interval_us`` file is for setting and getting the scheme's
305:ref:`apply_interval <damon_design_damos>` in microseconds.
306
307.. _sysfs_access_pattern:
308
309schemes/<N>/access_pattern/
310---------------------------
311
312The directory for the target access :ref:`pattern
313<damon_design_damos_access_pattern>` of the given DAMON-based operation scheme.
314
315Under the ``access_pattern`` directory, three directories (``sz``,
316``nr_accesses``, and ``age``) each having two files (``min`` and ``max``)
317exist.  You can set and get the access pattern for the given scheme by writing
318to and reading from the ``min`` and ``max`` files under ``sz``,
319``nr_accesses``, and ``age`` directories, respectively.  Note that the ``min``
320and the ``max`` form a closed interval.
321
322.. _sysfs_quotas:
323
324schemes/<N>/quotas/
325-------------------
326
327The directory for the :ref:`quotas <damon_design_damos_quotas>` of the given
328DAMON-based operation scheme.
329
330Under ``quotas`` directory, four files (``ms``, ``bytes``,
331``reset_interval_ms``, ``effective_bytes``) and two directores (``weights`` and
332``goals``) exist.
333
334You can set the ``time quota`` in milliseconds, ``size quota`` in bytes, and
335``reset interval`` in milliseconds by writing the values to the three files,
336respectively.  Then, DAMON tries to use only up to ``time quota`` milliseconds
337for applying the ``action`` to memory regions of the ``access_pattern``, and to
338apply the action to only up to ``bytes`` bytes of memory regions within the
339``reset_interval_ms``.  Setting both ``ms`` and ``bytes`` zero disables the
340quota limits unless at least one :ref:`goal <sysfs_schemes_quota_goals>` is
341set.
342
343The time quota is internally transformed to a size quota.  Between the
344transformed size quota and user-specified size quota, smaller one is applied.
345Based on the user-specified :ref:`goal <sysfs_schemes_quota_goals>`, the
346effective size quota is further adjusted.  Reading ``effective_bytes`` returns
347the current effective size quota.  The file is not updated in real time, so
348users should ask DAMON sysfs interface to update the content of the file for
349the stats by writing a special keyword, ``update_schemes_effective_quotas`` to
350the relevant ``kdamonds/<N>/state`` file.
351
352Under ``weights`` directory, three files (``sz_permil``,
353``nr_accesses_permil``, and ``age_permil``) exist.
354You can set the :ref:`prioritization weights
355<damon_design_damos_quotas_prioritization>` for size, access frequency, and age
356in per-thousand unit by writing the values to the three files under the
357``weights`` directory.
358
359.. _sysfs_schemes_quota_goals:
360
361schemes/<N>/quotas/goals/
362-------------------------
363
364The directory for the :ref:`automatic quota tuning goals
365<damon_design_damos_quotas_auto_tuning>` of the given DAMON-based operation
366scheme.
367
368In the beginning, this directory has only one file, ``nr_goals``.  Writing a
369number (``N``) to the file creates the number of child directories named ``0``
370to ``N-1``.  Each directory represents each goal and current achievement.
371Among the multiple feedback, the best one is used.
372
373Each goal directory contains three files, namely ``target_metric``,
374``target_value`` and ``current_value``.  Users can set and get the three
375parameters for the quota auto-tuning goals that specified on the :ref:`design
376doc <damon_design_damos_quotas_auto_tuning>` by writing to and reading from each
377of the files.  Note that users should further write
378``commit_schemes_quota_goals`` to the ``state`` file of the :ref:`kdamond
379directory <sysfs_kdamond>` to pass the feedback to DAMON.
380
381.. _sysfs_watermarks:
382
383schemes/<N>/watermarks/
384-----------------------
385
386The directory for the :ref:`watermarks <damon_design_damos_watermarks>` of the
387given DAMON-based operation scheme.
388
389Under the watermarks directory, five files (``metric``, ``interval_us``,
390``high``, ``mid``, and ``low``) for setting the metric, the time interval
391between check of the metric, and the three watermarks exist.  You can set and
392get the five values by writing to the files, respectively.
393
394Keywords and meanings of those that can be written to the ``metric`` file are
395as below.
396
397 - none: Ignore the watermarks
398 - free_mem_rate: System's free memory rate (per thousand)
399
400The ``interval`` should written in microseconds unit.
401
402.. _sysfs_filters:
403
404schemes/<N>/filters/
405--------------------
406
407The directory for the :ref:`filters <damon_design_damos_filters>` of the given
408DAMON-based operation scheme.
409
410In the beginning, this directory has only one file, ``nr_filters``.  Writing a
411number (``N``) to the file creates the number of child directories named ``0``
412to ``N-1``.  Each directory represents each filter.  The filters are evaluated
413in the numeric order.
414
415Each filter directory contains six files, namely ``type``, ``matcing``,
416``memcg_path``, ``addr_start``, ``addr_end``, and ``target_idx``.  To ``type``
417file, you can write one of five special keywords: ``anon`` for anonymous pages,
418``memcg`` for specific memory cgroup, ``young`` for young pages, ``addr`` for
419specific address range (an open-ended interval), or ``target`` for specific
420DAMON monitoring target filtering.  In case of the memory cgroup filtering, you
421can specify the memory cgroup of the interest by writing the path of the memory
422cgroup from the cgroups mount point to ``memcg_path`` file.  In case of the
423address range filtering, you can specify the start and end address of the range
424to ``addr_start`` and ``addr_end`` files, respectively.  For the DAMON
425monitoring target filtering, you can specify the index of the target between
426the list of the DAMON context's monitoring targets list to ``target_idx`` file.
427You can write ``Y`` or ``N`` to ``matching`` file to filter out pages that does
428or does not match to the type, respectively.  Then, the scheme's action will
429not be applied to the pages that specified to be filtered out.
430
431For example, below restricts a DAMOS action to be applied to only non-anonymous
432pages of all memory cgroups except ``/having_care_already``.::
433
434    # echo 2 > nr_filters
435    # # filter out anonymous pages
436    echo anon > 0/type
437    echo Y > 0/matching
438    # # further filter out all cgroups except one at '/having_care_already'
439    echo memcg > 1/type
440    echo /having_care_already > 1/memcg_path
441    echo Y > 1/matching
442
443Note that ``anon`` and ``memcg`` filters are currently supported only when
444``paddr`` :ref:`implementation <sysfs_context>` is being used.
445
446Also, memory regions that are filtered out by ``addr`` or ``target`` filters
447are not counted as the scheme has tried to those, while regions that filtered
448out by other type filters are counted as the scheme has tried to.  The
449difference is applied to :ref:`stats <damos_stats>` and
450:ref:`tried regions <sysfs_schemes_tried_regions>`.
451
452.. _sysfs_schemes_stats:
453
454schemes/<N>/stats/
455------------------
456
457DAMON counts the total number and bytes of regions that each scheme is tried to
458be applied, the two numbers for the regions that each scheme is successfully
459applied, and the total number of the quota limit exceeds.  This statistics can
460be used for online analysis or tuning of the schemes.
461
462The statistics can be retrieved by reading the files under ``stats`` directory
463(``nr_tried``, ``sz_tried``, ``nr_applied``, ``sz_applied``, and
464``qt_exceeds``), respectively.  The files are not updated in real time, so you
465should ask DAMON sysfs interface to update the content of the files for the
466stats by writing a special keyword, ``update_schemes_stats`` to the relevant
467``kdamonds/<N>/state`` file.
468
469.. _sysfs_schemes_tried_regions:
470
471schemes/<N>/tried_regions/
472--------------------------
473
474This directory initially has one file, ``total_bytes``.
475
476When a special keyword, ``update_schemes_tried_regions``, is written to the
477relevant ``kdamonds/<N>/state`` file, DAMON updates the ``total_bytes`` file so
478that reading it returns the total size of the scheme tried regions, and creates
479directories named integer starting from ``0`` under this directory.  Each
480directory contains files exposing detailed information about each of the memory
481region that the corresponding scheme's ``action`` has tried to be applied under
482this directory, during next :ref:`apply interval <damon_design_damos>` of the
483corresponding scheme.  The information includes address range, ``nr_accesses``,
484and ``age`` of the region.
485
486Writing ``update_schemes_tried_bytes`` to the relevant ``kdamonds/<N>/state``
487file will only update the ``total_bytes`` file, and will not create the
488subdirectories.
489
490The directories will be removed when another special keyword,
491``clear_schemes_tried_regions``, is written to the relevant
492``kdamonds/<N>/state`` file.
493
494The expected usage of this directory is investigations of schemes' behaviors,
495and query-like efficient data access monitoring results retrievals.  For the
496latter use case, in particular, users can set the ``action`` as ``stat`` and
497set the ``access pattern`` as their interested pattern that they want to query.
498
499.. _sysfs_schemes_tried_region:
500
501tried_regions/<N>/
502------------------
503
504In each region directory, you will find four files (``start``, ``end``,
505``nr_accesses``, and ``age``).  Reading the files will show the start and end
506addresses, ``nr_accesses``, and ``age`` of the region that corresponding
507DAMON-based operation scheme ``action`` has tried to be applied.
508
509Example
510~~~~~~~
511
512Below commands applies a scheme saying "If a memory region of size in [4KiB,
5138KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
514interval in [10, 20], page out the region.  For the paging out, use only up to
51510ms per second, and also don't page out more than 1GiB per second.  Under the
516limitation, page out memory regions having longer age first.  Also, check the
517free memory rate of the system every 5 seconds, start the monitoring and paging
518out when the free memory rate becomes lower than 50%, but stop it if the free
519memory rate becomes larger than 60%, or lower than 30%". ::
520
521    # cd <sysfs>/kernel/mm/damon/admin
522    # # populate directories
523    # echo 1 > kdamonds/nr_kdamonds; echo 1 > kdamonds/0/contexts/nr_contexts;
524    # echo 1 > kdamonds/0/contexts/0/schemes/nr_schemes
525    # cd kdamonds/0/contexts/0/schemes/0
526    # # set the basic access pattern and the action
527    # echo 4096 > access_pattern/sz/min
528    # echo 8192 > access_pattern/sz/max
529    # echo 0 > access_pattern/nr_accesses/min
530    # echo 5 > access_pattern/nr_accesses/max
531    # echo 10 > access_pattern/age/min
532    # echo 20 > access_pattern/age/max
533    # echo pageout > action
534    # # set quotas
535    # echo 10 > quotas/ms
536    # echo $((1024*1024*1024)) > quotas/bytes
537    # echo 1000 > quotas/reset_interval_ms
538    # # set watermark
539    # echo free_mem_rate > watermarks/metric
540    # echo 5000000 > watermarks/interval_us
541    # echo 600 > watermarks/high
542    # echo 500 > watermarks/mid
543    # echo 300 > watermarks/low
544
545Please note that it's highly recommended to use user space tools like `damo
546<https://github.com/damonitor/damo>`_ rather than manually reading and writing
547the files as above.  Above is only for an example.
548
549.. _tracepoint:
550
551Tracepoints for Monitoring Results
552==================================
553
554Users can get the monitoring results via the :ref:`tried_regions
555<sysfs_schemes_tried_regions>`.  The interface is useful for getting a
556snapshot, but it could be inefficient for fully recording all the monitoring
557results.  For the purpose, two trace points, namely ``damon:damon_aggregated``
558and ``damon:damos_before_apply``, are provided.  ``damon:damon_aggregated``
559provides the whole monitoring results, while ``damon:damos_before_apply``
560provides the monitoring results for regions that each DAMON-based Operation
561Scheme (:ref:`DAMOS <damon_design_damos>`) is gonna be applied.  Hence,
562``damon:damos_before_apply`` is more useful for recording internal behavior of
563DAMOS, or DAMOS target access
564:ref:`pattern <damon_design_damos_access_pattern>` based query-like efficient
565monitoring results recording.
566
567While the monitoring is turned on, you could record the tracepoint events and
568show results using tracepoint supporting tools like ``perf``.  For example::
569
570    # echo on > kdamonds/0/state
571    # perf record -e damon:damon_aggregated &
572    # sleep 5
573    # kill 9 $(pidof perf)
574    # echo off > kdamonds/0/state
575    # perf script
576    kdamond.0 46568 [027] 79357.842179: damon:damon_aggregated: target_id=0 nr_regions=11 122509119488-135708762112: 0 864
577    [...]
578
579Each line of the perf script output represents each monitoring region.  The
580first five fields are as usual other tracepoint outputs.  The sixth field
581(``target_id=X``) shows the ide of the monitoring target of the region.  The
582seventh field (``nr_regions=X``) shows the total number of monitoring regions
583for the target.  The eighth field (``X-Y:``) shows the start (``X``) and end
584(``Y``) addresses of the region in bytes.  The ninth field (``X``) shows the
585``nr_accesses`` of the region (refer to
586:ref:`design <damon_design_region_based_sampling>` for more details of the
587counter).  Finally the tenth field (``X``) shows the ``age`` of the region
588(refer to :ref:`design <damon_design_age_tracking>` for more details of the
589counter).
590
591If the event was ``damon:damos_beofre_apply``, the ``perf script`` output would
592be somewhat like below::
593
594    kdamond.0 47293 [000] 80801.060214: damon:damos_before_apply: ctx_idx=0 scheme_idx=0 target_idx=0 nr_regions=11 121932607488-135128711168: 0 136
595    [...]
596
597Each line of the output represents each monitoring region that each DAMON-based
598Operation Scheme was about to be applied at the traced time.  The first five
599fields are as usual.  It shows the index of the DAMON context (``ctx_idx=X``)
600of the scheme in the list of the contexts of the context's kdamond, the index
601of the scheme (``scheme_idx=X``) in the list of the schemes of the context, in
602addition to the output of ``damon_aggregated`` tracepoint.
603
604
605.. _debugfs_interface:
606
607debugfs Interface (DEPRECATED!)
608===============================
609
610.. note::
611
612  THIS IS DEPRECATED!
613
614  DAMON debugfs interface is deprecated, so users should move to the
615  :ref:`sysfs interface <sysfs_interface>`.  If you depend on this and cannot
616  move, please report your usecase to damon@lists.linux.dev and
617  linux-mm@kvack.org.
618
619DAMON exports nine files, ``DEPRECATED``, ``attrs``, ``target_ids``,
620``init_regions``, ``schemes``, ``monitor_on_DEPRECATED``, ``kdamond_pid``,
621``mk_contexts`` and ``rm_contexts`` under its debugfs directory,
622``<debugfs>/damon/``.
623
624
625``DEPRECATED`` is a read-only file for the DAMON debugfs interface deprecation
626notice.  Reading it returns the deprecation notice, as below::
627
628    # cat DEPRECATED
629    DAMON debugfs interface is deprecated, so users should move to DAMON_SYSFS. If you cannot, please report your usecase to damon@lists.linux.dev and linux-mm@kvack.org.
630
631
632Attributes
633----------
634
635Users can get and set the ``sampling interval``, ``aggregation interval``,
636``update interval``, and min/max number of monitoring target regions by
637reading from and writing to the ``attrs`` file.  To know about the monitoring
638attributes in detail, please refer to the :doc:`/mm/damon/design`.  For
639example, below commands set those values to 5 ms, 100 ms, 1,000 ms, 10 and
6401000, and then check it again::
641
642    # cd <debugfs>/damon
643    # echo 5000 100000 1000000 10 1000 > attrs
644    # cat attrs
645    5000 100000 1000000 10 1000
646
647
648Target IDs
649----------
650
651Some types of address spaces supports multiple monitoring target.  For example,
652the virtual memory address spaces monitoring can have multiple processes as the
653monitoring targets.  Users can set the targets by writing relevant id values of
654the targets to, and get the ids of the current targets by reading from the
655``target_ids`` file.  In case of the virtual address spaces monitoring, the
656values should be pids of the monitoring target processes.  For example, below
657commands set processes having pids 42 and 4242 as the monitoring targets and
658check it again::
659
660    # cd <debugfs>/damon
661    # echo 42 4242 > target_ids
662    # cat target_ids
663    42 4242
664
665Users can also monitor the physical memory address space of the system by
666writing a special keyword, "``paddr\n``" to the file.  Because physical address
667space monitoring doesn't support multiple targets, reading the file will show a
668fake value, ``42``, as below::
669
670    # cd <debugfs>/damon
671    # echo paddr > target_ids
672    # cat target_ids
673    42
674
675Note that setting the target ids doesn't start the monitoring.
676
677
678Initial Monitoring Target Regions
679---------------------------------
680
681In case of the virtual address space monitoring, DAMON automatically sets and
682updates the monitoring target regions so that entire memory mappings of target
683processes can be covered.  However, users can want to limit the monitoring
684region to specific address ranges, such as the heap, the stack, or specific
685file-mapped area.  Or, some users can know the initial access pattern of their
686workloads and therefore want to set optimal initial regions for the 'adaptive
687regions adjustment'.
688
689In contrast, DAMON do not automatically sets and updates the monitoring target
690regions in case of physical memory monitoring.  Therefore, users should set the
691monitoring target regions by themselves.
692
693In such cases, users can explicitly set the initial monitoring target regions
694as they want, by writing proper values to the ``init_regions`` file.  The input
695should be a sequence of three integers separated by white spaces that represent
696one region in below form.::
697
698    <target idx> <start address> <end address>
699
700The ``target idx`` should be the index of the target in ``target_ids`` file,
701starting from ``0``, and the regions should be passed in address order.  For
702example, below commands will set a couple of address ranges, ``1-100`` and
703``100-200`` as the initial monitoring target region of pid 42, which is the
704first one (index ``0``) in ``target_ids``, and another couple of address
705ranges, ``20-40`` and ``50-100`` as that of pid 4242, which is the second one
706(index ``1``) in ``target_ids``.::
707
708    # cd <debugfs>/damon
709    # cat target_ids
710    42 4242
711    # echo "0   1       100 \
712            0   100     200 \
713            1   20      40  \
714            1   50      100" > init_regions
715
716Note that this sets the initial monitoring target regions only.  In case of
717virtual memory monitoring, DAMON will automatically updates the boundary of the
718regions after one ``update interval``.  Therefore, users should set the
719``update interval`` large enough in this case, if they don't want the
720update.
721
722
723Schemes
724-------
725
726Users can get and set the DAMON-based operation :ref:`schemes
727<damon_design_damos>` by reading from and writing to ``schemes`` debugfs file.
728Reading the file also shows the statistics of each scheme.  To the file, each
729of the schemes should be represented in each line in below form::
730
731    <target access pattern> <action> <quota> <watermarks>
732
733You can disable schemes by simply writing an empty string to the file.
734
735Target Access Pattern
736~~~~~~~~~~~~~~~~~~~~~
737
738The target access :ref:`pattern <damon_design_damos_access_pattern>` of the
739scheme.  The ``<target access pattern>`` is constructed with three ranges in
740below form::
741
742    min-size max-size min-acc max-acc min-age max-age
743
744Specifically, bytes for the size of regions (``min-size`` and ``max-size``),
745number of monitored accesses per aggregate interval for access frequency
746(``min-acc`` and ``max-acc``), number of aggregate intervals for the age of
747regions (``min-age`` and ``max-age``) are specified.  Note that the ranges are
748closed interval.
749
750Action
751~~~~~~
752
753The ``<action>`` is a predefined integer for memory management :ref:`actions
754<damon_design_damos_action>`.  The mapping between the ``<action>`` values and
755the memory management actions is as below.  For the detailed meaning of the
756action and DAMON operations set supporting each action, please refer to the
757list on :ref:`design doc <damon_design_damos_action>`.
758
759 - 0: ``willneed``
760 - 1: ``cold``
761 - 2: ``pageout``
762 - 3: ``hugepage``
763 - 4: ``nohugepage``
764 - 5: ``stat``
765
766Quota
767~~~~~
768
769Users can set the :ref:`quotas <damon_design_damos_quotas>` of the given scheme
770via the ``<quota>`` in below form::
771
772    <ms> <sz> <reset interval> <priority weights>
773
774This makes DAMON to try to use only up to ``<ms>`` milliseconds for applying
775the action to memory regions of the ``target access pattern`` within the
776``<reset interval>`` milliseconds, and to apply the action to only up to
777``<sz>`` bytes of memory regions within the ``<reset interval>``.  Setting both
778``<ms>`` and ``<sz>`` zero disables the quota limits.
779
780For the :ref:`prioritization <damon_design_damos_quotas_prioritization>`, users
781can set the weights for the three properties in ``<priority weights>`` in below
782form::
783
784    <size weight> <access frequency weight> <age weight>
785
786Watermarks
787~~~~~~~~~~
788
789Users can specify :ref:`watermarks <damon_design_damos_watermarks>` of the
790given scheme via ``<watermarks>`` in below form::
791
792    <metric> <check interval> <high mark> <middle mark> <low mark>
793
794``<metric>`` is a predefined integer for the metric to be checked.  The
795supported numbers and their meanings are as below.
796
797 - 0: Ignore the watermarks
798 - 1: System's free memory rate (per thousand)
799
800The value of the metric is checked every ``<check interval>`` microseconds.
801
802If the value is higher than ``<high mark>`` or lower than ``<low mark>``, the
803scheme is deactivated.  If the value is lower than ``<mid mark>``, the scheme
804is activated.
805
806.. _damos_stats:
807
808Statistics
809~~~~~~~~~~
810
811It also counts the total number and bytes of regions that each scheme is tried
812to be applied, the two numbers for the regions that each scheme is successfully
813applied, and the total number of the quota limit exceeds.  This statistics can
814be used for online analysis or tuning of the schemes.
815
816The statistics can be shown by reading the ``schemes`` file.  Reading the file
817will show each scheme you entered in each line, and the five numbers for the
818statistics will be added at the end of each line.
819
820Example
821~~~~~~~
822
823Below commands applies a scheme saying "If a memory region of size in [4KiB,
8248KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
825interval in [10, 20], page out the region.  For the paging out, use only up to
82610ms per second, and also don't page out more than 1GiB per second.  Under the
827limitation, page out memory regions having longer age first.  Also, check the
828free memory rate of the system every 5 seconds, start the monitoring and paging
829out when the free memory rate becomes lower than 50%, but stop it if the free
830memory rate becomes larger than 60%, or lower than 30%".::
831
832    # cd <debugfs>/damon
833    # scheme="4096 8192  0 5    10 20    2"  # target access pattern and action
834    # scheme+=" 10 $((1024*1024*1024)) 1000" # quotas
835    # scheme+=" 0 0 100"                     # prioritization weights
836    # scheme+=" 1 5000000 600 500 300"       # watermarks
837    # echo "$scheme" > schemes
838
839
840Turning On/Off
841--------------
842
843Setting the files as described above doesn't incur effect unless you explicitly
844start the monitoring.  You can start, stop, and check the current status of the
845monitoring by writing to and reading from the ``monitor_on_DEPRECATED`` file.
846Writing ``on`` to the file starts the monitoring of the targets with the
847attributes.  Writing ``off`` to the file stops those.  DAMON also stops if
848every target process is terminated.  Below example commands turn on, off, and
849check the status of DAMON::
850
851    # cd <debugfs>/damon
852    # echo on > monitor_on_DEPRECATED
853    # echo off > monitor_on_DEPRECATED
854    # cat monitor_on_DEPRECATED
855    off
856
857Please note that you cannot write to the above-mentioned debugfs files while
858the monitoring is turned on.  If you write to the files while DAMON is running,
859an error code such as ``-EBUSY`` will be returned.
860
861
862Monitoring Thread PID
863---------------------
864
865DAMON does requested monitoring with a kernel thread called ``kdamond``.  You
866can get the pid of the thread by reading the ``kdamond_pid`` file.  When the
867monitoring is turned off, reading the file returns ``none``. ::
868
869    # cd <debugfs>/damon
870    # cat monitor_on_DEPRECATED
871    off
872    # cat kdamond_pid
873    none
874    # echo on > monitor_on_DEPRECATED
875    # cat kdamond_pid
876    18594
877
878
879Using Multiple Monitoring Threads
880---------------------------------
881
882One ``kdamond`` thread is created for each monitoring context.  You can create
883and remove monitoring contexts for multiple ``kdamond`` required use case using
884the ``mk_contexts`` and ``rm_contexts`` files.
885
886Writing the name of the new context to the ``mk_contexts`` file creates a
887directory of the name on the DAMON debugfs directory.  The directory will have
888DAMON debugfs files for the context. ::
889
890    # cd <debugfs>/damon
891    # ls foo
892    # ls: cannot access 'foo': No such file or directory
893    # echo foo > mk_contexts
894    # ls foo
895    # attrs  init_regions  kdamond_pid  schemes  target_ids
896
897If the context is not needed anymore, you can remove it and the corresponding
898directory by putting the name of the context to the ``rm_contexts`` file. ::
899
900    # echo foo > rm_contexts
901    # ls foo
902    # ls: cannot access 'foo': No such file or directory
903
904Note that ``mk_contexts``, ``rm_contexts``, and ``monitor_on_DEPRECATED`` files
905are in the root directory only.
906