xref: /linux/Documentation/admin-guide/mm/damon/usage.rst (revision 4c1f287f8c80268a870d8a51012ac4adf8fcbec5)
1c4ba6014SSeongJae Park.. SPDX-License-Identifier: GPL-2.0
2c4ba6014SSeongJae Park
3c4ba6014SSeongJae Park===============
4c4ba6014SSeongJae ParkDetailed Usages
5c4ba6014SSeongJae Park===============
6c4ba6014SSeongJae Park
7c4ba6014SSeongJae ParkDAMON provides below three interfaces for different users.
8c4ba6014SSeongJae Park
9c4ba6014SSeongJae Park- *DAMON user space tool.*
1035b43d40SSeongJae Park  `This <https://github.com/awslabs/damo>`_ is for privileged people such as
1135b43d40SSeongJae Park  system administrators who want a just-working human-friendly interface.
1235b43d40SSeongJae Park  Using this, users can use the DAMON’s major features in a human-friendly way.
1335b43d40SSeongJae Park  It may not be highly tuned for special cases, though.  It supports both
1435b43d40SSeongJae Park  virtual and physical address spaces monitoring.  For more detail, please
1535b43d40SSeongJae Park  refer to its `usage document
1635b43d40SSeongJae Park  <https://github.com/awslabs/damo/blob/next/USAGE.md>`_.
17c4ba6014SSeongJae Park- *debugfs interface.*
1835b43d40SSeongJae Park  :ref:`This <debugfs_interface>` is for privileged user space programmers who
1935b43d40SSeongJae Park  want more optimized use of DAMON.  Using this, users can use DAMON’s major
2035b43d40SSeongJae Park  features by reading from and writing to special debugfs files.  Therefore,
2135b43d40SSeongJae Park  you can write and use your personalized DAMON debugfs wrapper programs that
2235b43d40SSeongJae Park  reads/writes the debugfs files instead of you.  The `DAMON user space tool
2335b43d40SSeongJae Park  <https://github.com/awslabs/damo>`_ is one example of such programs.  It
244492bf45SSeongJae Park  supports both virtual and physical address spaces monitoring.  Note that this
254492bf45SSeongJae Park  interface provides only simple :ref:`statistics <damos_stats>` for the
264492bf45SSeongJae Park  monitoring results.  For detailed monitoring results, DAMON provides a
274492bf45SSeongJae Park  :ref:`tracepoint <tracepoint>`.
28c4ba6014SSeongJae Park- *Kernel Space Programming Interface.*
2935b43d40SSeongJae Park  :doc:`This </vm/damon/api>` is for kernel space programmers.  Using this,
3035b43d40SSeongJae Park  users can utilize every feature of DAMON most flexibly and efficiently by
3135b43d40SSeongJae Park  writing kernel space DAMON application programs for you.  You can even extend
3235b43d40SSeongJae Park  DAMON for various address spaces.  For detail, please refer to the interface
3335b43d40SSeongJae Park  :doc:`document </vm/damon/api>`.
34c4ba6014SSeongJae Park
3535b43d40SSeongJae Park
3635b43d40SSeongJae Park.. _debugfs_interface:
37c4ba6014SSeongJae Park
38c4ba6014SSeongJae Parkdebugfs Interface
39c4ba6014SSeongJae Park=================
40c4ba6014SSeongJae Park
41995d739cSSeongJae ParkDAMON exports eight files, ``attrs``, ``target_ids``, ``init_regions``,
42995d739cSSeongJae Park``schemes``, ``monitor_on``, ``kdamond_pid``, ``mk_contexts`` and
43995d739cSSeongJae Park``rm_contexts`` under its debugfs directory, ``<debugfs>/damon/``.
44c4ba6014SSeongJae Park
45c4ba6014SSeongJae Park
46c4ba6014SSeongJae ParkAttributes
47c4ba6014SSeongJae Park----------
48c4ba6014SSeongJae Park
49c4ba6014SSeongJae ParkUsers can get and set the ``sampling interval``, ``aggregation interval``,
50*4c1f287fSSeongJae Park``update interval``, and min/max number of monitoring target regions by
51c4ba6014SSeongJae Parkreading from and writing to the ``attrs`` file.  To know about the monitoring
52c4ba6014SSeongJae Parkattributes in detail, please refer to the :doc:`/vm/damon/design`.  For
53c4ba6014SSeongJae Parkexample, below commands set those values to 5 ms, 100 ms, 1,000 ms, 10 and
54c4ba6014SSeongJae Park1000, and then check it again::
55c4ba6014SSeongJae Park
56c4ba6014SSeongJae Park    # cd <debugfs>/damon
57c4ba6014SSeongJae Park    # echo 5000 100000 1000000 10 1000 > attrs
58c4ba6014SSeongJae Park    # cat attrs
59c4ba6014SSeongJae Park    5000 100000 1000000 10 1000
60c4ba6014SSeongJae Park
61c4ba6014SSeongJae Park
62c4ba6014SSeongJae ParkTarget IDs
63c4ba6014SSeongJae Park----------
64c4ba6014SSeongJae Park
65c4ba6014SSeongJae ParkSome types of address spaces supports multiple monitoring target.  For example,
66c4ba6014SSeongJae Parkthe virtual memory address spaces monitoring can have multiple processes as the
67c4ba6014SSeongJae Parkmonitoring targets.  Users can set the targets by writing relevant id values of
68c4ba6014SSeongJae Parkthe targets to, and get the ids of the current targets by reading from the
69c4ba6014SSeongJae Park``target_ids`` file.  In case of the virtual address spaces monitoring, the
70c4ba6014SSeongJae Parkvalues should be pids of the monitoring target processes.  For example, below
71c4ba6014SSeongJae Parkcommands set processes having pids 42 and 4242 as the monitoring targets and
72c4ba6014SSeongJae Parkcheck it again::
73c4ba6014SSeongJae Park
74c4ba6014SSeongJae Park    # cd <debugfs>/damon
75c4ba6014SSeongJae Park    # echo 42 4242 > target_ids
76c4ba6014SSeongJae Park    # cat target_ids
77c4ba6014SSeongJae Park    42 4242
78c4ba6014SSeongJae Park
79c6380721SSeongJae ParkUsers can also monitor the physical memory address space of the system by
80c6380721SSeongJae Parkwriting a special keyword, "``paddr\n``" to the file.  Because physical address
81c6380721SSeongJae Parkspace monitoring doesn't support multiple targets, reading the file will show a
82c6380721SSeongJae Parkfake value, ``42``, as below::
83c6380721SSeongJae Park
84c6380721SSeongJae Park    # cd <debugfs>/damon
85c6380721SSeongJae Park    # echo paddr > target_ids
86c6380721SSeongJae Park    # cat target_ids
87c6380721SSeongJae Park    42
88c6380721SSeongJae Park
89c4ba6014SSeongJae ParkNote that setting the target ids doesn't start the monitoring.
90c4ba6014SSeongJae Park
91c4ba6014SSeongJae Park
92c2fe4987SSeongJae ParkInitial Monitoring Target Regions
93c2fe4987SSeongJae Park---------------------------------
94c2fe4987SSeongJae Park
95c6380721SSeongJae ParkIn case of the virtual address space monitoring, DAMON automatically sets and
96c6380721SSeongJae Parkupdates the monitoring target regions so that entire memory mappings of target
97c2fe4987SSeongJae Parkprocesses can be covered.  However, users can want to limit the monitoring
98c2fe4987SSeongJae Parkregion to specific address ranges, such as the heap, the stack, or specific
99c2fe4987SSeongJae Parkfile-mapped area.  Or, some users can know the initial access pattern of their
100c2fe4987SSeongJae Parkworkloads and therefore want to set optimal initial regions for the 'adaptive
101c2fe4987SSeongJae Parkregions adjustment'.
102c2fe4987SSeongJae Park
103c6380721SSeongJae ParkIn contrast, DAMON do not automatically sets and updates the monitoring target
104c6380721SSeongJae Parkregions in case of physical memory monitoring.  Therefore, users should set the
105c6380721SSeongJae Parkmonitoring target regions by themselves.
106c6380721SSeongJae Park
107c2fe4987SSeongJae ParkIn such cases, users can explicitly set the initial monitoring target regions
108c2fe4987SSeongJae Parkas they want, by writing proper values to the ``init_regions`` file.  Each line
109c2fe4987SSeongJae Parkof the input should represent one region in below form.::
110c2fe4987SSeongJae Park
1118041c87bSSeongJae Park    <target idx> <start address> <end address>
112c2fe4987SSeongJae Park
1138041c87bSSeongJae ParkThe ``target idx`` should be the index of the target in ``target_ids`` file,
1148041c87bSSeongJae Parkstarting from ``0``, and the regions should be passed in address order.  For
1158041c87bSSeongJae Parkexample, below commands will set a couple of address ranges, ``1-100`` and
1168041c87bSSeongJae Park``100-200`` as the initial monitoring target region of pid 42, which is the
1178041c87bSSeongJae Parkfirst one (index ``0``) in ``target_ids``, and another couple of address
1188041c87bSSeongJae Parkranges, ``20-40`` and ``50-100`` as that of pid 4242, which is the second one
1198041c87bSSeongJae Park(index ``1``) in ``target_ids``.::
120c2fe4987SSeongJae Park
121c2fe4987SSeongJae Park    # cd <debugfs>/damon
1228041c87bSSeongJae Park    # cat target_ids
1238041c87bSSeongJae Park    42 4242
1248041c87bSSeongJae Park    # echo "0   1       100
1258041c87bSSeongJae Park            0   100     200
1268041c87bSSeongJae Park            1   20      40
1278041c87bSSeongJae Park            1   50      100" > init_regions
128c2fe4987SSeongJae Park
129c2fe4987SSeongJae ParkNote that this sets the initial monitoring target regions only.  In case of
130c2fe4987SSeongJae Parkvirtual memory monitoring, DAMON will automatically updates the boundary of the
131*4c1f287fSSeongJae Parkregions after one ``update interval``.  Therefore, users should set the
132*4c1f287fSSeongJae Park``update interval`` large enough in this case, if they don't want the
133c2fe4987SSeongJae Parkupdate.
134c2fe4987SSeongJae Park
135c2fe4987SSeongJae Park
13668536f8eSSeongJae ParkSchemes
13768536f8eSSeongJae Park-------
13868536f8eSSeongJae Park
13968536f8eSSeongJae ParkFor usual DAMON-based data access aware memory management optimizations, users
14068536f8eSSeongJae Parkwould simply want the system to apply a memory management action to a memory
1416322416bSSeongJae Parkregion of a specific access pattern.  DAMON receives such formalized operation
1426322416bSSeongJae Parkschemes from the user and applies those to the target processes.
14368536f8eSSeongJae Park
14468536f8eSSeongJae ParkUsers can get and set the schemes by reading from and writing to ``schemes``
14568536f8eSSeongJae Parkdebugfs file.  Reading the file also shows the statistics of each scheme.  To
1466322416bSSeongJae Parkthe file, each of the schemes should be represented in each line in below
1476322416bSSeongJae Parkform::
14868536f8eSSeongJae Park
1496322416bSSeongJae Park    <target access pattern> <action> <quota> <watermarks>
15068536f8eSSeongJae Park
1516322416bSSeongJae ParkYou can disable schemes by simply writing an empty string to the file.
1526322416bSSeongJae Park
1536322416bSSeongJae ParkTarget Access Pattern
1546322416bSSeongJae Park~~~~~~~~~~~~~~~~~~~~~
1556322416bSSeongJae Park
1566322416bSSeongJae ParkThe ``<target access pattern>`` is constructed with three ranges in below
1576322416bSSeongJae Parkform::
1586322416bSSeongJae Park
1596322416bSSeongJae Park    min-size max-size min-acc max-acc min-age max-age
1606322416bSSeongJae Park
1616322416bSSeongJae ParkSpecifically, bytes for the size of regions (``min-size`` and ``max-size``),
1626322416bSSeongJae Parknumber of monitored accesses per aggregate interval for access frequency
1636322416bSSeongJae Park(``min-acc`` and ``max-acc``), number of aggregate intervals for the age of
1646322416bSSeongJae Parkregions (``min-age`` and ``max-age``) are specified.  Note that the ranges are
1656322416bSSeongJae Parkclosed interval.
1666322416bSSeongJae Park
1676322416bSSeongJae ParkAction
1686322416bSSeongJae Park~~~~~~
1696322416bSSeongJae Park
1706322416bSSeongJae ParkThe ``<action>`` is a predefined integer for memory management actions, which
1716322416bSSeongJae ParkDAMON will apply to the regions having the target access pattern.  The
1726322416bSSeongJae Parksupported numbers and their meanings are as below.
17368536f8eSSeongJae Park
17468536f8eSSeongJae Park - 0: Call ``madvise()`` for the region with ``MADV_WILLNEED``
17568536f8eSSeongJae Park - 1: Call ``madvise()`` for the region with ``MADV_COLD``
17668536f8eSSeongJae Park - 2: Call ``madvise()`` for the region with ``MADV_PAGEOUT``
17768536f8eSSeongJae Park - 3: Call ``madvise()`` for the region with ``MADV_HUGEPAGE``
17868536f8eSSeongJae Park - 4: Call ``madvise()`` for the region with ``MADV_NOHUGEPAGE``
17968536f8eSSeongJae Park - 5: Do nothing but count the statistics
18068536f8eSSeongJae Park
1816322416bSSeongJae ParkQuota
1826322416bSSeongJae Park~~~~~
1836322416bSSeongJae Park
1846322416bSSeongJae ParkOptimal ``target access pattern`` for each ``action`` is workload dependent, so
1856322416bSSeongJae Parknot easy to find.  Worse yet, setting a scheme of some action too aggressive
1866322416bSSeongJae Parkcan cause severe overhead.  To avoid such overhead, users can limit time and
1876322416bSSeongJae Parksize quota for the scheme via the ``<quota>`` in below form::
1886322416bSSeongJae Park
1896322416bSSeongJae Park    <ms> <sz> <reset interval> <priority weights>
1906322416bSSeongJae Park
1916322416bSSeongJae ParkThis makes DAMON to try to use only up to ``<ms>`` milliseconds for applying
1926322416bSSeongJae Parkthe action to memory regions of the ``target access pattern`` within the
1936322416bSSeongJae Park``<reset interval>`` milliseconds, and to apply the action to only up to
1946322416bSSeongJae Park``<sz>`` bytes of memory regions within the ``<reset interval>``.  Setting both
1956322416bSSeongJae Park``<ms>`` and ``<sz>`` zero disables the quota limits.
1966322416bSSeongJae Park
1976322416bSSeongJae ParkWhen the quota limit is expected to be exceeded, DAMON prioritizes found memory
1986322416bSSeongJae Parkregions of the ``target access pattern`` based on their size, access frequency,
1996322416bSSeongJae Parkand age.  For personalized prioritization, users can set the weights for the
2006322416bSSeongJae Parkthree properties in ``<priority weights>`` in below form::
2016322416bSSeongJae Park
2026322416bSSeongJae Park    <size weight> <access frequency weight> <age weight>
2036322416bSSeongJae Park
2046322416bSSeongJae ParkWatermarks
2056322416bSSeongJae Park~~~~~~~~~~
2066322416bSSeongJae Park
2076322416bSSeongJae ParkSome schemes would need to run based on current value of the system's specific
2086322416bSSeongJae Parkmetrics like free memory ratio.  For such cases, users can specify watermarks
2096322416bSSeongJae Parkfor the condition.::
2106322416bSSeongJae Park
2116322416bSSeongJae Park    <metric> <check interval> <high mark> <middle mark> <low mark>
2126322416bSSeongJae Park
2136322416bSSeongJae Park``<metric>`` is a predefined integer for the metric to be checked.  The
2146322416bSSeongJae Parksupported numbers and their meanings are as below.
2156322416bSSeongJae Park
2166322416bSSeongJae Park - 0: Ignore the watermarks
2176322416bSSeongJae Park - 1: System's free memory rate (per thousand)
2186322416bSSeongJae Park
2196322416bSSeongJae ParkThe value of the metric is checked every ``<check interval>`` microseconds.
2206322416bSSeongJae Park
2216322416bSSeongJae ParkIf the value is higher than ``<high mark>`` or lower than ``<low mark>``, the
2226322416bSSeongJae Parkscheme is deactivated.  If the value is lower than ``<mid mark>``, the scheme
2236322416bSSeongJae Parkis activated.
2246322416bSSeongJae Park
2254492bf45SSeongJae Park.. _damos_stats:
2264492bf45SSeongJae Park
2276322416bSSeongJae ParkStatistics
2286322416bSSeongJae Park~~~~~~~~~~
2296322416bSSeongJae Park
230dbcb9b9fSSeongJae ParkIt also counts the total number and bytes of regions that each scheme is tried
231dbcb9b9fSSeongJae Parkto be applied, the two numbers for the regions that each scheme is successfully
232dbcb9b9fSSeongJae Parkapplied, and the total number of the quota limit exceeds.  This statistics can
233dbcb9b9fSSeongJae Parkbe used for online analysis or tuning of the schemes.
2346322416bSSeongJae Park
2356322416bSSeongJae ParkThe statistics can be shown by reading the ``schemes`` file.  Reading the file
236dbcb9b9fSSeongJae Parkwill show each scheme you entered in each line, and the five numbers for the
2376322416bSSeongJae Parkstatistics will be added at the end of each line.
2386322416bSSeongJae Park
2396322416bSSeongJae ParkExample
2406322416bSSeongJae Park~~~~~~~
2416322416bSSeongJae Park
2426322416bSSeongJae ParkBelow commands applies a scheme saying "If a memory region of size in [4KiB,
2436322416bSSeongJae Park8KiB] is showing accesses per aggregate interval in [0, 5] for aggregate
2446322416bSSeongJae Parkinterval in [10, 20], page out the region.  For the paging out, use only up to
2456322416bSSeongJae Park10ms per second, and also don't page out more than 1GiB per second.  Under the
2466322416bSSeongJae Parklimitation, page out memory regions having longer age first.  Also, check the
2476322416bSSeongJae Parkfree memory rate of the system every 5 seconds, start the monitoring and paging
2486322416bSSeongJae Parkout when the free memory rate becomes lower than 50%, but stop it if the free
2496322416bSSeongJae Parkmemory rate becomes larger than 60%, or lower than 30%".::
25068536f8eSSeongJae Park
25168536f8eSSeongJae Park    # cd <debugfs>/damon
2526322416bSSeongJae Park    # scheme="4096 8192  0 5    10 20    2"  # target access pattern and action
2536322416bSSeongJae Park    # scheme+=" 10 $((1024*1024*1024)) 1000" # quotas
2546322416bSSeongJae Park    # scheme+=" 0 0 100"                     # prioritization weights
2556322416bSSeongJae Park    # scheme+=" 1 5000000 600 500 300"       # watermarks
2566322416bSSeongJae Park    # echo "$scheme" > schemes
25768536f8eSSeongJae Park
25868536f8eSSeongJae Park
259c4ba6014SSeongJae ParkTurning On/Off
260c4ba6014SSeongJae Park--------------
261c4ba6014SSeongJae Park
262c4ba6014SSeongJae ParkSetting the files as described above doesn't incur effect unless you explicitly
263c4ba6014SSeongJae Parkstart the monitoring.  You can start, stop, and check the current status of the
264c4ba6014SSeongJae Parkmonitoring by writing to and reading from the ``monitor_on`` file.  Writing
265c4ba6014SSeongJae Park``on`` to the file starts the monitoring of the targets with the attributes.
266c4ba6014SSeongJae ParkWriting ``off`` to the file stops those.  DAMON also stops if every target
267c4ba6014SSeongJae Parkprocess is terminated.  Below example commands turn on, off, and check the
268c4ba6014SSeongJae Parkstatus of DAMON::
269c4ba6014SSeongJae Park
270c4ba6014SSeongJae Park    # cd <debugfs>/damon
271c4ba6014SSeongJae Park    # echo on > monitor_on
272c4ba6014SSeongJae Park    # echo off > monitor_on
273c4ba6014SSeongJae Park    # cat monitor_on
274c4ba6014SSeongJae Park    off
275c4ba6014SSeongJae Park
276c4ba6014SSeongJae ParkPlease note that you cannot write to the above-mentioned debugfs files while
277c4ba6014SSeongJae Parkthe monitoring is turned on.  If you write to the files while DAMON is running,
278c4ba6014SSeongJae Parkan error code such as ``-EBUSY`` will be returned.
279c4ba6014SSeongJae Park
280c4ba6014SSeongJae Park
281995d739cSSeongJae ParkMonitoring Thread PID
282995d739cSSeongJae Park---------------------
283995d739cSSeongJae Park
284995d739cSSeongJae ParkDAMON does requested monitoring with a kernel thread called ``kdamond``.  You
285995d739cSSeongJae Parkcan get the pid of the thread by reading the ``kdamond_pid`` file.  When the
286995d739cSSeongJae Parkmonitoring is turned off, reading the file returns ``none``. ::
287995d739cSSeongJae Park
288995d739cSSeongJae Park    # cd <debugfs>/damon
289995d739cSSeongJae Park    # cat monitor_on
290995d739cSSeongJae Park    off
291995d739cSSeongJae Park    # cat kdamond_pid
292995d739cSSeongJae Park    none
293995d739cSSeongJae Park    # echo on > monitor_on
294995d739cSSeongJae Park    # cat kdamond_pid
295995d739cSSeongJae Park    18594
296995d739cSSeongJae Park
297995d739cSSeongJae Park
298995d739cSSeongJae ParkUsing Multiple Monitoring Threads
299995d739cSSeongJae Park---------------------------------
300995d739cSSeongJae Park
301995d739cSSeongJae ParkOne ``kdamond`` thread is created for each monitoring context.  You can create
302995d739cSSeongJae Parkand remove monitoring contexts for multiple ``kdamond`` required use case using
303995d739cSSeongJae Parkthe ``mk_contexts`` and ``rm_contexts`` files.
304995d739cSSeongJae Park
305995d739cSSeongJae ParkWriting the name of the new context to the ``mk_contexts`` file creates a
306995d739cSSeongJae Parkdirectory of the name on the DAMON debugfs directory.  The directory will have
307995d739cSSeongJae ParkDAMON debugfs files for the context. ::
308995d739cSSeongJae Park
309995d739cSSeongJae Park    # cd <debugfs>/damon
310995d739cSSeongJae Park    # ls foo
311995d739cSSeongJae Park    # ls: cannot access 'foo': No such file or directory
312995d739cSSeongJae Park    # echo foo > mk_contexts
313995d739cSSeongJae Park    # ls foo
314995d739cSSeongJae Park    # attrs  init_regions  kdamond_pid  schemes  target_ids
315995d739cSSeongJae Park
316995d739cSSeongJae ParkIf the context is not needed anymore, you can remove it and the corresponding
317995d739cSSeongJae Parkdirectory by putting the name of the context to the ``rm_contexts`` file. ::
318995d739cSSeongJae Park
319995d739cSSeongJae Park    # echo foo > rm_contexts
320995d739cSSeongJae Park    # ls foo
321995d739cSSeongJae Park    # ls: cannot access 'foo': No such file or directory
322995d739cSSeongJae Park
323995d739cSSeongJae ParkNote that ``mk_contexts``, ``rm_contexts``, and ``monitor_on`` files are in the
324995d739cSSeongJae Parkroot directory only.
325995d739cSSeongJae Park
326995d739cSSeongJae Park
3274492bf45SSeongJae Park.. _tracepoint:
3284492bf45SSeongJae Park
329c4ba6014SSeongJae ParkTracepoint for Monitoring Results
330c4ba6014SSeongJae Park=================================
331c4ba6014SSeongJae Park
332c4ba6014SSeongJae ParkDAMON provides the monitoring results via a tracepoint,
333c4ba6014SSeongJae Park``damon:damon_aggregated``.  While the monitoring is turned on, you could
334c4ba6014SSeongJae Parkrecord the tracepoint events and show results using tracepoint supporting tools
335c4ba6014SSeongJae Parklike ``perf``.  For example::
336c4ba6014SSeongJae Park
337c4ba6014SSeongJae Park    # echo on > monitor_on
338c4ba6014SSeongJae Park    # perf record -e damon:damon_aggregated &
339c4ba6014SSeongJae Park    # sleep 5
340c4ba6014SSeongJae Park    # kill 9 $(pidof perf)
341c4ba6014SSeongJae Park    # echo off > monitor_on
342c4ba6014SSeongJae Park    # perf script
343