Skip to content

Commit 2aca254

Browse files
sjp38akpm00
authored andcommitted
samples/damon: introduce a skeleton of a smaple DAMON module for proactive reclamation
DAMON is not only for monitoring of access patterns, but also for access-aware system operations. For the system operations, DAMON provides a feature called DAMOS (Data Access Monitoring-based Operation Schemes). There is no sample API usage of DAMOS, though. Copy the working set size estimation sample modules with changed names of the module and symbols, to use it as a skeleton for a sample module showing the DAMOS API usage. The following commit will make it proactively reclaim cold memory of the given process, using DAMOS. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: SeongJae Park <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 65cc56d commit 2aca254

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

samples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/
4040
obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/
4141
obj-$(CONFIG_SAMPLES_RUST) += rust/
4242
obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon/
43+
obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon/

samples/damon/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ config SAMPLE_DAMON_WSSE
1414

1515
If unsure, say N.
1616

17+
config SAMPLE_DAMON_PRCL
18+
bool "DAMON sameple module for access-aware proactive reclamation"
19+
depends on DAMON && DAMON_VADDR
20+
help
21+
This builds DAMON sample module for access-aware proactive
22+
reclamation.
23+
24+
The module receives a pid, monitor access to the virtual address
25+
space of the process, find memory regions that not accessed, and
26+
proactively reclaim the regions.
27+
28+
If unsure, say N.
29+
1730
endmenu

samples/damon/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
obj-$(CONFIG_SAMPLE_DAMON_WSSE) += wsse.o
4+
obj-$(CONFIG_SAMPLE_DAMON_PRCL) += prcl.o

samples/damon/prcl.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* proactive reclamation: monitor access pattern of a given process, find
4+
* regiosn that seems not accessed, and proactively page out the regions.
5+
*/
6+
7+
#define pr_fmt(fmt) "damon_sample_prcl: " fmt
8+
9+
#include <linux/damon.h>
10+
#include <linux/init.h>
11+
#include <linux/kernel.h>
12+
#include <linux/module.h>
13+
14+
static int target_pid __read_mostly;
15+
module_param(target_pid, int, 0600);
16+
17+
static int damon_sample_prcl_enable_store(
18+
const char *val, const struct kernel_param *kp);
19+
20+
static const struct kernel_param_ops enable_param_ops = {
21+
.set = damon_sample_prcl_enable_store,
22+
.get = param_get_bool,
23+
};
24+
25+
static bool enable __read_mostly;
26+
module_param_cb(enable, &enable_param_ops, &enable, 0600);
27+
MODULE_PARM_DESC(enable, "Enable of disable DAMON_SAMPLE_WSSE");
28+
29+
static struct damon_ctx *ctx;
30+
static struct pid *target_pidp;
31+
32+
static int damon_sample_prcl_after_aggregate(struct damon_ctx *c)
33+
{
34+
struct damon_target *t;
35+
36+
damon_for_each_target(t, c) {
37+
struct damon_region *r;
38+
unsigned long wss = 0;
39+
40+
damon_for_each_region(r, t) {
41+
if (r->nr_accesses > 0)
42+
wss += r->ar.end - r->ar.start;
43+
}
44+
pr_info("wss: %lu\n", wss);
45+
}
46+
return 0;
47+
}
48+
49+
static int damon_sample_prcl_start(void)
50+
{
51+
struct damon_target *target;
52+
53+
pr_info("start\n");
54+
55+
ctx = damon_new_ctx();
56+
if (!ctx)
57+
return -ENOMEM;
58+
if (damon_select_ops(ctx, DAMON_OPS_VADDR)) {
59+
damon_destroy_ctx(ctx);
60+
return -EINVAL;
61+
}
62+
63+
target = damon_new_target();
64+
if (!target) {
65+
damon_destroy_ctx(ctx);
66+
return -ENOMEM;
67+
}
68+
damon_add_target(ctx, target);
69+
target_pidp = find_get_pid(target_pid);
70+
if (!target_pidp) {
71+
damon_destroy_ctx(ctx);
72+
return -EINVAL;
73+
}
74+
target->pid = target_pidp;
75+
76+
ctx->callback.after_aggregation = damon_sample_prcl_after_aggregate;
77+
78+
return damon_start(&ctx, 1, true);
79+
}
80+
81+
static void damon_sample_prcl_stop(void)
82+
{
83+
pr_info("stop\n");
84+
if (ctx) {
85+
damon_stop(&ctx, 1);
86+
damon_destroy_ctx(ctx);
87+
}
88+
if (target_pidp)
89+
put_pid(target_pidp);
90+
}
91+
92+
static int damon_sample_prcl_enable_store(
93+
const char *val, const struct kernel_param *kp)
94+
{
95+
bool enabled = enable;
96+
int err;
97+
98+
err = kstrtobool(val, &enable);
99+
if (err)
100+
return err;
101+
102+
if (enable == enabled)
103+
return 0;
104+
105+
if (enable)
106+
return damon_sample_prcl_start();
107+
damon_sample_prcl_stop();
108+
return 0;
109+
}
110+
111+
static int __init damon_sample_prcl_init(void)
112+
{
113+
return 0;
114+
}
115+
116+
module_init(damon_sample_prcl_init);

0 commit comments

Comments
 (0)