From: SJ Park <sj@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: SJ Park <sj@kernel.org>,
damon@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: [PATCH 01/18] mm/damon/core: introduce damon_nr_accesses_mvsum()
Date: Mon, 29 Jun 2026 21:07:54 -0700 [thread overview]
Message-ID: <20260630040812.149729-2-sj@kernel.org> (raw)
In-Reply-To: <20260630040812.149729-1-sj@kernel.org>
Introduce a new DAMON core function, damon_nr_accesses_mvsum(). It
returns a pseudo moving sum value of a given region's nr_accesses for
the last aggregation interval. The internal logic is the same to
nr_accesses_bp. The difference is that nr_accesses_bp is updated for
each sampling interval, while the new function needs to be executed only
when requested. Hence the function's return value is the same as the
value of nr_accesses_bp.
Signed-off-by: SJ Park <sj@kernel.org>
---
include/linux/damon.h | 2 ++
mm/damon/core.c | 62 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/include/linux/damon.h b/include/linux/damon.h
index cfbbf8ba28f63..87c1ff479da82 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -1011,6 +1011,8 @@ struct damon_probe *damon_new_probe(void);
void damon_add_probe(struct damon_ctx *ctx, struct damon_probe *probe);
struct damon_region *damon_new_region(unsigned long start, unsigned long end);
+unsigned int damon_nr_accesses_mvsum(struct damon_region *r,
+ struct damon_ctx *ctx);
int damon_set_regions(struct damon_target *t, struct damon_addr_range *ranges,
unsigned int nr_ranges, unsigned long min_region_sz);
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 972a19fcee3ec..c588f943080f9 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -208,6 +208,68 @@ static struct damon_probe *damon_nth_probe(int n, struct damon_ctx *ctx)
return NULL;
}
+/*
+ * damon_mvsum() - Returns pseudo moving sum value for a time window.
+ * @current_nr: The value of the current aggregation window.
+ * @last_nr: The value of the last aggregation window.
+ * @left_window_bp: Left time of the current aggregation window.
+ *
+ * This function calculates a pseudo moving sum value of a counter that is
+ * aggregated for each time window. @current_nr is the value of the counter
+ * that aggregated so far (maybe not yet complete), from the beginning of the
+ * current aggregation time window. @last_nr is the value of the counter that
+ * has completely aggregated in the last aggregation time window.
+ * @left_window_bp represents how much time is left for the current aggregation
+ * time window in bp (1/10,000). For example, the aggregation time window is
+ * for every 10 seconds and 7 seconds has passed since the beginning of the
+ * current window, this parameter will be 3000 ((10 - 7) / 10 * 10000).
+ *
+ * The logic assumes the aggregation in the last phase was made in a single
+ * speed. Based on the assumption, the value from the last window that needs
+ * to be added to the current value is calculated as a portion of the last
+ * value based on the remaining time window.
+ */
+static unsigned long damon_mvsum(unsigned long current_nr,
+ unsigned long last_nr, unsigned long left_window_bp)
+{
+ return current_nr + mult_frac(last_nr, left_window_bp, 10000);
+}
+
+/**
+ * damon_nr_accesses_mvsum() - Returns moving sum access frequency score.
+ * @r: Region to get the access frequency of.
+ * @ctx: DAMON context of @r.
+ *
+ * This function returns for how many sampling iterations in the last
+ * aggregation interval (&damon_attrs->aggr_interval) the region was found to
+ * be accessed. Hence the value can be interpreted as the relative access
+ * frequency score of the region (@r). The value is calculated as a pseudo
+ * moving sum, and hence it is not an exact value but just a best-effort
+ * reasonable estimation.
+ *
+ * Return: the pseudo moving sum access frequency score.
+ */
+unsigned int damon_nr_accesses_mvsum(struct damon_region *r,
+ struct damon_ctx *ctx)
+{
+ unsigned long sample_interval, aggr_interval;
+ unsigned long window_len, left_window, left_window_bp;
+
+ sample_interval = ctx->attrs.sample_interval ? : 1;
+ aggr_interval = ctx->attrs.aggr_interval ? : 1;
+ window_len = aggr_interval / sample_interval;
+ if (time_after_eq(ctx->passed_sample_intervals,
+ ctx->next_aggregation_sis))
+ left_window = 0;
+ else
+ left_window = ctx->next_aggregation_sis -
+ ctx->passed_sample_intervals;
+ left_window_bp = mult_frac(left_window, 10000, window_len);
+
+ return damon_mvsum(r->nr_accesses, r->last_nr_accesses,
+ left_window_bp);
+}
+
#ifdef CONFIG_DAMON_DEBUG_SANITY
static void damon_verify_new_region(unsigned long start, unsigned long end)
{
--
2.47.3
next prev parent reply other threads:[~2026-06-30 4:08 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-30 4:07 [PATCH 00/18] mm/damon: optimize out nr_accesses_bp SJ Park
2026-06-30 4:07 ` SJ Park [this message]
2026-06-30 4:07 ` [PATCH 02/18] mm/damon/tests/core-kunit: test damon_mvsum() SJ Park
2026-06-30 4:07 ` [PATCH 03/18] mm/damon/core: always update ->last_nr_accesses for intervals change SJ Park
2026-06-30 4:07 ` [PATCH 04/18] mm/damon/core: handle unreset nr_accesses in damon_nr_accesses_mvsum() SJ Park
2026-06-30 4:07 ` [PATCH 05/18] mm/damon/core: use damon_nr_accesses_mvsum() in __damos_valid_target() SJ Park
2026-06-30 4:07 ` [PATCH 06/18] mm/damon/core: use damon_nr_accesses_mvsum() for damos region tracing SJ Park
2026-06-30 4:08 ` [PATCH 07/18] mm/damon/sysfs-schemes: use damon_nr_accesses_mvsum() for damo regions SJ Park
2026-06-30 4:08 ` [PATCH 08/18] mm/damon/core: remove damon_warn_fix_nr_accesses_corruption() SJ Park
2026-06-30 4:08 ` [PATCH 09/18] mm/damon/core: remove damon_verify_reset_aggregated() SJ Park
2026-06-30 4:08 ` [PATCH 10/18] mm/damon/core: remove damon_verify_merge_regions_of() SJ Park
2026-06-30 4:08 ` [PATCH 11/18] mm/damon/tests/core-kunit: remove nr_accesses_bp setup and tests SJ Park
2026-06-30 4:08 ` [PATCH 12/18] selftests/damon/drgn_dump_damon_status: do not dump nr_accesses_bp SJ Park
2026-06-30 4:08 ` [PATCH 13/18] mm/damon/core: remove nr_accesses_bp setups and updates SJ Park
2026-06-30 4:08 ` [PATCH 14/18] mm/damon/core: remove attrs param from damon_update_region_access_rate() SJ Park
2026-06-30 4:48 ` SJ Park
2026-06-30 4:08 ` [PATCH 15/18] mm/damon/paddr: remove attrs param from __damon_pa_check_access() SJ Park
2026-06-30 4:08 ` [PATCH 16/18] mm/damon/vaddr: remove attrs param from __damon_va_check_access() SJ Park
2026-06-30 4:08 ` [PATCH 17/18] mm/damon/core: remove damon_moving_sum() and its unit test SJ Park
2026-06-30 4:08 ` [PATCH 18/18] mm/damon/core: remove damon_region->nr_accesses_bp SJ Park
2026-06-30 4:52 ` [PATCH 00/18] mm/damon: optimize out nr_accesses_bp SJ Park
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260630040812.149729-2-sj@kernel.org \
--to=sj@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=damon@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox