usage.rst 37 KB

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