xref: /linux/Documentation/admin-guide/mm/damon/usage.rst (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1.. SPDX-License-Identifier: GPL-2.0
2
3===============
4Detailed Usages
5===============
6
7DAMON provides below interfaces for different users.
8
9- *Special-purpose DAMON modules.*
10  :ref:`This <damon_modules_special_purpose>` is for people who are building,
11  distributing, and/or administrating the kernel with special-purpose DAMON
12  usages.  Using this, users can use DAMON's major features for the given
13  purposes in build, boot, or runtime in simple ways.
14- *DAMON user space tool.*
15  `This <https://github.com/damonitor/damo>`_ is for privileged people such as
16  system administrators who want a just-working human-friendly interface.
17  Using this, users can use the DAMON’s major features in a human-friendly way.
18  It may not be highly tuned for special cases, though.  For more detail,
19  please refer to its `usage document
20  <https://github.com/damonitor/damo/blob/next/USAGE.md>`_.
21- *sysfs interface.*
22  :ref:`This <sysfs_interface>` is for privileged user space programmers who
23  want more optimized use of DAMON.  Using this, users can use DAMON’s major
24  features by reading from and writing to special sysfs files.  Therefore,
25  you can write and use your personalized DAMON sysfs wrapper programs that
26  reads/writes the sysfs files instead of you.  The `DAMON user space tool
27  <https://github.com/damonitor/damo>`_ is one example of such programs.
28- *Kernel Space Programming Interface.*
29  :doc:`This </mm/damon/api>` is for kernel space programmers.  Using this,
30  users can utilize every feature of DAMON most flexibly and efficiently by
31  writing kernel space DAMON application programs for you.  You can even extend
32  DAMON for various address spaces.  For detail, please refer to the interface
33  :doc:`document </mm/damon/api>`.
34
35.. _sysfs_interface:
36
37sysfs Interface
38===============
39
40DAMON sysfs interface is built when ``CONFIG_DAMON_SYSFS`` is defined.  It
41creates multiple directories and files under its sysfs directory,
42``<sysfs>/kernel/mm/damon/``.  You can control DAMON by writing to and reading
43from the files under the directory.
44
45For a short example, users can monitor the virtual address space of a given
46workload as below. ::
47
48    # cd /sys/kernel/mm/damon/admin/
49    # echo 1 > kdamonds/nr_kdamonds && echo 1 > kdamonds/0/contexts/nr_contexts
50    # echo vaddr > kdamonds/0/contexts/0/operations
51    # echo 1 > kdamonds/0/contexts/0/targets/nr_targets
52    # echo $(pidof <workload>) > kdamonds/0/contexts/0/targets/0/pid_target
53    # echo on > kdamonds/0/state
54
55Files Hierarchy
56---------------
57
58The files hierarchy of DAMON sysfs interface is shown below.  In the below
59figure, parents-children relations are represented with indentations, each
60directory is having ``/`` suffix, and files in each directory are separated by
61comma (",").
62
63.. parsed-literal::
64
65    :ref:`/sys/kernel/mm/damon <sysfs_root>`/admin
66    │ :ref:`kdamonds <sysfs_kdamonds>`/nr_kdamonds
67    │ │ :ref:`0 <sysfs_kdamond>`/state,pid,refresh_ms
68    │ │ │ :ref:`contexts <sysfs_contexts>`/nr_contexts
69    │ │ │ │ :ref:`0 <sysfs_context>`/avail_operations,operations,addr_unit,
70    │ │ │ │   pause
71    │ │ │ │ │ :ref:`monitoring_attrs <sysfs_monitoring_attrs>`/
72    │ │ │ │ │ │ intervals/sample_us,aggr_us,update_us
73    │ │ │ │ │ │ │ intervals_goal/access_bp,aggrs,min_sample_us,max_sample_us
74    │ │ │ │ │ │ nr_regions/min,max
75    │ │ │ │ │ │ :ref:`probes <damon_usage_sysfs_probes>`/nr_probes
76    │ │ │ │ │ │ │ 0/filters/nr_filters
77    │ │ │ │ │ │ │ │ 0/type,matching,allow,path
78    │ │ │ │ │ │ │ │ ...
79    │ │ │ │ │ │ │ ...
80    │ │ │ │ │ :ref:`targets <sysfs_targets>`/nr_targets
81    │ │ │ │ │ │ :ref:`0 <sysfs_target>`/pid_target,obsolete_target
82    │ │ │ │ │ │ │ :ref:`regions <sysfs_regions>`/nr_regions
83    │ │ │ │ │ │ │ │ :ref:`0 <sysfs_region>`/start,end
84    │ │ │ │ │ │ │ │ ...
85    │ │ │ │ │ │ ...
86    │ │ │ │ │ :ref:`schemes <sysfs_schemes>`/nr_schemes
87    │ │ │ │ │ │ :ref:`0 <sysfs_scheme>`/action,target_nid,apply_interval_us
88    │ │ │ │ │ │ │ :ref:`access_pattern <sysfs_access_pattern>`/
89    │ │ │ │ │ │ │ │ sz/min,max
90    │ │ │ │ │ │ │ │ nr_accesses/min,max
91    │ │ │ │ │ │ │ │ age/min,max
92    │ │ │ │ │ │ │ :ref:`quotas <sysfs_quotas>`/ms,bytes,reset_interval_ms,
93    │ │ │ │ │ │ │     effective_bytes,goal_tuner,
94    │ │ │ │ │ │ │     fail_charge_num,fail_charge_denom
95    │ │ │ │ │ │ │ │ weights/sz_permil,nr_accesses_permil,age_permil
96    │ │ │ │ │ │ │ │ :ref:`goals <sysfs_schemes_quota_goals>`/nr_goals
97    │ │ │ │ │ │ │ │ │ 0/target_metric,target_value,current_value,nid,path
98    │ │ │ │ │ │ │ :ref:`watermarks <sysfs_watermarks>`/metric,interval_us,high,mid,low
99    │ │ │ │ │ │ │ :ref:`{core_,ops_,}filters <sysfs_filters>`/nr_filters
100    │ │ │ │ │ │ │ │ 0/type,matching,allow,memcg_path,addr_start,addr_end,damon_target_idx,min,max
101    │ │ │ │ │ │ │ :ref:`dests <damon_sysfs_dests>`/nr_dests
102    │ │ │ │ │ │ │ │ 0/id,weight
103    │ │ │ │ │ │ │ :ref:`stats <sysfs_schemes_stats>`/nr_tried,sz_tried,nr_applied,sz_applied,sz_ops_filter_passed,qt_exceeds,nr_snapshots,max_nr_snapshots
104    │ │ │ │ │ │ │ :ref:`tried_regions <sysfs_schemes_tried_regions>`/total_bytes
105    │ │ │ │ │ │ │ │ 0/start,end,nr_accesses,age,sz_filter_passed
106    │ │ │ │ │ │ │ │ │ probes
107    │ │ │ │ │ │ │ │ │ │ 0/hits
108    │ │ │ │ │ │ │ │ │ │ ...
109    │ │ │ │ │ │ │ │ ...
110    │ │ │ │ │ │ ...
111    │ │ │ │ ...
112    │ │ ...
113
114.. _sysfs_root:
115
116Root
117----
118
119The root of the DAMON sysfs interface is ``<sysfs>/kernel/mm/damon/``, and it
120has one directory named ``admin``.  The directory contains the files for
121privileged user space programs' control of DAMON.  User space tools or daemons
122having the root permission could use this directory.
123
124.. _sysfs_kdamonds:
125
126kdamonds/
127---------
128
129Under the ``admin`` directory, one directory, ``kdamonds``, which has files for
130controlling the kdamonds (refer to
131:ref:`design <damon_design_execution_model_and_data_structures>` for more
132details) exists.  In the beginning, this directory has only one file,
133``nr_kdamonds``.  Writing a number (``N``) to the file creates the number of
134child directories named ``0`` to ``N-1``.  Each directory represents each
135kdamond.
136
137.. _sysfs_kdamond:
138
139kdamonds/<N>/
140-------------
141
142In each kdamond directory, three files (``state``, ``pid`` and ``refresh_ms``)
143and one directory (``contexts``) exist.
144
145Reading ``state`` returns ``on`` if the kdamond is currently running, or
146``off`` if it is not running.
147
148Users can write below commands for the kdamond to the ``state`` file.
149
150- ``on``: Start running.
151- ``off``: Stop running.
152- ``commit``: Read the user inputs in the sysfs files except ``state`` file
153  again.  Monitoring :ref:`target region <sysfs_regions>` inputs are also be
154  ignored if no target region is specified.
155- ``update_tuned_intervals``: Update the contents of ``sample_us`` and
156  ``aggr_us`` files of the kdamond with the auto-tuning applied ``sampling
157  interval`` and ``aggregation interval`` for the files.  Please refer to
158  :ref:`intervals_goal section <damon_usage_sysfs_monitoring_intervals_goal>`
159  for more details.
160- ``commit_schemes_quota_goals``: Read the DAMON-based operation schemes'
161  :ref:`quota goals <sysfs_schemes_quota_goals>`.
162- ``update_schemes_stats``: Update the contents of stats files for each
163  DAMON-based operation scheme of the kdamond.  For details of the stats,
164  please refer to :ref:`stats section <sysfs_schemes_stats>`.
165- ``update_schemes_tried_regions``: Update the DAMON-based operation scheme
166  action tried regions directory for each DAMON-based operation scheme of the
167  kdamond.  For details of the DAMON-based operation scheme action tried
168  regions directory, please refer to
169  :ref:`tried_regions section <sysfs_schemes_tried_regions>`.
170- ``update_schemes_tried_bytes``: Update only ``.../tried_regions/total_bytes``
171  files.
172- ``clear_schemes_tried_regions``: Clear the DAMON-based operating scheme
173  action tried regions directory for each DAMON-based operation scheme of the
174  kdamond.
175- ``update_schemes_effective_quotas``: Update the contents of
176  ``effective_bytes`` files for each DAMON-based operation scheme of the
177  kdamond.  For more details, refer to :ref:`quotas directory <sysfs_quotas>`.
178
179If the state is ``on``, reading ``pid`` shows the pid of the kdamond thread.
180
181Users can ask the kernel to periodically update files showing auto-tuned
182parameters and DAMOS stats instead of manually writing
183``update_tuned_intervals`` like keywords to ``state`` file.  For this, users
184should write the desired update time interval in milliseconds to ``refresh_ms``
185file.  If the interval is zero, the periodic update is disabled.  Reading the
186file shows currently set time interval.
187
188``contexts`` directory contains files for controlling the monitoring contexts
189that this kdamond will execute.
190
191.. _sysfs_contexts:
192
193kdamonds/<N>/contexts/
194----------------------
195
196In the beginning, this directory has only one file, ``nr_contexts``.  Writing a
197number (``N``) to the file creates the number of child directories named as
198``0`` to ``N-1``.  Each directory represents each monitoring context (refer to
199:ref:`design <damon_design_execution_model_and_data_structures>` for more
200details).  At the moment, only one context per kdamond is supported, so only
201``0`` or ``1`` can be written to the file.
202
203.. _sysfs_context:
204
205contexts/<N>/
206-------------
207
208In each context directory, four files (``avail_operations``, ``operations``,
209``addr_unit`` and ``pause``) and three directories (``monitoring_attrs``,
210``targets``, and ``schemes``) exist.
211
212DAMON supports multiple types of :ref:`monitoring operations
213<damon_design_configurable_operations_set>`, including those for virtual address
214space and the physical address space.  You can get the list of available
215monitoring operations set on the currently running kernel by reading
216``avail_operations`` file.  Based on the kernel configuration, the file will
217list different available operation sets.  Please refer to the :ref:`design
218<damon_operations_set>` for the list of all available operation sets and their
219brief explanations.
220
221You can set and get what type of monitoring operations DAMON will use for the
222context by writing one of the keywords listed in ``avail_operations`` file and
223reading from the ``operations`` file.
224
225``addr_unit`` file is for setting and getting the :ref:`address unit
226<damon_design_addr_unit>` parameter of the operations set.
227
228``pause`` file is for setting and getting the :ref:`pause request
229<damon_design_execution_model_and_data_structures>` parameter of the context.
230
231.. _sysfs_monitoring_attrs:
232
233contexts/<N>/monitoring_attrs/
234------------------------------
235
236Files for specifying attributes of the monitoring including required quality
237and efficiency of the monitoring are in ``monitoring_attrs`` directory.
238Specifically, three directories, ``intervals``, ``nr_regions`` and ``probes``
239exist in this directory.
240
241Under ``intervals`` directory, three files for DAMON's sampling interval
242(``sample_us``), aggregation interval (``aggr_us``), and update interval
243(``update_us``) exist.  You can set and get the values in micro-seconds by
244writing to and reading from the files.
245
246Under ``nr_regions`` directory, two files for the lower-bound and upper-bound
247of DAMON's monitoring regions (``min`` and ``max``, respectively), which
248controls the monitoring overhead, exist.  You can set and get the values by
249writing to and rading from the files.
250
251For more details about the intervals and monitoring regions range, please refer
252to the Design document (:doc:`/mm/damon/design`).
253
254.. _damon_usage_sysfs_monitoring_intervals_goal:
255
256contexts/<N>/monitoring_attrs/intervals/intervals_goal/
257-------------------------------------------------------
258
259Under the ``intervals`` directory, one directory for automated tuning of
260``sample_us`` and ``aggr_us``, namely ``intervals_goal`` directory also exists.
261Under the directory, four files for the auto-tuning control, namely
262``access_bp``, ``aggrs``, ``min_sample_us`` and ``max_sample_us`` exist.
263Please refer to  the :ref:`design document of the feature
264<damon_design_monitoring_intervals_autotuning>` for the internal of the tuning
265mechanism.  Reading and writing the four files under ``intervals_goal``
266directory shows and updates the tuning parameters that described in the
267:ref:design doc <damon_design_monitoring_intervals_autotuning>` with the same
268names.  The tuning starts with the user-set ``sample_us`` and ``aggr_us``.  The
269tuning-applied current values of the two intervals can be read from the
270``sample_us`` and ``aggr_us`` files after writing ``update_tuned_intervals`` to
271the ``state`` file.
272
273.. _damon_usage_sysfs_probes:
274
275contexts/<N>/monitoring_attrs/probes/
276-------------------------------------
277
278A directory for registering :ref:`data attributes monitoring
279<damon_design_data_attrs_monitoring>` probes.
280
281In the beginning, this directory has only one file, ``nr_probes``.  Writing a
282number (``N``) to the file creates the number of child directories named ``0``
283to ``N-1``.  Each directory represents each monitoring probe.
284
285In each probe directory, one directory, ``filters`` exists.  The directory
286contains files for installing filters for the probe, that is used to determine
287the data attribute for the probe.
288
289In the beginning, ``filters`` directory has only one file, ``nr_filters``.
290Writing a number (``N``) to the file creates the number of child directories
291named ``0`` to ``N-1``.  Each directory represents each filter and works in a
292way similar to that for :ref:`DAMOS filter <sysfs_filters>`.  When the filter
293``type`` is ``memcg``, ``path`` file acts as ``memcg_path`` for :ref:`DAMOS
294filter <sysfs_filters>`.
295
296.. _sysfs_targets:
297
298contexts/<N>/targets/
299---------------------
300
301In the beginning, this directory has only one file, ``nr_targets``.  Writing a
302number (``N``) to the file creates the number of child directories named ``0``
303to ``N-1``.  Each directory represents each monitoring target.
304
305.. _sysfs_target:
306
307targets/<N>/
308------------
309
310In each target directory, two files (``pid_target`` and ``obsolete_target``)
311and one directory (``regions``) exist.
312
313If you wrote ``vaddr`` to the ``contexts/<N>/operations``, each target should
314be a process.  You can specify the process to DAMON by writing the pid of the
315process to the ``pid_target`` file.
316
317Users can selectively remove targets in the middle of the targets array by
318writing non-zero value to ``obsolete_target`` file and committing it (writing
319``commit`` to ``state`` file).  DAMON will remove the matching targets from its
320internal targets array.  Users are responsible to construct target directories
321again, so that those correctly represent the changed internal targets array.
322
323
324.. _sysfs_regions:
325
326targets/<N>/regions
327-------------------
328
329In case of ``fvaddr`` or ``paddr`` monitoring operations sets, users are
330required to set the monitoring target address ranges.  In case of ``vaddr``
331operations set, it is not mandatory, but users can optionally set the initial
332monitoring region to specific address ranges.  Please refer to the :ref:`design
333<damon_design_vaddr_target_regions_construction>` for more details.
334
335For such cases, users can explicitly set the initial monitoring target regions
336as they want, by writing proper values to the files under this directory.
337
338In the beginning, this directory has only one file, ``nr_regions``.  Writing a
339number (``N``) to the file creates the number of child directories named ``0``
340to ``N-1``.  Each directory represents each initial monitoring target region.
341
342If ``nr_regions`` is zero when committing new DAMON parameters online (writing
343``commit`` to ``state`` file of :ref:`kdamond <sysfs_kdamond>`), the commit
344logic ignores the target regions.  In other words, the current monitoring
345results for the target are preserved.
346
347.. _sysfs_region:
348
349regions/<N>/
350------------
351
352In each region directory, you will find two files (``start`` and ``end``).  You
353can set and get the start and end addresses of the initial monitoring target
354region by writing to and reading from the files, respectively.
355
356Each region should not overlap with others.  ``end`` of directory ``N`` should
357be equal or smaller than ``start`` of directory ``N+1``.
358
359.. _sysfs_schemes:
360
361contexts/<N>/schemes/
362---------------------
363
364The directory for DAMON-based Operation Schemes (:ref:`DAMOS
365<damon_design_damos>`).  Users can get and set the schemes by reading from and
366writing to files under this directory.
367
368In the beginning, this directory has only one file, ``nr_schemes``.  Writing a
369number (``N``) to the file creates the number of child directories named ``0``
370to ``N-1``.  Each directory represents each DAMON-based operation scheme.
371
372.. _sysfs_scheme:
373
374schemes/<N>/
375------------
376
377In each scheme directory, nine directories (``access_pattern``, ``quotas``,
378``watermarks``, ``core_filters``, ``ops_filters``, ``filters``, ``dests``,
379``stats``, and ``tried_regions``) and three files (``action``, ``target_nid``
380and ``apply_interval``) exist.
381
382The ``action`` file is for setting and getting the scheme's :ref:`action
383<damon_design_damos_action>`.  The keywords that can be written to and read
384from the file and their meaning are same to those of the list on
385:ref:`design doc <damon_design_damos_action>`.
386
387The ``target_nid`` file is for setting the migration target node, which is
388only meaningful when the ``action`` is either ``migrate_hot`` or
389``migrate_cold``.
390
391The ``apply_interval_us`` file is for setting and getting the scheme's
392:ref:`apply_interval <damon_design_damos>` in microseconds.
393
394.. _sysfs_access_pattern:
395
396schemes/<N>/access_pattern/
397---------------------------
398
399The directory for the target access :ref:`pattern
400<damon_design_damos_access_pattern>` of the given DAMON-based operation scheme.
401
402Under the ``access_pattern`` directory, three directories (``sz``,
403``nr_accesses``, and ``age``) each having two files (``min`` and ``max``)
404exist.  You can set and get the access pattern for the given scheme by writing
405to and reading from the ``min`` and ``max`` files under ``sz``,
406``nr_accesses``, and ``age`` directories, respectively.  Note that the ``min``
407and the ``max`` form a closed interval.
408
409.. _sysfs_quotas:
410
411schemes/<N>/quotas/
412-------------------
413
414The directory for the :ref:`quotas <damon_design_damos_quotas>` of the given
415DAMON-based operation scheme.
416
417Under ``quotas`` directory, seven files (``ms``, ``bytes``,
418``reset_interval_ms``, ``effective_bytes``, ``goal_tuner``, ``fail_charge_num``
419and ``fail_charge_denom``) and two directories (``weights`` and ``goals``)
420exist.
421
422You can set the ``time quota`` in milliseconds, ``size quota`` in bytes, and
423``reset interval`` in milliseconds by writing the values to the three files,
424respectively.  Then, DAMON tries to use only up to ``time quota`` milliseconds
425for applying the ``action`` to memory regions of the ``access_pattern``, and to
426apply the action to only up to ``bytes`` bytes of memory regions within the
427``reset_interval_ms``.  Setting both ``ms`` and ``bytes`` zero disables the
428quota limits unless at least one :ref:`goal <sysfs_schemes_quota_goals>` is
429set.
430
431You can set the goal-based effective quota auto-tuning algorithm to use, by
432writing the algorithm name to ``goal_tuner`` file.  Reading the file returns
433the currently selected tuner algorithm.  Refer to the design documentation of
434:ref:`automatic quota tuning goals <damon_design_damos_quotas_auto_tuning>` for
435the background design of the feature and the name of the selectable algorithms.
436Refer to :ref:`goals directory <sysfs_schemes_quota_goals>` for the goals
437setup.
438
439You can set the action-failed memory quota charging ratio by writing the
440numerator and the denominator for the ratio to ``fail_charge_num`` and
441``fail_charge_denom`` files, respectively.  Reading those files will return the
442current set values.  Refer to :ref:`design
443<damon_design_damos_quotas_failed_memory_charging_ratio>` for more details of
444the ratio feature.
445
446The time quota is internally transformed to a size quota.  Between the
447transformed size quota and user-specified size quota, smaller one is applied.
448Based on the user-specified :ref:`goal <sysfs_schemes_quota_goals>`, the
449effective size quota is further adjusted.  Reading ``effective_bytes`` returns
450the current effective size quota.  The file is not updated in real time, so
451users should ask DAMON sysfs interface to update the content of the file for
452the stats by writing a special keyword, ``update_schemes_effective_quotas`` to
453the relevant ``kdamonds/<N>/state`` file.
454
455Under ``weights`` directory, three files (``sz_permil``,
456``nr_accesses_permil``, and ``age_permil``) exist.
457You can set the :ref:`prioritization weights
458<damon_design_damos_quotas_prioritization>` for size, access frequency, and age
459in per-thousand unit by writing the values to the three files under the
460``weights`` directory.
461
462.. _sysfs_schemes_quota_goals:
463
464schemes/<N>/quotas/goals/
465-------------------------
466
467The directory for the :ref:`automatic quota tuning goals
468<damon_design_damos_quotas_auto_tuning>` of the given DAMON-based operation
469scheme.
470
471In the beginning, this directory has only one file, ``nr_goals``.  Writing a
472number (``N``) to the file creates the number of child directories named ``0``
473to ``N-1``.  Each directory represents each goal and current achievement.
474Among the multiple feedback, the best one is used.
475
476Each goal directory contains five files, namely ``target_metric``,
477``target_value``, ``current_value``, ``nid``, and ``path``.  Users can set and
478get the five parameters for the quota auto-tuning goals that specified on the
479:ref:`design doc <damon_design_damos_quotas_auto_tuning>` by writing to and
480reading from each of the files.  Because the kernel does not update
481``current_value``, reading it only makes sense when ``target_metric`` is
482``user_input``.  Note that users should further write
483``commit_schemes_quota_goals`` to the ``state`` file of the :ref:`kdamond
484directory <sysfs_kdamond>` to pass the feedback to DAMON.
485
486.. _sysfs_watermarks:
487
488schemes/<N>/watermarks/
489-----------------------
490
491The directory for the :ref:`watermarks <damon_design_damos_watermarks>` of the
492given DAMON-based operation scheme.
493
494Under the watermarks directory, five files (``metric``, ``interval_us``,
495``high``, ``mid``, and ``low``) for setting the metric, the time interval
496between check of the metric, and the three watermarks exist.  You can set and
497get the five values by writing to and reading from the files, respectively.
498
499Keywords and meanings of those that can be written to the ``metric`` file are
500as below.
501
502 - none: Ignore the watermarks
503 - free_mem_rate: System's free memory rate (per thousand)
504
505The ``interval_us`` should be written in microseconds unit.
506
507.. _sysfs_filters:
508
509schemes/<N>/{core\_,ops\_,}filters/
510-----------------------------------
511
512Directories for :ref:`filters <damon_design_damos_filters>` of the given
513DAMON-based operation scheme.
514
515``core_filters`` and ``ops_filters`` directories are for the filters handled by
516the DAMON core layer and operations set layer, respectively.  ``filters``
517directory can be used for installing filters regardless of their handled
518layers.  Filters that requested by ``core_filters`` and ``ops_filters`` will be
519installed before those of ``filters``.  All three directories have same files.
520
521Use of ``filters`` directory can make filters evaluation orders confusing to
522expect.  For this reason, ``filters`` directory is deprecated.  It is still
523functioning, but is scheduled for removal in the near future.  Users should use
524``core_filters`` and ``ops_filters`` directories instead.
525
526In the beginning, the directory has only one file, ``nr_filters``.  Writing a
527number (``N``) to the file creates the number of child directories named ``0``
528to ``N-1``.  Each directory represents each filter.  The filters are evaluated
529in the numeric order.
530
531Each filter directory contains nine files, namely ``type``, ``matching``,
532``allow``, ``memcg_path``, ``addr_start``, ``addr_end``, ``min``, ``max``
533and ``damon_target_idx``.  To ``type`` file, you can write the type of the
534filter.  Refer to :ref:`the design doc <damon_design_damos_filters>` for
535available type names, their meaning and on what layer those are handled.
536
537For ``memcg`` type, you can specify the memory cgroup of the interest by
538writing the path of the memory cgroup from the cgroups mount point to
539``memcg_path`` file.  For ``addr`` type, you can specify the start and end
540address of the range (open-ended interval) to ``addr_start`` and ``addr_end``
541files, respectively.  For ``hugepage_size`` type, you can specify the minimum
542and maximum size of the range (closed interval) to ``min`` and ``max`` files,
543respectively.  For ``target`` type, you can specify the index of the target
544between the list of the DAMON context's monitoring targets list to
545``damon_target_idx`` file.
546
547You can write ``Y`` or ``N`` to ``matching`` file to specify whether the filter
548is for memory that matches the ``type``.  You can write ``Y`` or ``N`` to
549``allow`` file to specify if applying the action to the memory that satisfies
550the ``type`` and ``matching`` should be allowed or not.
551
552For example, below restricts a DAMOS action to be applied to only non-anonymous
553pages of all memory cgroups except ``/having_care_already``.::
554
555    # cd ops_filters/0/
556    # echo 2 > nr_filters
557    # # disallow anonymous pages
558    echo anon > 0/type
559    echo Y > 0/matching
560    echo N > 0/allow
561    # # further filter out all cgroups except one at '/having_care_already'
562    echo memcg > 1/type
563    echo /having_care_already > 1/memcg_path
564    echo Y > 1/matching
565    echo N > 1/allow
566
567Refer to the :ref:`DAMOS filters design documentation
568<damon_design_damos_filters>` for more details including how multiple filters
569of different ``allow`` works, when each of the filters are supported, and
570differences on stats.
571
572.. _damon_sysfs_dests:
573
574schemes/<N>/dests/
575------------------
576
577Directory for specifying the destinations of given DAMON-based operation
578scheme's action.  This directory is ignored if the action of the given scheme
579is not supporting multiple destinations.  Only ``DAMOS_MIGRATE_{HOT,COLD}``
580actions are supporting multiple destinations.
581
582In the beginning, the directory has only one file, ``nr_dests``.  Writing a
583number (``N``) to the file creates the number of child directories named ``0``
584to ``N-1``.  Each directory represents each action destination.
585
586Each destination directory contains two files, namely ``id`` and ``weight``.
587Users can write and read the identifier of the destination to ``id`` file.
588For ``DAMOS_MIGRATE_{HOT,COLD}`` actions, the migrate destination node's node
589id should be written to ``id`` file.  Users can write and read the weight of
590the destination among the given destinations to the ``weight`` file.  The
591weight can be an arbitrary integer.  When DAMOS apply the action to each entity
592of the memory region, it will select the destination of the action based on the
593relative weights of the destinations.
594
595.. _sysfs_schemes_stats:
596
597schemes/<N>/stats/
598------------------
599
600DAMON counts statistics for each scheme.  This statistics can be used for
601online analysis or tuning of the schemes.  Refer to :ref:`design doc
602<damon_design_damos_stat>` for more details about the stats.
603
604The statistics can be retrieved by reading the files under ``stats`` directory
605(``nr_tried``, ``sz_tried``, ``nr_applied``, ``sz_applied``,
606``sz_ops_filter_passed``, ``qt_exceeds``, ``nr_snapshots`` and
607``max_nr_snapshots``), respectively.
608
609The files are not updated in real time by default.  Users should ask DAMON
610sysfs interface to periodically update those using ``refresh_ms``, or do a one
611time update by writing a special keyword, ``update_schemes_stats`` to the
612relevant ``kdamonds/<N>/state`` file.  Refer to :ref:`kdamond directory
613<sysfs_kdamond>` for more details.
614
615.. _sysfs_schemes_tried_regions:
616
617schemes/<N>/tried_regions/
618--------------------------
619
620This directory initially has one file, ``total_bytes``.
621
622When a special keyword, ``update_schemes_tried_regions``, is written to the
623relevant ``kdamonds/<N>/state`` file, DAMON updates the ``total_bytes`` file so
624that reading it returns the total size of the scheme tried regions, and creates
625directories named integer starting from ``0`` under this directory.  Each
626directory contains files exposing detailed information about each of the memory
627region that the corresponding scheme's ``action`` has tried to be applied under
628this directory, during next :ref:`apply interval <damon_design_damos>` of the
629corresponding scheme.  The information includes address range, ``nr_accesses``,
630and ``age`` of the region.
631
632Writing ``update_schemes_tried_bytes`` to the relevant ``kdamonds/<N>/state``
633file will only update the ``total_bytes`` file, and will not create the
634subdirectories.
635
636The directories will be removed when another special keyword,
637``clear_schemes_tried_regions``, is written to the relevant
638``kdamonds/<N>/state`` file.
639
640The expected usage of this directory is investigations of schemes' behaviors,
641and query-like efficient data access monitoring results retrievals.  For the
642latter use case, in particular, users can set the ``action`` as ``stat`` and
643set the ``access pattern`` as their interested pattern that they want to query.
644
645.. _sysfs_schemes_tried_region:
646
647tried_regions/<N>/
648------------------
649
650In each region directory, you will find five files (``start``, ``end``,
651``nr_accesses``, ``age`` and ``sz_filter_passed``).  Reading the files will
652show the properties of the region that corresponding DAMON-based operation
653scheme ``action`` has tried to be applied.
654
655tried_regions/<N>/probes/
656-------------------------
657
658In each region directory, one directory (``probes``) also exists.  In the
659directory, subdirectories named ``0`` to ``N-1`` exists.  ``N`` is the number
660of installed probes.  In each number-named directory, a file (``hits``) exist.
661Reading the file shows the number of data attributes monitoring probe-hit
662positive samples of the region.
663
664Example
665~~~~~~~
666
667Below commands applies a scheme saying "If a memory region of size in [4KiB,
6688KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
669interval in [10, 20], page out the region.  For the paging out, use only up to
67010ms per second, and also don't page out more than 1GiB per second.  Under the
671limitation, page out memory regions having longer age first.  Also, check the
672free memory rate of the system every 5 seconds, start the monitoring and paging
673out when the free memory rate becomes lower than 50%, but stop it if the free
674memory rate becomes larger than 60%, or lower than 30%". ::
675
676    # cd <sysfs>/kernel/mm/damon/admin
677    # # populate directories
678    # echo 1 > kdamonds/nr_kdamonds; echo 1 > kdamonds/0/contexts/nr_contexts;
679    # echo 1 > kdamonds/0/contexts/0/schemes/nr_schemes
680    # cd kdamonds/0/contexts/0/schemes/0
681    # # set the basic access pattern and the action
682    # echo 4096 > access_pattern/sz/min
683    # echo 8192 > access_pattern/sz/max
684    # echo 0 > access_pattern/nr_accesses/min
685    # echo 5 > access_pattern/nr_accesses/max
686    # echo 10 > access_pattern/age/min
687    # echo 20 > access_pattern/age/max
688    # echo pageout > action
689    # # set quotas
690    # echo 10 > quotas/ms
691    # echo $((1024*1024*1024)) > quotas/bytes
692    # echo 1000 > quotas/reset_interval_ms
693    # # set watermark
694    # echo free_mem_rate > watermarks/metric
695    # echo 5000000 > watermarks/interval_us
696    # echo 600 > watermarks/high
697    # echo 500 > watermarks/mid
698    # echo 300 > watermarks/low
699
700Please note that it's highly recommended to use user space tools like `damo
701<https://github.com/damonitor/damo>`_ rather than manually reading and writing
702the files as above.  Above is only for an example.
703
704.. _tracepoint:
705
706Tracepoints for Monitoring Results
707==================================
708
709Users can get the monitoring results via the :ref:`tried_regions
710<sysfs_schemes_tried_regions>`.  The interface is useful for getting a
711snapshot, but it could be inefficient for fully recording all the monitoring
712results.  For the purpose, two trace points, namely ``damon:damon_aggregated``
713and ``damon:damos_before_apply``, are provided.  ``damon:damon_aggregated``
714provides the whole monitoring results, while ``damon:damos_before_apply``
715provides the monitoring results for regions that each DAMON-based Operation
716Scheme (:ref:`DAMOS <damon_design_damos>`) is gonna be applied.  Hence,
717``damon:damos_before_apply`` is more useful for recording internal behavior of
718DAMOS, or DAMOS target access
719:ref:`pattern <damon_design_damos_access_pattern>` based query-like efficient
720monitoring results recording.
721
722While the monitoring is turned on, you could record the tracepoint events and
723show results using tracepoint supporting tools like ``perf``.  For example::
724
725    # echo on > kdamonds/0/state
726    # perf record -e damon:damon_aggregated &
727    # sleep 5
728    # kill 9 $(pidof perf)
729    # echo off > kdamonds/0/state
730    # perf script
731    kdamond.0 46568 [027] 79357.842179: damon:damon_aggregated: target_id=0 nr_regions=11 122509119488-135708762112: 0 864
732    [...]
733
734Each line of the perf script output represents each monitoring region.  The
735first five fields are as usual other tracepoint outputs.  The sixth field
736(``target_id=X``) shows the id of the monitoring target of the region.  The
737seventh field (``nr_regions=X``) shows the total number of monitoring regions
738for the target.  The eighth field (``X-Y:``) shows the start (``X``) and end
739(``Y``) addresses of the region in bytes.  The ninth field (``X``) shows the
740``nr_accesses`` of the region (refer to
741:ref:`design <damon_design_region_based_sampling>` for more details of the
742counter).  Finally the tenth field (``X``) shows the ``age`` of the region
743(refer to :ref:`design <damon_design_age_tracking>` for more details of the
744counter).
745
746If the event was ``damon:damos_beofre_apply``, the ``perf script`` output would
747be somewhat like below::
748
749    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
750    [...]
751
752Each line of the output represents each monitoring region that each DAMON-based
753Operation Scheme was about to be applied at the traced time.  The first five
754fields are as usual.  It shows the index of the DAMON context (``ctx_idx=X``)
755of the scheme in the list of the contexts of the context's kdamond, the index
756of the scheme (``scheme_idx=X``) in the list of the schemes of the context, in
757addition to the output of ``damon_aggregated`` tracepoint.
758