Commit | Line | Data |
---|---|---|
2224d848 SP |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | /* | |
3 | * DAMON api | |
4 | * | |
5 | * Author: SeongJae Park <sjpark@amazon.de> | |
6 | */ | |
7 | ||
8 | #ifndef _DAMON_H_ | |
9 | #define _DAMON_H_ | |
10 | ||
98def236 | 11 | #include <linux/memcontrol.h> |
2224d848 SP |
12 | #include <linux/mutex.h> |
13 | #include <linux/time64.h> | |
14 | #include <linux/types.h> | |
9b2a38d6 | 15 | #include <linux/random.h> |
2224d848 | 16 | |
b9a6ac4e SP |
17 | /* Minimal region size. Every damon_region is aligned by this. */ |
18 | #define DAMON_MIN_REGION PAGE_SIZE | |
38683e00 SP |
19 | /* Max priority score for DAMON-based operation schemes */ |
20 | #define DAMOS_MAX_SCORE (99) | |
b9a6ac4e | 21 | |
9b2a38d6 | 22 | /* Get a random number in [l, r) */ |
234d6873 XH |
23 | static inline unsigned long damon_rand(unsigned long l, unsigned long r) |
24 | { | |
8032bf12 | 25 | return l + get_random_u32_below(r - l); |
234d6873 | 26 | } |
9b2a38d6 | 27 | |
f23b8eee SP |
28 | /** |
29 | * struct damon_addr_range - Represents an address region of [@start, @end). | |
30 | * @start: Start address of the region (inclusive). | |
31 | * @end: End address of the region (exclusive). | |
32 | */ | |
33 | struct damon_addr_range { | |
34 | unsigned long start; | |
35 | unsigned long end; | |
36 | }; | |
37 | ||
38 | /** | |
39 | * struct damon_region - Represents a monitoring target region. | |
40 | * @ar: The address range of the region. | |
41 | * @sampling_addr: Address of the sample for the next access check. | |
42 | * @nr_accesses: Access frequency of this region. | |
43 | * @list: List head for siblings. | |
fda504fa SP |
44 | * @age: Age of this region. |
45 | * | |
46 | * @age is initially zero, increased for each aggregation interval, and reset | |
47 | * to zero again if the access frequency is significantly changed. If two | |
48 | * regions are merged into a new region, both @nr_accesses and @age of the new | |
49 | * region are set as region size-weighted average of those of the two regions. | |
f23b8eee SP |
50 | */ |
51 | struct damon_region { | |
52 | struct damon_addr_range ar; | |
53 | unsigned long sampling_addr; | |
54 | unsigned int nr_accesses; | |
55 | struct list_head list; | |
fda504fa SP |
56 | |
57 | unsigned int age; | |
58 | /* private: Internal value for age calculation. */ | |
59 | unsigned int last_nr_accesses; | |
f23b8eee SP |
60 | }; |
61 | ||
62 | /** | |
63 | * struct damon_target - Represents a monitoring target. | |
1971bd63 | 64 | * @pid: The PID of the virtual address space to monitor. |
b9a6ac4e | 65 | * @nr_regions: Number of monitoring target regions of this target. |
f23b8eee SP |
66 | * @regions_list: Head of the monitoring target regions of this target. |
67 | * @list: List head for siblings. | |
68 | * | |
69 | * Each monitoring context could have multiple targets. For example, a context | |
70 | * for virtual memory address spaces could have multiple target processes. The | |
f7d911c3 SP |
71 | * @pid should be set for appropriate &struct damon_operations including the |
72 | * virtual address spaces monitoring operations. | |
f23b8eee SP |
73 | */ |
74 | struct damon_target { | |
1971bd63 | 75 | struct pid *pid; |
b9a6ac4e | 76 | unsigned int nr_regions; |
f23b8eee SP |
77 | struct list_head regions_list; |
78 | struct list_head list; | |
79 | }; | |
80 | ||
1f366e42 SP |
81 | /** |
82 | * enum damos_action - Represents an action of a Data Access Monitoring-based | |
83 | * Operation Scheme. | |
84 | * | |
85 | * @DAMOS_WILLNEED: Call ``madvise()`` for the region with MADV_WILLNEED. | |
86 | * @DAMOS_COLD: Call ``madvise()`` for the region with MADV_COLD. | |
87 | * @DAMOS_PAGEOUT: Call ``madvise()`` for the region with MADV_PAGEOUT. | |
88 | * @DAMOS_HUGEPAGE: Call ``madvise()`` for the region with MADV_HUGEPAGE. | |
89 | * @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE. | |
8cdcc532 | 90 | * @DAMOS_LRU_PRIO: Prioritize the region on its LRU lists. |
99cdc2cd | 91 | * @DAMOS_LRU_DEPRIO: Deprioritize the region on its LRU lists. |
2f0b548c | 92 | * @DAMOS_STAT: Do nothing but count the stat. |
5257f36e | 93 | * @NR_DAMOS_ACTIONS: Total number of DAMOS actions |
fb6f026b SP |
94 | * |
95 | * The support of each action is up to running &struct damon_operations. | |
96 | * &enum DAMON_OPS_VADDR and &enum DAMON_OPS_FVADDR supports all actions except | |
97 | * &enum DAMOS_LRU_PRIO and &enum DAMOS_LRU_DEPRIO. &enum DAMON_OPS_PADDR | |
98 | * supports only &enum DAMOS_PAGEOUT, &enum DAMOS_LRU_PRIO, &enum | |
99 | * DAMOS_LRU_DEPRIO, and &DAMOS_STAT. | |
1f366e42 SP |
100 | */ |
101 | enum damos_action { | |
102 | DAMOS_WILLNEED, | |
103 | DAMOS_COLD, | |
104 | DAMOS_PAGEOUT, | |
105 | DAMOS_HUGEPAGE, | |
106 | DAMOS_NOHUGEPAGE, | |
8cdcc532 | 107 | DAMOS_LRU_PRIO, |
99cdc2cd | 108 | DAMOS_LRU_DEPRIO, |
2f0b548c | 109 | DAMOS_STAT, /* Do nothing but only record the stat */ |
5257f36e | 110 | NR_DAMOS_ACTIONS, |
1f366e42 SP |
111 | }; |
112 | ||
2b8a248d SP |
113 | /** |
114 | * struct damos_quota - Controls the aggressiveness of the given scheme. | |
1cd24303 | 115 | * @ms: Maximum milliseconds that the scheme can use. |
2b8a248d SP |
116 | * @sz: Maximum bytes of memory that the action can be applied. |
117 | * @reset_interval: Charge reset interval in milliseconds. | |
118 | * | |
38683e00 SP |
119 | * @weight_sz: Weight of the region's size for prioritization. |
120 | * @weight_nr_accesses: Weight of the region's nr_accesses for prioritization. | |
121 | * @weight_age: Weight of the region's age for prioritization. | |
122 | * | |
2b8a248d | 123 | * To avoid consuming too much CPU time or IO resources for applying the |
1cd24303 SP |
124 | * &struct damos->action to large memory, DAMON allows users to set time and/or |
125 | * size quotas. The quotas can be set by writing non-zero values to &ms and | |
126 | * &sz, respectively. If the time quota is set, DAMON tries to use only up to | |
127 | * &ms milliseconds within &reset_interval for applying the action. If the | |
128 | * size quota is set, DAMON tries to apply the action only up to &sz bytes | |
129 | * within &reset_interval. | |
130 | * | |
131 | * Internally, the time quota is transformed to a size quota using estimated | |
132 | * throughput of the scheme's action. DAMON then compares it against &sz and | |
133 | * uses smaller one as the effective quota. | |
38683e00 SP |
134 | * |
135 | * For selecting regions within the quota, DAMON prioritizes current scheme's | |
f7d911c3 | 136 | * target memory regions using the &struct damon_operations->get_scheme_score. |
38683e00 | 137 | * You could customize the prioritization logic by setting &weight_sz, |
f7d911c3 | 138 | * &weight_nr_accesses, and &weight_age, because monitoring operations are |
38683e00 | 139 | * encouraged to respect those. |
2b8a248d SP |
140 | */ |
141 | struct damos_quota { | |
1cd24303 | 142 | unsigned long ms; |
2b8a248d SP |
143 | unsigned long sz; |
144 | unsigned long reset_interval; | |
145 | ||
38683e00 SP |
146 | unsigned int weight_sz; |
147 | unsigned int weight_nr_accesses; | |
148 | unsigned int weight_age; | |
149 | ||
1cd24303 SP |
150 | /* private: */ |
151 | /* For throughput estimation */ | |
152 | unsigned long total_charged_sz; | |
153 | unsigned long total_charged_ns; | |
154 | ||
155 | unsigned long esz; /* Effective size quota in bytes */ | |
156 | ||
157 | /* For charging the quota */ | |
2b8a248d SP |
158 | unsigned long charged_sz; |
159 | unsigned long charged_from; | |
50585192 SP |
160 | struct damon_target *charge_target_from; |
161 | unsigned long charge_addr_from; | |
38683e00 SP |
162 | |
163 | /* For prioritization */ | |
164 | unsigned long histogram[DAMOS_MAX_SCORE + 1]; | |
165 | unsigned int min_score; | |
2b8a248d SP |
166 | }; |
167 | ||
ee801b7d SP |
168 | /** |
169 | * enum damos_wmark_metric - Represents the watermark metric. | |
170 | * | |
171 | * @DAMOS_WMARK_NONE: Ignore the watermarks of the given scheme. | |
172 | * @DAMOS_WMARK_FREE_MEM_RATE: Free memory rate of the system in [0,1000]. | |
5257f36e | 173 | * @NR_DAMOS_WMARK_METRICS: Total number of DAMOS watermark metrics |
ee801b7d SP |
174 | */ |
175 | enum damos_wmark_metric { | |
176 | DAMOS_WMARK_NONE, | |
177 | DAMOS_WMARK_FREE_MEM_RATE, | |
5257f36e | 178 | NR_DAMOS_WMARK_METRICS, |
ee801b7d SP |
179 | }; |
180 | ||
181 | /** | |
182 | * struct damos_watermarks - Controls when a given scheme should be activated. | |
183 | * @metric: Metric for the watermarks. | |
184 | * @interval: Watermarks check time interval in microseconds. | |
185 | * @high: High watermark. | |
186 | * @mid: Middle watermark. | |
187 | * @low: Low watermark. | |
188 | * | |
189 | * If &metric is &DAMOS_WMARK_NONE, the scheme is always active. Being active | |
190 | * means DAMON does monitoring and applying the action of the scheme to | |
191 | * appropriate memory regions. Else, DAMON checks &metric of the system for at | |
192 | * least every &interval microseconds and works as below. | |
193 | * | |
194 | * If &metric is higher than &high, the scheme is inactivated. If &metric is | |
195 | * between &mid and &low, the scheme is activated. If &metric is lower than | |
196 | * &low, the scheme is inactivated. | |
197 | */ | |
198 | struct damos_watermarks { | |
199 | enum damos_wmark_metric metric; | |
200 | unsigned long interval; | |
201 | unsigned long high; | |
202 | unsigned long mid; | |
203 | unsigned long low; | |
204 | ||
205 | /* private: */ | |
206 | bool activated; | |
207 | }; | |
208 | ||
0e92c2ee SP |
209 | /** |
210 | * struct damos_stat - Statistics on a given scheme. | |
211 | * @nr_tried: Total number of regions that the scheme is tried to be applied. | |
212 | * @sz_tried: Total size of regions that the scheme is tried to be applied. | |
213 | * @nr_applied: Total number of regions that the scheme is applied. | |
214 | * @sz_applied: Total size of regions that the scheme is applied. | |
6268eac3 | 215 | * @qt_exceeds: Total number of times the quota of the scheme has exceeded. |
0e92c2ee SP |
216 | */ |
217 | struct damos_stat { | |
218 | unsigned long nr_tried; | |
219 | unsigned long sz_tried; | |
220 | unsigned long nr_applied; | |
221 | unsigned long sz_applied; | |
6268eac3 | 222 | unsigned long qt_exceeds; |
0e92c2ee SP |
223 | }; |
224 | ||
98def236 SP |
225 | /** |
226 | * enum damos_filter_type - Type of memory for &struct damos_filter | |
227 | * @DAMOS_FILTER_TYPE_ANON: Anonymous pages. | |
228 | * @DAMOS_FILTER_TYPE_MEMCG: Specific memcg's pages. | |
229 | * @NR_DAMOS_FILTER_TYPES: Number of filter types. | |
55901e89 SP |
230 | * |
231 | * The support of each filter type is up to running &struct damon_operations. | |
232 | * &enum DAMON_OPS_PADDR is supporting all filter types, while | |
233 | * &enum DAMON_OPS_VADDR and &enum DAMON_OPS_FVADDR are not supporting any | |
234 | * filter types. | |
98def236 SP |
235 | */ |
236 | enum damos_filter_type { | |
237 | DAMOS_FILTER_TYPE_ANON, | |
238 | DAMOS_FILTER_TYPE_MEMCG, | |
239 | NR_DAMOS_FILTER_TYPES, | |
240 | }; | |
241 | ||
242 | /** | |
243 | * struct damos_filter - DAMOS action target memory filter. | |
244 | * @type: Type of the page. | |
245 | * @matching: If the matching page should filtered out or in. | |
246 | * @memcg_id: Memcg id of the question if @type is DAMOS_FILTER_MEMCG. | |
247 | * @list: List head for siblings. | |
248 | * | |
249 | * Before applying the &damos->action to a memory region, DAMOS checks if each | |
250 | * page of the region matches to this and avoid applying the action if so. | |
251 | * Note that the check support is up to &struct damon_operations | |
252 | * implementation. | |
253 | */ | |
254 | struct damos_filter { | |
255 | enum damos_filter_type type; | |
256 | bool matching; | |
257 | union { | |
258 | unsigned short memcg_id; | |
259 | }; | |
260 | struct list_head list; | |
261 | }; | |
262 | ||
1f366e42 | 263 | /** |
f5a79d7c | 264 | * struct damos_access_pattern - Target access pattern of the given scheme. |
1f366e42 SP |
265 | * @min_sz_region: Minimum size of target regions. |
266 | * @max_sz_region: Maximum size of target regions. | |
267 | * @min_nr_accesses: Minimum ``->nr_accesses`` of target regions. | |
268 | * @max_nr_accesses: Maximum ``->nr_accesses`` of target regions. | |
269 | * @min_age_region: Minimum age of target regions. | |
270 | * @max_age_region: Maximum age of target regions. | |
f5a79d7c YD |
271 | */ |
272 | struct damos_access_pattern { | |
273 | unsigned long min_sz_region; | |
274 | unsigned long max_sz_region; | |
275 | unsigned int min_nr_accesses; | |
276 | unsigned int max_nr_accesses; | |
277 | unsigned int min_age_region; | |
278 | unsigned int max_age_region; | |
279 | }; | |
280 | ||
281 | /** | |
282 | * struct damos - Represents a Data Access Monitoring-based Operation Scheme. | |
283 | * @pattern: Access pattern of target regions. | |
1f366e42 | 284 | * @action: &damo_action to be applied to the target regions. |
2b8a248d | 285 | * @quota: Control the aggressiveness of this scheme. |
ee801b7d | 286 | * @wmarks: Watermarks for automated (in)activation of this scheme. |
98def236 | 287 | * @filters: Additional set of &struct damos_filter for &action. |
0e92c2ee | 288 | * @stat: Statistics of this scheme. |
1f366e42 SP |
289 | * @list: List head for siblings. |
290 | * | |
2b8a248d | 291 | * For each aggregation interval, DAMON finds regions which fit in the |
f5a79d7c YD |
292 | * &pattern and applies &action to those. To avoid consuming too much |
293 | * CPU time or IO resources for the &action, "a is used. | |
2b8a248d | 294 | * |
ee801b7d SP |
295 | * To do the work only when needed, schemes can be activated for specific |
296 | * system situations using &wmarks. If all schemes that registered to the | |
297 | * monitoring context are inactive, DAMON stops monitoring either, and just | |
298 | * repeatedly checks the watermarks. | |
299 | * | |
300 | * If all schemes that registered to a &struct damon_ctx are inactive, DAMON | |
301 | * stops monitoring and just repeatedly checks the watermarks. | |
302 | * | |
98def236 SP |
303 | * Before applying the &action to a memory region, &struct damon_operations |
304 | * implementation could check pages of the region and skip &action to respect | |
305 | * &filters | |
306 | * | |
2b8a248d SP |
307 | * After applying the &action to each region, &stat_count and &stat_sz is |
308 | * updated to reflect the number of regions and total size of regions that the | |
309 | * &action is applied. | |
1f366e42 SP |
310 | */ |
311 | struct damos { | |
f5a79d7c | 312 | struct damos_access_pattern pattern; |
1f366e42 | 313 | enum damos_action action; |
2b8a248d | 314 | struct damos_quota quota; |
ee801b7d | 315 | struct damos_watermarks wmarks; |
98def236 | 316 | struct list_head filters; |
0e92c2ee | 317 | struct damos_stat stat; |
1f366e42 SP |
318 | struct list_head list; |
319 | }; | |
320 | ||
9f7b053a SP |
321 | /** |
322 | * enum damon_ops_id - Identifier for each monitoring operations implementation | |
323 | * | |
324 | * @DAMON_OPS_VADDR: Monitoring operations for virtual address spaces | |
de6d0154 SP |
325 | * @DAMON_OPS_FVADDR: Monitoring operations for only fixed ranges of virtual |
326 | * address spaces | |
9f7b053a | 327 | * @DAMON_OPS_PADDR: Monitoring operations for the physical address space |
d4a157f5 | 328 | * @NR_DAMON_OPS: Number of monitoring operations implementations |
9f7b053a SP |
329 | */ |
330 | enum damon_ops_id { | |
331 | DAMON_OPS_VADDR, | |
de6d0154 | 332 | DAMON_OPS_FVADDR, |
9f7b053a SP |
333 | DAMON_OPS_PADDR, |
334 | NR_DAMON_OPS, | |
335 | }; | |
336 | ||
2224d848 SP |
337 | struct damon_ctx; |
338 | ||
339 | /** | |
f7d911c3 | 340 | * struct damon_operations - Monitoring operations for given use cases. |
2224d848 | 341 | * |
9f7b053a | 342 | * @id: Identifier of this operations set. |
f7d911c3 SP |
343 | * @init: Initialize operations-related data structures. |
344 | * @update: Update operations-related data structures. | |
2224d848 SP |
345 | * @prepare_access_checks: Prepare next access check of target regions. |
346 | * @check_accesses: Check the accesses to target regions. | |
347 | * @reset_aggregated: Reset aggregated accesses monitoring results. | |
38683e00 | 348 | * @get_scheme_score: Get the score of a region for a scheme. |
1f366e42 | 349 | * @apply_scheme: Apply a DAMON-based operation scheme. |
2224d848 SP |
350 | * @target_valid: Determine if the target is valid. |
351 | * @cleanup: Clean up the context. | |
352 | * | |
353 | * DAMON can be extended for various address spaces and usages. For this, | |
f7d911c3 SP |
354 | * users should register the low level operations for their target address |
355 | * space and usecase via the &damon_ctx.ops. Then, the monitoring thread | |
2224d848 | 356 | * (&damon_ctx.kdamond) calls @init and @prepare_access_checks before starting |
6b3f013b | 357 | * the monitoring, @update after each &damon_attrs.ops_update_interval, and |
2224d848 | 358 | * @check_accesses, @target_valid and @prepare_access_checks after each |
6b3f013b SP |
359 | * &damon_attrs.sample_interval. Finally, @reset_aggregated is called after |
360 | * each &damon_attrs.aggr_interval. | |
2224d848 | 361 | * |
9f7b053a SP |
362 | * Each &struct damon_operations instance having valid @id can be registered |
363 | * via damon_register_ops() and selected by damon_select_ops() later. | |
f7d911c3 | 364 | * @init should initialize operations-related data structures. For example, |
2224d848 | 365 | * this could be used to construct proper monitoring target regions and link |
f23b8eee | 366 | * those to @damon_ctx.adaptive_targets. |
f7d911c3 | 367 | * @update should update the operations-related data structures. For example, |
2224d848 SP |
368 | * this could be used to update monitoring target regions for current status. |
369 | * @prepare_access_checks should manipulate the monitoring regions to be | |
370 | * prepared for the next access check. | |
371 | * @check_accesses should check the accesses to each region that made after the | |
372 | * last preparation and update the number of observed accesses of each region. | |
b9a6ac4e SP |
373 | * It should also return max number of observed accesses that made as a result |
374 | * of its update. The value will be used for regions adjustment threshold. | |
2224d848 SP |
375 | * @reset_aggregated should reset the access monitoring results that aggregated |
376 | * by @check_accesses. | |
38683e00 SP |
377 | * @get_scheme_score should return the priority score of a region for a scheme |
378 | * as an integer in [0, &DAMOS_MAX_SCORE]. | |
1f366e42 SP |
379 | * @apply_scheme is called from @kdamond when a region for user provided |
380 | * DAMON-based operation scheme is found. It should apply the scheme's action | |
0e92c2ee SP |
381 | * to the region and return bytes of the region that the action is successfully |
382 | * applied. | |
2224d848 SP |
383 | * @target_valid should check whether the target is still valid for the |
384 | * monitoring. | |
385 | * @cleanup is called from @kdamond just before its termination. | |
386 | */ | |
f7d911c3 | 387 | struct damon_operations { |
9f7b053a | 388 | enum damon_ops_id id; |
2224d848 SP |
389 | void (*init)(struct damon_ctx *context); |
390 | void (*update)(struct damon_ctx *context); | |
391 | void (*prepare_access_checks)(struct damon_ctx *context); | |
b9a6ac4e | 392 | unsigned int (*check_accesses)(struct damon_ctx *context); |
2224d848 | 393 | void (*reset_aggregated)(struct damon_ctx *context); |
38683e00 SP |
394 | int (*get_scheme_score)(struct damon_ctx *context, |
395 | struct damon_target *t, struct damon_region *r, | |
396 | struct damos *scheme); | |
0e92c2ee SP |
397 | unsigned long (*apply_scheme)(struct damon_ctx *context, |
398 | struct damon_target *t, struct damon_region *r, | |
399 | struct damos *scheme); | |
16bc1b0f | 400 | bool (*target_valid)(struct damon_target *t); |
2224d848 SP |
401 | void (*cleanup)(struct damon_ctx *context); |
402 | }; | |
403 | ||
d2f272b3 SP |
404 | /** |
405 | * struct damon_callback - Monitoring events notification callbacks. | |
2224d848 SP |
406 | * |
407 | * @before_start: Called before starting the monitoring. | |
6e74d2bf | 408 | * @after_wmarks_check: Called after each schemes' watermarks check. |
2224d848 SP |
409 | * @after_sampling: Called after each sampling. |
410 | * @after_aggregation: Called after each aggregation. | |
44467bbb | 411 | * @before_damos_apply: Called before applying DAMOS action. |
2224d848 SP |
412 | * @before_terminate: Called before terminating the monitoring. |
413 | * @private: User private data. | |
414 | * | |
415 | * The monitoring thread (&damon_ctx.kdamond) calls @before_start and | |
416 | * @before_terminate just before starting and finishing the monitoring, | |
417 | * respectively. Therefore, those are good places for installing and cleaning | |
418 | * @private. | |
419 | * | |
6e74d2bf SP |
420 | * The monitoring thread calls @after_wmarks_check after each DAMON-based |
421 | * operation schemes' watermarks check. If users need to make changes to the | |
422 | * attributes of the monitoring context while it's deactivated due to the | |
423 | * watermarks, this is the good place to do. | |
424 | * | |
2224d848 SP |
425 | * The monitoring thread calls @after_sampling and @after_aggregation for each |
426 | * of the sampling intervals and aggregation intervals, respectively. | |
427 | * Therefore, users can safely access the monitoring results without additional | |
428 | * protection. For the reason, users are recommended to use these callback for | |
429 | * the accesses to the results. | |
430 | * | |
431 | * If any callback returns non-zero, monitoring stops. | |
432 | */ | |
433 | struct damon_callback { | |
434 | void *private; | |
435 | ||
436 | int (*before_start)(struct damon_ctx *context); | |
6e74d2bf | 437 | int (*after_wmarks_check)(struct damon_ctx *context); |
2224d848 SP |
438 | int (*after_sampling)(struct damon_ctx *context); |
439 | int (*after_aggregation)(struct damon_ctx *context); | |
44467bbb SP |
440 | int (*before_damos_apply)(struct damon_ctx *context, |
441 | struct damon_target *target, | |
442 | struct damon_region *region, | |
443 | struct damos *scheme); | |
658f9ae7 | 444 | void (*before_terminate)(struct damon_ctx *context); |
2224d848 SP |
445 | }; |
446 | ||
447 | /** | |
cbeaa77b | 448 | * struct damon_attrs - Monitoring attributes for accuracy/overhead control. |
2224d848 SP |
449 | * |
450 | * @sample_interval: The time between access samplings. | |
451 | * @aggr_interval: The time between monitor results aggregations. | |
f7d911c3 | 452 | * @ops_update_interval: The time between monitoring operations updates. |
cbeaa77b SP |
453 | * @min_nr_regions: The minimum number of adaptive monitoring |
454 | * regions. | |
455 | * @max_nr_regions: The maximum number of adaptive monitoring | |
456 | * regions. | |
2224d848 SP |
457 | * |
458 | * For each @sample_interval, DAMON checks whether each region is accessed or | |
459 | * not. It aggregates and keeps the access information (number of accesses to | |
460 | * each region) for @aggr_interval time. DAMON also checks whether the target | |
461 | * memory regions need update (e.g., by ``mmap()`` calls from the application, | |
462 | * in case of virtual memory monitoring) and applies the changes for each | |
f7d911c3 SP |
463 | * @ops_update_interval. All time intervals are in micro-seconds. |
464 | * Please refer to &struct damon_operations and &struct damon_callback for more | |
2224d848 | 465 | * detail. |
cbeaa77b SP |
466 | */ |
467 | struct damon_attrs { | |
468 | unsigned long sample_interval; | |
469 | unsigned long aggr_interval; | |
470 | unsigned long ops_update_interval; | |
471 | unsigned long min_nr_regions; | |
472 | unsigned long max_nr_regions; | |
473 | }; | |
474 | ||
475 | /** | |
476 | * struct damon_ctx - Represents a context for each monitoring. This is the | |
477 | * main interface that allows users to set the attributes and get the results | |
478 | * of the monitoring. | |
2224d848 | 479 | * |
cbeaa77b | 480 | * @attrs: Monitoring attributes for accuracy/overhead control. |
2224d848 | 481 | * @kdamond: Kernel thread who does the monitoring. |
2224d848 SP |
482 | * @kdamond_lock: Mutex for the synchronizations with @kdamond. |
483 | * | |
484 | * For each monitoring context, one kernel thread for the monitoring is | |
485 | * created. The pointer to the thread is stored in @kdamond. | |
486 | * | |
487 | * Once started, the monitoring thread runs until explicitly required to be | |
488 | * terminated or every monitoring target is invalid. The validity of the | |
f7d911c3 | 489 | * targets is checked via the &damon_operations.target_valid of @ops. The |
bcc728eb CZ |
490 | * termination can also be explicitly requested by calling damon_stop(). |
491 | * The thread sets @kdamond to NULL when it terminates. Therefore, users can | |
492 | * know whether the monitoring is ongoing or terminated by reading @kdamond. | |
493 | * Reads and writes to @kdamond from outside of the monitoring thread must | |
494 | * be protected by @kdamond_lock. | |
495 | * | |
496 | * Note that the monitoring thread protects only @kdamond via @kdamond_lock. | |
497 | * Accesses to other fields must be protected by themselves. | |
2224d848 | 498 | * |
f7d911c3 | 499 | * @ops: Set of monitoring operations for given use cases. |
2224d848 SP |
500 | * @callback: Set of callbacks for monitoring events notifications. |
501 | * | |
b9a6ac4e | 502 | * @adaptive_targets: Head of monitoring targets (&damon_target) list. |
1f366e42 | 503 | * @schemes: Head of schemes (&damos) list. |
2224d848 SP |
504 | */ |
505 | struct damon_ctx { | |
cbeaa77b | 506 | struct damon_attrs attrs; |
2224d848 SP |
507 | |
508 | /* private: internal use only */ | |
509 | struct timespec64 last_aggregation; | |
f7d911c3 | 510 | struct timespec64 last_ops_update; |
2224d848 SP |
511 | |
512 | /* public: */ | |
513 | struct task_struct *kdamond; | |
2224d848 SP |
514 | struct mutex kdamond_lock; |
515 | ||
f7d911c3 | 516 | struct damon_operations ops; |
2224d848 SP |
517 | struct damon_callback callback; |
518 | ||
b9a6ac4e | 519 | struct list_head adaptive_targets; |
1f366e42 | 520 | struct list_head schemes; |
2224d848 SP |
521 | }; |
522 | ||
88f86dcf SP |
523 | static inline struct damon_region *damon_next_region(struct damon_region *r) |
524 | { | |
525 | return container_of(r->list.next, struct damon_region, list); | |
526 | } | |
f23b8eee | 527 | |
88f86dcf SP |
528 | static inline struct damon_region *damon_prev_region(struct damon_region *r) |
529 | { | |
530 | return container_of(r->list.prev, struct damon_region, list); | |
531 | } | |
f23b8eee | 532 | |
88f86dcf SP |
533 | static inline struct damon_region *damon_last_region(struct damon_target *t) |
534 | { | |
535 | return list_last_entry(&t->regions_list, struct damon_region, list); | |
536 | } | |
50585192 | 537 | |
36001cba KX |
538 | static inline struct damon_region *damon_first_region(struct damon_target *t) |
539 | { | |
540 | return list_first_entry(&t->regions_list, struct damon_region, list); | |
541 | } | |
542 | ||
652e0446 XH |
543 | static inline unsigned long damon_sz_region(struct damon_region *r) |
544 | { | |
545 | return r->ar.end - r->ar.start; | |
546 | } | |
547 | ||
548 | ||
f23b8eee SP |
549 | #define damon_for_each_region(r, t) \ |
550 | list_for_each_entry(r, &t->regions_list, list) | |
551 | ||
36001cba KX |
552 | #define damon_for_each_region_from(r, t) \ |
553 | list_for_each_entry_from(r, &t->regions_list, list) | |
554 | ||
f23b8eee SP |
555 | #define damon_for_each_region_safe(r, next, t) \ |
556 | list_for_each_entry_safe(r, next, &t->regions_list, list) | |
557 | ||
558 | #define damon_for_each_target(t, ctx) \ | |
b9a6ac4e | 559 | list_for_each_entry(t, &(ctx)->adaptive_targets, list) |
f23b8eee SP |
560 | |
561 | #define damon_for_each_target_safe(t, next, ctx) \ | |
b9a6ac4e | 562 | list_for_each_entry_safe(t, next, &(ctx)->adaptive_targets, list) |
f23b8eee | 563 | |
1f366e42 SP |
564 | #define damon_for_each_scheme(s, ctx) \ |
565 | list_for_each_entry(s, &(ctx)->schemes, list) | |
566 | ||
567 | #define damon_for_each_scheme_safe(s, next, ctx) \ | |
568 | list_for_each_entry_safe(s, next, &(ctx)->schemes, list) | |
569 | ||
98def236 SP |
570 | #define damos_for_each_filter(f, scheme) \ |
571 | list_for_each_entry(f, &(scheme)->filters, list) | |
572 | ||
573 | #define damos_for_each_filter_safe(f, next, scheme) \ | |
574 | list_for_each_entry_safe(f, next, &(scheme)->filters, list) | |
575 | ||
2224d848 SP |
576 | #ifdef CONFIG_DAMON |
577 | ||
f23b8eee | 578 | struct damon_region *damon_new_region(unsigned long start, unsigned long end); |
2cd4b8e1 GJ |
579 | |
580 | /* | |
581 | * Add a region between two other regions | |
582 | */ | |
583 | static inline void damon_insert_region(struct damon_region *r, | |
b9a6ac4e | 584 | struct damon_region *prev, struct damon_region *next, |
2cd4b8e1 GJ |
585 | struct damon_target *t) |
586 | { | |
587 | __list_add(&r->list, &prev->list, &next->list); | |
588 | t->nr_regions++; | |
589 | } | |
590 | ||
f23b8eee | 591 | void damon_add_region(struct damon_region *r, struct damon_target *t); |
b9a6ac4e | 592 | void damon_destroy_region(struct damon_region *r, struct damon_target *t); |
d0723bc0 SP |
593 | int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges, |
594 | unsigned int nr_ranges); | |
f23b8eee | 595 | |
98def236 SP |
596 | struct damos_filter *damos_new_filter(enum damos_filter_type type, |
597 | bool matching); | |
598 | void damos_add_filter(struct damos *s, struct damos_filter *f); | |
599 | void damos_destroy_filter(struct damos_filter *f); | |
600 | ||
f5a79d7c YD |
601 | struct damos *damon_new_scheme(struct damos_access_pattern *pattern, |
602 | enum damos_action action, struct damos_quota *quota, | |
603 | struct damos_watermarks *wmarks); | |
1f366e42 SP |
604 | void damon_add_scheme(struct damon_ctx *ctx, struct damos *s); |
605 | void damon_destroy_scheme(struct damos *s); | |
606 | ||
1971bd63 | 607 | struct damon_target *damon_new_target(void); |
f23b8eee | 608 | void damon_add_target(struct damon_ctx *ctx, struct damon_target *t); |
b5ca3e83 | 609 | bool damon_targets_empty(struct damon_ctx *ctx); |
f23b8eee SP |
610 | void damon_free_target(struct damon_target *t); |
611 | void damon_destroy_target(struct damon_target *t); | |
b9a6ac4e | 612 | unsigned int damon_nr_regions(struct damon_target *t); |
f23b8eee | 613 | |
2224d848 SP |
614 | struct damon_ctx *damon_new_ctx(void); |
615 | void damon_destroy_ctx(struct damon_ctx *ctx); | |
bead3b00 | 616 | int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs); |
cc713520 | 617 | void damon_set_schemes(struct damon_ctx *ctx, |
1f366e42 | 618 | struct damos **schemes, ssize_t nr_schemes); |
4bc05954 | 619 | int damon_nr_running_ctxs(void); |
152e5617 | 620 | bool damon_is_registered_ops(enum damon_ops_id id); |
9f7b053a SP |
621 | int damon_register_ops(struct damon_operations *ops); |
622 | int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id); | |
2224d848 | 623 | |
c9e124e0 SP |
624 | static inline bool damon_target_has_pid(const struct damon_ctx *ctx) |
625 | { | |
626 | return ctx->ops.id == DAMON_OPS_VADDR || ctx->ops.id == DAMON_OPS_FVADDR; | |
627 | } | |
628 | ||
629 | ||
8b9b0d33 | 630 | int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive); |
2224d848 SP |
631 | int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); |
632 | ||
233f0b31 KX |
633 | int damon_set_region_biggest_system_ram_default(struct damon_target *t, |
634 | unsigned long *start, unsigned long *end); | |
0d83b2d8 | 635 | |
2224d848 SP |
636 | #endif /* CONFIG_DAMON */ |
637 | ||
638 | #endif /* _DAMON_H */ |