Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3664989rdh; Mon, 27 Nov 2023 23:35:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNsYxENUmIE1tXNL+Qt3pyeq6GOmUrxtO+OYmffyi/NhN9/ChDbR1ZynFbi8iPaVDvDuos X-Received: by 2002:a05:6a20:144b:b0:18c:382e:48fe with SMTP id a11-20020a056a20144b00b0018c382e48femr12139416pzi.15.1701156899741; Mon, 27 Nov 2023 23:34:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701156899; cv=none; d=google.com; s=arc-20160816; b=U7jD1CGA29ClHJh8pwQ8+U4jTZkHDgKi5OWTlYuDrndC7UuBBcbNWuzLyABxd5l2Xh ikMHbfRszCNeh4FVP509XmEOlSGLiYOVg5THVruaQiqF9nAFFQ4ccPhicpCnPJCBd6YE ZrU5DgbQE1h2Jrn0uZhge+AmjvK3RN74ld/GjCFeS4EMXs3bjGVfU13Ueb1+kD3Z4n0h eHBZn4rjG4hBANaN5wWR7q5tSMvx5uMr0f0BDynFNlPb8lyVeQMpisE2H866WpSW0BZS 5G46E1owoaCutmOwmcXJZsAwP94hZKSUR/tJRpx6btp2rcOHBqVZPRrRFXz4QYVAiOWR QKWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=8tC1vo962immBLHWFedYDSlvhqp+GdE0dvr3eRBBUwo=; fh=SASQAF++FG2r/6pkTp3tLbWgq9BSOmcc8mEq/Ej1QmE=; b=GeEFPOA3fZViLG4gVBbANsI7KHHUqXTdPUlaslwDqDbQBq1fYfTc3cgAkixLCcLke+ iGbX4Xk0MjCLG2zuL6yqedfyuxL3OUmzqLN7woB1j0JWDPd9uzo7DwBpIbbySyBsda9j vGa8jOFuUeeBwGEj4dfpsLqJJQjJi9gwxXr/ds233YyrZjFpkL/aRI7ewJRNmp+J5JXU 8j/TxR55xUHR08dQ5wlhOPNIttdArA+RXT6k0E8FOljYOwnrvC4UHhRa/m0+4xHRtdLm 2V0xRTMtRwicf6vg3YcLiiOirdyLZY/IbVd5kHROwCNN5qwTlgdu9ZToiGQXFW6uHo5i B5MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ES4q3Uev; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id o123-20020a634181000000b005644a9be955si11494335pga.179.2023.11.27.23.34.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 23:34:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ES4q3Uev; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 8A0BE804E6B5; Mon, 27 Nov 2023 23:34:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343748AbjK1Her (ORCPT + 99 others); Tue, 28 Nov 2023 02:34:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234615AbjK1Hep (ORCPT ); Tue, 28 Nov 2023 02:34:45 -0500 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19E90D5A for ; Mon, 27 Nov 2023 23:34:49 -0800 (PST) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-6cd8c3a9edaso1782164b3a.2 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=vger.kernel.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=ES4q3UevYiYeKtZA5iYXPjHUzePk45sQp0QCna3dZovBoNBi+W2MKtLvFRjSfuHy96 yjvtbbBDoCdLdKquKeHfd62GQbP6mftmgnm7Q6pPTPPa49GmU3/jTYDJhuqWWoeTxo1R bI7HE/sFtxLZtZtwHveJR/6HENkOJzeWqmTpzMufQtDwJ7bZ4MZywB1kiiGtJILhlYLj gCep35R6KKqz0ccVSe1Fbf+onH6Uko+O29OqZCeCZfzjniB6BwqT0C4L0/SWP/2ivcJW u9LmUsUhM9QIA2TluiI/5F3bVaOwsCHNTFAtMrOWOH6MP6EXpzy70oJRogrRi9AE2xra u/6A== 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=D0myEWqFQgYbmifloo0mS7FG5EKxOiF5IcwSHd8CBbNsffmV1H4HdoNs9zXPgGUDPH tYW2aRqkqp0tGyWYA3wAXLMhZhHPTNl/LvfJcsK+r1HXOdohDSSqhxrVqtNfJP7eaIQZ MK2MB/jyYh3bCeDhXFVIwdi1opdYNxoH4tA2IYHqE8ZFDHs4tpKSHOenyPGUeiY7gW5w XXiHfUU1dfeXiVLV0NthfwIYWg1hJNGiVXp3D1FylGyOIBqpPw1jbSqJOi/kWggax+O8 lqQ+VN2gzUoHWGZbUs0JT/XQDB1ss/+Ab8dXl0bORN0MxY4o5CPGPkR6/obeh+IKe18K ur4g== X-Gm-Message-State: AOJu0Yy9q5nydXX8zQwF2CpqXJ1FOQV4Fwrnz9EUoSoSQd8EgPrYDJaK ajfpQUY1PlMDaK8f+1fSarU= 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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 27 Nov 2023 23:34:58 -0800 (PST) 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: + * + *