Skip to contentSkip to conversation

mm/damon: add sample modules

PATCH Applied SeongJae Park 1 participant 6 messages Dec 10, 2024

Subsystems (2)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon: add sample modules

DAMON provides API for other subsystems or modules in the kernel space. The API functions have their own kernel-doc comments, but having working example usages would further helps beginners. There are DAMON modules built using DAMON API, including DAMON_{SYSFS,RECLAIM,LRU_SORT}. But those are for real world usage rather than examples. Add sample DAMON modules for exasmple usages of DAMON API for beginners.

Changes from RFC
(https://lore.kernel.org/[email protected])
- Add minimum age to prcl
- Wordsmith commit message
- Remove module_{license,author,description} that unnecessary for static modules

SeongJae Park (5):
samples: add a skeleton of a sample DAMON module for working set size
estimation
samples/damon/wsse: start and stop DAMON as the user requests 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   | 136 +++++++++++++++++++++++++++++++++++++++++
 samples/damon/wsse.c   | 116 +++++++++++++++++++++++++++++++++++
 7 files changed, 291 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: d344b591933d7f56960f43d9c86effd14559f0f2

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

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

Add a skeleton for a sample DAMON static module that can be used for estimating working set size of a given process. Note that it is a static module since DAMON is not exporting symbols to loadable modules for now. It exposes two module parameters, namely 'pid' and 'enable'. 'pid' will specify the process that the module will estimate the working set size of. 'enable' will receive whether to start or stop the estimation. Because this is just a skeleton, the parameters do nothing, though. The functionalities 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   | 65 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+)
 create mode 100644 samples/damon/Kconfig
 create mode 100644 samples/damon/Makefile
 create mode 100644 samples/damon/wsse.c
MAINTAINERS +1 -0
index 1e930c7a58b1..68d825a4c69c 100644
@@ -6328,6 +6328,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 +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH 2/5 samples/damon/wsse: start and stop DAMON as the user requests

In reply to SeongJae Park

Start running DAMON to monitor accesses of a process that the user specified via 'target_pid' parameter, when 'y' is passed to 'enable' parameter. Stop running DAMON when 'n' is passed to 'enable' parameter. Estimating the working set size from DAMON's monitoring results and reporting it to the user 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 7f2cb76a1a70..2ba0c91baad9 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

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 iterates memory regions in DAMON-generated access pattern snapshot for every aggregation interval 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 reported to the user by printing it to the kernel log.

---
 samples/damon/wsse.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
samples/damon/wsse.c +18 -0
index 2ba0c91baad9..11be25803274 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

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

In reply to SeongJae Park

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.

---
 samples/Makefile       |   1 +
 samples/damon/Kconfig  |  13 +++++
 samples/damon/Makefile |   1 +
 samples/damon/prcl.c   | 116 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 131 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 +116 -0

116 lines not on this page · raw message

Signature · 1 line
2.39.5
author maintainer
#5 lore raw

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

In reply to SeongJae Park

Implement a proactive cold memory regions reclaiming logic of prcl sample module using DAMOS. The logic treats memory regions that not accessed at all for five or more seconds as cold, and reclaim those as soon as found.

---
 samples/damon/prcl.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
samples/damon/prcl.c +20 -0
index b34b9bfed532..c3acbdab7a62 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,25 @@ 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,
+			.min_age_region = 50,
+			.max_age_region = UINT_MAX},
+			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