Skip to contentSkip to conversation

mm/damon/tests/core-kunit: improve nr_samples_per_aggr test isolation

PATCH Superseded Kunwu Chan 4 participants 5 messages Sep 8 – Sep 9

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

PATCH 1/1 mm/damon/tests/core-kunit: improve nr_samples_per_aggr test isolation

The damon_nr_samples_per_aggr() test sets both intervals to zero, which exercises two different fallback paths at once.

Use separate compound initializers for each case so that each case tests one fallback path independently. Also make the overflow case use an explicit non-zero sample interval so that it does not depend on the zero sample interval fallback.

---
 mm/damon/tests/core-kunit.h | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
mm/damon/tests/core-kunit.h +15 -4
index c01e6a75cad..ff23208c181 100644
@@ -627,12 +627,20 @@ static void damon_test_set_regions(struct kunit *test)
 
 static void damon_test_nr_samples_per_aggr(struct kunit *test)
 {
-	struct damon_attrs attrs = {
+	struct damon_attrs attrs;
+
+	/* Zero sample interval is treated as one. */
+	attrs = (struct damon_attrs){
 		.sample_interval = 0,
-		.aggr_interval = 0,
+		.aggr_interval = 5000,
 	};
+	KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs), 5000);
 
-	/* Zero aggregation interval doesn't cause division by zero */
+	/* Zero samples per aggregation is treated as one. */
+	attrs = (struct damon_attrs){
+		.sample_interval = 5000,
+		.aggr_interval = 0,
+	};
 	KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs), 1);
 
 	/*
@@ -640,7 +648,10 @@ static void damon_test_nr_samples_per_aggr(struct kunit *test)
 	 * overflow
 	 */
 	if (ULONG_MAX > UINT_MAX) {
-		attrs.aggr_interval = (unsigned long)UINT_MAX + 1;
+		attrs = (struct damon_attrs){
+			.sample_interval = 1,
+			.aggr_interval = (unsigned long)UINT_MAX + 1,
+		};
 		KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs),
 				UINT_MAX);
 	}

base-commit: d118502628f8b673be9023db8bdf878f64a7ed45

Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

The damon_nr_samples_per_aggr() test sets both intervals to zero, which exercises two different fallback paths at once.

Use separate compound initializers for each case so that each case tests one fallback path independently. Also make the overflow case use an explicit non-zero sample interval so that it does not depend on the zero sample interval fallback.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Kunwu Chan
Quoted 7 lines · On Tue, 8 Sep 2026 14:36:35 +0800 Kunwu Chan <[email protected]> wrote:

The damon_nr_samples_per_aggr() test sets both intervals to zero, which exercises two different fallback paths at once.

Use separate compound initializers for each case so that each case tests one fallback path independently. Also make the overflow case use an explicit non-zero sample interval so that it does not depend on the zero sample interval fallback.

Hi Kunwu,

The separate initializers now exercise the zero sample-interval fallback, the zero samples-per-aggregation fallback, and the 64-bit UINT_MAX saturation path independently. Using a sample interval of one for the last case also keeps it independent from the zero sample-interval fallback.

Looks good to me.

Thanks,
Lian

maintainer
#3 lore raw
In reply to Kunwu Chan

Hi Kunwu,

Quoted 7 lines · On Tue, 8 Sep 2026 14:36:35 +0800 Kunwu Chan <[email protected]> wrote:

The damon_nr_samples_per_aggr() test sets both intervals to zero, which exercises two different fallback paths at once.

Use separate compound initializers for each case so that each case tests one fallback path independently. Also make the overflow case use an explicit non-zero sample interval so that it does not depend on the zero sample interval fallback.

Thank you for sharing this patch!

Quoted 14 lines
---
 mm/damon/tests/core-kunit.h | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
mm/damon/tests/core-kunit.h +0 -0
index c01e6a75cad..ff23208c181 100644
@@ -627,12 +627,20 @@ static void damon_test_set_regions(struct kunit *test)
 
 static void damon_test_nr_samples_per_aggr(struct kunit *test)
 {

mm/damon/tests/core-kunit.h static void damon_test_set_regions(struct kunit *test) · line 637

Quoted 10 lines
mm/damon/tests/core-kunit.h +6 -2
-	struct damon_attrs attrs = {
+	struct damon_attrs attrs;
+
+	/* Zero sample interval is treated as one. */
+	attrs = (struct damon_attrs){
 		.sample_interval = 0,
-		.aggr_interval = 0,
+		.aggr_interval = 5000,
 	};
+	KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs), 5000);

Thank you for adding this test case.

mm/damon/tests/core-kunit.h static void damon_test_set_regions(struct kunit *test) · line 644

