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