Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3665562rdh; Mon, 27 Nov 2023 23:36:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJNbpN2r2BrcAR986Qwd09mi+zVl7wxMyNozTjY9GW2J4frKVKs/5OZleSvRNnG6WbZ+vB X-Received: by 2002:a05:6a20:7f98:b0:188:290d:17dc with SMTP id d24-20020a056a207f9800b00188290d17dcmr15281892pzj.60.1701157001648; Mon, 27 Nov 2023 23:36:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701157001; cv=none; d=google.com; s=arc-20160816; b=D/9AhhJ1HEYLYBXHfPBln9gpqSJ8bBowuDgZ90C0rv3PYzGGkbU0UCT3kpNQqSpGPh /0Kz3EVaQeAUhx/4jXciU+fKFT/M5VnW69SgM3/R89GZtuI+ezuiG1+J9Dh3LmJEAcjK rAwMMZ4PqsbZz2d0syKXQ2dRzEJDha8O7cpGliBJfqd/A1bDaGLXSU8HGFjDjbGlBJ/k oC5AHNIv7MYIhvOl2yHkeoUzi0tJDF3UxhkgY7NT/anlJuopktmNRzIBe4F+VwMGjuI+ YcLW4PD0RfTuz+VGN2pWrBePVv8eCGkidiAxBCiPNOyVGoprFJnF/ZtkPMmd3wLuQGnD HoYQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZkYXobmQORad3/hpJazSzxaZjqFiG7V4HM30XOOtlqU=; fh=SASQAF++FG2r/6pkTp3tLbWgq9BSOmcc8mEq/Ej1QmE=; b=ZOhb7/ukbLMIQAAZKF55SBtQNFibLfJ2VJcF2tb2bdm14//KlcZclL/Prmar53AV0c LMOk27PfHJlZD8qVcQbmEkQZSIfMH8z+//q88ALA2UyQUjr7VJZwVZoST5eP+wHQPnEZ NqIDpVeq+IUgABY4PpHduMwVtQUQFnnmCviywREWfNKBWwlLx3WU2EzK603gvMwBkZ8y bWwW+oNYtBdKX70lB0mJkVITJKCPCjn+sgH/KoBAD1i8cfbQcV5pBg/WS9rigplTy0lP jg6Wio5zp7n1WJ1Ih3H49u1CWSMdGnx9TdKRsS3xi1GkP7kW+txXP2hFvMjZB0Pd+W/k jypA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Wei5KdWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id oj13-20020a17090b4d8d00b002858a23002asi10106619pjb.20.2023.11.27.23.36.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 23:36:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Wei5KdWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id D655680A6888; Mon, 27 Nov 2023 23:35:47 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343885AbjK1HfU (ORCPT + 99 others); Tue, 28 Nov 2023 02:35:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343814AbjK1HfK (ORCPT ); Tue, 28 Nov 2023 02:35:10 -0500 Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DBDC19B7 for ; Mon, 27 Nov 2023 23:35:07 -0800 (PST) Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-5cc77e23218so52166877b3.3 for ; Mon, 27 Nov 2023 23:35:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701156906; x=1701761706; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZkYXobmQORad3/hpJazSzxaZjqFiG7V4HM30XOOtlqU=; b=Wei5KdWt6COAubw+NNgbXg5tQnBd29H6QJX7Dofrk6R4zIP5tul8Fkun6CIa6JRsYB WiT+CnVUf+7AMsQhjmwR5ZG+7XgFft5FaD8ZiQKsE0URJg8v9r2SxZHLGcVl+2KZ20xt dRhxLXmi8yJ3SfbonkKRS86oOZaRM1M9eacqV/kt6Metx3BrXkvG5igjjrFodnuSExDx HIOr+ARqhQLbwB+nJSM6UtcVk2PFMZgtZi9PPmmfdR9spB/2IpmakkXQ5E/VFRm8HZyk ynv3MvtTlh70aZmYjgetwsyvZ9PbiyQx734sO6AK6tknYcU2KbJp875dVPLeavxvxYJ0 KM7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701156906; x=1701761706; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZkYXobmQORad3/hpJazSzxaZjqFiG7V4HM30XOOtlqU=; b=HvWr4gfH4szweGgm1RyeUwqulQeO4Kt5vGH+//ciSjRprVkHyAHm0Y8DH73FAxpTKM gNYw9VbvW74TEVWQEalbZ6s5EYlJm1wfEpcGFEexcQn5XfN0m6NKYd1NuyNDqWPIE6Jy RL60omU3VagSPmPY6yWKrr+Ujros8EMdfq/TxG3O7cZeeRnfX9q4cgUh0rQ8B0c5o9KJ 8413FY8txNJQRBI1r3tqoCuNgp21c3t+P1Hvws7lDobfMFgVX4mIHl5jHppPYSKRDyVK Hab/hqyuJ8G0lXX0ApZgmbMzFcC7JuaUbwnGsZjR9U52MfssmVrscLrYPfmRj7f9BOhT 1TGQ== X-Gm-Message-State: AOJu0YzeYmNxisvhRNFecHJMxqjSpW61FdzDKk0oCxu1PeW3hNXdZZsP I1fCHF6piOIoJgor0H+cumM= X-Received: by 2002:a81:af10:0:b0:5cd:f783:df03 with SMTP id n16-20020a81af10000000b005cdf783df03mr10556684ywh.23.1701156906369; Mon, 27 Nov 2023 23:35:06 -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.35.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 23:35:06 -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 2/2] mm/damon/core: add sysfs nodes to set last_nr_accesses weight Date: Tue, 28 Nov 2023 15:34:40 +0800 Message-ID: <20231128073440.11894-2-cuiyangpei@xiaomi.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231128073440.11894-1-cuiyangpei@xiaomi.com> References: <20231128073440.11894-1-cuiyangpei@xiaomi.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email 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 (pete.vger.email [0.0.0.0]); Mon, 27 Nov 2023 23:35:48 -0800 (PST) The date access frequency is cleared every time the aggregation interval is reached. In order to refer to the past time access frequency, the current access frequency can be calculated by setting the weight of the last access frequency. Signed-off-by: cuiyangpei --- .../ABI/testing/sysfs-kernel-mm-damon | 6 ++++ include/linux/damon.h | 3 ++ mm/damon/core.c | 29 +++++++++++++++++ mm/damon/sysfs.c | 31 +++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-damon b/Documentation/ABI/testing/sysfs-kernel-mm-damon index 819534dcfb6c..8367e26bf4da 100644 --- a/Documentation/ABI/testing/sysfs-kernel-mm-damon +++ b/Documentation/ABI/testing/sysfs-kernel-mm-damon @@ -74,6 +74,12 @@ Description: Writing a keyword for a monitoring operations set ('vaddr' for Note that only the operations sets that listed in 'avail_operations' file are valid inputs. +What: /sys/kernel/mm/damon/admin/kdamonds//contexts//monitoring_attrs/last_nr_accesses_weight +Date: Nov 2023 +Contact: Ping Xiong +Description: Writing a value to this file sets last_nr_accesses weight + of the DAMON context. Reading this file returns the value. + What: /sys/kernel/mm/damon/admin/kdamonds//contexts//monitoring_attrs/intervals/sample_us Date: Mar 2022 Contact: SeongJae Park diff --git a/include/linux/damon.h b/include/linux/damon.h index 6495513cc6de..71e67a9d0996 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -18,6 +18,8 @@ #define DAMON_MIN_REGION PAGE_SIZE /* Max priority score for DAMON-based operation schemes */ #define DAMOS_MAX_SCORE (99) +/* set last_nr_accesses weight */ +#define LAST_NR_ACCESSES_WEIGHT 0 #define MIN_RECORD_BUFFER_LEN 1024 #define MAX_RECORD_BUFFER_LEN (4 * 1024 * 1024) @@ -522,6 +524,7 @@ struct damon_attrs { unsigned long ops_update_interval; unsigned long min_nr_regions; unsigned long max_nr_regions; + unsigned int last_nr_accesses_weight; }; /** diff --git a/mm/damon/core.c b/mm/damon/core.c index 630077d95dc6..230afcceea22 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -1142,6 +1142,34 @@ static void kdamond_apply_schemes(struct damon_ctx *c) } } +static unsigned int __calculate_nr_accesses(struct damon_ctx *c, struct damon_region *r) +{ + unsigned int rem_old, rem_new; + unsigned int res; + unsigned int weight = c->attrs.last_nr_accesses_weight; + + res = div_u64_rem(r->nr_accesses, 100, &rem_new) * (100 - weight) + + div_u64_rem(r->last_nr_accesses, 100, &rem_old) * weight; + + if (rem_new) + res += rem_new * (100 - weight) / 100; + if (rem_old) + res += rem_old * weight / 100; + + return res; +} + +static void kdamon_update_nr_accesses(struct damon_ctx *c) +{ + struct damon_target *t; + struct damon_region *r; + + damon_for_each_target(t, c) { + damon_for_each_region(r, t) + r->nr_accesses = __calculate_nr_accesses(c, r); + } +} + /* * Merge two adjacent regions into one region */ @@ -1469,6 +1497,7 @@ static int kdamond_fn(void *data) max_nr_accesses = ctx->ops.check_accesses(ctx); if (ctx->passed_sample_intervals == next_aggregation_sis) { + kdamon_update_nr_accesses(ctx); kdamond_merge_regions(ctx, max_nr_accesses / 10, sz_limit); diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 7a7d41e609e3..2946b0e91ad8 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -544,6 +544,7 @@ struct damon_sysfs_attrs { struct kobject kobj; struct damon_sysfs_intervals *intervals; struct damon_sysfs_ul_range *nr_regions_range; + unsigned int last_nr_accesses_weight; }; static struct damon_sysfs_attrs *damon_sysfs_attrs_alloc(void) @@ -553,6 +554,7 @@ static struct damon_sysfs_attrs *damon_sysfs_attrs_alloc(void) if (!attrs) return NULL; attrs->kobj = (struct kobject){}; + attrs->last_nr_accesses_weight = LAST_NR_ACCESSES_WEIGHT; return attrs; } @@ -602,12 +604,40 @@ static void damon_sysfs_attrs_rm_dirs(struct damon_sysfs_attrs *attrs) kobject_put(&attrs->intervals->kobj); } +static ssize_t last_nr_accesses_weight_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damon_sysfs_attrs *attrs = container_of(kobj, + struct damon_sysfs_attrs, kobj); + + return sysfs_emit(buf, "%u\n", attrs->last_nr_accesses_weight); +} + +static ssize_t last_nr_accesses_weight_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damon_sysfs_attrs *attrs = container_of(kobj, + struct damon_sysfs_attrs, kobj); + int err = kstrtoint(buf, 0, &attrs->last_nr_accesses_weight); + + if (err) + return -EINVAL; + if (attrs->last_nr_accesses_weight > 100) + return -EINVAL; + + return count; +} + static void damon_sysfs_attrs_release(struct kobject *kobj) { kfree(container_of(kobj, struct damon_sysfs_attrs, kobj)); } +static struct kobj_attribute damon_sysfs_attrs_last_nr_accesses_weight_attr = + __ATTR_RW_MODE(last_nr_accesses_weight, 0600); + static struct attribute *damon_sysfs_attrs_attrs[] = { + &damon_sysfs_attrs_last_nr_accesses_weight_attr.attr, NULL, }; ATTRIBUTE_GROUPS(damon_sysfs_attrs); @@ -1083,6 +1113,7 @@ static int damon_sysfs_set_attrs(struct damon_ctx *ctx, .ops_update_interval = sys_intervals->update_us, .min_nr_regions = sys_nr_regions->min, .max_nr_regions = sys_nr_regions->max, + .last_nr_accesses_weight = sys_attrs->last_nr_accesses_weight, }; return damon_set_attrs(ctx, &attrs); } -- 2.43.0