mm/damon/core: account age of target regions
[linux-2.6-microblaze.git] / include / linux / damon.h
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
15 /* Minimal region size.  Every damon_region is aligned by this. */
16 #define DAMON_MIN_REGION        PAGE_SIZE
17
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.
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.
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;
46
47         unsigned int age;
48 /* private: Internal value for age calculation. */
49         unsigned int last_nr_accesses;
50 };
51
52 /**
53  * struct damon_target - Represents a monitoring target.
54  * @id:                 Unique identifier for this target.
55  * @nr_regions:         Number of monitoring target regions of this target.
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;
67         unsigned int nr_regions;
68         struct list_head regions_list;
69         struct list_head list;
70 };
71
72 struct damon_ctx;
73
74 /**
75  * struct damon_primitive - Monitoring primitives for given use cases.
76  *
77  * @init:                       Initialize primitive-internal data structures.
78  * @update:                     Update primitive-internal data structures.
79  * @prepare_access_checks:      Prepare next access check of target regions.
80  * @check_accesses:             Check the accesses to target regions.
81  * @reset_aggregated:           Reset aggregated accesses monitoring results.
82  * @target_valid:               Determine if the target is valid.
83  * @cleanup:                    Clean up the context.
84  *
85  * DAMON can be extended for various address spaces and usages.  For this,
86  * users should register the low level primitives for their target address
87  * space and usecase via the &damon_ctx.primitive.  Then, the monitoring thread
88  * (&damon_ctx.kdamond) calls @init and @prepare_access_checks before starting
89  * the monitoring, @update after each &damon_ctx.primitive_update_interval, and
90  * @check_accesses, @target_valid and @prepare_access_checks after each
91  * &damon_ctx.sample_interval.  Finally, @reset_aggregated is called after each
92  * &damon_ctx.aggr_interval.
93  *
94  * @init should initialize primitive-internal data structures.  For example,
95  * this could be used to construct proper monitoring target regions and link
96  * those to @damon_ctx.adaptive_targets.
97  * @update should update the primitive-internal data structures.  For example,
98  * this could be used to update monitoring target regions for current status.
99  * @prepare_access_checks should manipulate the monitoring regions to be
100  * prepared for the next access check.
101  * @check_accesses should check the accesses to each region that made after the
102  * last preparation and update the number of observed accesses of each region.
103  * It should also return max number of observed accesses that made as a result
104  * of its update.  The value will be used for regions adjustment threshold.
105  * @reset_aggregated should reset the access monitoring results that aggregated
106  * by @check_accesses.
107  * @target_valid should check whether the target is still valid for the
108  * monitoring.
109  * @cleanup is called from @kdamond just before its termination.
110  */
111 struct damon_primitive {
112         void (*init)(struct damon_ctx *context);
113         void (*update)(struct damon_ctx *context);
114         void (*prepare_access_checks)(struct damon_ctx *context);
115         unsigned int (*check_accesses)(struct damon_ctx *context);
116         void (*reset_aggregated)(struct damon_ctx *context);
117         bool (*target_valid)(void *target);
118         void (*cleanup)(struct damon_ctx *context);
119 };
120
121 /**
122  * struct damon_callback - Monitoring events notification callbacks.
123  *
124  * @before_start:       Called before starting the monitoring.
125  * @after_sampling:     Called after each sampling.
126  * @after_aggregation:  Called after each aggregation.
127  * @before_terminate:   Called before terminating the monitoring.
128  * @private:            User private data.
129  *
130  * The monitoring thread (&damon_ctx.kdamond) calls @before_start and
131  * @before_terminate just before starting and finishing the monitoring,
132  * respectively.  Therefore, those are good places for installing and cleaning
133  * @private.
134  *
135  * The monitoring thread calls @after_sampling and @after_aggregation for each
136  * of the sampling intervals and aggregation intervals, respectively.
137  * Therefore, users can safely access the monitoring results without additional
138  * protection.  For the reason, users are recommended to use these callback for
139  * the accesses to the results.
140  *
141  * If any callback returns non-zero, monitoring stops.
142  */
143 struct damon_callback {
144         void *private;
145
146         int (*before_start)(struct damon_ctx *context);
147         int (*after_sampling)(struct damon_ctx *context);
148         int (*after_aggregation)(struct damon_ctx *context);
149         int (*before_terminate)(struct damon_ctx *context);
150 };
151
152 /**
153  * struct damon_ctx - Represents a context for each monitoring.  This is the
154  * main interface that allows users to set the attributes and get the results
155  * of the monitoring.
156  *
157  * @sample_interval:            The time between access samplings.
158  * @aggr_interval:              The time between monitor results aggregations.
159  * @primitive_update_interval:  The time between monitoring primitive updates.
160  *
161  * For each @sample_interval, DAMON checks whether each region is accessed or
162  * not.  It aggregates and keeps the access information (number of accesses to
163  * each region) for @aggr_interval time.  DAMON also checks whether the target
164  * memory regions need update (e.g., by ``mmap()`` calls from the application,
165  * in case of virtual memory monitoring) and applies the changes for each
166  * @primitive_update_interval.  All time intervals are in micro-seconds.
167  * Please refer to &struct damon_primitive and &struct damon_callback for more
168  * detail.
169  *
170  * @kdamond:            Kernel thread who does the monitoring.
171  * @kdamond_stop:       Notifies whether kdamond should stop.
172  * @kdamond_lock:       Mutex for the synchronizations with @kdamond.
173  *
174  * For each monitoring context, one kernel thread for the monitoring is
175  * created.  The pointer to the thread is stored in @kdamond.
176  *
177  * Once started, the monitoring thread runs until explicitly required to be
178  * terminated or every monitoring target is invalid.  The validity of the
179  * targets is checked via the &damon_primitive.target_valid of @primitive.  The
180  * termination can also be explicitly requested by writing non-zero to
181  * @kdamond_stop.  The thread sets @kdamond to NULL when it terminates.
182  * Therefore, users can know whether the monitoring is ongoing or terminated by
183  * reading @kdamond.  Reads and writes to @kdamond and @kdamond_stop from
184  * outside of the monitoring thread must be protected by @kdamond_lock.
185  *
186  * Note that the monitoring thread protects only @kdamond and @kdamond_stop via
187  * @kdamond_lock.  Accesses to other fields must be protected by themselves.
188  *
189  * @primitive:  Set of monitoring primitives for given use cases.
190  * @callback:   Set of callbacks for monitoring events notifications.
191  *
192  * @min_nr_regions:     The minimum number of adaptive monitoring regions.
193  * @max_nr_regions:     The maximum number of adaptive monitoring regions.
194  * @adaptive_targets:   Head of monitoring targets (&damon_target) list.
195  */
196 struct damon_ctx {
197         unsigned long sample_interval;
198         unsigned long aggr_interval;
199         unsigned long primitive_update_interval;
200
201 /* private: internal use only */
202         struct timespec64 last_aggregation;
203         struct timespec64 last_primitive_update;
204
205 /* public: */
206         struct task_struct *kdamond;
207         bool kdamond_stop;
208         struct mutex kdamond_lock;
209
210         struct damon_primitive primitive;
211         struct damon_callback callback;
212
213         unsigned long min_nr_regions;
214         unsigned long max_nr_regions;
215         struct list_head adaptive_targets;
216 };
217
218 #define damon_next_region(r) \
219         (container_of(r->list.next, struct damon_region, list))
220
221 #define damon_prev_region(r) \
222         (container_of(r->list.prev, struct damon_region, list))
223
224 #define damon_for_each_region(r, t) \
225         list_for_each_entry(r, &t->regions_list, list)
226
227 #define damon_for_each_region_safe(r, next, t) \
228         list_for_each_entry_safe(r, next, &t->regions_list, list)
229
230 #define damon_for_each_target(t, ctx) \
231         list_for_each_entry(t, &(ctx)->adaptive_targets, list)
232
233 #define damon_for_each_target_safe(t, next, ctx)        \
234         list_for_each_entry_safe(t, next, &(ctx)->adaptive_targets, list)
235
236 #ifdef CONFIG_DAMON
237
238 struct damon_region *damon_new_region(unsigned long start, unsigned long end);
239 inline void damon_insert_region(struct damon_region *r,
240                 struct damon_region *prev, struct damon_region *next,
241                 struct damon_target *t);
242 void damon_add_region(struct damon_region *r, struct damon_target *t);
243 void damon_destroy_region(struct damon_region *r, struct damon_target *t);
244
245 struct damon_target *damon_new_target(unsigned long id);
246 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t);
247 void damon_free_target(struct damon_target *t);
248 void damon_destroy_target(struct damon_target *t);
249 unsigned int damon_nr_regions(struct damon_target *t);
250
251 struct damon_ctx *damon_new_ctx(void);
252 void damon_destroy_ctx(struct damon_ctx *ctx);
253 int damon_set_targets(struct damon_ctx *ctx,
254                 unsigned long *ids, ssize_t nr_ids);
255 int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
256                 unsigned long aggr_int, unsigned long primitive_upd_int,
257                 unsigned long min_nr_reg, unsigned long max_nr_reg);
258 int damon_nr_running_ctxs(void);
259
260 int damon_start(struct damon_ctx **ctxs, int nr_ctxs);
261 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
262
263 #endif  /* CONFIG_DAMON */
264
265 #ifdef CONFIG_DAMON_VADDR
266
267 /* Monitoring primitives for virtual memory address spaces */
268 void damon_va_init(struct damon_ctx *ctx);
269 void damon_va_update(struct damon_ctx *ctx);
270 void damon_va_prepare_access_checks(struct damon_ctx *ctx);
271 unsigned int damon_va_check_accesses(struct damon_ctx *ctx);
272 bool damon_va_target_valid(void *t);
273 void damon_va_cleanup(struct damon_ctx *ctx);
274 void damon_va_set_primitives(struct damon_ctx *ctx);
275
276 #endif  /* CONFIG_DAMON_VADDR */
277
278 #endif  /* _DAMON_H */