From nobody Fri Dec 19 14:22:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C20E0287519; Mon, 19 May 2025 16:44:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747673061; cv=none; b=giVXgRdYWPorwIABzjcTPtYniAkfOoy9i/kSS4v+DGcjGQqkYBzb1+VlTbYbZp/tblODUXL3GTdRxhVUqVSbUF/jXD0U8pd/OuGglT7PFjE0Z1awCTbKSXZny/JyS3nsvjqlx4nAJdjxZGcQaCGyAZ1amZ9ljACbOiEe9XHY+nI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747673061; c=relaxed/simple; bh=z0JjO0m2glY+dWvePC6PxEeqvFGxsuXbQo7p4xrgJ7o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HHnFfXSRHjUHqPEg864huY/m14PZNMcOVIHyXJxB0ir6zl9Z0R8x5y4hQw+QWApnRr2fvauggWDN5p4BqLsXExIVAKMxJITpESuGKTtEaKtBiv+1GX6P3GMhEXpj5LSRFAxZ7Bgh0FGN4fkFwNOu/g4xQjOY1gb+WOMSsaAgMWg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A4096UE/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A4096UE/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E78CAC4CEE4; Mon, 19 May 2025 16:44:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747673061; bh=z0JjO0m2glY+dWvePC6PxEeqvFGxsuXbQo7p4xrgJ7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A4096UE/qe57WsG567bTv86VBECxPRLhGkVvxhfmJYwVtD6749du/aRo8RlV6MNUj ZUdwzVY6+7NqUFkcDXCHDH4bsJQq1p1EeBqV1/6j+g3q0eknIgPtm5X5muz7Su3C5t c+ULcSB+XACMT//o4AhxXYxgfXf9jaEzdRqrLljVATMS1KUaGJXo+8jF0m9NvXPpAP 8wMZfUROpkQSU0/hsJmBStb3sAetXd5fqXWD9V4E+UQh1lSsGBncboCUmA/2SZrFQf aVKqpcy6iJQI0q8/jonwMli41sWtQgI2E3DtFruY2X1h4DP7bfw2SY5gCFzwktw8/3 dv8loBkXhB2QQ== From: SeongJae Park To: Cc: SeongJae Park , Andrew Morton , damon@lists.linux.dev, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 1/3] mm/damon: introduce DAMON_STAT module Date: Mon, 19 May 2025 09:44:13 -0700 Message-Id: <20250519164415.43935-2-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250519164415.43935-1-sj@kernel.org> References: <20250519164415.43935-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To use DAMON for monitoring access pattern of the system, users should manually start DAMON via DAMON sysfs ABI with a number of parameters for specifying the monitoring target address space, address ranges, and monitoring intervals. After that, users should also wait until desired amount of time data is captured into DAMON's monitoring results. It is bothersome and take a long time to be practical for access monitoring on large fleet level production environments. For access-aware system operations use cases like proactive cold memory reclamation, similar problem existed and we solved those by introducing dedicated static kernel modules such as DAMON_RECLAIM. Implement such static kernel module for access monitoring, namely DAMON_STAT. It monitors the entire physical address space with auto-tuned monitoring intervals. The auto-tuning is set to capture 4 % of observable access events in each snapshot while keeping the sampling intervals in a range of 5 milliseconds and 10 seconds. From production environemnts, we confirmed this setup provides high quality monitoring results with minimum overheads. The module therefore receives only one user input, whether to enable or disable it. It can be set on build or boot time via build configuration or kernel boot command line. It can also be overridden at runtime. Note that this commit only implements the DAMON control part of the module. Users could to get the monitoring results via damon:damon_aggregated tracepoints, but that's of course not the recommended way. Following commits will implement convenient and optimized ways for serving the monitoring results to users. Signed-off-by: SeongJae Park --- mm/damon/Kconfig | 16 ++++++ mm/damon/Makefile | 1 + mm/damon/stat.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 mm/damon/stat.c diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig index 551745df011b..9f482e3adc67 100644 --- a/mm/damon/Kconfig +++ b/mm/damon/Kconfig @@ -95,4 +95,20 @@ config DAMON_LRU_SORT protect frequently accessed (hot) pages while rarely accessed (cold) pages reclaimed first under memory pressure. =20 +config DAMON_STAT + bool "Build data access monitoring stat (DAMON_STAT)" + depends on DAMON_PADDR + help + This builds the DAMON-based access monitoring statistics subsystem. + It runs DAMON and expose access monitoring results in simple stat + metrics. + +config DAMON_STAT_ENABLED_DEFAULT + bool "Enable DAMON_STAT by default" + depends on DAMON_PADDR + default DAMON_STAT + help + Whether to enable DAMON_STAT by default. Users can disable it in + boot or runtime using its 'enabled' parameter. + endmenu diff --git a/mm/damon/Makefile b/mm/damon/Makefile index 8b49012ba8c3..d8d6bf5f8bff 100644 --- a/mm/damon/Makefile +++ b/mm/damon/Makefile @@ -6,3 +6,4 @@ obj-$(CONFIG_DAMON_PADDR) +=3D ops-common.o paddr.o obj-$(CONFIG_DAMON_SYSFS) +=3D sysfs-common.o sysfs-schemes.o sysfs.o obj-$(CONFIG_DAMON_RECLAIM) +=3D modules-common.o reclaim.o obj-$(CONFIG_DAMON_LRU_SORT) +=3D modules-common.o lru_sort.o +obj-$(CONFIG_DAMON_STAT) +=3D modules-common.o stat.o diff --git a/mm/damon/stat.c b/mm/damon/stat.c new file mode 100644 index 000000000000..852848ce844e --- /dev/null +++ b/mm/damon/stat.c @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Shows data access monitoring resutls in simple metrics. + */ + +#define pr_fmt(fmt) "damon-stat: " fmt + +#include +#include +#include +#include +#include + +#ifdef MODULE_PARAM_PREFIX +#undef MODULE_PARAM_PREFIX +#endif +#define MODULE_PARAM_PREFIX "damon_stat." + +static int damon_stat_enabled_store( + const char *val, const struct kernel_param *kp); + +static const struct kernel_param_ops enabled_param_ops =3D { + .set =3D damon_stat_enabled_store, + .get =3D param_get_bool, +}; + +static bool enabled __read_mostly =3D CONFIG_DAMON_STAT_ENABLED_DEFAULT; +module_param_cb(enabled, &enabled_param_ops, &enabled, 0600); +MODULE_PARM_DESC(enabled, "Enable of disable DAMON_STAT"); + +static struct damon_ctx *damon_stat_context; + +static struct damon_ctx *damon_stat_build_ctx(void) +{ + struct damon_ctx *ctx; + struct damon_attrs attrs; + struct damon_target *target; + unsigned long start =3D 0, end =3D 0; + + ctx =3D damon_new_ctx(); + if (!ctx) + return NULL; + attrs =3D (struct damon_attrs) { + .sample_interval =3D 5 * USEC_PER_MSEC, + .aggr_interval =3D 100 * USEC_PER_MSEC, + .ops_update_interval =3D 60 * USEC_PER_MSEC * MSEC_PER_SEC, + .min_nr_regions =3D 10, + .max_nr_regions =3D 1000, + }; + /* + * auto-tune sampling and aggregation interval aiming 4% DAMON-observed + * accesses ratio, keeping sampling interval in [5ms, 10s] range. + */ + attrs.intervals_goal =3D (struct damon_intervals_goal) { + .access_bp =3D 400, .aggrs =3D 3, + .min_sample_us =3D 5000, .max_sample_us =3D 10000000, + }; + if (damon_set_attrs(ctx, &attrs)) + goto free_out; + + /* + * auto-tune sampling and aggregation interval aiming 4% DAMON-observed + * accesses ratio, keeping sampling interval in [5ms, 10s] range. + */ + ctx->attrs.intervals_goal =3D (struct damon_intervals_goal) { + .access_bp =3D 400, .aggrs =3D 3, + .min_sample_us =3D 5000, .max_sample_us =3D 10000000, + }; + if (damon_select_ops(ctx, DAMON_OPS_PADDR)) + goto free_out; + + target =3D damon_new_target(); + if (!target) + goto free_out; + damon_add_target(ctx, target); + if (damon_set_region_biggest_system_ram_default(target, &start, &end)) + goto free_out; + return ctx; +free_out: + damon_destroy_ctx(ctx); + return NULL; +} + +static int damon_stat_start(void) +{ + damon_stat_context =3D damon_stat_build_ctx(); + if (!damon_stat_context) + return -ENOMEM; + return damon_start(&damon_stat_context, 1, true); +} + +static void damon_stat_stop(void) +{ + damon_stop(&damon_stat_context, 1); + damon_destroy_ctx(damon_stat_context); +} + +static bool damon_stat_init_called; + +static int damon_stat_enabled_store( + const char *val, const struct kernel_param *kp) +{ + bool is_enabled =3D enabled; + int err; + + err =3D kstrtobool(val, &enabled); + if (err) + return err; + + if (is_enabled =3D=3D enabled) + return 0; + + if (!damon_stat_init_called) + /* + * probably called from command line parsing (parse_args()). + * Cannot call damon_new_ctx(). Let damon_stat_init() handle. + */ + return 0; + + if (enabled) + return damon_stat_start(); + damon_stat_stop(); + return 0; +} + +static int __init damon_stat_init(void) +{ + int err =3D 0; + + damon_stat_init_called =3D true; + + /* probably set via command line */ + if (enabled) + err =3D damon_stat_start(); + return err; +} + +module_init(damon_stat_init); --=20 2.39.5 From nobody Fri Dec 19 14:22:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B4EE288506; Mon, 19 May 2025 16:44:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747673063; cv=none; b=jVXDIr2K7eW6HOYupUHwajwbi+tJ5WASihVJIU1jd2ByzedRIzrlj/a5L7UOkgoJF6oIVARUB3GQ35l0SDLLZWIoCLT126UQmLqXVDKZH47uk7ZCQwN3HZNhJnV7Zm9fvhcukdyv1qABHmx49P/opWvaW5/LMmhw+BRz2eUX/TE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747673063; c=relaxed/simple; bh=aEHaE8W06Fq5hALcrvZOQ2IWuHzzkcZJHOLo+mefNe8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gSoIdZUhIvc6RtPIeD+87CZ/Cb1Q/HHPn3mtwadcJCjOY3/SBkWmquChyKoKnJBXYFCfG02xVWbeuifnmsvW5f3NmL83fF0s0EVP2GiEBKfFoQKH4eTTaK/3ua9pH99eDOGBrTBurd6xJz6gA7vUpZC3qC6sFtqoPfTVdP8Mhc4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=g46DMzW0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="g46DMzW0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5174CC4CEE9; Mon, 19 May 2025 16:44:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747673062; bh=aEHaE8W06Fq5hALcrvZOQ2IWuHzzkcZJHOLo+mefNe8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g46DMzW0Ra+JSIykpWnDpHN1/7r2XsIY1kQ/mXixnOkME0IZGmBYlIqCPsGxKlzST GspthY0pYkZstR2betw+cfpgvYUBhMRJt0Oiuu3AUy5Qu5ZbLe4P7uqkCJPU3pW6cs 9M7dZ166PO4qDqsr49f1g5eu3egKwHzBGG9SdLdENvez9v3f0dnHFaE/zseY/ERREN AY6Cq8VytXLt9NZrs6JfLCT4LcdX3irWKgyPQfsRBBh3PAkDTuCsdYPRP80Zg+aq3J U7M5t03ANUf9JzHeksEUDG1JLSW/euot7o1RFgA5hu0H047Dc0CsH7thvnbxPcyJo3 WKDmz1d5UTU8Q== From: SeongJae Park To: Cc: SeongJae Park , Andrew Morton , damon@lists.linux.dev, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 2/3] mm/damon/stat: calculate and expose estimated memory bandwidth Date: Mon, 19 May 2025 09:44:14 -0700 Message-Id: <20250519164415.43935-3-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250519164415.43935-1-sj@kernel.org> References: <20250519164415.43935-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The raw form of DAMON's monitoring results captures many details of the information. However, not every bit of the information is always required for understanding practical access pattern. Especially on real world production systems of high scale time and size, the raw form is difficult to be aggregated and compared. Convert the raw monitoring results into a single number metric, namely estimated memory bandwidth and expose it to users as a read-only DAMON_STAT parameter. The metric represents access intensiveness (hotness) of the system. It can easily be aggregated and compared for high level understanding of the access pattern on large systems. Signed-off-by: SeongJae Park --- mm/damon/stat.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/mm/damon/stat.c b/mm/damon/stat.c index 852848ce844e..f9ae44db265b 100644 --- a/mm/damon/stat.c +++ b/mm/damon/stat.c @@ -28,8 +28,42 @@ static bool enabled __read_mostly =3D CONFIG_DAMON_STAT_= ENABLED_DEFAULT; module_param_cb(enabled, &enabled_param_ops, &enabled, 0600); MODULE_PARM_DESC(enabled, "Enable of disable DAMON_STAT"); =20 +static unsigned long estimated_memory_bandwidth __read_mostly; +module_param(estimated_memory_bandwidth, ulong, 0400); +MODULE_PARM_DESC(estimated_memory_bandwidth, + "Estimated memory bandwidth usage in bytes per second"); + static struct damon_ctx *damon_stat_context; =20 +static void damon_stat_set_estimated_memory_bandwidth(struct damon_ctx *c) +{ + struct damon_target *t; + struct damon_region *r; + unsigned long access_bytes =3D 0; + + damon_for_each_target(t, c) { + damon_for_each_region(r, t) + access_bytes +=3D (r->ar.end - r->ar.start) * + r->nr_accesses; + } + estimated_memory_bandwidth =3D access_bytes * USEC_PER_MSEC * + MSEC_PER_SEC / c->attrs.aggr_interval; +} + +static int damon_stat_after_aggregation(struct damon_ctx *c) +{ + static unsigned long last_refresh_jiffies; + + /* avoid unnecessarily frequent stat update */ + if (time_before_eq(jiffies, last_refresh_jiffies + + msecs_to_jiffies(5 * MSEC_PER_SEC))) + return 0; + last_refresh_jiffies =3D jiffies; + + damon_stat_set_estimated_memory_bandwidth(c); + return 0; +} + static struct damon_ctx *damon_stat_build_ctx(void) { struct damon_ctx *ctx; @@ -75,6 +109,7 @@ static struct damon_ctx *damon_stat_build_ctx(void) damon_add_target(ctx, target); if (damon_set_region_biggest_system_ram_default(target, &start, &end)) goto free_out; + ctx->callback.after_aggregation =3D damon_stat_after_aggregation; return ctx; free_out: damon_destroy_ctx(ctx); --=20 2.39.5 From nobody Fri Dec 19 14:22:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BB552749F5; Mon, 19 May 2025 16:44:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747673064; cv=none; b=UeWdjDzb2uXPtC98Dj1gPCZPcimLus5afTwpUgTF/lZ/GcqGPWGs+xV9DRqLPAZsVXgWhAvpjSV15vLQar+T8loXhaBruixCChXW/OeF+oK/RPt+EtfomBlAUL4Dv8QyKZz+rZKAV9rGXx0FEhvm6FijMJwgx8C/wk63u1bIZa0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747673064; c=relaxed/simple; bh=/8/4pFjPPVhMHFQr9b7mNlWI254qxXmUWO34UkwncD0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pfb19L6gb6pVfhxr+lzxOpfGb9tY7TOo8R9mts5gJ/EvuiqRXXRlj1EW8B5n9KeFX/TCiHemluICc1iUhOKjjK/DH36DWOjQBqTp45OdNAap8VIxaEM4I0ejjlXfcYXIorYTCLaax0zSjKeUre5rLnRuzI17Cota/pO2ztIl5oQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oVe2cC2L; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oVe2cC2L" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2A74C4CEF0; Mon, 19 May 2025 16:44:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747673063; bh=/8/4pFjPPVhMHFQr9b7mNlWI254qxXmUWO34UkwncD0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oVe2cC2LWrFButcYacjM61U3B0tTyqTFHXvQXBW9j7iWzRkPUb2mn+annjBmoQzxH kAbDowbPqUb5KzwvuonOxVMl0NX/vbim+YekttfSP4u7lw3dod7RaZCjwS1Lrtn29b H4ojoYD1FA11sIDss8VMhsr76El3O98DAJo84Apc4biyP1Fwldctw6pQNMfAr5eyPC keAyT3fR0/egdROn5n0hjeH0dv0LiWZNu048H8tgD65W2ilCqHG5M7g4NxBxZJDi+J +E2GozUxhJwpnXxUVIScww6jCBeOBsJGXI+XwDPfEDhQZwoXuH/iv+2YiTXyWVXNQy nQTelKv1FRUOw== From: SeongJae Park To: Cc: SeongJae Park , Andrew Morton , damon@lists.linux.dev, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 3/3] mm/damon/stat: calculate and expose idle time percentiles Date: Mon, 19 May 2025 09:44:15 -0700 Message-Id: <20250519164415.43935-4-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250519164415.43935-1-sj@kernel.org> References: <20250519164415.43935-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Knowing how much memory are how cold can be useful for understanding coldness and utilization efficiency of memory. The raw form of DAMON's monitoring results has the information. Convert the raw results into the per-byte idle time distributions and expose it as percentiles metric to users, as a read-only DAMON_STAT parameter. In detail, the metrics are calcualted as following. First, DAMON's per-region access frequency and age information is converted into per-byte idle time. If access frequency of a region is higher than zero, every byte of the region has zero idle time. If access frequency of a region is zero, every byte of the region has idle time as the age of the region. Then the logic sorts the per-byte idle times and provides the value at 0/100, 1/100, ..., 99/100 and 100/100 location of the sorted array. The metric can be easily aggregated and compared on large scale production systems. For example, if an average of 75-th percentile idle time of machines that collected on similar time is two minutes, it means the system's 25 percent memory is not accessed at all for two minutes or more in average. If a workload considers two minutes as unit work time, we can conclude its working set size is only 75 percent of the memory. If the system utilizes proactive reclamation and it supports coldness-based thresholds like DAMON_RECLAIM, te idle time percentiles can be used to find more safe or aggressive coldness threshold for aimed memory saving. Signed-off-by: SeongJae Park --- mm/damon/stat.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/mm/damon/stat.c b/mm/damon/stat.c index f9ae44db265b..7ef13ea22221 100644 --- a/mm/damon/stat.c +++ b/mm/damon/stat.c @@ -33,6 +33,11 @@ module_param(estimated_memory_bandwidth, ulong, 0400); MODULE_PARM_DESC(estimated_memory_bandwidth, "Estimated memory bandwidth usage in bytes per second"); =20 +static unsigned long memory_idle_ms_percentiles[101] __read_mostly =3D {0,= }; +module_param_array(memory_idle_ms_percentiles, ulong, NULL, 0400); +MODULE_PARM_DESC(memory_idle_ms_percentiles, + "Memory idle time percentiles in milliseconds"); + static struct damon_ctx *damon_stat_context; =20 static void damon_stat_set_estimated_memory_bandwidth(struct damon_ctx *c) @@ -50,6 +55,72 @@ static void damon_stat_set_estimated_memory_bandwidth(st= ruct damon_ctx *c) MSEC_PER_SEC / c->attrs.aggr_interval; } =20 +static unsigned int damon_stat_idletime(const struct damon_region *r) +{ + if (r->nr_accesses) + return 0; + return r->age + 1; +} + +static int damon_stat_cmp_regions(const void *a, const void *b) +{ + const struct damon_region *ra =3D *(const struct damon_region **)a; + const struct damon_region *rb =3D *(const struct damon_region **)b; + + return damon_stat_idletime(ra) - damon_stat_idletime(rb); +} + +static int damon_stat_sort_regions(struct damon_ctx *c, + struct damon_region ***sorted_ptr, int *nr_regions_ptr, + unsigned long *total_sz_ptr) +{ + struct damon_target *t; + struct damon_region *r; + struct damon_region **region_pointers; + unsigned int nr_regions =3D 0; + unsigned long total_sz =3D 0; + + damon_for_each_target(t, c) { + /* there is only one target */ + region_pointers =3D kmalloc_array(damon_nr_regions(t), + sizeof(*region_pointers), GFP_KERNEL); + if (!region_pointers) + return -ENOMEM; + damon_for_each_region(r, t) { + region_pointers[nr_regions++] =3D r; + total_sz +=3D r->ar.end - r->ar.start; + } + } + sort(region_pointers, nr_regions, sizeof(*region_pointers), + damon_stat_cmp_regions, NULL); + *sorted_ptr =3D region_pointers; + *nr_regions_ptr =3D nr_regions; + *total_sz_ptr =3D total_sz; + return 0; +} + +static void damon_stat_set_idletime_percentiles(struct damon_ctx *c) +{ + struct damon_region **sorted_regions, *region; + int nr_regions; + unsigned long total_sz, accounted_bytes =3D 0; + int err, i, next_percentile =3D 0; + + err =3D damon_stat_sort_regions(c, &sorted_regions, &nr_regions, + &total_sz); + if (err) + return; + for (i =3D 0; i < nr_regions; i++) { + region =3D sorted_regions[i]; + accounted_bytes +=3D region->ar.end - region->ar.start; + while (next_percentile <=3D accounted_bytes * 100 / total_sz) + memory_idle_ms_percentiles[next_percentile++] =3D + damon_stat_idletime(region) * + c->attrs.aggr_interval / USEC_PER_MSEC; + } + kfree(sorted_regions); +} + static int damon_stat_after_aggregation(struct damon_ctx *c) { static unsigned long last_refresh_jiffies; @@ -61,6 +132,7 @@ static int damon_stat_after_aggregation(struct damon_ctx= *c) last_refresh_jiffies =3D jiffies; =20 damon_stat_set_estimated_memory_bandwidth(c); + damon_stat_set_idletime_percentiles(c); return 0; } =20 --=20 2.39.5