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