Skip to contentSkip to conversation

mm/damon: add sample modules under samples/damon/

RFC PATCH New SeongJae Park 1 participant 6 messages Nov 13, 2024

Subsystems (2)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon: add sample modules under samples/damon/

DAMON is a kernel framework that provides API for other subsystems or modules in the kernel space. The API functions have their own kernel-doc comments, and DAMON modules including DAMON_SYSFS, DAMON_RECLAIM, and DAMON_LRU_SORT can be used as a refernce usages. However, kernel-doc is not very easy for beginner, and the existing DAMON modules for real usage are bit complicated. Add sample DAMON modules showing basic usage of DAMON kernel API.

Note that some of the code for similar purpose that I used at kernel summit 2021 DAMON live coding session[1] is reused here.

[1] https://linuxplumbersconf.org/event/11/contributions/984/

SeongJae Park (5):
samples: introduce a skeleton of a sample DAMON module for working set
size estimation
samples/damon/wsse: implement DAMON starting and stopping samples/damon/wsse: implement working set size estimation and logging samples/damon: introduce a skeleton of a smaple DAMON module for
proactive reclamation
samples/damon/prcl: implement schemes setup

 MAINTAINERS            |   1 +
 samples/Kconfig        |   2 +
 samples/Makefile       |   2 +
 samples/damon/Kconfig  |  30 +++++++++
 samples/damon/Makefile |   4 ++
 samples/damon/prcl.c   | 138 +++++++++++++++++++++++++++++++++++++++++
 samples/damon/wsse.c   | 120 +++++++++++++++++++++++++++++++++++
 7 files changed, 297 insertions(+)
 create mode 100644 samples/damon/Kconfig
 create mode 100644 samples/damon/Makefile
 create mode 100644 samples/damon/prcl.c
 create mode 100644 samples/damon/wsse.c

base-commit: 981cf179b2760af4215419c67a801ec5d028a546

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

RFC PATCH 1/5 samples: introduce a skeleton of a sample DAMON module for working set size estimation

Add a skeleton for a sample DAMON module that can be used for estimating working set size of a given process. It exposes two module parameters, namely 'pid' and 'enable', which receives what process the module should estimate the working set size of, and whether to start the work or not. Nonetheless, because this is just a skeleton, no real work is implemented. The functionality will be implemented by following commits.

---
 MAINTAINERS            |  1 +
 samples/Kconfig        |  2 ++
 samples/Makefile       |  1 +
 samples/damon/Kconfig  | 17 +++++++++++
 samples/damon/Makefile |  3 ++
 samples/damon/wsse.c   | 69 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 93 insertions(+)
 create mode 100644 samples/damon/Kconfig
 create mode 100644 samples/damon/Makefile
 create mode 100644 samples/damon/wsse.c
MAINTAINERS +1 -0
index 0383fd7e0a40..d61f4f76c3d5 100644
@@ -6256,6 +6256,7 @@ F:	Documentation/mm/damon/
 F:	include/linux/damon.h
 F:	include/trace/events/damon.h
 F:	mm/damon/
+F:	samples/damon/
 F:	tools/testing/selftests/damon/
 
 DAVICOM FAST ETHERNET (DMFE) NETWORK DRIVER
samples/Kconfig +2 -0
index b288d9991d27..8d5a36f0e5d6 100644
@@ -293,6 +293,8 @@ config SAMPLE_CGROUP
 
 source "samples/rust/Kconfig"
 
+source "samples/damon/Kconfig"
+
 endif # SAMPLES
 
 config HAVE_SAMPLE_FTRACE_DIRECT
samples/Makefile +1 -0
index b85fa64390c5..726bb5293486 100644
@@ -39,3 +39,4 @@ obj-$(CONFIG_SAMPLE_KMEMLEAK)		+= kmemleak/
 obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG)	+= coresight/
 obj-$(CONFIG_SAMPLE_FPROBE)		+= fprobe/
 obj-$(CONFIG_SAMPLES_RUST)		+= rust/
