From patchwork Tue Dec 5 02:28:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13479312 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5573C4167B for ; Tue, 5 Dec 2023 02:29:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 957066B0083; Mon, 4 Dec 2023 21:29:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88BEA6B0087; Mon, 4 Dec 2023 21:29:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C7056B0088; Mon, 4 Dec 2023 21:29:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4C8126B0083 for ; Mon, 4 Dec 2023 21:29:13 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2560F1404E5 for ; Tue, 5 Dec 2023 02:29:13 +0000 (UTC) X-FDA: 81531182586.24.434EAEA Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf11.hostedemail.com (Postfix) with ESMTP id C5E5F4000B for ; Tue, 5 Dec 2023 02:29:10 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BOwMnIXZ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701743351; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IwYULYvPao66nYRi+bI6jWlFfhnrXGS+KvGsljmfapM=; b=k9Y9+QUwS7qnTnHCkY4zNBjivEbOy6FUYtybzsIBWFJPCmwqw+OWtj2Xo/09joV42JnFHE jGRFnRJGNvZ80ahyPqSIu23U8Z0gIfSTKDq7C7VUIMU6aOw+k/urzGT/wT9eZ5Oc0DyMVe XRiSjMUWRhGzC69xGBwNTVyWzBBlHic= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BOwMnIXZ; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701743351; a=rsa-sha256; cv=none; b=nyWocNpbJO/vA94k1dpSpT5hc2brmpUz7p3/XVHaUKT6g5j2D5hCF0pXJfZ5eoBD+ShehJ MxgjaVoT4KUcxyDTglNAq9s2YOS+ezc9Uc2vOnkQ/Tn6Axko7xYVBf77x3MfG0woYgjSMv X5FK2NSkovVT8brLHXEL6phOFlwoFBU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id BAEDACE1309; Tue, 5 Dec 2023 02:29:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D7EEC433C7; Tue, 5 Dec 2023 02:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701743347; bh=ZeGpXOS+wd63sq2lkChRaopNGfsPvBCMhfOksZfRY9A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BOwMnIXZYJDQI1HCnzULsLA0qvMK46eLCyBuzxRAQ6Eg5fnT+Bu5C5oDT7TdjE7y5 YaJTK/X0+IlM557gdEGgjHVbmFQeG31hFfb5Xgq3bWipkdAyprSD3cg8KC4ExBtIm9 67SJizuZmmS5SZSAqzTXjHvccTBV0FMQ3Kmf5OaV4cLgHq3axODascmXu22QuKfjb+ MHja/A2oBVUWaxLthN3+mn1B184NYu0+zaQ7PKXl9ofdxY+74fph6BlZIxV7AwG2vp IKWJ1HTJtl+Up9qgi4EhVkOAeMreCh0DBnL41QsKvj/A43N/nyjM64YhDuZ4q75Zvv U4i5KBTX3I5sw== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] samples: add working set size estimation DAMON sample module Date: Tue, 5 Dec 2023 02:28:57 +0000 Message-Id: <20231205022858.1540-3-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205022858.1540-1-sj@kernel.org> References: <20231205022858.1540-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C5E5F4000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: d96rems4y9zuee7qbh8eag3da64ko4d6 X-HE-Tag: 1701743350-316423 X-HE-Meta: U2FsdGVkX19HVEe2Rvkbyiizu5Qi6dMnKnLytc4InI2sPzWHtJipcUKt6xb8D6dnMZvc4X14RfLL1xXfONYpn/pDpDx2HQ/vQoWKInGq7KClm15nonCjkfrdgf8Ur8PpZqlE4dWyOjHGWsWeTwvuwY8gj8xOkTwotq4zq5QFH06Bum0S88xXngJUSy+khigHqo4dNobe+nTKRWenKtLNM20y2Cu4eNP5UyDtTWZvw52y4K8g0lu75SelSXniHgGgUuUgoPU1Jo4sQRaH5hOLIVQaDmkP7W2IbjJ/7ZemXFy+615UhqjPbtgqBvP7jY0OS3vtpxu4j4HiZ/kgRV1PtQ64LPstDX0yVNN09g1y1e7NIjaECZDi7sWd6qxrjmqNKXBHWu7HV0CfP3kpJKDyA3v+MZeMPViVqpIH7p3N3SraSuCCKpBZNFn7do0ZArOCWqW0O4vqEacsEZHXaAY3nY/R3MBDriM8wO+q59FcgipqbO6/T8TnPAKdEuCL/TmrDNhBful1yC6iSv1hVrY5oUiELYhJHugjWDNgv9Tbw2E0HgFLBt52kmFf3vzlw+Iv/jmxBg3XxUzW6Z+DikUTaOljnGeZkGrjK+EavNRQH7Wzbmis9PQSOR95u6cqVWf9mGndEXkZC243EiOqIzNfqRqicjwJb7cQP++asGxTUI/vJfpUkOrVBTxHkaOxVdUEVEsmuH3OOkirrxtK0YEzvVBwTMcofWnNNeqVgpfQ9aXWTGZIuwuU/6l6j0a6i8MoI3U2UQbcn2wQ0V13stvOuXSgP70M8oCqGpdQKA0V90PxmuXoiCHTW6MabhO0RmbgNQfThieQj4XsUyYejtG0zuiTIG/3jVNAqCyLZjQFvL+Jee13BsouauUW6Inth3IIAehTE+xIvQpdbnPPmHLUqDcBEZxraIwJjjdKik0xQhfAqmAoA+2llRTA1NflEHhv8YGrQMsMcTtnIcwElUT RW54Gv/y P/XpdN9xPLti8efZS9fSyGqX0uM1u22cQaI9DVXuM31+PxWj4hshjHslWGZCt1qlez0kUazaQdWPTq1w54Cpu32KXcAU9LspvQ85B1wdW/cW7he59BxS50nAKYOUNdUUSxdoLisGSKyvkrj7qDwBtGXTgemXzCueh3X9PaTs3QmM5I9sjLCKlRkbJrQaMvSoY7n2cfe86r1FLQS/97x0fBA0ZJc4iut8L6wCp0dED/MkbHOibJ9iTJUXz1peaSN/bH6iWY4yGfirdSXzhCqBsVibU2x9ptHI9+hISiOUdPCgyBGo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add a sample DAMON application kernel module, namely damon_sample_wsse. It is same to the one of the example kernel modules that presented at kernel summit 2021 with 10 minutes live coding[1] but updated for the latest DAMON interface. The sample module receives a pid of a process to monitor the access, monitors the access to the virtual address space of the process, calculate estimated working set size, and repeatedly print it on the kernel log. [1] https://linuxplumbersconf.org/event/11/contributions/984/ Signed-off-by: SeongJae Park --- MAINTAINERS | 1 + samples/Kconfig | 2 + samples/Makefile | 2 + samples/damon/Kconfig | 17 +++++++ samples/damon/Makefile | 2 + samples/damon/damon_sample_wsse.c | 85 +++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 samples/damon/Kconfig create mode 100644 samples/damon/Makefile create mode 100644 samples/damon/damon_sample_wsse.c diff --git a/MAINTAINERS b/MAINTAINERS index d1fcae09c910..5a44258810ef 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5764,6 +5764,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 diff --git a/samples/Kconfig b/samples/Kconfig index b288d9991d27..8d5a36f0e5d6 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -293,6 +293,8 @@ config SAMPLE_CGROUP source "samples/rust/Kconfig" +source "samples/damon/Kconfig" + endif # SAMPLES config HAVE_SAMPLE_FTRACE_DIRECT diff --git a/samples/Makefile b/samples/Makefile index b85fa64390c5..5af6bb8afb07 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -39,3 +39,5 @@ 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/ +obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon/ diff --git a/samples/damon/Kconfig b/samples/damon/Kconfig new file mode 100644 index 000000000000..f8b9a73717f9 --- /dev/null +++ b/samples/damon/Kconfig @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 + +menu "DAMON Samples" + +config SAMPLE_DAMON_WSSE + tristate "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 pid of a process, monitor access to the virtual + address space of the process, calculate the estimated working set + size of the process, and repeatedly print the size on the kernel log. + + If unsure, say N. + +endmenu diff --git a/samples/damon/Makefile b/samples/damon/Makefile new file mode 100644 index 000000000000..1ace3ad4aff0 --- /dev/null +++ b/samples/damon/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon_sample_wsse.o diff --git a/samples/damon/damon_sample_wsse.c b/samples/damon/damon_sample_wsse.c new file mode 100644 index 000000000000..897e6b136a1a --- /dev/null +++ b/samples/damon/damon_sample_wsse.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * working set size estimation: monitor access pattern of given process and + * print estimated working set size (total size of regions that showing some + * access). + */ + +#define pr_fmt(fmt) "damon_sample_wsse: " fmt + +#include +#include +#include +#include + +static int target_pid __read_mostly; +module_param(target_pid, int, 0600); + +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 __init damon_sample_wsse_init(void) +{ + struct damon_target *target; + + pr_info("Hello\n"); + + 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; + + ctx->callback.after_aggregation = damon_sample_wsse_after_aggregate; + return damon_start(&ctx, 1, true); +} + +static void __exit damon_sample_wsse_exit(void) +{ + pr_info("Goodbye\n"); + if (ctx) { + damon_stop(&ctx, 1); + damon_destroy_ctx(ctx); + } + if (target_pidp) + put_pid(target_pidp); +} + +module_init(damon_sample_wsse_init); +module_exit(damon_sample_wsse_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("SeongJae Park"); +MODULE_DESCRIPTION("DAMON sample module for working set size estimation");