design.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======
  3. Design
  4. ======
  5. Configurable Layers
  6. ===================
  7. DAMON provides data access monitoring functionality while making the accuracy
  8. and the overhead controllable. The fundamental access monitorings require
  9. primitives that dependent on and optimized for the target address space. On
  10. the other hand, the accuracy and overhead tradeoff mechanism, which is the core
  11. of DAMON, is in the pure logic space. DAMON separates the two parts in
  12. different layers and defines its interface to allow various low level
  13. primitives implementations configurable with the core logic. We call the low
  14. level primitives implementations monitoring operations.
  15. Due to this separated design and the configurable interface, users can extend
  16. DAMON for any address space by configuring the core logics with appropriate
  17. monitoring operations. If appropriate one is not provided, users can implement
  18. the operations on their own.
  19. For example, physical memory, virtual memory, swap space, those for specific
  20. processes, NUMA nodes, files, and backing memory devices would be supportable.
  21. Also, if some architectures or devices support special optimized access check
  22. primitives, those will be easily configurable.
  23. Reference Implementations of Address Space Specific Monitoring Operations
  24. =========================================================================
  25. The monitoring operations are defined in two parts:
  26. 1. Identification of the monitoring target address range for the address space.
  27. 2. Access check of specific address range in the target space.
  28. DAMON currently provides the implementations of the operations for the physical
  29. and virtual address spaces. Below two subsections describe how those work.
  30. VMA-based Target Address Range Construction
  31. -------------------------------------------
  32. This is only for the virtual address space monitoring operations
  33. implementation. That for the physical address space simply asks users to
  34. manually set the monitoring target address ranges.
  35. Only small parts in the super-huge virtual address space of the processes are
  36. mapped to the physical memory and accessed. Thus, tracking the unmapped
  37. address regions is just wasteful. However, because DAMON can deal with some
  38. level of noise using the adaptive regions adjustment mechanism, tracking every
  39. mapping is not strictly required but could even incur a high overhead in some
  40. cases. That said, too huge unmapped areas inside the monitoring target should
  41. be removed to not take the time for the adaptive mechanism.
  42. For the reason, this implementation converts the complex mappings to three
  43. distinct regions that cover every mapped area of the address space. The two
  44. gaps between the three regions are the two biggest unmapped areas in the given
  45. address space. The two biggest unmapped areas would be the gap between the
  46. heap and the uppermost mmap()-ed region, and the gap between the lowermost
  47. mmap()-ed region and the stack in most of the cases. Because these gaps are
  48. exceptionally huge in usual address spaces, excluding these will be sufficient
  49. to make a reasonable trade-off. Below shows this in detail::
  50. <heap>
  51. <BIG UNMAPPED REGION 1>
  52. <uppermost mmap()-ed region>
  53. (small mmap()-ed regions and munmap()-ed regions)
  54. <lowermost mmap()-ed region>
  55. <BIG UNMAPPED REGION 2>
  56. <stack>
  57. PTE Accessed-bit Based Access Check
  58. -----------------------------------
  59. Both of the implementations for physical and virtual address spaces use PTE
  60. Accessed-bit for basic access checks. Only one difference is the way of
  61. finding the relevant PTE Accessed bit(s) from the address. While the
  62. implementation for the virtual address walks the page table for the target task
  63. of the address, the implementation for the physical address walks every page
  64. table having a mapping to the address. In this way, the implementations find
  65. and clear the bit(s) for next sampling target address and checks whether the
  66. bit(s) set again after one sampling period. This could disturb other kernel
  67. subsystems using the Accessed bits, namely Idle page tracking and the reclaim
  68. logic. DAMON does nothing to avoid disturbing Idle page tracking, so handling
  69. the interference is the responsibility of sysadmins. However, it solves the
  70. conflict with the reclaim logic using ``PG_idle`` and ``PG_young`` page flags,
  71. as Idle page tracking does.
  72. Address Space Independent Core Mechanisms
  73. =========================================
  74. Below four sections describe each of the DAMON core mechanisms and the five
  75. monitoring attributes, ``sampling interval``, ``aggregation interval``,
  76. ``update interval``, ``minimum number of regions``, and ``maximum number of
  77. regions``.
  78. Access Frequency Monitoring
  79. ---------------------------
  80. The output of DAMON says what pages are how frequently accessed for a given
  81. duration. The resolution of the access frequency is controlled by setting
  82. ``sampling interval`` and ``aggregation interval``. In detail, DAMON checks
  83. access to each page per ``sampling interval`` and aggregates the results. In
  84. other words, counts the number of the accesses to each page. After each
  85. ``aggregation interval`` passes, DAMON calls callback functions that previously
  86. registered by users so that users can read the aggregated results and then
  87. clears the results. This can be described in below simple pseudo-code::
  88. while monitoring_on:
  89. for page in monitoring_target:
  90. if accessed(page):
  91. nr_accesses[page] += 1
  92. if time() % aggregation_interval == 0:
  93. for callback in user_registered_callbacks:
  94. callback(monitoring_target, nr_accesses)
  95. for page in monitoring_target:
  96. nr_accesses[page] = 0
  97. sleep(sampling interval)
  98. The monitoring overhead of this mechanism will arbitrarily increase as the
  99. size of the target workload grows.
  100. Region Based Sampling
  101. ---------------------
  102. To avoid the unbounded increase of the overhead, DAMON groups adjacent pages
  103. that assumed to have the same access frequencies into a region. As long as the
  104. assumption (pages in a region have the same access frequencies) is kept, only
  105. one page in the region is required to be checked. Thus, for each ``sampling
  106. interval``, DAMON randomly picks one page in each region, waits for one
  107. ``sampling interval``, checks whether the page is accessed meanwhile, and
  108. increases the access frequency of the region if so. Therefore, the monitoring
  109. overhead is controllable by setting the number of regions. DAMON allows users
  110. to set the minimum and the maximum number of regions for the trade-off.
  111. This scheme, however, cannot preserve the quality of the output if the
  112. assumption is not guaranteed.
  113. Adaptive Regions Adjustment
  114. ---------------------------
  115. Even somehow the initial monitoring target regions are well constructed to
  116. fulfill the assumption (pages in same region have similar access frequencies),
  117. the data access pattern can be dynamically changed. This will result in low
  118. monitoring quality. To keep the assumption as much as possible, DAMON
  119. adaptively merges and splits each region based on their access frequency.
  120. For each ``aggregation interval``, it compares the access frequencies of
  121. adjacent regions and merges those if the frequency difference is small. Then,
  122. after it reports and clears the aggregated access frequency of each region, it
  123. splits each region into two or three regions if the total number of regions
  124. will not exceed the user-specified maximum number of regions after the split.
  125. In this way, DAMON provides its best-effort quality and minimal overhead while
  126. keeping the bounds users set for their trade-off.
  127. Dynamic Target Space Updates Handling
  128. -------------------------------------
  129. The monitoring target address range could dynamically changed. For example,
  130. virtual memory could be dynamically mapped and unmapped. Physical memory could
  131. be hot-plugged.
  132. As the changes could be quite frequent in some cases, DAMON allows the
  133. monitoring operations to check dynamic changes including memory mapping changes
  134. and applies it to monitoring operations-related data structures such as the
  135. abstracted monitoring target memory area only for each of a user-specified time
  136. interval (``update interval``).