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