From mboxrd@z Thu Jan 1 00:00:00 1970 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 5285AC4167B for ; Tue, 28 Nov 2023 07:34:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C68986B0289; Tue, 28 Nov 2023 02:34:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BF0726B028B; Tue, 28 Nov 2023 02:34:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A69CF6B028D; Tue, 28 Nov 2023 02:34:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8FA796B0289 for ; Tue, 28 Nov 2023 02:34:53 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6371714011E for ; Tue, 28 Nov 2023 07:34:53 +0000 (UTC) X-FDA: 81506551266.17.F928540 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf29.hostedemail.com (Postfix) with ESMTP id 1F99812000A for ; Tue, 28 Nov 2023 07:34:49 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="fcvXEGu/"; spf=pass (imf29.hostedemail.com: domain of cuiyangpei@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=cuiyangpei@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701156890; 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:references:dkim-signature; bh=8tC1vo962immBLHWFedYDSlvhqp+GdE0dvr3eRBBUwo=; b=n65dd+QIEmiF10rCHqW1/qtU3EJJr81BgDqHqQ8sl4giAywC7VKzbgJeMtOL5T4kXXolup hdsr7wN0Ls7HQ4yfh+wsWBCa3FDqVMxLphw8iguMyTaj/dBkXSWMlJO+dQPrv6vVvXOE5Q 2KedsORAQg/azMMCPqmUtYA26nMnHq8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="fcvXEGu/"; spf=pass (imf29.hostedemail.com: domain of cuiyangpei@gmail.com designates 209.85.210.178 as permitted sender) smtp.mailfrom=cuiyangpei@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701156890; a=rsa-sha256; cv=none; b=O0jXvJd2/UNRLBJU3674lOMR6dENfwobUCdlH91+IZ+ps+vRHUfpwcKq9aWQnlWig2nZsV av5S3mHFYdSfQHxxlNyrbAKhkZ9DvDiz79QxplpKeWd4iKZbkJ/IxH2NbvQx1W5wew9bc6 FpjbNMyi4yCW7mJG6VZ1kZtoVpetBBc= Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6cd97c135e8so1277481b3a.0 for ; Mon, 27 Nov 2023 23:34:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701156889; x=1701761689; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8tC1vo962immBLHWFedYDSlvhqp+GdE0dvr3eRBBUwo=; b=fcvXEGu/B92L+GKHMJJyUwSh6wvU7pQuGR5KRHc45SXmB2xPg+yylwHeNVJf+NfCcR 1/hPW3FbWRPrTcLTIzd8LFwh3/GThRrUBNGDfJArC8SdW68lqnDX7Wo0v8pcDI/OvGrN v+x0r4PPBvXHkUyja+oPcsdtzBvehc5GNEjsg63krr3dfldLrMtbX/otHfpOfgVQ0sKc vDS4d84+6uZboxAvyntTtpgyWbgXUd3fAfUCXhWp19Lfs+qq4i5SNsb0Jmj4S4/7VLm3 ZrXdwj9FyH1laa3lSUzZMcl3ASuWsfI9eyxu8ruavjfshaDIZDbqMN0Fp9Kfl6LwBQfw 1OzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701156889; x=1701761689; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8tC1vo962immBLHWFedYDSlvhqp+GdE0dvr3eRBBUwo=; b=q2FNDDfqtwV5OqFboHNzK8aKQbrcUyZNZEbRdmUv+nSLmePr6fk7TTpOXbNiNbLqpW F09dsC6nEfdtwQ1UqBE6cSiCI94tRUog7SRknzb1EReLQ5b3T/+2sJIGy5/Xi2g22Pcz mx4Ag/WIZ1c+HqFsjJexPJBLio59I7cjeKBA3KhMIBkQcvfCTXP65ysBP3HvokzpHvkf wHv3w2AcScFCBpFr0tNRjvIFLV1kaPO7krvcHEKgzgXQbRfzQKOoOr/aQrMhQ59+yi6S 4Dqmi3M2EI0fOuHzhb0fmZFVDUXaECDsxij6MMrGsQ8rYNQme/d5p2ksCtjhg3YjmfGR K8XA== X-Gm-Message-State: AOJu0YwR3ARLfuPxIHc/efBn/rs31jp+qBIkPWaVvCXK/cMEdWFdp5Qq nO6/eotdrXfXBniwRRaDghw= X-Google-Smtp-Source: AGHT+IEwSoEUaKAMjeNT/B4UJ5bMwZdWDAfLciF7L1mX1p1kKd8ibEh0049/OWwsh4tfQl6RWFuIfQ== X-Received: by 2002:a05:6a00:21d2:b0:68e:3772:4e40 with SMTP id t18-20020a056a0021d200b0068e37724e40mr14373197pfj.3.1701156888736; Mon, 27 Nov 2023 23:34:48 -0800 (PST) Received: from cuiyangpei.mioffice.cn ([43.224.245.227]) by smtp.gmail.com with ESMTPSA id d25-20020aa78159000000b006cb4fa1174dsm8331333pfn.124.2023.11.27.23.34.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 23:34:48 -0800 (PST) From: cuiyangpei X-Google-Original-From: cuiyangpei To: sj@kernel.org, akpm@linux-foundation.org, damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: cuiyangpei Subject: [PATCH 1/2] mm/damon/sysfs: Implement recording feature Date: Tue, 28 Nov 2023 15:34:39 +0800 Message-ID: <20231128073440.11894-1-cuiyangpei@xiaomi.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 1F99812000A X-Rspam-User: X-Stat-Signature: d96cdk8su44op997hpyez7ejtn6wnzmb X-Rspamd-Server: rspam01 X-HE-Tag: 1701156889-107398 X-HE-Meta: U2FsdGVkX1+TN1sAdLlxCBvT5317TEK7MsbVTmkTgxqKLVDWqF+CnRj0sPKtjCjHdHggt3VjxK81mU5DKfAdK40j58RK2Fsj6Jl438iXrCzgm9n9Ykm86p45AuYS2RNx88FWikiujHaHt9TdadTVUfJfQQ33fwagP14QOlX5qbvCRe3Hq5StWsaOcvmcZLT7mo4YbL0MHDIL8eB/VlugzrHMh9X3+HyKN8UjAqkJ3X887VjnwDnCt9QGGT8kuaxdc71bdWLJC2VhLCjNBOUm/rxpnUK1Go3saJY6xNfK2FNgx3mqh5yFS5IUPi+AvsFcwz8sKS8Io+WpW5Y5flzb17hBEFd6iX+ai98WyYYvh4lPHe9f0VIJrdO1A5l4jWzxyJ3AZnLpx5bSNu+kKH36ZN9NCyMcYFym86VD64i33Lg7+p6KTYyigUPtyjoYEkB9I6l3ZYS+OMcTluRk4oU8QuU7JHqlCmD0NgT6Rv1sX6k4hA0Hmc3vAjv2OIFzZt7a2q5yJSaNhar7uKO3aC8uKusAajFRgw2LOEV8xxpLLY+sLmqAiG+T3hq4hu66dha5ZJSR7uu5DjxNqyUClDawGsmD7L3O2lj1LFt9JlpxcJuUFPea3OWblG19McuLsX00Qqza/LqNh7AoGy6PI5s9Kue/hamiKCwzGeOFZAahTSf5kq77vYkk4gwdAsXiN+puzX5UX5j4suba6FyrMa4iF7SACeQUgfmOyDgoBimeP8fGk0UNNLC4mL7RL/pmq9FOvPJWcLfo1glPkFsn9A434QyEs3/HTtqH8nbQ2f+uNxLtuAcDGGde97z9jk9bU1Dh+sBfcSK1uLdt4FrgNB5z1d20hI9fC+fcN5KTR+fQRWCzzIS/saBUWsrVrUlV85CO4rg5/urBH1YfpWb8im7uC5H/+1ALTcP+SOh2UnzAOwjjelWF8BbR87A475bZUqPVsUvUuNWBl163Kp9IYZb WS3vxubQ SJuA6pC8wg9+5CBN+9RRquKZQPoT4V9tUE3K6o2bLaoEIa6SDHmqMxe+iHZuAaD0Kh1XRoDsH1Yp6O8ieC9PwCh9hPPZ+YdqYdgb0KbdrKFOmG6Z952vUwk443YB2HxBWJByCjmohf/q7or0L7fS3GGyZAZGIEcUFokTQG+6UTpXMs9yKTo6ebs4RUgCJYJ676t2wCS2V7ZAN3CE0WrhpVkTANDfwEsFhXk0i7GKNvvhboYkblXB7sY7YgLf75Hw1/1JLqYoaJnh9/2ym+WMJqBQSygAcUhCm7oi19qoE6MwkWjduoFQODTga+87IMjpzYakHSZ601tR2B/JDuL/4udcObVafmg13c4mm9NIUrACKb41zKMoJaHChICkpcw8f8hP/f6Fwfb4odH9GFRGhbErJQL+Cx/QF66Llpx/GexkcflPtPK6OGHRJsWX1HNRqM5G2fyqtJXx1igJ+nlq0QaOuV11kcAoB93T7JrnWd71559Qxw20hldKLQ4lBmq35EE4b 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: The user space users can control DAMON and get the monitoring results via implements 'recording' feature in 'damon-sysfs'. The feature can be used via 'record' and 'state' file in the '/kernel/mm/ damon/admin/kdamonds/N/' directory. The file allows users to record monitored access patterns in a text file. Firstly, users set the size of the buffer and the path of the result file by writing to the ``record`` file. Then the recorded results are first written in an in-memory buffer and flushed the recorded results to a file in batch by writing 'record' to the ``state`` file. For example, below commands set the buffer to be 4 KiB and the result to be saved in ``/damon.txt``. :: # cd /kernel/mm/damon/admin/kdamonds/N # echo "4096 /damon.txt" > record # echo "record" > state Signed-off-by: cuiyangpei --- .../ABI/testing/sysfs-kernel-mm-damon | 20 +- include/linux/damon.h | 11 + mm/damon/sysfs.c | 282 ++++++++++++++++++ 3 files changed, 307 insertions(+), 6 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-damon b/Documentation/ABI/testing/sysfs-kernel-mm-damon index b35649a46a2f..819534dcfb6c 100644 --- a/Documentation/ABI/testing/sysfs-kernel-mm-damon +++ b/Documentation/ABI/testing/sysfs-kernel-mm-damon @@ -25,15 +25,23 @@ Description: Writing 'on' or 'off' to this file makes the kdamond starts or stops, respectively. Reading the file returns the keywords based on the current status. Writing 'commit' to this file makes the kdamond reads the user inputs in the sysfs files - except 'state' again. Writing 'update_schemes_stats' to the - file updates contents of schemes stats files of the kdamond. - Writing 'update_schemes_tried_regions' to the file updates - contents of 'tried_regions' directory of every scheme directory - of this kdamond. Writing 'update_schemes_tried_bytes' to the - file updates only '.../tried_regions/total_bytes' files of this + except 'state' again. Writing 'record' to this file makes the + kdamond saves the monitoring results to file specified by the + /record file. Writing 'update_schemes_stats'to the file updates + contents of schemes stats files of the kdamond. Writing + 'update_schemes_tried_regions' to the file updates contents of + 'tried_regions' directory of every scheme directory of this + kdamond. Writing 'update_schemes_tried_bytes' to the file + updates only '.../tried_regions/total_bytes' files of this kdamond. Writing 'clear_schemes_tried_regions' to the file removes contents of the 'tried_regions' directory. +What: /sys/kernel/mm/damon/admin/kdamonds//record +Date: Nov 2023 +Contact: Ping Xiong +Description: Writing a string '4096 /damon.txt' to this file makes the + user to record monitored access patterns in a text file. + What: /sys/kernel/mm/damon/admin/kdamonds//pid Date: Mar 2022 Contact: SeongJae Park diff --git a/include/linux/damon.h b/include/linux/damon.h index ab2f17d9926b..6495513cc6de 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -19,6 +19,17 @@ /* Max priority score for DAMON-based operation schemes */ #define DAMOS_MAX_SCORE (99) +#define MIN_RECORD_BUFFER_LEN 1024 +#define MAX_RECORD_BUFFER_LEN (4 * 1024 * 1024) +#define MAX_RFILE_PATH_LEN 256 + +struct sysfs_recorder { + unsigned char *rbuf; + unsigned int rbuf_len; + unsigned int rbuf_offset; + char *rfile_path; +}; + /* Get a random number in [l, r) */ static inline unsigned long damon_rand(unsigned long l, unsigned long r) { diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index e27846708b5a..7a7d41e609e3 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -994,6 +994,8 @@ enum damon_sysfs_cmd { DAMON_SYSFS_CMD_OFF, /* @DAMON_SYSFS_CMD_COMMIT: Update kdamond inputs. */ DAMON_SYSFS_CMD_COMMIT, + /* @DAMON_SYSFS_CMD_RECORD: Save the monitoring results to file. */ + DAMON_SYSFS_CMD_RECORD, /* * @DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: Update scheme stats sysfs * files. @@ -1025,6 +1027,7 @@ static const char * const damon_sysfs_cmd_strs[] = { "on", "off", "commit", + "record", "update_schemes_stats", "update_schemes_tried_bytes", "update_schemes_tried_regions", @@ -1349,6 +1352,160 @@ static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond) kdamond->contexts->contexts_arr[0]); } +/* + * Flush the content in the result buffer to the result file + */ +static int sysfs_flush_rbuffer(struct sysfs_recorder *rec) +{ + ssize_t sz; + loff_t pos = 0; + struct file *rfile; + + if (!rec->rbuf_offset) + return 0; + + rfile = filp_open(rec->rfile_path, + O_CREAT | O_RDWR | O_APPEND | O_LARGEFILE, 0644); + if (IS_ERR(rfile)) { + pr_err("Cannot open the result file %s\n", + rec->rfile_path); + return PTR_ERR(rfile); + } + + while (rec->rbuf_offset) { + sz = kernel_write(rfile, rec->rbuf, rec->rbuf_offset, &pos); + if (sz < 0) { + filp_close(rfile, NULL); + return sz; + } + rec->rbuf_offset -= sz; + } + filp_close(rfile, NULL); + + return 0; +} + +/* + * Write a data into the result buffer + */ +static void sysfs_write_rbuf(struct damon_ctx *ctx, char *data, int size) +{ + struct sysfs_recorder *rec = ctx->callback.private; + + if (!rec->rbuf_len || !rec->rbuf || !rec->rfile_path) + return; + if (rec->rbuf_offset + size > rec->rbuf_len) + sysfs_flush_rbuffer(ctx->callback.private); + if (rec->rbuf_offset + size > rec->rbuf_len) { + pr_warn("%s: flush failed, or wrong size given(%u, %d)\n", + __func__, rec->rbuf_offset, size); + return; + } + + memcpy(&rec->rbuf[rec->rbuf_offset], data, size); + rec->rbuf_offset += size; +} + +static unsigned int nr_damon_targets(struct damon_ctx *ctx) +{ + struct damon_target *t; + unsigned int nr_targets = 0; + int count = 0; + + damon_for_each_target(t, ctx) { + count++; + nr_targets++; + } + + return nr_targets; +} + +/* + * Store the aggregated monitoring results to the result buffer + * + * The format for the result buffer is as below: + * + *