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> | |
14 | ||
b9a6ac4e SP |
15 | /* Minimal region size. Every damon_region is aligned by this. */ |
16 | #define DAMON_MIN_REGION PAGE_SIZE | |
17 | ||
f23b8eee SP |
18 | /** |
19 | * struct damon_addr_range - Represents an address region of [@start, @end). | |
20 | * @start: Start address of the region (inclusive). | |
21 | * @end: End address of the region (exclusive). | |
22 | */ | |
23 | struct damon_addr_range { | |
24 | unsigned long start; | |
25 | unsigned long end; | |
26 | }; | |
27 | ||
28 | /** | |
29 | * struct damon_region - Represents a monitoring target region. | |
30 | * @ar: The address range of the region. | |
31 | * @sampling_addr: Address of the sample for the next access check. | |
32 | * @nr_accesses: Access frequency of this region. | |
33 | * @list: List head for siblings. | |
fda504fa SP |
34 | * @age: Age of this region. |
35 | * | |
36 | * @age is initially zero, increased for each aggregation interval, and reset | |
37 | * to zero again if the access frequency is significantly changed. If two | |
38 | * regions are merged into a new region, both @nr_accesses and @age of the new | |
39 | * region are set as region size-weighted average of those of the two regions. | |
f23b8eee SP |
40 | */ |
41 | struct damon_region { | |
42 | struct damon_addr_range ar; | |
43 | unsigned long sampling_addr; | |
44 | unsigned int nr_accesses; | |
45 | struct list_head list; | |
fda504fa SP |
46 | |
47 | unsigned int age; | |
48 | /* private: Internal value for age calculation. */ | |
49 | unsigned int last_nr_accesses; | |
f23b8eee SP |
50 | }; |
51 | ||
52 | /** | |
53 | * struct damon_target - Represents a monitoring target. | |
54 | * @id: Unique identifier for this target. | |
b9a6ac4e | 55 | * @nr_regions: Number of monitoring target regions of this target. |
f23b8eee SP |
56 | * @regions_list: Head of the monitoring target regions of this target. |
57 | * @list: List head for siblings. | |
58 | * | |
59 | * Each monitoring context could have multiple targets. For example, a context | |
60 | * for virtual memory address spaces could have multiple target processes. The | |
61 | * @id of each target should be unique among the targets of the context. For | |
62 | * example, in the virtual address monitoring context, it could be a pidfd or | |
63 | * an address of an mm_struct. | |
64 | */ | |
65 | struct damon_target { | |
66 | unsigned long id; | |
b9a6ac4e | 67 | unsigned int nr_regions; |
f23b8eee SP |
68 | struct list_head regions_list; |
69 | struct list_head list; | |
70 | }; | |
71 | ||
1f366e42 SP |
72 | /** |
73 | * enum damos_action - Represents an action of a Data Access Monitoring-based | |
74 | * Operation Scheme. | |
75 | * | |
76 | * @DAMOS_WILLNEED: Call ``madvise()`` for the region with MADV_WILLNEED. | |
77 | * @DAMOS_COLD: Call ``madvise()`` for the region with MADV_COLD. | |
78 | * @DAMOS_PAGEOUT: Call ``madvise()`` for the region with MADV_PAGEOUT. | |
79 | * @DAMOS_HUGEPAGE: Call ``madvise()`` for the region with MADV_HUGEPAGE. | |
80 | * @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE. | |
2f0b548c | 81 | * @DAMOS_STAT: Do nothing but count the stat. |
1f366e42 SP |
82 | */ |
83 | enum damos_action { | |
84 | DAMOS_WILLNEED, | |
85 | DAMOS_COLD, | |
86 | DAMOS_PAGEOUT, | |
87 | DAMOS_HUGEPAGE, | |
88 | DAMOS_NOHUGEPAGE, | |
2f0b548c | 89 | DAMOS_STAT, /* Do nothing but only record the stat */ |
1f366e42 SP |
90 | }; |
91 | ||
92 | /** | |
93 | * struct damos - Represents a Data Access Monitoring-based Operation Scheme. | |
94 | * @min_sz_region: Minimum size of target regions. | |
95 | * @max_sz_region: Maximum size of target regions. | |
96 | * @min_nr_accesses: Minimum ``->nr_accesses`` of target regions. | |
97 | * @max_nr_accesses: Maximum ``->nr_accesses`` of target regions. | |
98 | * @min_age_region: Minimum age of target regions. | |
99 | * @max_age_region: Maximum age of target regions. | |
100 | * @action: &damo_action to be applied to the target regions. | |
2f0b548c SP |
101 | * @stat_count: Total number of regions that this scheme is applied. |
102 | * @stat_sz: Total size of regions that this scheme is applied. | |
1f366e42 SP |
103 | * @list: List head for siblings. |
104 | * | |
2f0b548c SP |
105 | * For each aggregation interval, DAMON applies @action to monitoring target |
106 | * regions fit in the condition and updates the statistics. Note that both | |
107 | * the minimums and the maximums are inclusive. | |
1f366e42 SP |
108 | */ |
109 | struct damos { | |
110 | unsigned long min_sz_region; | |
111 | unsigned long max_sz_region; | |
112 | unsigned int min_nr_accesses; | |
113 | unsigned int max_nr_accesses; | |
114 | unsigned int min_age_region; | |
115 | unsigned int max_age_region; | |
116 | enum damos_action action; | |
2f0b548c SP |
117 | unsigned long stat_count; |
118 | unsigned long stat_sz; | |
1f366e42 SP |
119 | struct list_head list; |
120 | }; | |
121 | ||
2224d848 SP |
122 | struct damon_ctx; |
123 | ||
124 | /** | |
d2f272b3 | 125 | * struct damon_primitive - Monitoring primitives for given use cases. |
2224d848 SP |
126 | * |
127 | * @init: Initialize primitive-internal data structures. | |
128 | * @update: Update primitive-internal data structures. | |
129 | * @prepare_access_checks: Prepare next access check of target regions. | |
130 | * @check_accesses: Check the accesses to target regions. | |
131 | * @reset_aggregated: Reset aggregated accesses monitoring results. | |
1f366e42 | 132 | * @apply_scheme: Apply a DAMON-based operation scheme. |
2224d848 SP |
133 | * @target_valid: Determine if the target is valid. |
134 | * @cleanup: Clean up the context. | |
135 | * | |
136 | * DAMON can be extended for various address spaces and usages. For this, | |
137 | * users should register the low level primitives for their target address | |
138 | * space and usecase via the &damon_ctx.primitive. Then, the monitoring thread | |
139 | * (&damon_ctx.kdamond) calls @init and @prepare_access_checks before starting | |
140 | * the monitoring, @update after each &damon_ctx.primitive_update_interval, and | |
141 | * @check_accesses, @target_valid and @prepare_access_checks after each | |
142 | * &damon_ctx.sample_interval. Finally, @reset_aggregated is called after each | |
143 | * &damon_ctx.aggr_interval. | |
144 | * | |
145 | * @init should initialize primitive-internal data structures. For example, | |
146 | * this could be used to construct proper monitoring target regions and link | |
f23b8eee | 147 | * those to @damon_ctx.adaptive_targets. |
2224d848 SP |
148 | * @update should update the primitive-internal data structures. For example, |
149 | * this could be used to update monitoring target regions for current status. | |
150 | * @prepare_access_checks should manipulate the monitoring regions to be | |
151 | * prepared for the next access check. | |
152 | * @check_accesses should check the accesses to each region that made after the | |
153 | * last preparation and update the number of observed accesses of each region. | |
b9a6ac4e SP |
154 | * It should also return max number of observed accesses that made as a result |
155 | * of its update. The value will be used for regions adjustment threshold. | |
2224d848 SP |
156 | * @reset_aggregated should reset the access monitoring results that aggregated |
157 | * by @check_accesses. | |
1f366e42 SP |
158 | * @apply_scheme is called from @kdamond when a region for user provided |
159 | * DAMON-based operation scheme is found. It should apply the scheme's action | |
160 | * to the region. This is not used for &DAMON_ARBITRARY_TARGET case. | |
2224d848 SP |
161 | * @target_valid should check whether the target is still valid for the |
162 | * monitoring. | |
163 | * @cleanup is called from @kdamond just before its termination. | |
164 | */ | |
165 | struct damon_primitive { | |
166 | void (*init)(struct damon_ctx *context); | |
167 | void (*update)(struct damon_ctx *context); | |
168 | void (*prepare_access_checks)(struct damon_ctx *context); | |
b9a6ac4e | 169 | unsigned int (*check_accesses)(struct damon_ctx *context); |
2224d848 | 170 | void (*reset_aggregated)(struct damon_ctx *context); |
1f366e42 SP |
171 | int (*apply_scheme)(struct damon_ctx *context, struct damon_target *t, |
172 | struct damon_region *r, struct damos *scheme); | |
2224d848 SP |
173 | bool (*target_valid)(void *target); |
174 | void (*cleanup)(struct damon_ctx *context); | |
175 | }; | |
176 | ||
d2f272b3 SP |
177 | /** |
178 | * struct damon_callback - Monitoring events notification callbacks. | |
2224d848 SP |
179 | * |
180 | * @before_start: Called before starting the monitoring. | |
181 | * @after_sampling: Called after each sampling. | |
182 | * @after_aggregation: Called after each aggregation. | |
183 | * @before_terminate: Called before terminating the monitoring. | |
184 | * @private: User private data. | |
185 | * | |
186 | * The monitoring thread (&damon_ctx.kdamond) calls @before_start and | |
187 | * @before_terminate just before starting and finishing the monitoring, | |
188 | * respectively. Therefore, those are good places for installing and cleaning | |
189 | * @private. | |
190 | * | |
191 | * The monitoring thread calls @after_sampling and @after_aggregation for each | |
192 | * of the sampling intervals and aggregation intervals, respectively. | |
193 | * Therefore, users can safely access the monitoring results without additional | |
194 | * protection. For the reason, users are recommended to use these callback for | |
195 | * the accesses to the results. | |
196 | * | |
197 | * If any callback returns non-zero, monitoring stops. | |
198 | */ | |
199 | struct damon_callback { | |
200 | void *private; | |
201 | ||
202 | int (*before_start)(struct damon_ctx *context); | |
203 | int (*after_sampling)(struct damon_ctx *context); | |
204 | int (*after_aggregation)(struct damon_ctx *context); | |
205 | int (*before_terminate)(struct damon_ctx *context); | |
206 | }; | |
207 | ||
208 | /** | |
209 | * struct damon_ctx - Represents a context for each monitoring. This is the | |
210 | * main interface that allows users to set the attributes and get the results | |
211 | * of the monitoring. | |
212 | * | |
213 | * @sample_interval: The time between access samplings. | |
214 | * @aggr_interval: The time between monitor results aggregations. | |
215 | * @primitive_update_interval: The time between monitoring primitive updates. | |
216 | * | |
217 | * For each @sample_interval, DAMON checks whether each region is accessed or | |
218 | * not. It aggregates and keeps the access information (number of accesses to | |
219 | * each region) for @aggr_interval time. DAMON also checks whether the target | |
220 | * memory regions need update (e.g., by ``mmap()`` calls from the application, | |
221 | * in case of virtual memory monitoring) and applies the changes for each | |
222 | * @primitive_update_interval. All time intervals are in micro-seconds. | |
223 | * Please refer to &struct damon_primitive and &struct damon_callback for more | |
224 | * detail. | |
225 | * | |
226 | * @kdamond: Kernel thread who does the monitoring. | |
227 | * @kdamond_stop: Notifies whether kdamond should stop. | |
228 | * @kdamond_lock: Mutex for the synchronizations with @kdamond. | |
229 | * | |
230 | * For each monitoring context, one kernel thread for the monitoring is | |
231 | * created. The pointer to the thread is stored in @kdamond. | |
232 | * | |
233 | * Once started, the monitoring thread runs until explicitly required to be | |
234 | * terminated or every monitoring target is invalid. The validity of the | |
235 | * targets is checked via the &damon_primitive.target_valid of @primitive. The | |
236 | * termination can also be explicitly requested by writing non-zero to | |
237 | * @kdamond_stop. The thread sets @kdamond to NULL when it terminates. | |
238 | * Therefore, users can know whether the monitoring is ongoing or terminated by | |
239 | * reading @kdamond. Reads and writes to @kdamond and @kdamond_stop from | |
240 | * outside of the monitoring thread must be protected by @kdamond_lock. | |
241 | * | |
242 | * Note that the monitoring thread protects only @kdamond and @kdamond_stop via | |
243 | * @kdamond_lock. Accesses to other fields must be protected by themselves. | |
244 | * | |
245 | * @primitive: Set of monitoring primitives for given use cases. | |
246 | * @callback: Set of callbacks for monitoring events notifications. | |
247 | * | |
b9a6ac4e SP |
248 | * @min_nr_regions: The minimum number of adaptive monitoring regions. |
249 | * @max_nr_regions: The maximum number of adaptive monitoring regions. | |
250 | * @adaptive_targets: Head of monitoring targets (&damon_target) list. | |
1f366e42 | 251 | * @schemes: Head of schemes (&damos) list. |
2224d848 SP |
252 | */ |
253 | struct damon_ctx { | |
254 | unsigned long sample_interval; | |
255 | unsigned long aggr_interval; | |
256 | unsigned long primitive_update_interval; | |
257 | ||
258 | /* private: internal use only */ | |
259 | struct timespec64 last_aggregation; | |
260 | struct timespec64 last_primitive_update; | |
261 | ||
262 | /* public: */ | |
263 | struct task_struct *kdamond; | |
264 | bool kdamond_stop; | |
265 | struct mutex kdamond_lock; | |
266 | ||
267 | struct damon_primitive primitive; | |
268 | struct damon_callback callback; | |
269 | ||
b9a6ac4e SP |
270 | unsigned long min_nr_regions; |
271 | unsigned long max_nr_regions; | |
272 | struct list_head adaptive_targets; | |
1f366e42 | 273 | struct list_head schemes; |
2224d848 SP |
274 | }; |
275 | ||
f23b8eee SP |
276 | #define damon_next_region(r) \ |
277 | (container_of(r->list.next, struct damon_region, list)) | |
278 | ||
279 | #define damon_prev_region(r) \ | |
280 | (container_of(r->list.prev, struct damon_region, list)) | |
281 | ||
282 | #define damon_for_each_region(r, t) \ | |
283 | list_for_each_entry(r, &t->regions_list, list) | |
284 | ||
285 | #define damon_for_each_region_safe(r, next, t) \ | |
286 | list_for_each_entry_safe(r, next, &t->regions_list, list) | |
287 | ||
288 | #define damon_for_each_target(t, ctx) \ | |
b9a6ac4e | 289 | list_for_each_entry(t, &(ctx)->adaptive_targets, list) |
f23b8eee SP |
290 | |
291 | #define damon_for_each_target_safe(t, next, ctx) \ | |
b9a6ac4e | 292 | list_for_each_entry_safe(t, next, &(ctx)->adaptive_targets, list) |
f23b8eee | 293 | |
1f366e42 SP |
294 | #define damon_for_each_scheme(s, ctx) \ |
295 | list_for_each_entry(s, &(ctx)->schemes, list) | |
296 | ||
297 | #define damon_for_each_scheme_safe(s, next, ctx) \ | |
298 | list_for_each_entry_safe(s, next, &(ctx)->schemes, list) | |
299 | ||
2224d848 SP |
300 | #ifdef CONFIG_DAMON |
301 | ||
f23b8eee SP |
302 | struct damon_region *damon_new_region(unsigned long start, unsigned long end); |
303 | inline void damon_insert_region(struct damon_region *r, | |
b9a6ac4e SP |
304 | struct damon_region *prev, struct damon_region *next, |
305 | struct damon_target *t); | |
f23b8eee | 306 | void damon_add_region(struct damon_region *r, struct damon_target *t); |
b9a6ac4e | 307 | void damon_destroy_region(struct damon_region *r, struct damon_target *t); |
f23b8eee | 308 | |
1f366e42 SP |
309 | struct damos *damon_new_scheme( |
310 | unsigned long min_sz_region, unsigned long max_sz_region, | |
311 | unsigned int min_nr_accesses, unsigned int max_nr_accesses, | |
312 | unsigned int min_age_region, unsigned int max_age_region, | |
313 | enum damos_action action); | |
314 | void damon_add_scheme(struct damon_ctx *ctx, struct damos *s); | |
315 | void damon_destroy_scheme(struct damos *s); | |
316 | ||
f23b8eee SP |
317 | struct damon_target *damon_new_target(unsigned long id); |
318 | void damon_add_target(struct damon_ctx *ctx, struct damon_target *t); | |
319 | void damon_free_target(struct damon_target *t); | |
320 | void damon_destroy_target(struct damon_target *t); | |
b9a6ac4e | 321 | unsigned int damon_nr_regions(struct damon_target *t); |
f23b8eee | 322 | |
2224d848 SP |
323 | struct damon_ctx *damon_new_ctx(void); |
324 | void damon_destroy_ctx(struct damon_ctx *ctx); | |
4bc05954 SP |
325 | int damon_set_targets(struct damon_ctx *ctx, |
326 | unsigned long *ids, ssize_t nr_ids); | |
2224d848 | 327 | int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, |
b9a6ac4e SP |
328 | unsigned long aggr_int, unsigned long primitive_upd_int, |
329 | unsigned long min_nr_reg, unsigned long max_nr_reg); | |
1f366e42 SP |
330 | int damon_set_schemes(struct damon_ctx *ctx, |
331 | struct damos **schemes, ssize_t nr_schemes); | |
4bc05954 | 332 | int damon_nr_running_ctxs(void); |
2224d848 SP |
333 | |
334 | int damon_start(struct damon_ctx **ctxs, int nr_ctxs); | |
335 | int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); | |
336 | ||
337 | #endif /* CONFIG_DAMON */ | |
338 | ||
3f49584b SP |
339 | #ifdef CONFIG_DAMON_VADDR |
340 | ||
341 | /* Monitoring primitives for virtual memory address spaces */ | |
342 | void damon_va_init(struct damon_ctx *ctx); | |
343 | void damon_va_update(struct damon_ctx *ctx); | |
344 | void damon_va_prepare_access_checks(struct damon_ctx *ctx); | |
345 | unsigned int damon_va_check_accesses(struct damon_ctx *ctx); | |
346 | bool damon_va_target_valid(void *t); | |
347 | void damon_va_cleanup(struct damon_ctx *ctx); | |
6dea8add SP |
348 | int damon_va_apply_scheme(struct damon_ctx *context, struct damon_target *t, |
349 | struct damon_region *r, struct damos *scheme); | |
3f49584b SP |
350 | void damon_va_set_primitives(struct damon_ctx *ctx); |
351 | ||
352 | #endif /* CONFIG_DAMON_VADDR */ | |
353 | ||
a28397be SP |
354 | #ifdef CONFIG_DAMON_PADDR |
355 | ||
356 | /* Monitoring primitives for the physical memory address space */ | |
357 | void damon_pa_prepare_access_checks(struct damon_ctx *ctx); | |
358 | unsigned int damon_pa_check_accesses(struct damon_ctx *ctx); | |
359 | bool damon_pa_target_valid(void *t); | |
57223ac2 SP |
360 | int damon_pa_apply_scheme(struct damon_ctx *context, struct damon_target *t, |
361 | struct damon_region *r, struct damos *scheme); | |
a28397be SP |
362 | void damon_pa_set_primitives(struct damon_ctx *ctx); |
363 | ||
364 | #endif /* CONFIG_DAMON_PADDR */ | |
365 | ||
2224d848 | 366 | #endif /* _DAMON_H */ |