mm/damon/core: skip needless update of next_{aggregation,ops_update}_sis
Subsystems (2)
PATCH 1/1 mm/damon/core: skip needless update of next_{aggregation,ops_update}_sis
From: Bijan Tabatabai <[email protected]>
In damon_set_attrs(), ctx->next_{aggregation,ops_update}_sis would be reset, even if the sample interval, aggregation interval, or ops update interval were not changed. If damon_set_attrs() is called relatively frequently, such as by frequent "commit" operations, aggregation and ops update operations could be needlessly delayed.
This patch avoids this by only updating next_{aggregation,ops_update}_sis if the relevant intervals were changed.
---
This patch came from discussions in [1].
[1] https://lore.kernel.org/all/[email protected]/
--- mm/damon/core.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
mm/damon/core.c +12 -4
@@ -693,6 +693,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)unsigned long sample_interval = attrs->sample_interval ? attrs->sample_interval :1 ;struct damos *s; +bool sample_interval_changed = ctx->attrs.sample_interval != + attrs->sample_interval; +bool aggr_interval_changed = ctx->attrs.aggr_interval != + attrs->aggr_interval; +bool ops_update_interval_changed = ctx->attrs.ops_update_interval != + attrs->ops_update_interval;bool aggregating = ctx->passed_sample_intervals < ctx->next_aggregation_sis;
@@ -710,10 +716,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)if (!attrs->aggr_samples) attrs->aggr_samples = attrs->aggr_interval / sample_interval; - ctx->next_aggregation_sis = ctx->passed_sample_intervals + - attrs->aggr_interval / sample_interval; - ctx->next_ops_update_sis = ctx->passed_sample_intervals + - attrs->ops_update_interval / sample_interval; +if (sample_interval_changed || aggr_interval_changed) + ctx->next_aggregation_sis = ctx->passed_sample_intervals + + attrs->aggr_interval / sample_interval; +if (sample_interval_changed || ops_update_interval_changed) + ctx->next_ops_update_sis = ctx->passed_sample_intervals + + attrs->ops_update_interval / sample_interval;damon_update_monitoring_results (ctx, attrs, aggregating); ctx->attrs = *attrs;
Signature · 1 line
2.43.0
SeongJae Park
maintainerHi Bijan,
Quoted 11 lines · On Wed, 6 Aug 2025 11:43:16 -0500 Bijan Tabatabai <[email protected]> wrote:
From: Bijan Tabatabai <[email protected]>
In
damon_set_attrs(), ctx->next_{aggregation,ops_update}_sis would be reset, even if the sample interval, aggregation interval, or ops update interval were not changed. Ifdamon_set_attrs()is called relatively frequently, such as by frequent "commit" operations, aggregation and ops update operations could be needlessly delayed.This patch avoids this by only updating next_{aggregation,ops_update}_sis if the relevant intervals were changed.
Quoted 6 lines
Cc Bijan Tabatabai <[email protected]>Signed-off-by Bijan Tabatabai <[email protected]>---
This patch came from discussions in [1].
Thank you for sending this patch as we discussed on the thread!
Quoted 28 lines
--- mm/damon/core.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)mm/damon/core.c +6
-3@@ -693,6 +693,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)unsigned long sample_interval = attrs->sample_interval ? attrs->sample_interval :1 ;struct damos *s; +bool sample_interval_changed = ctx->attrs.sample_interval != + attrs->sample_interval; +bool aggr_interval_changed = ctx->attrs.aggr_interval != + attrs->aggr_interval; +bool ops_update_interval_changed = ctx->attrs.ops_update_interval != + attrs->ops_update_interval;bool aggregating = ctx->passed_sample_intervals < ctx->next_aggregation_sis;@@ -710,10 +716,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)if (!attrs->aggr_samples) attrs->aggr_samples = attrs->aggr_interval / sample_interval; - ctx->next_aggregation_sis = ctx->passed_sample_intervals + - attrs->aggr_interval / sample_interval; - ctx->next_ops_update_sis = ctx->passed_sample_intervals +
mm/damon/core.c int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs) · line 727
Quoted 10 lines
mm/damon/core.c +6
-1- attrs->ops_update_interval / sample_interval; +if (sample_interval_changed || aggr_interval_changed) + ctx->next_aggregation_sis = ctx->passed_sample_intervals + + attrs->aggr_interval / sample_interval; +if (sample_interval_changed || ops_update_interval_changed) + ctx->next_ops_update_sis = ctx->passed_sample_intervals + + attrs->ops_update_interval / sample_interval;damon_update_monitoring_results (ctx, attrs, aggregating); ctx->attrs = *attrs;
Long story short, this (original) code is bit complicated and hence I suggest to make this change less optimum but simpler.
damon_update_monitoring_results() assumes it is called only just after next_{aggr,ops_update}_sis are updated. And the assumption is important for the pseudo-moving-sum access frequency maintenance. As a result, this can make the monitoring results temporarily corrupted, and splat warning once. Please refer to commit 591c4c78be063 ("mm/damon/core: warn and fix nr_accesses[_bp] corruption") or the patch thread[1] if you want more details.
Also damon_set_attrs() is called not only from commit situation. So I think this maybe not an ideal part to modify.
What about modifying damon_commit_ctx() to check if new and old damon_ctx->attrs are entirely same, and skip calling damon_set_attrs() in the case? Doing the entire damon_attrs comparison might be suboptimum, but would make the change simpler. I assume the suboptimum comparison is not a real problem for your use case, so I think that could be a good tradeoff?
Also, I realize the moving sum access frequencies maintenance and damon_set_attrs() are not well documented and organized. I will try to make those better cleaned up and documented.
[1] https://lkml.kernel.org/r/[email protected]
Thanks,
SJ
Quoted 2 lines
Signature · 1 line
2.43.0
Quoted 74 lines · SeongJae Park
Hi Bijan,
Quoted 11 lines · On Wed, 6 Aug 2025 11:43:16 -0500 Bijan Tabatabai <[email protected]> wrote:
From: Bijan Tabatabai <[email protected]>
In
damon_set_attrs(), ctx->next_{aggregation,ops_update}_sis would be reset, even if the sample interval, aggregation interval, or ops update interval were not changed. Ifdamon_set_attrs()is called relatively frequently, such as by frequent "commit" operations, aggregation and ops update operations could be needlessly delayed.This patch avoids this by only updating next_{aggregation,ops_update}_sis if the relevant intervals were changed.
Quoted 6 lines
Cc Bijan Tabatabai <[email protected]>Signed-off-by Bijan Tabatabai <[email protected]>---
This patch came from discussions in [1].Thank you for sending this patch as we discussed on the thread!
Quoted 28 lines
--- mm/damon/core.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)mm/damon/core.c +6
-3@@ -693,6 +693,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)unsigned long sample_interval = attrs->sample_interval ? attrs->sample_interval :1 ;struct damos *s; +bool sample_interval_changed = ctx->attrs.sample_interval != + attrs->sample_interval; +bool aggr_interval_changed = ctx->attrs.aggr_interval != + attrs->aggr_interval; +bool ops_update_interval_changed = ctx->attrs.ops_update_interval != + attrs->ops_update_interval;bool aggregating = ctx->passed_sample_intervals < ctx->next_aggregation_sis;@@ -710,10 +716,12 @@ int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs)if (!attrs->aggr_samples) attrs->aggr_samples = attrs->aggr_interval / sample_interval; - ctx->next_aggregation_sis = ctx->passed_sample_intervals + - attrs->aggr_interval / sample_interval; - ctx->next_ops_update_sis = ctx->passed_sample_intervals +mm/damon/core.c int damon_set_attrs(struct damon_ctx *ctx, struct damon_attrs *attrs) · line 727
Quoted 10 lines
mm/damon/core.c +6
-1- attrs->ops_update_interval / sample_interval; +if (sample_interval_changed || aggr_interval_changed) + ctx->next_aggregation_sis = ctx->passed_sample_intervals + + attrs->aggr_interval / sample_interval; +if (sample_interval_changed || ops_update_interval_changed) + ctx->next_ops_update_sis = ctx->passed_sample_intervals + + attrs->ops_update_interval / sample_interval;damon_update_monitoring_results (ctx, attrs, aggregating); ctx->attrs = *attrs;Long story short, this (original) code is bit complicated and hence I suggest to make this change less optimum but simpler.
damon_update_monitoring_results()assumes it is called only just after next_{aggr,ops_update}_sis are updated. And the assumption is important for the pseudo-moving-sum access frequency maintenance. As a result, this can make the monitoring results temporarily corrupted, and splat warning once. Please refer to commit591c4c78be063("mm/damon/core: warn and fix nr_accesses[_bp] corruption") or the patch thread[1] if you want more details.
Quoted 8 lines
Also
damon_set_attrs()is called not only from commit situation. So I think this maybe not an ideal part to modify.What about modifying
damon_commit_ctx()to check if new and olddamon_ctx->attrsare entirely same, and skip callingdamon_set_attrs()in the case? Doing the entiredamon_attrscomparison might be suboptimum, but would make the change simpler. I assume the suboptimum comparison is not a real problem for your use case, so I think that could be a good tradeoff?
I can definitely do this. Checking a few extra fields is no big deal.
Silly question, but think it's best to get it out of the way before sending another patch: do you think there's a more elegant way of just having a dumb comparison function like
bool damon_attrs_equal(struct damon_attrs *a, struct damon_attrs *b)
{
return a->sample_interval == b->sample_interval &&
a->aggr_interval == b->aggr_interval &&
...
}And I assume I shouldn't compare the aggr_samples field because it's private, is that right?
[...]
Thanks for your patience,
Bijan
SeongJae Park
maintainerQuoted 16 lines · On Wed, 6 Aug 2025 14:49:21 -0500 Bijan Tabatabai <[email protected]> wrote:
Quoted 15 lines · SeongJae Park
Hi Bijan,
Quoted 10 lines · On Wed, 6 Aug 2025 11:43:16 -0500 Bijan Tabatabai <[email protected]> wrote:
From: Bijan Tabatabai <[email protected]>
In
damon_set_attrs(), ctx->next_{aggregation,ops_update}_sis would be reset, even if the sample interval, aggregation interval, or ops update interval were not changed. Ifdamon_set_attrs()is called relatively frequently, such as by frequent "commit" operations, aggregation and ops update operations could be needlessly delayed.This patch avoids this by only updating next_{aggregation,ops_update}_sis if the relevant intervals were changed.
[...]
Quoted 13 lines
Quoted 5 lines
What about modifying
damon_commit_ctx()to check if new and olddamon_ctx->attrsare entirely same, and skip callingdamon_set_attrs()in the case? Doing the entiredamon_attrscomparison might be suboptimum, but would make the change simpler. I assume the suboptimum comparison is not a real problem for your use case, so I think that could be a good tradeoff?I can definitely do this. Checking a few extra fields is no big deal.
Silly question, but think it's best to get it out of the way before sending another patch: do you think there's a more elegant way of just having a dumb comparison function like
bool
damon_attrs_equal(struct damon_attrs *a, struct damon_attrs *b)
Quoted 8 lines
{ return a->sample_interval == b->sample_interval && a->aggr_interval == b->aggr_interval && ... }And I assume I shouldn't compare the
aggr_samplesfield because it's private, is that right?
Ah, you're right, thank you for asking this!
Maybe we can copy src->attrs to a local damon_attrs variable, overwrite aggr_samples of the copy and dst, and memcmp() the copy and dst?
I wouldn't mind the dumb comparison function, though, if you prefer. I'll defer the decision to you!
Thanks,
SJ
[...]