+obj-$(CONFIG_SAMPLE_DAMON_WSSE)		+= damon/
samples/damon/Kconfig new file +17 -0
new file mode 100644
index 000000000000..b799e01345c8
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+
+menu "DAMON Samples"
+
+config SAMPLE_DAMON_WSSE
+	bool "DAMON sameple module for working set size estimation"
+	depends on DAMON && DAMON_VADDR
+	help
+	  This builds DAMON sample module for working set size estimation.
+
+	  The module receives a pid, monitor access to the virtual address
+	  space of the process, estimate working set size of the process, and
+	  repeatedly prints the size on the kernel log.
+
+	  If unsure, say N.
+
+endmenu
samples/damon/Makefile new file +3 -0
new file mode 100644
index 000000000000..ccbe93d40130
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+obj-$(CONFIG_SAMPLE_DAMON_WSSE) += wsse.o
samples/damon/wsse.c new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.39.5
author maintainer
#2 lore raw

RFC PATCH 2/5 samples/damon/wsse: implement DAMON starting and stopping

In reply to SeongJae Park

Start running DAMON for the process that user requested to estimate the process via 'pid' parameter, when 'y' is passed to 'enable' parameter. If 'n' is passed, stop running DAMON. Iterating the DAMON monitoring results and estimating the working set side is not yet implemented. It will be implemented by the following commit.

---
 samples/damon/wsse.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
samples/damon/wsse.c +34 -1
index 5912f15181c8..119ac8a2c901 100644
@@ -27,15 +27,48 @@ static bool enable __read_mostly;
 module_param_cb(enable, &enable_param_ops, &enable, 0600);
 MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE_WSSE");
 
+static struct damon_ctx *ctx;
+static struct pid *target_pidp;
+
 static int damon_sample_wsse_start(void)
 {
+	struct damon_target *target;
+
 	pr_info("start\n");
-	return 0;
+
+	ctx = damon_new_ctx();
+	if (!ctx)
+		return -ENOMEM;
+	if (damon_select_ops(ctx, DAMON_OPS_VADDR)) {
+		damon_destroy_ctx(ctx);
+		return -EINVAL;
+	}
+
+	target = damon_new_target();
+	if (!target) {
+		damon_destroy_ctx(ctx);
+		return -ENOMEM;
+	}
+	damon_add_target(ctx, target);
+	target_pidp = find_get_pid(target_pid);
+	if (!target_pidp) {
+		damon_destroy_ctx(ctx);
+		return -EINVAL;
+	}
+	target->pid = target_pidp;
+
+	return damon_start(&ctx, 1, true);
 }
 
 static void damon_sample_wsse_stop(void)
 {
 	pr_info("stop\n");
+	if (ctx) {
+		damon_stop(&ctx, 1);
+		damon_destroy_ctx(ctx);
+	}
+	if (target_pidp)
+		put_pid(target_pidp);
 }
 
 static int damon_sample_wsse_enable_store(
Signature · 1 line
2.39.5
author maintainer
#3 lore raw

RFC PATCH 3/5 samples/damon/wsse: implement working set size estimation and logging

In reply to SeongJae Park

Implement the DAMON-based working set size estimation logic. The logic simply iterates memory regions in DAMON-generated access pattern snapshot for every aggregation, and get the total sum of the size of any region having one or higher 'nr_accesses' count. That is, it assumes any region having one or higher 'nr_accesses' to be a part of the working set. The estimated value is printed to the kernel log.

---
 samples/damon/wsse.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
samples/damon/wsse.c +18 -0
index 119ac8a2c901..a2a94c505938 100644
@@ -30,6 +30,23 @@ MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE_WSSE");
 static struct damon_ctx *ctx;
 static struct pid *target_pidp;
 
+static int damon_sample_wsse_after_aggregate(struct damon_ctx *c)
+{
+	struct damon_target *t;
+
+	damon_for_each_target(t, c) {
+		struct damon_region *r;
+		unsigned long wss = 0;
+
+		damon_for_each_region(r, t) {
+			if (r->nr_accesses > 0)
+				wss += r->ar.end - r->ar.start;
+		}
+		pr_info("wss: %lu\n", wss);
+	}
+	return 0;
+}
+
 static int damon_sample_wsse_start(void)
 {
 	struct damon_target *target;
@@ -57,6 +74,7 @@ static int damon_sample_wsse_start(void)
 	}
 	target->pid = target_pidp;
 
+	ctx->callback.after_aggregation = damon_sample_wsse_after_aggregate;
 	return damon_start(&ctx, 1, true);
 }
 
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

RFC PATCH 4/5 samples/damon: introduce a skeleton of a smaple DAMON module for proactive reclamation

In reply to SeongJae Park

Add a skeleton for a sample DAMON module that can be used for proactively reclaiming cold memory regions of a given process. The skeleton is actually same to working set size estimation sample module, but only the names of the file and symbols are changed.

---
 samples/Makefile       |   1 +
 samples/damon/Kconfig  |  13 +++++
 samples/damon/Makefile |   1 +
 samples/damon/prcl.c   | 120 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 135 insertions(+)
 create mode 100644 samples/damon/prcl.c
samples/Makefile +1 -0
index 726bb5293486..5af6bb8afb07 100644
@@ -40,3 +40,4 @@ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG)	+= coresight/
 obj-$(CONFIG_SAMPLE_FPROBE)		+= fprobe/
 obj-$(CONFIG_SAMPLES_RUST)		+= rust/
 obj-$(CONFIG_SAMPLE_DAMON_WSSE)		+= damon/
+obj-$(CONFIG_SAMPLE_DAMON_PRCL)		+= damon/
samples/damon/Kconfig +13 -0
index b799e01345c8..63f6dcd71daa 100644
@@ -14,4 +14,17 @@ config SAMPLE_DAMON_WSSE
 
 	  If unsure, say N.
 
+config SAMPLE_DAMON_PRCL
+	bool "DAMON sameple module for access-aware proactive reclamation"
+	depends on DAMON && DAMON_VADDR
+	help
+	  This builds DAMON sample module for access-aware proactive
+	  reclamation.
+
+	  The module receives a pid, monitor access to the virtual address
+	  space of the process, find memory regions that not accessed, and
+	  proactively reclaim the regions.
+
+	  If unsure, say N.
+
 endmenu
samples/damon/Makefile +1 -0
index ccbe93d40130..7f155143f237 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_SAMPLE_DAMON_WSSE) += wsse.o
+obj-$(CONFIG_SAMPLE_DAMON_PRCL) += prcl.o
samples/damon/prcl.c new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
2.39.5
author maintainer
#5 lore raw

RFC PATCH 5/5 samples/damon/prcl: implement schemes setup

In reply to SeongJae Park

Implement the DAMOS-based cold memory regions proactive reclamation logic. The logic treats memory regions that assumed to not be accessed at all for five or more seconds as cold, and reclaim as soon as found.

---
 samples/damon/prcl.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
samples/damon/prcl.c +18 -0
index 32ccdd91cf55..cf8c6f29d8a3 100644
@@ -49,6 +49,7 @@ static int damon_sample_prcl_after_aggregate(struct damon_ctx *c)
 static int damon_sample_prcl_start(void)
 {
 	struct damon_target *target;
+	struct damos *scheme;
 
 	pr_info("start\n");
 
@@ -75,6 +76,23 @@ static int damon_sample_prcl_start(void)
 
 	ctx->callback.after_aggregation = damon_sample_prcl_after_aggregate;
 
+	scheme = damon_new_scheme(
+			&(struct damos_access_pattern) {
+			.min_sz_region = PAGE_SIZE,
+			.max_sz_region = ULONG_MAX,
+			.min_nr_accesses = 0,
+			.max_nr_accesses = 0},
+			DAMOS_PAGEOUT,
+			0,
+			&(struct damos_quota){},
+			&(struct damos_watermarks){},
+			NUMA_NO_NODE);
+	if (!scheme) {
+		damon_destroy_ctx(ctx);
+		return -ENOMEM;
+	}
+	damon_set_schemes(ctx, &scheme, 1);
+
 	return damon_start(&ctx, 1, true);
 }
 
Signature · 1 line
2.39.5