Skip to content

Commit 4bc0595

Browse files
sj-awstorvalds
authored andcommitted
mm/damon: implement a debugfs-based user space interface
DAMON is designed to be used by kernel space code such as the memory management subsystems, and therefore it provides only kernel space API. That said, letting the user space control DAMON could provide some benefits to them. For example, it will allow user space to analyze their specific workloads and make their own special optimizations. For such cases, this commit implements a simple DAMON application kernel module, namely 'damon-dbgfs', which merely wraps the DAMON api and exports those to the user space via the debugfs. 'damon-dbgfs' exports three files, ``attrs``, ``target_ids``, and ``monitor_on`` under its debugfs directory, ``<debugfs>/damon/``. Attributes ---------- Users can read and write the ``sampling interval``, ``aggregation interval``, ``regions update interval``, and min/max number of monitoring target regions by reading from and writing to the ``attrs`` file. For example, below commands set those values to 5 ms, 100 ms, 1,000 ms, 10, 1000 and check it again:: # cd <debugfs>/damon # echo 5000 100000 1000000 10 1000 > attrs # cat attrs 5000 100000 1000000 10 1000 Target IDs ---------- Some types of address spaces supports multiple monitoring target. For example, the virtual memory address spaces monitoring can have multiple processes as the monitoring targets. Users can set the targets by writing relevant id values of the targets to, and get the ids of the current targets by reading from the ``target_ids`` file. In case of the virtual address spaces monitoring, the values should be pids of the monitoring target processes. For example, below commands set processes having pids 42 and 4242 as the monitoring targets and check it again:: # cd <debugfs>/damon # echo 42 4242 > target_ids # cat target_ids 42 4242 Note that setting the target ids doesn't start the monitoring. Turning On/Off -------------- Setting the files as described above doesn't incur effect unless you explicitly start the monitoring. You can start, stop, and check the current status of the monitoring by writing to and reading from the ``monitor_on`` file. Writing ``on`` to the file starts the monitoring of the targets with the attributes. Writing ``off`` to the file stops those. DAMON also stops if every targets are invalidated (in case of the virtual memory monitoring, target processes are invalidated when terminated). Below example commands turn on, off, and check the status of DAMON:: # cd <debugfs>/damon # echo on > monitor_on # echo off > monitor_on # cat monitor_on off Please note that you cannot write to the above-mentioned debugfs files while the monitoring is turned on. If you write to the files while DAMON is running, an error code such as ``-EBUSY`` will be returned. [[email protected]: remove unneeded "alloc failed" printks] [[email protected]: replace macro with static inline] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: SeongJae Park <[email protected]> Reviewed-by: Leonard Foerster <[email protected]> Reviewed-by: Fernand Sieber <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Amit Shah <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Brendan Higgins <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: David Rientjes <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Fan Du <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Greg Thelen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Joe Perches <[email protected]> Cc: Jonathan Cameron <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Marco Elver <[email protected]> Cc: Markus Boehme <[email protected]> Cc: Maximilian Heyne <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Steven Rostedt (VMware) <[email protected]> Cc: Vladimir Davydov <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 2fcb936 commit 4bc0595

File tree

5 files changed

+457
-0
lines changed

5 files changed

+457
-0
lines changed

include/linux/damon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,12 @@ unsigned int damon_nr_regions(struct damon_target *t);
240240

241241
struct damon_ctx *damon_new_ctx(void);
242242
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);
243245
int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
244246
unsigned long aggr_int, unsigned long primitive_upd_int,
245247
unsigned long min_nr_reg, unsigned long max_nr_reg);
248+
int damon_nr_running_ctxs(void);
246249

247250
int damon_start(struct damon_ctx **ctxs, int nr_ctxs);
248251
int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);

mm/damon/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ config DAMON_VADDR
2020
This builds the default data access monitoring primitives for DAMON
2121
that works for virtual address spaces.
2222

23+
config DAMON_DBGFS
24+
bool "DAMON debugfs interface"
25+
depends on DAMON_VADDR && DEBUG_FS
26+
help
27+
This builds the debugfs interface for DAMON. The user space admins
28+
can use the interface for arbitrary data access monitoring.
29+
30+
If unsure, say N.
31+
2332
endmenu

mm/damon/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
obj-$(CONFIG_DAMON) := core.o
44
obj-$(CONFIG_DAMON_VADDR) += vaddr.o
5+
obj-$(CONFIG_DAMON_DBGFS) += dbgfs.o

mm/damon/core.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,39 @@ void damon_destroy_ctx(struct damon_ctx *ctx)
171171
kfree(ctx);
172172
}
173173

174+
/**
175+
* damon_set_targets() - Set monitoring targets.
176+
* @ctx: monitoring context
177+
* @ids: array of target ids
178+
* @nr_ids: number of entries in @ids
179+
*
180+
* This function should not be called while the kdamond is running.
181+
*
182+
* Return: 0 on success, negative error code otherwise.
183+
*/
184+
int damon_set_targets(struct damon_ctx *ctx,
185+
unsigned long *ids, ssize_t nr_ids)
186+
{
187+
ssize_t i;
188+
struct damon_target *t, *next;
189+
190+
damon_destroy_targets(ctx);
191+
192+
for (i = 0; i < nr_ids; i++) {
193+
t = damon_new_target(ids[i]);
194+
if (!t) {
195+
pr_err("Failed to alloc damon_target\n");
196+
/* The caller should do cleanup of the ids itself */
197+
damon_for_each_target_safe(t, next, ctx)
198+
damon_destroy_target(t);
199+
return -ENOMEM;
200+
}
201+
damon_add_target(ctx, t);
202+
}
203+
204+
return 0;
205+
}
206+
174207
/**
175208
* damon_set_attrs() - Set attributes for the monitoring.
176209
* @ctx: monitoring context
@@ -209,6 +242,20 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
209242
return 0;
210243
}
211244

245+
/**
246+
* damon_nr_running_ctxs() - Return number of currently running contexts.
247+
*/
248+
int damon_nr_running_ctxs(void)
249+
{
250+
int nr_ctxs;
251+
252+
mutex_lock(&damon_lock);
253+
nr_ctxs = nr_running_ctxs;
254+
mutex_unlock(&damon_lock);
255+
256+
return nr_ctxs;
257+
}
258+
212259
/* Returns the size upper limit for each monitoring region */
213260
static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
214261
{

0 commit comments

Comments
 (0)