Debian Codegraph @

Documentation/mm/damon/design.rst

raw
1 .. SPDX-License-Identifier: GPL-2.0
2
3 ======
4 Design
5 ======
6
7
8 .. _damon_design_execution_model_and_data_structures:
9
10 Execution Model and Data Structures
11 ===================================
12
13 The monitoring-related information including the monitoring request
14 specification and DAMON-based operation schemes are stored in a data structure
15 called DAMON ``context``. DAMON executes each context with a kernel thread
16 called ``kdamond``. Multiple kdamonds could run in parallel, for different
17 types of monitoring.
18
19 To know how user-space can do the configurations and start/stop DAMON, refer to
20 :ref:`DAMON sysfs interface <sysfs_interface>` documentation.
21
22
23 Overall Architecture
24 ====================
25
26 DAMON subsystem is configured with three layers including
27
28 - :ref:`Operations Set <damon_operations_set>`: Implements fundamental
29 operations for DAMON that depends on the given monitoring target
30 address-space and available set of software/hardware primitives,
31 - :ref:`Core <damon_core_logic>`: Implements core logics including monitoring
32 overhead/accuracy control and access-aware system operations on top of the
33 operations set layer, and
34 - :ref:`Modules <damon_modules>`: Implements kernel modules for various
35 purposes that provides interfaces for the user space, on top of the core
36 layer.
37
38
39 .. _damon_operations_set:
40
41 Operations Set Layer
42 ====================
43
44 .. _damon_design_configurable_operations_set:
45
46 For data access monitoring and additional low level work, DAMON needs a set of
47 implementations for specific operations that are dependent on and optimized for
48 the given target address space. For example, below two operations for access
49 monitoring are address-space dependent.
50
51 1. Identification of the monitoring target address range for the address space.
52 2. Access check of specific address range in the target space.
53
54 DAMON consolidates these implementations in a layer called DAMON Operations
55 Set, and defines the interface between it and the upper layer. The upper layer
56 is dedicated for DAMON's core logics including the mechanism for control of the
57 monitoring accuracy and the overhead.
58
59 Hence, DAMON can easily be extended for any address space and/or available
60 hardware features by configuring the core logic to use the appropriate
61 operations set. If there is no available operations set for a given purpose, a
62 new operations set can be implemented following the interface between the
63 layers.
64
65 For example, physical memory, virtual memory, swap space, those for specific
66 processes, NUMA nodes, files, and backing memory devices would be supportable.
67 Also, if some architectures or devices support special optimized access check
68 features, those will be easily configurable.
69
70 DAMON currently provides below three operation sets. Below three subsections
71 describe how those work.
72
73 - vaddr: Monitor virtual address spaces of specific processes
74 - fvaddr: Monitor fixed virtual address ranges
75 - paddr: Monitor the physical address space of the system
76
77 To know how user-space can do the configuration via :ref:`DAMON sysfs interface
78 <sysfs_interface>`, refer to :ref:`operations <sysfs_context>` file part of the
79 documentation.
80
81
82 .. _damon_design_vaddr_target_regions_construction:
83
84 VMA-based Target Address Range Construction
85 -------------------------------------------
86
87 A mechanism of ``vaddr`` DAMON operations set that automatically initializes
88 and updates the monitoring target address regions so that entire memory
89 mappings of the target processes can be covered.
90
91 This mechanism is only for the ``vaddr`` operations set. In cases of
92 ``fvaddr`` and ``paddr`` operation sets, users are asked to manually set the
93 monitoring target address ranges.
94
95 Only small parts in the super-huge virtual address space of the processes are
96 mapped to the physical memory and accessed. Thus, tracking the unmapped
97 address regions is just wasteful. However, because DAMON can deal with some
98 level of noise using the adaptive regions adjustment mechanism, tracking every
99 mapping is not strictly required but could even incur a high overhead in some
100 cases. That said, too huge unmapped areas inside the monitoring target should
101 be removed to not take the time for the adaptive mechanism.
102
103 For the reason, this implementation converts the complex mappings to three
104 distinct regions that cover every mapped area of the address space. The two
105 gaps between the three regions are the two biggest unmapped areas in the given
106 address space. The two biggest unmapped areas would be the gap between the
107 heap and the uppermost mmap()-ed region, and the gap between the lowermost
108 mmap()-ed region and the stack in most of the cases. Because these gaps are
109 exceptionally huge in usual address spaces, excluding these will be sufficient
110 to make a reasonable trade-off. Below shows this in detail::
111
112 <heap>
113 <BIG UNMAPPED REGION 1>
114 <uppermost mmap()-ed region>
115 (small mmap()-ed regions and munmap()-ed regions)
116 <lowermost mmap()-ed region>
117 <BIG UNMAPPED REGION 2>
118 <stack>
119
120
121 PTE Accessed-bit Based Access Check
122 -----------------------------------
123
124 Both of the implementations for physical and virtual address spaces use PTE
125 Accessed-bit for basic access checks. Only one difference is the way of
126 finding the relevant PTE Accessed bit(s) from the address. While the
127 implementation for the virtual address walks the page table for the target task
128 of the address, the implementation for the physical address walks every page
129 table having a mapping to the address. In this way, the implementations find
130 and clear the bit(s) for next sampling target address and checks whether the
131 bit(s) set again after one sampling period. This could disturb other kernel
132 subsystems using the Accessed bits, namely Idle page tracking and the reclaim
133 logic. DAMON does nothing to avoid disturbing Idle page tracking, so handling
134 the interference is the responsibility of sysadmins. However, it solves the
135 conflict with the reclaim logic using ``PG_idle`` and ``PG_young`` page flags,
136 as Idle page tracking does.
137
138 .. _damon_design_addr_unit:
139
140 Address Unit
141 ------------
142
143 DAMON core layer uses ``unsinged long`` type for monitoring target address
144 ranges. In some cases, the address space for a given operations set could be
145 too large to be handled with the type. ARM (32-bit) with large physical
146 address extension is an example. For such cases, a per-operations set
147 parameter called ``address unit`` is provided. It represents the scale factor
148 that need to be multiplied to the core layer's address for calculating real
149 address on the given address space. Support of ``address unit`` parameter is
150 up to each operations set implementation. ``paddr`` is the only operations set
151 implementation that supports the parameter.
152
153 .. _damon_core_logic:
154
155 Core Logics
156 ===========
157
158 .. _damon_design_monitoring:
159
160 Monitoring
161 ----------
162
163 Below four sections describe each of the DAMON core mechanisms and the five
164 monitoring attributes, ``sampling interval``, ``aggregation interval``,
165 ``update interval``, ``minimum number of regions``, and ``maximum number of
166 regions``.
167
168 To know how user-space can set the attributes via :ref:`DAMON sysfs interface
169 <sysfs_interface>`, refer to :ref:`monitoring_attrs <sysfs_monitoring_attrs>`
170 part of the documentation.
171
172
173 Access Frequency Monitoring
174 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
175
176 The output of DAMON says what pages are how frequently accessed for a given
177 duration. The resolution of the access frequency is controlled by setting
178 ``sampling interval`` and ``aggregation interval``. In detail, DAMON checks
179 access to each page per ``sampling interval`` and aggregates the results. In
180 other words, counts the number of the accesses to each page. After each
181 ``aggregation interval`` passes, DAMON calls callback functions that previously
182 registered by users so that users can read the aggregated results and then
183 clears the results. This can be described in below simple pseudo-code::
184
185 while monitoring_on:
186 for page in monitoring_target:
187 if accessed(page):
188 nr_accesses[page] += 1
189 if time() % aggregation_interval == 0:
190 for callback in user_registered_callbacks:
191 callback(monitoring_target, nr_accesses)
192 for page in monitoring_target:
193 nr_accesses[page] = 0
194 sleep(sampling interval)
195
196 The monitoring overhead of this mechanism will arbitrarily increase as the
197 size of the target workload grows.
198
199
200 .. _damon_design_region_based_sampling:
201
202 Region Based Sampling
203 ~~~~~~~~~~~~~~~~~~~~~
204
205 To avoid the unbounded increase of the overhead, DAMON groups adjacent pages
206 that assumed to have the same access frequencies into a region. As long as the
207 assumption (pages in a region have the same access frequencies) is kept, only
208 one page in the region is required to be checked. Thus, for each ``sampling
209 interval``, DAMON randomly picks one page in each region, waits for one
210 ``sampling interval``, checks whether the page is accessed meanwhile, and
211 increases the access frequency counter of the region if so. The counter is
212 called ``nr_accesses`` of the region. Therefore, the monitoring overhead is
213 controllable by setting the number of regions. DAMON allows users to set the
214 minimum and the maximum number of regions for the trade-off.
215
216 This scheme, however, cannot preserve the quality of the output if the
217 assumption is not guaranteed.
218
219
220 .. _damon_design_adaptive_regions_adjustment:
221
222 Adaptive Regions Adjustment
223 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
224
225 Even somehow the initial monitoring target regions are well constructed to
226 fulfill the assumption (pages in same region have similar access frequencies),
227 the data access pattern can be dynamically changed. This will result in low
228 monitoring quality. To keep the assumption as much as possible, DAMON
229 adaptively merges and splits each region based on their access frequency.
230
231 For each ``aggregation interval``, it compares the access frequencies
232 (``nr_accesses``) of adjacent regions. If the difference is small, and if the
233 sum of the two regions' sizes is smaller than the size of total regions divided
234 by the ``minimum number of regions``, DAMON merges the two regions. If the
235 resulting number of total regions is still higher than ``maximum number of
236 regions``, it repeats the merging with increasing access frequenceis difference
237 threshold until the upper-limit of the number of regions is met, or the
238 threshold becomes higher than possible maximum value (``aggregation interval``
239 divided by ``sampling interval``). Then, after it reports and clears the
240 aggregated access frequency of each region, it splits each region into two or
241 three regions if the total number of regions will not exceed the user-specified
242 maximum number of regions after the split.
243
244 In this way, DAMON provides its best-effort quality and minimal overhead while
245 keeping the bounds users set for their trade-off.
246
247
248 .. _damon_design_age_tracking:
249
250 Age Tracking
251 ~~~~~~~~~~~~
252
253 By analyzing the monitoring results, users can also find how long the current
254 access pattern of a region has maintained. That could be used for good
255 understanding of the access pattern. For example, page placement algorithm
256 utilizing both the frequency and the recency could be implemented using that.
257 To make such access pattern maintained period analysis easier, DAMON maintains
258 yet another counter called ``age`` in each region. For each ``aggregation
259 interval``, DAMON checks if the region's size and access frequency
260 (``nr_accesses``) has significantly changed. If so, the counter is reset to
261 zero. Otherwise, the counter is increased.
262
263
264 Dynamic Target Space Updates Handling
265 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
266
267 The monitoring target address range could dynamically changed. For example,
268 virtual memory could be dynamically mapped and unmapped. Physical memory could
269 be hot-plugged.
270
271 As the changes could be quite frequent in some cases, DAMON allows the
272 monitoring operations to check dynamic changes including memory mapping changes
273 and applies it to monitoring operations-related data structures such as the
274 abstracted monitoring target memory area only for each of a user-specified time
275 interval (``update interval``).
276
277 User-space can get the monitoring results via DAMON sysfs interface and/or
278 tracepoints. For more details, please refer to the documentations for
279 :ref:`DAMOS tried regions <sysfs_schemes_tried_regions>` and :ref:`tracepoint`,
280 respectively.
281
282
283 .. _damon_design_monitoring_params_tuning_guide:
284
285 Monitoring Parameters Tuning Guide
286 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
287
288 In short, set ``aggregation interval`` to capture meaningful amount of accesses
289 for the purpose. The amount of accesses can be measured using ``nr_accesses``
290 and ``age`` of regions in the aggregated monitoring results snapshot. The
291 default value of the interval, ``100ms``, turns out to be too short in many
292 cases. Set ``sampling interval`` proportional to ``aggregation interval``. By
293 default, ``1/20`` is recommended as the ratio.
294
295 ``Aggregation interval`` should be set as the time interval that the workload
296 can make an amount of accesses for the monitoring purpose, within the interval.
297 If the interval is too short, only small number of accesses are captured. As a
298 result, the monitoring results look everything is samely accessed only rarely.
299 For many purposes, that would be useless. If it is too long, however, the time
300 to converge regions with the :ref:`regions adjustment mechanism
301 <damon_design_adaptive_regions_adjustment>` can be too long, depending on the
302 time scale of the given purpose. This could happen if the workload is actually
303 making only rare accesses but the user thinks the amount of accesses for the
304 monitoring purpose too high. For such cases, the target amount of access to
305 capture per ``aggregation interval`` should carefully reconsidered. Also, note
306 that the captured amount of accesses is represented with not only
307 ``nr_accesses``, but also ``age``. For example, even if every region on the
308 monitoring results show zero ``nr_accesses``, regions could still be
309 distinguished using ``age`` values as the recency information.
310
311 Hence the optimum value of ``aggregation interval`` depends on the access
312 intensiveness of the workload. The user should tune the interval based on the
313 amount of access that captured on each aggregated snapshot of the monitoring
314 results.
315
316 Note that the default value of the interval is 100 milliseconds, which is too
317 short in many cases, especially on large systems.
318
319 ``Sampling interval`` defines the resolution of each aggregation. If it is set
320 too large, monitoring results will look like every region was samely rarely
321 accessed, or samely frequently accessed. That is, regions become
322 undistinguishable based on access pattern, and therefore the results will be
323 useless in many use cases. If ``sampling interval`` is too small, it will not
324 degrade the resolution, but will increase the monitoring overhead. If it is
325 appropriate enough to provide a resolution of the monitoring results that
326 sufficient for the given purpose, it shouldn't be unnecessarily further
327 lowered. It is recommended to be set proportional to ``aggregation interval``.
328 By default, the ratio is set as ``1/20``, and it is still recommended.
329
330 Based on the manual tuning guide, DAMON provides more intuitive knob-based
331 intervals auto tuning mechanism. Please refer to :ref:`the design document of
332 the feature <damon_design_monitoring_intervals_autotuning>` for detail.
333
334 Refer to below documents for an example tuning based on the above guide.
335
336 .. toctree::
337 :maxdepth: 1
338
339 monitoring_intervals_tuning_example
340
341
342 .. _damon_design_monitoring_intervals_autotuning:
343
344 Monitoring Intervals Auto-tuning
345 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
346
347 DAMON provides automatic tuning of the ``sampling interval`` and ``aggregation
348 interval`` based on the :ref:`the tuning guide idea
349 <damon_design_monitoring_params_tuning_guide>`. The tuning mechanism allows
350 users to set the aimed amount of access events to observe via DAMON within
351 given time interval. The target can be specified by the user as a ratio of
352 DAMON-observed access events to the theoretical maximum amount of the events
353 (``access_bp``) that measured within a given number of aggregations
354 (``aggrs``).
355
356 The DAMON-observed access events are calculated in byte granularity based on
357 DAMON :ref:`region assumption <damon_design_region_based_sampling>`. For
358 example, if a region of size ``X`` bytes of ``Y`` ``nr_accesses`` is found, it
359 means ``X * Y`` access events are observed by DAMON. Theoretical maximum
360 access events for the region is calculated in same way, but replacing ``Y``
361 with theoretical maximum ``nr_accesses``, which can be calculated as
362 ``aggregation interval / sampling interval``.
363
364 The mechanism calculates the ratio of access events for ``aggrs`` aggregations,
365 and increases or decrease the ``sampleing interval`` and ``aggregation
366 interval`` in same ratio, if the observed access ratio is lower or higher than
367 the target, respectively. The ratio of the intervals change is decided in
368 proportion to the distance between current samples ratio and the target ratio.
369
370 The user can further set the minimum and maximum ``sampling interval`` that can
371 be set by the tuning mechanism using two parameters (``min_sample_us`` and
372 ``max_sample_us``). Because the tuning mechanism changes ``sampling interval``
373 and ``aggregation interval`` in same ratio always, the minimum and maximum
374 ``aggregation interval`` after each of the tuning changes can automatically set
375 together.
376
377 The tuning is turned off by default, and need to be set explicitly by the user.
378 As a rule of thumbs and the Parreto principle, 4% access samples ratio target
379 is recommended. Note that Parreto principle (80/20 rule) has applied twice.
380 That is, assumes 4% (20% of 20%) DAMON-observed access events ratio (source)
381 to capture 64% (80% multipled by 80%) real access events (outcomes).
382
383 To know how user-space can use this feature via :ref:`DAMON sysfs interface
384 <sysfs_interface>`, refer to :ref:`intervals_goal
385 <damon_usage_sysfs_monitoring_intervals_goal>` part of the documentation.
386
387
388 .. _damon_design_damos:
389
390 Operation Schemes
391 -----------------
392
393 One common purpose of data access monitoring is access-aware system efficiency
394 optimizations. For example,
395
396 paging out memory regions that are not accessed for more than two minutes
397
398 or
399
400 using THP for memory regions that are larger than 2 MiB and showing a high
401 access frequency for more than one minute.
402
403 One straightforward approach for such schemes would be profile-guided
404 optimizations. That is, getting data access monitoring results of the
405 workloads or the system using DAMON, finding memory regions of special
406 characteristics by profiling the monitoring results, and making system
407 operation changes for the regions. The changes could be made by modifying or
408 providing advice to the software (the application and/or the kernel), or
409 reconfiguring the hardware. Both offline and online approaches could be
410 available.
411
412 Among those, providing advice to the kernel at runtime would be flexible and
413 effective, and therefore widely be used. However, implementing such schemes
414 could impose unnecessary redundancy and inefficiency. The profiling could be
415 redundant if the type of interest is common. Exchanging the information
416 including monitoring results and operation advice between kernel and user
417 spaces could be inefficient.
418
419 To allow users to reduce such redundancy and inefficiencies by offloading the
420 works, DAMON provides a feature called Data Access Monitoring-based Operation
421 Schemes (DAMOS). It lets users specify their desired schemes at a high
422 level. For such specifications, DAMON starts monitoring, finds regions having
423 the access pattern of interest, and applies the user-desired operation actions
424 to the regions, for every user-specified time interval called
425 ``apply_interval``.
426
427 To know how user-space can set ``apply_interval`` via :ref:`DAMON sysfs
428 interface <sysfs_interface>`, refer to :ref:`apply_interval_us <sysfs_scheme>`
429 part of the documentation.
430
431
432 .. _damon_design_damos_action:
433
434 Operation Action
435 ~~~~~~~~~~~~~~~~
436
437 The management action that the users desire to apply to the regions of their
438 interest. For example, paging out, prioritizing for next reclamation victim
439 selection, advising ``khugepaged`` to collapse or split, or doing nothing but
440 collecting statistics of the regions.
441
442 The list of supported actions is defined in DAMOS, but the implementation of
443 each action is in the DAMON operations set layer because the implementation
444 normally depends on the monitoring target address space. For example, the code
445 for paging specific virtual address ranges out would be different from that for
446 physical address ranges. And the monitoring operations implementation sets are
447 not mandated to support all actions of the list. Hence, the availability of
448 specific DAMOS action depends on what operations set is selected to be used
449 together.
450
451 The list of the supported actions, their meaning, and DAMON operations sets
452 that supports each action are as below.
453
454 - ``willneed``: Call ``madvise()`` for the region with ``MADV_WILLNEED``.
455 Supported by ``vaddr`` and ``fvaddr`` operations set.
456 - ``cold``: Call ``madvise()`` for the region with ``MADV_COLD``.
457 Supported by ``vaddr`` and ``fvaddr`` operations set.
458 - ``pageout``: Reclaim the region.
459 Supported by ``vaddr``, ``fvaddr`` and ``paddr`` operations set.
460 - ``hugepage``: Call ``madvise()`` for the region with ``MADV_HUGEPAGE``.
461 Supported by ``vaddr`` and ``fvaddr`` operations set.
462 - ``nohugepage``: Call ``madvise()`` for the region with ``MADV_NOHUGEPAGE``.
463 Supported by ``vaddr`` and ``fvaddr`` operations set.
464 - ``lru_prio``: Prioritize the region on its LRU lists.
465 Supported by ``paddr`` operations set.
466 - ``lru_deprio``: Deprioritize the region on its LRU lists.
467 Supported by ``paddr`` operations set.
468 - ``migrate_hot``: Migrate the regions prioritizing warmer regions.
469 Supported by ``vaddr``, ``fvaddr`` and ``paddr`` operations set.
470 - ``migrate_cold``: Migrate the regions prioritizing colder regions.
471 Supported by ``vaddr``, ``fvaddr`` and ``paddr`` operations set.
472 - ``stat``: Do nothing but count the statistics.
473 Supported by all operations sets.
474
475 Applying the actions except ``stat`` to a region is considered as changing the
476 region's characteristics. Hence, DAMOS resets the age of regions when any such
477 actions are applied to those.
478
479 To know how user-space can set the action via :ref:`DAMON sysfs interface
480 <sysfs_interface>`, refer to :ref:`action <sysfs_scheme>` part of the
481 documentation.
482
483
484 .. _damon_design_damos_access_pattern:
485
486 Target Access Pattern
487 ~~~~~~~~~~~~~~~~~~~~~
488
489 The access pattern of the schemes' interest. The patterns are constructed with
490 the properties that DAMON's monitoring results provide, specifically the size,
491 the access frequency, and the age. Users can describe their access pattern of
492 interest by setting minimum and maximum values of the three properties. If a
493 region's three properties are in the ranges, DAMOS classifies it as one of the
494 regions that the scheme is having an interest in.
495
496 To know how user-space can set the access pattern via :ref:`DAMON sysfs
497 interface <sysfs_interface>`, refer to :ref:`access_pattern
498 <sysfs_access_pattern>` part of the documentation.
499
500
501 .. _damon_design_damos_quotas:
502
503 Quotas
504 ~~~~~~
505
506 DAMOS upper-bound overhead control feature. DAMOS could incur high overhead if
507 the target access pattern is not properly tuned. For example, if a huge memory
508 region having the access pattern of interest is found, applying the scheme's
509 action to all pages of the huge region could consume unacceptably large system
510 resources. Preventing such issues by tuning the access pattern could be
511 challenging, especially if the access patterns of the workloads are highly
512 dynamic.
513
514 To mitigate that situation, DAMOS provides an upper-bound overhead control
515 feature called quotas. It lets users specify an upper limit of time that DAMOS
516 can use for applying the action, and/or a maximum bytes of memory regions that
517 the action can be applied within a user-specified time duration.
518
519 To know how user-space can set the basic quotas via :ref:`DAMON sysfs interface
520 <sysfs_interface>`, refer to :ref:`quotas <sysfs_quotas>` part of the
521 documentation.
522
523
524 .. _damon_design_damos_quotas_prioritization:
525
526 Prioritization
527 ^^^^^^^^^^^^^^
528
529 A mechanism for making a good decision under the quotas. When the action
530 cannot be applied to all regions of interest due to the quotas, DAMOS
531 prioritizes regions and applies the action to only regions having high enough
532 priorities so that it will not exceed the quotas.
533
534 The prioritization mechanism should be different for each action. For example,
535 rarely accessed (colder) memory regions would be prioritized for page-out
536 scheme action. In contrast, the colder regions would be deprioritized for huge
537 page collapse scheme action. Hence, the prioritization mechanisms for each
538 action are implemented in each DAMON operations set, together with the actions.
539
540 Though the implementation is up to the DAMON operations set, it would be common
541 to calculate the priority using the access pattern properties of the regions.
542 Some users would want the mechanisms to be personalized for their specific
543 case. For example, some users would want the mechanism to weigh the recency
544 (``age``) more than the access frequency (``nr_accesses``). DAMOS allows users
545 to specify the weight of each access pattern property and passes the
546 information to the underlying mechanism. Nevertheless, how and even whether
547 the weight will be respected are up to the underlying prioritization mechanism
548 implementation.
549
550 To know how user-space can set the prioritization weights via :ref:`DAMON sysfs
551 interface <sysfs_interface>`, refer to :ref:`weights <sysfs_quotas>` part of
552 the documentation.
553
554
555 .. _damon_design_damos_quotas_auto_tuning:
556
557 Aim-oriented Feedback-driven Auto-tuning
558 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
559
560 Automatic feedback-driven quota tuning. Instead of setting the absolute quota
561 value, users can specify the metric of their interest, and what target value
562 they want the metric value to be. DAMOS then automatically tunes the
563 aggressiveness (the quota) of the corresponding scheme. For example, if DAMOS
564 is under achieving the goal, DAMOS automatically increases the quota. If DAMOS
565 is over achieving the goal, it decreases the quota.
566
567 The goal can be specified with five parameters, namely ``target_metric``,
568 ``target_value``, ``current_value``, ``nid`` and ``path``. The auto-tuning
569 mechanism tries to make ``current_value`` of ``target_metric`` be same to
570 ``target_value``.
571
572 - ``user_input``: User-provided value. Users could use any metric that they
573 has interest in for the value. Use space main workload's latency or
574 throughput, system metrics like free memory ratio or memory pressure stall
575 time (PSI) could be examples. Note that users should explicitly set
576 ``current_value`` on their own in this case. In other words, users should
577 repeatedly provide the feedback.
578 - ``some_mem_psi_us``: System-wide ``some`` memory pressure stall information
579 in microseconds that measured from last quota reset to next quota reset.
580 DAMOS does the measurement on its own, so only ``target_value`` need to be
581 set by users at the initial time. In other words, DAMOS does self-feedback.
582 - ``node_mem_used_bp``: Specific NUMA node's used memory ratio in bp (1/10,000).
583 - ``node_mem_free_bp``: Specific NUMA node's free memory ratio in bp (1/10,000).
584 - ``node_memcg_used_bp``: Specific cgroup's node used memory ratio for a
585 specific NUMA node, in bp (1/10,000).
586 - ``node_memcg_free_bp``: Specific cgroup's node unused memory ratio for a
587 specific NUMA node, in bp (1/10,000).
588 - ``active_mem_bp``: Active to active + inactive (LRU) memory size ratio in bp
589 (1/10,000).
590 - ``inactive_mem_bp``: Inactive to active + inactive (LRU) memory size ratio in
591 bp (1/10,000).
592
593 ``nid`` is optionally required for only ``node_mem_used_bp``,
594 ``node_mem_free_bp``, ``node_memcg_used_bp`` and ``node_memcg_free_bp`` to
595 point the specific NUMA node.
596
597 ``path`` is optionally required for only ``node_memcg_used_bp`` and
598 ``node_memcg_free_bp`` to point the path to the cgroup. The value should be
599 the path of the memory cgroup from the cgroups mount point.
600
601 To know how user-space can set the tuning goal metric, the target value, and/or
602 the current value via :ref:`DAMON sysfs interface <sysfs_interface>`, refer to
603 :ref:`quota goals <sysfs_schemes_quota_goals>` part of the documentation.
604
605
606 .. _damon_design_damos_watermarks:
607
608 Watermarks
609 ~~~~~~~~~~
610
611 Conditional DAMOS (de)activation automation. Users might want DAMOS to run
612 only under certain situations. For example, when a sufficient amount of free
613 memory is guaranteed, running a scheme for proactive reclamation would only
614 consume unnecessary system resources. To avoid such consumption, the user would
615 need to manually monitor some metrics such as free memory ratio, and turn
616 DAMON/DAMOS on or off.
617
618 DAMOS allows users to offload such works using three watermarks. It allows the
619 users to configure the metric of their interest, and three watermark values,
620 namely high, middle, and low. If the value of the metric becomes above the
621 high watermark or below the low watermark, the scheme is deactivated. If the
622 metric becomes below the mid watermark but above the low watermark, the scheme
623 is activated. If all schemes are deactivated by the watermarks, the monitoring
624 is also deactivated. In this case, the DAMON worker thread only periodically
625 checks the watermarks and therefore incurs nearly zero overhead.
626
627 To know how user-space can set the watermarks via :ref:`DAMON sysfs interface
628 <sysfs_interface>`, refer to :ref:`watermarks <sysfs_watermarks>` part of the
629 documentation.
630
631
632 .. _damon_design_damos_filters:
633
634 Filters
635 ~~~~~~~
636
637 Non-access pattern-based target memory regions filtering. If users run
638 self-written programs or have good profiling tools, they could know something
639 more than the kernel, such as future access patterns or some special
640 requirements for specific types of memory. For example, some users may know
641 only anonymous pages can impact their program's performance. They can also
642 have a list of latency-critical processes.
643
644 To let users optimize DAMOS schemes with such special knowledge, DAMOS provides
645 a feature called DAMOS filters. The feature allows users to set an arbitrary
646 number of filters for each scheme. Each filter specifies
647
648 - a type of memory (``type``),
649 - whether it is for the memory of the type or all except the type
650 (``matching``), and
651 - whether it is to allow (include) or reject (exclude) applying
652 the scheme's action to the memory (``allow``).
653
654 For efficient handling of filters, some types of filters are handled by the
655 core layer, while others are handled by operations set. In the latter case,
656 hence, support of the filter types depends on the DAMON operations set. In
657 case of the core layer-handled filters, the memory regions that excluded by the
658 filter are not counted as the scheme has tried to the region. In contrast, if
659 a memory regions is filtered by an operations set layer-handled filter, it is
660 counted as the scheme has tried. This difference affects the statistics.
661
662 When multiple filters are installed, the group of filters that handled by the
663 core layer are evaluated first. After that, the group of filters that handled
664 by the operations layer are evaluated. Filters in each of the groups are
665 evaluated in the installed order. If a part of memory is matched to one of the
666 filter, next filters are ignored. If the part passes through the filters
667 evaluation stage because it is not matched to any of the filters, applying the
668 scheme's action to it depends on the last filter's allowance type. If the last
669 filter was for allowing, the part of memory will be rejected, and vice versa.
670
671 For example, let's assume 1) a filter for allowing anonymous pages and 2)
672 another filter for rejecting young pages are installed in the order. If a page
673 of a region that eligible to apply the scheme's action is an anonymous page,
674 the scheme's action will be applied to the page regardless of whether it is
675 young or not, since it matches with the first allow-filter. If the page is
676 not anonymous but young, the scheme's action will not be applied, since the
677 second reject-filter blocks it. If the page is neither anonymous nor young,
678 the page will pass through the filters evaluation stage since there is no
679 matching filter, and the action will be applied to the page.
680
681 Below ``type`` of filters are currently supported.
682
683 - Core layer handled
684 - addr
685 - Applied to pages that belonging to a given address range.
686 - target
687 - Applied to pages that belonging to a given DAMON monitoring target.
688 - Operations layer handled, supported by only ``paddr`` operations set.
689 - anon
690 - Applied to pages that containing data that not stored in files.
691 - active
692 - Applied to active pages.
693 - memcg
694 - Applied to pages that belonging to a given cgroup.
695 - young
696 - Applied to pages that are accessed after the last access check from the
697 scheme.
698 - hugepage_size
699 - Applied to pages that managed in a given size range.
700 - unmapped
701 - Applied to pages that unmapped.
702
703 To know how user-space can set the filters via :ref:`DAMON sysfs interface
704 <sysfs_interface>`, refer to :ref:`filters <sysfs_filters>` part of the
705 documentation.
706
707 .. _damon_design_damos_stat:
708
709 Statistics
710 ~~~~~~~~~~
711
712 The statistics of DAMOS behaviors that designed to help monitoring, tuning and
713 debugging of DAMOS.
714
715 DAMOS accounts below statistics for each scheme, from the beginning of the
716 scheme's execution.
717
718 - ``nr_tried``: Total number of regions that the scheme is tried to be applied.
719 - ``sz_tried``: Total size of regions that the scheme is tried to be applied.
720 - ``sz_ops_filter_passed``: Total bytes that passed operations set
721 layer-handled DAMOS filters.
722 - ``nr_applied``: Total number of regions that the scheme is applied.
723 - ``sz_applied``: Total size of regions that the scheme is applied.
724 - ``qt_exceeds``: Total number of times the quota of the scheme has exceeded.
725 - ``nr_snapshots``: Total number of DAMON snapshots that the scheme is tried to
726 be applied.
727 - ``max_nr_snapshots``: Upper limit of ``nr_snapshots``.
728
729 "A scheme is tried to be applied to a region" means DAMOS core logic determined
730 the region is eligible to apply the scheme's :ref:`action
731 <damon_design_damos_action>`. The :ref:`access pattern
732 <damon_design_damos_access_pattern>`, :ref:`quotas
733 <damon_design_damos_quotas>`, :ref:`watermarks
734 <damon_design_damos_watermarks>`, and :ref:`filters
735 <damon_design_damos_filters>` that handled on core logic could affect this.
736 The core logic will only ask the underlying :ref:`operation set
737 <damon_operations_set>` to do apply the action to the region, so whether the
738 action is really applied or not is unclear. That's why it is called "tried".
739
740 "A scheme is applied to a region" means the :ref:`operation set
741 <damon_operations_set>` has applied the action to at least a part of the
742 region. The :ref:`filters <damon_design_damos_filters>` that handled by the
743 operation set, and the types of the :ref:`action <damon_design_damos_action>`
744 and the pages of the region can affect this. For example, if a filter is set
745 to exclude anonymous pages and the region has only anonymous pages, or if the
746 action is ``pageout`` while all pages of the region are unreclaimable, applying
747 the action to the region will fail.
748
749 Unlike normal stats, ``max_nr_snapshots`` is set by users. If it is set as
750 non-zero and ``nr_snapshots`` be same to or greater than ``nr_snapshots``, the
751 scheme is deactivated.
752
753 To know how user-space can read the stats via :ref:`DAMON sysfs interface
754 <sysfs_interface>`, refer to :ref:s`stats <sysfs_stats>` part of the
755 documentation.
756
757 Regions Walking
758 ~~~~~~~~~~~~~~~
759
760 DAMOS feature allowing users access each region that a DAMOS action has just
761 applied. Using this feature, DAMON :ref:`API <damon_design_api>` allows users
762 access full properties of the regions including the access monitoring results
763 and amount of the region's internal memory that passed the DAMOS filters.
764 :ref:`DAMON sysfs interface <sysfs_interface>` also allows users read the data
765 via special :ref:`files <sysfs_schemes_tried_regions>`.
766
767 .. _damon_design_api:
768
769 Application Programming Interface
770 ---------------------------------
771
772 The programming interface for kernel space data access-aware applications.
773 DAMON is a framework, so it does nothing by itself. Instead, it only helps
774 other kernel components such as subsystems and modules building their data
775 access-aware applications using DAMON's core features. For this, DAMON exposes
776 its all features to other kernel components via its application programming
777 interface, namely ``include/linux/damon.h``. Please refer to the API
778 :doc:`document </mm/damon/api>` for details of the interface.
779
780
781 .. _damon_modules:
782
783 Modules
784 =======
785
786 Because the core of DAMON is a framework for kernel components, it doesn't
787 provide any direct interface for the user space. Such interfaces should be
788 implemented by each DAMON API user kernel components, instead. DAMON subsystem
789 itself implements such DAMON API user modules, which are supposed to be used
790 for general purpose DAMON control and special purpose data access-aware system
791 operations, and provides stable application binary interfaces (ABI) for the
792 user space. The user space can build their efficient data access-aware
793 applications using the interfaces.
794
795
796 General Purpose User Interface Modules
797 --------------------------------------
798
799 DAMON modules that provide user space ABIs for general purpose DAMON usage in
800 runtime.
801
802 Like many other ABIs, the modules create files on pseudo file systems like
803 'sysfs', allow users to specify their requests to and get the answers from
804 DAMON by writing to and reading from the files. As a response to such I/O,
805 DAMON user interface modules control DAMON and retrieve the results as user
806 requested via the DAMON API, and return the results to the user-space.
807
808 The ABIs are designed to be used for user space applications development,
809 rather than human beings' fingers. Human users are recommended to use such
810 user space tools. One such Python-written user space tool is available at
811 Github (https://github.com/damonitor/damo), Pypi
812 (https://pypistats.org/packages/damo), and multiple distros
813 (https://repology.org/project/damo/versions).
814
815 Currently, one module for this type, namely 'DAMON sysfs interface' is
816 available. Please refer to the ABI :ref:`doc <sysfs_interface>` for details of
817 the interfaces.
818
819
820 .. _damon_modules_special_purpose:
821
822 Special-Purpose Access-aware Kernel Modules
823 -------------------------------------------
824
825 DAMON modules that provide user space ABI for specific purpose DAMON usage.
826
827 DAMON user interface modules are for full control of all DAMON features in
828 runtime. For each special-purpose system-wide data access-aware system
829 operations such as proactive reclamation or LRU lists balancing, the interfaces
830 could be simplified by removing unnecessary knobs for the specific purpose, and
831 extended for boot-time and even compile time control. Default values of DAMON
832 control parameters for the usage would also need to be optimized for the
833 purpose.
834
835 To support such cases, yet more DAMON API user kernel modules that provide more
836 simple and optimized user space interfaces are available. Currently, two
837 modules for proactive reclamation and LRU lists manipulation are provided. For
838 more detail, please read the usage documents for those
839 (:doc:`/admin-guide/mm/damon/stat`, :doc:`/admin-guide/mm/damon/reclaim` and
840 :doc:`/admin-guide/mm/damon/lru_sort`).
841
842
843 Sample DAMON Modules
844 --------------------
845
846 DAMON modules that provides example DAMON kernel API usages.
847
848 kernel programmers can build their own special or general purpose DAMON modules
849 using DAMON kernel API. To help them easily understand how DAMON kernel API
850 can be used, a few sample modules are provided under ``samples/damon/`` of the
851 linux source tree. Please note that these modules are not developed for being
852 used on real products, but only for showing how DAMON kernel API can be used in
853 simple ways.