1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Author: SeongJae Park <sjpark@amazon.de>
11 #include <linux/mutex.h>
12 #include <linux/time64.h>
13 #include <linux/types.h>
15 /* Minimal region size. Every damon_region is aligned by this. */
16 #define DAMON_MIN_REGION PAGE_SIZE
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).
23 struct damon_addr_range {
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.
36 struct damon_addr_range ar;
37 unsigned long sampling_addr;
38 unsigned int nr_accesses;
39 struct list_head list;
43 * struct damon_target - Represents a monitoring target.
44 * @id: Unique identifier for this target.
45 * @nr_regions: Number of monitoring target regions of this target.
46 * @regions_list: Head of the monitoring target regions of this target.
47 * @list: List head for siblings.
49 * Each monitoring context could have multiple targets. For example, a context
50 * for virtual memory address spaces could have multiple target processes. The
51 * @id of each target should be unique among the targets of the context. For
52 * example, in the virtual address monitoring context, it could be a pidfd or
53 * an address of an mm_struct.
57 unsigned int nr_regions;
58 struct list_head regions_list;
59 struct list_head list;
65 * struct damon_primitive Monitoring primitives for given use cases.
67 * @init: Initialize primitive-internal data structures.
68 * @update: Update primitive-internal data structures.
69 * @prepare_access_checks: Prepare next access check of target regions.
70 * @check_accesses: Check the accesses to target regions.
71 * @reset_aggregated: Reset aggregated accesses monitoring results.
72 * @target_valid: Determine if the target is valid.
73 * @cleanup: Clean up the context.
75 * DAMON can be extended for various address spaces and usages. For this,
76 * users should register the low level primitives for their target address
77 * space and usecase via the &damon_ctx.primitive. Then, the monitoring thread
78 * (&damon_ctx.kdamond) calls @init and @prepare_access_checks before starting
79 * the monitoring, @update after each &damon_ctx.primitive_update_interval, and
80 * @check_accesses, @target_valid and @prepare_access_checks after each
81 * &damon_ctx.sample_interval. Finally, @reset_aggregated is called after each
82 * &damon_ctx.aggr_interval.
84 * @init should initialize primitive-internal data structures. For example,
85 * this could be used to construct proper monitoring target regions and link
86 * those to @damon_ctx.adaptive_targets.
87 * @update should update the primitive-internal data structures. For example,
88 * this could be used to update monitoring target regions for current status.
89 * @prepare_access_checks should manipulate the monitoring regions to be
90 * prepared for the next access check.
91 * @check_accesses should check the accesses to each region that made after the
92 * last preparation and update the number of observed accesses of each region.
93 * It should also return max number of observed accesses that made as a result
94 * of its update. The value will be used for regions adjustment threshold.
95 * @reset_aggregated should reset the access monitoring results that aggregated
97 * @target_valid should check whether the target is still valid for the
99 * @cleanup is called from @kdamond just before its termination.
101 struct damon_primitive {
102 void (*init)(struct damon_ctx *context);
103 void (*update)(struct damon_ctx *context);
104 void (*prepare_access_checks)(struct damon_ctx *context);
105 unsigned int (*check_accesses)(struct damon_ctx *context);
106 void (*reset_aggregated)(struct damon_ctx *context);
107 bool (*target_valid)(void *target);
108 void (*cleanup)(struct damon_ctx *context);
112 * struct damon_callback Monitoring events notification callbacks.
114 * @before_start: Called before starting the monitoring.
115 * @after_sampling: Called after each sampling.
116 * @after_aggregation: Called after each aggregation.
117 * @before_terminate: Called before terminating the monitoring.
118 * @private: User private data.
120 * The monitoring thread (&damon_ctx.kdamond) calls @before_start and
121 * @before_terminate just before starting and finishing the monitoring,
122 * respectively. Therefore, those are good places for installing and cleaning
125 * The monitoring thread calls @after_sampling and @after_aggregation for each
126 * of the sampling intervals and aggregation intervals, respectively.
127 * Therefore, users can safely access the monitoring results without additional
128 * protection. For the reason, users are recommended to use these callback for
129 * the accesses to the results.
131 * If any callback returns non-zero, monitoring stops.
133 struct damon_callback {
136 int (*before_start)(struct damon_ctx *context);
137 int (*after_sampling)(struct damon_ctx *context);
138 int (*after_aggregation)(struct damon_ctx *context);
139 int (*before_terminate)(struct damon_ctx *context);
143 * struct damon_ctx - Represents a context for each monitoring. This is the
144 * main interface that allows users to set the attributes and get the results
147 * @sample_interval: The time between access samplings.
148 * @aggr_interval: The time between monitor results aggregations.
149 * @primitive_update_interval: The time between monitoring primitive updates.
151 * For each @sample_interval, DAMON checks whether each region is accessed or
152 * not. It aggregates and keeps the access information (number of accesses to
153 * each region) for @aggr_interval time. DAMON also checks whether the target
154 * memory regions need update (e.g., by ``mmap()`` calls from the application,
155 * in case of virtual memory monitoring) and applies the changes for each
156 * @primitive_update_interval. All time intervals are in micro-seconds.
157 * Please refer to &struct damon_primitive and &struct damon_callback for more
160 * @kdamond: Kernel thread who does the monitoring.
161 * @kdamond_stop: Notifies whether kdamond should stop.
162 * @kdamond_lock: Mutex for the synchronizations with @kdamond.
164 * For each monitoring context, one kernel thread for the monitoring is
165 * created. The pointer to the thread is stored in @kdamond.
167 * Once started, the monitoring thread runs until explicitly required to be
168 * terminated or every monitoring target is invalid. The validity of the
169 * targets is checked via the &damon_primitive.target_valid of @primitive. The
170 * termination can also be explicitly requested by writing non-zero to
171 * @kdamond_stop. The thread sets @kdamond to NULL when it terminates.
172 * Therefore, users can know whether the monitoring is ongoing or terminated by
173 * reading @kdamond. Reads and writes to @kdamond and @kdamond_stop from
174 * outside of the monitoring thread must be protected by @kdamond_lock.
176 * Note that the monitoring thread protects only @kdamond and @kdamond_stop via
177 * @kdamond_lock. Accesses to other fields must be protected by themselves.
179 * @primitive: Set of monitoring primitives for given use cases.
180 * @callback: Set of callbacks for monitoring events notifications.
182 * @min_nr_regions: The minimum number of adaptive monitoring regions.
183 * @max_nr_regions: The maximum number of adaptive monitoring regions.
184 * @adaptive_targets: Head of monitoring targets (&damon_target) list.
187 unsigned long sample_interval;
188 unsigned long aggr_interval;
189 unsigned long primitive_update_interval;
191 /* private: internal use only */
192 struct timespec64 last_aggregation;
193 struct timespec64 last_primitive_update;
196 struct task_struct *kdamond;
198 struct mutex kdamond_lock;
200 struct damon_primitive primitive;
201 struct damon_callback callback;
203 unsigned long min_nr_regions;
204 unsigned long max_nr_regions;
205 struct list_head adaptive_targets;
208 #define damon_next_region(r) \
209 (container_of(r->list.next, struct damon_region, list))
211 #define damon_prev_region(r) \
212 (container_of(r->list.prev, struct damon_region, list))
214 #define damon_for_each_region(r, t) \
215 list_for_each_entry(r, &t->regions_list, list)
217 #define damon_for_each_region_safe(r, next, t) \
218 list_for_each_entry_safe(r, next, &t->regions_list, list)
220 #define damon_for_each_target(t, ctx) \
221 list_for_each_entry(t, &(ctx)->adaptive_targets, list)
223 #define damon_for_each_target_safe(t, next, ctx) \
224 list_for_each_entry_safe(t, next, &(ctx)->adaptive_targets, list)
228 struct damon_region *damon_new_region(unsigned long start, unsigned long end);
229 inline void damon_insert_region(struct damon_region *r,
230 struct damon_region *prev, struct damon_region *next,
231 struct damon_target *t);
232 void damon_add_region(struct damon_region *r, struct damon_target *t);
233 void damon_destroy_region(struct damon_region *r, struct damon_target *t);
235 struct damon_target *damon_new_target(unsigned long id);
236 void damon_add_target(struct damon_ctx *ctx, struct damon_target *t);
237 void damon_free_target(struct damon_target *t);
238 void damon_destroy_target(struct damon_target *t);
239 unsigned int damon_nr_regions(struct damon_target *t);
241 struct damon_ctx *damon_new_ctx(void);
242 void damon_destroy_ctx(struct damon_ctx *ctx);
243 int damon_set_targets(struct damon_ctx *ctx,
244 unsigned long *ids, ssize_t nr_ids);
245 int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
246 unsigned long aggr_int, unsigned long primitive_upd_int,
247 unsigned long min_nr_reg, unsigned long max_nr_reg);
248 int damon_nr_running_ctxs(void);
250 int damon_start(struct damon_ctx **ctxs, int nr_ctxs);
251 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
253 #endif /* CONFIG_DAMON */
255 #ifdef CONFIG_DAMON_VADDR
257 /* Monitoring primitives for virtual memory address spaces */
258 void damon_va_init(struct damon_ctx *ctx);
259 void damon_va_update(struct damon_ctx *ctx);
260 void damon_va_prepare_access_checks(struct damon_ctx *ctx);
261 unsigned int damon_va_check_accesses(struct damon_ctx *ctx);
262 bool damon_va_target_valid(void *t);
263 void damon_va_cleanup(struct damon_ctx *ctx);
264 void damon_va_set_primitives(struct damon_ctx *ctx);
266 #endif /* CONFIG_DAMON_VADDR */
268 #endif /* _DAMON_H */