Quoted 8 lines
mm/damon/tests/core-kunit.h +5 -1
-	/* Zero aggregation interval doesn't cause division by zero */
+	/* Zero samples per aggregation is treated as one. */
+	attrs = (struct damon_attrs){
+		.sample_interval = 5000,
+		.aggr_interval = 0,
+	};
 	KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs), 1);

But damon_set_attrs() disallows sample intervals that are larger than the aggregation intervals. Hence this setup is somewhat unrealistic. Should we set sample_interval zero?

Quoted 15 lines
mm/damon/tests/core-kunit.h +4 -1
 	/*
@@ -640,7 +648,10 @@ static void damon_test_nr_samples_per_aggr(struct kunit *test)
 	 * overflow
 	 */
 	if (ULONG_MAX > UINT_MAX) {
-		attrs.aggr_interval = (unsigned long)UINT_MAX + 1;
+		attrs = (struct damon_attrs){
+			.sample_interval = 1,
+			.aggr_interval = (unsigned long)UINT_MAX + 1,
+		};
 		KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs),
 				UINT_MAX);
 	}
Quoted 3 lines

base-commit: d118502628f8b673be9023db8bdf878f64a7ed45

Signature · 1 line
2.43.0

Thanks,
SJ

author
#4 lore raw
Quoted 42 lines · On Tue, 8 Sep 2026 06:36:50 -0700 SJ Park <[email protected]> wrote:

Hi Kunwu,

Quoted 7 lines · On Tue, 8 Sep 2026 14:36:35 +0800 Kunwu Chan <[email protected]> wrote:

The damon_nr_samples_per_aggr() test sets both intervals to zero, which exercises two different fallback paths at once.

Use separate compound initializers for each case so that each case tests one fallback path independently. Also make the overflow case use an explicit non-zero sample interval so that it does not depend on the zero sample interval fallback.

Thank you for sharing this patch!

Quoted 14 lines
---
 mm/damon/tests/core-kunit.h | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
mm/damon/tests/core-kunit.h +0 -0
index c01e6a75cad..ff23208c181 100644
@@ -627,12 +627,20 @@ static void damon_test_set_regions(struct kunit *test)
 
 static void damon_test_nr_samples_per_aggr(struct kunit *test)
 {

mm/damon/tests/core-kunit.h static void damon_test_set_regions(struct kunit *test) · line 637

Quoted 10 lines
mm/damon/tests/core-kunit.h +6 -2
-	struct damon_attrs attrs = {
+	struct damon_attrs attrs;
+
+	/* Zero sample interval is treated as one. */
+	attrs = (struct damon_attrs){
 		.sample_interval = 0,
-		.aggr_interval = 0,
+		.aggr_interval = 5000,
 	};
+	KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs), 5000);

Thank you for adding this test case.

Quoted 1 line
Quoted 11 lines

mm/damon/tests/core-kunit.h static void damon_test_set_regions(struct kunit *test) · line 644

Quoted 7 lines
mm/damon/tests/core-kunit.h +5 -1
-	/* Zero aggregation interval doesn't cause division by zero */
+	/* Zero samples per aggregation is treated as one. */
+	attrs = (struct damon_attrs){
+		.sample_interval = 5000,
+		.aggr_interval = 0,
+	};
 	KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs), 1);

But damon_set_attrs() disallows sample intervals that are larger than the aggregation intervals. Hence this setup is somewhat unrealistic. Should we set sample_interval zero?

Good point.
I checked damon_set_attrs() as well, and the sample_interval <= aggr_interval constraint makes 0/0 the consistent boundary case here.

I'll update the second case accordingly.

Thanks,
KunWu

Quoted 16 lines
Quoted 16 lines
mm/damon/tests/core-kunit.h +4 -1
 	/*
@@ -640,7 +648,10 @@ static void damon_test_nr_samples_per_aggr(struct kunit *test)
 	 * overflow
 	 */
 	if (ULONG_MAX > UINT_MAX) {
-		attrs.aggr_interval = (unsigned long)UINT_MAX + 1;
+		attrs = (struct damon_attrs){
+			.sample_interval = 1,
+			.aggr_interval = (unsigned long)UINT_MAX + 1,
+		};
 		KUNIT_EXPECT_EQ(test, damon_nr_samples_per_aggr(&attrs),
 				UINT_MAX);
 	}
Quoted 8 lines
Quoted 3 lines

base-commit: d118502628f8b673be9023db8bdf878f64a7ed45

Signature · 1 line
2.43.0

Thanks,
SJ

Sent using hkml (https://github.com/sjp38/hackermail)