selftests/damon: misc fixes for test bugs
Subsystems (3)
cover selftests/damon: misc fixes for test bugs
From: Kunwu Chan <[email protected]>
This series fixes several bugs in the DAMON selftests. Most are trivial but cause tests to silently pass when they shouldn't, or fail prematurely on slow machines.
Patch 1 fixes mutable default arguments in DamonCtx.__init__() that cause state to leak between test instances.
Patch 2 fixes a trailing comma in DamosFilter that turns memcg_path from a string into a tuple, silently breaking memcg filter setup.
Patch 3-4 fix bugs in damos_tried_regions.py: wrong operator precedence drops the "not met" prefix from failure output, and empty early aggregation cycles cause premature test failure.
Patch 5 fixes several wrong strings that produce dead elif branches, skipped file existence checks, and broken dict key lookups.
Based on next-20260529.
base-commit: 7da7f07112610a520567421dd2ffcb51beaefbcc
Kunwu Chan (5):
selftests/damon: prevent cross-context state pollution in DamonCtx selftests/damon: fix memcg filter path handling selftests/damon/damos_tried_regions: fix expectation output and join
TypeError
selftests/damon/damos_tried_regions: handle empty tried regions in
early cycles
selftests/damon: fix dead code, skipped checks, and broken lookups
tools/testing/selftests/damon/_damon_sysfs.py | 14 +++++++++---- .../selftests/damon/damos_apply_interval.py | 2 +- .../selftests/damon/damos_quota_goal.py | 2 +- .../selftests/damon/damos_tried_regions.py | 20 ++++++++++--------- .../selftests/damon/drgn_dump_damon_status.py | 2 +- tools/testing/selftests/damon/sysfs.py | 4 ++-- tools/testing/selftests/damon/sysfs.sh | 6 +++--- ...te_schemes_tried_regions_wss_estimation.py | 2 +- 8 files changed, 30 insertions(+), 22 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/5 selftests/damon: prevent cross-context state pollution in DamonCtx
From: Kunwu Chan <[email protected]>
DamonCtx.__init__() uses mutable default values for monitoring_attrs, targets, and schemes. In Python these are evaluated once at function definition time, so multiple DamonCtx instances can unintentionally share the same lists and DamonAttrs instance.
Replace the mutable defaults with None sentinels and initialize the objects when needed.
--- tools/testing/selftests/damon/_damon_sysfs.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/damon/_damon_sysfs.py +8 -2
@@ -624,17 +624,23 @@ class DamonCtx: pause = None idx = None - def __init__(self, ops='paddr', monitoring_attrs=DamonAttrs(), targets=[], - schemes=[], pause=False): + def __init__(self, ops='paddr', monitoring_attrs=None, targets=None, + schemes=None, pause=False): self.ops = ops + if monitoring_attrs is None: + monitoring_attrs = DamonAttrs() self.monitoring_attrs = monitoring_attrs self.monitoring_attrs.context = self + if targets is None: + targets = [] self.targets = targets for idx, target in enumerate(self.targets): target.idx = idx target.context = self + if schemes is None: + schemes = [] self.schemes = schemes for idx, scheme in enumerate(self.schemes): scheme.idx = idx
Signature · 1 line
2.43.0
PATCH 2/5 selftests/damon: fix memcg filter path handling
From: Kunwu Chan <[email protected]>
A trailing comma in the memcg_path assignment makes memcg_path a single-element tuple instead of a string.
As a result, an invalid value is written to sysfs and memcg filter configuration fails.
Remove the comma.
--- tools/testing/selftests/damon/_damon_sysfs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/damon/_damon_sysfs.py +1 -1
@@ -271,7 +271,7 @@ class DamosFilter: self.type_ = type_ self.matching = matching self.allow = allow - self.memcg_path = memcg_path, + self.memcg_path = memcg_path self.addr_start = addr_start self.addr_end = addr_end self.target_idx = target_idx
Signature · 1 line
2.43.0
PATCH 3/5 selftests/damon/damos_tried_regions: fix expectation output and join TypeError
In reply to Kunwu ChanFrom: Kunwu Chan <[email protected]>
The expectation print has wrong operator precedence: '%' binds before the conditional expression, so the else branch prints 'not met' without the prefix 'expectation (>= 14) is'. Add parentheses to fix it.
Also, '\n'.join() on the list of ints raises TypeError; convert to str in the list comprehension.
--- tools/testing/selftests/damon/damos_tried_regions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/damon/damos_tried_regions.py +2 -2
@@ -55,10 +55,10 @@ def main(): collected_nr_regions.sort() sample = collected_nr_regions[4] print('50-th percentile nr_regions: %d' % sample) - print('expectation (>= 14) is %s' % 'met' if sample >= 14 else 'not met') + print('expectation (>= 14) is %s' % ('met' if sample >= 14 else 'not met')) if collected_nr_regions[4] < 14: print('full nr_regions:') - print('\n'.join(collected_nr_regions)) + print('\n'.join(['%d' % x for x in collected_nr_regions])) exit(1) if __name__ == '__main__':
Signature · 1 line
2.43.0
PATCH 4/5 selftests/damon/damos_tried_regions: handle empty tried regions in early cycles
In reply to Kunwu ChanFrom: Kunwu Chan <[email protected]>
The test aborts if the initial aggregation cycles produce zero tried regions. This can happen on slow machines, causing false failures. Skip empty cycles and retry up to 200 times before giving up. Also check that enough samples were collected before computing the 50th percentile.
--- .../selftests/damon/damos_tried_regions.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
tools/testing/selftests/damon/damos_tried_regions.py +9 -7
@@ -28,7 +28,9 @@ def main(): exit(1) collected_nr_regions = [] - while proc.poll() is None: + nr_retries = 0 + while proc.poll() is None and nr_retries < 200: + nr_retries += 1 time.sleep(0.1) err = kdamonds.kdamonds[0].update_schemes_tried_regions() if err is not None:
@@ -38,20 +40,20 @@ def main(): scheme = kdamonds.kdamonds[0].contexts[0].schemes[0] if scheme.tried_regions is None: - proc.terminate() - print('tried regions is not collected') - exit(1) + continue nr_tried_regions = len(scheme.tried_regions) if nr_tried_regions <= 0: - proc.terminate() - print('tried regions is not created') - exit(1) + continue collected_nr_regions.append(nr_tried_regions) if len(collected_nr_regions) > 10: break proc.terminate() + if len(collected_nr_regions) <= 4: + print('too few tried regions samples collected') + exit(1) + collected_nr_regions.sort() sample = collected_nr_regions[4] print('50-th percentile nr_regions: %d' % sample)
Signature · 1 line
2.43.0
PATCH 5/5 selftests/damon: fix dead code, skipped checks, and broken lookups
In reply to Kunwu ChanFrom: Kunwu Chan <[email protected]>
'hugeapge_size' in drgn_dump_damon_status.py was a dead elif branch. $fail_reason in sysfs.sh was undefined, silently emptying the error message. 'exit' instead of 'exist' in sysfs.sh skipped a file existence check. 'nohugeapge' in sysfs.py broke an action dict lookup.
Fix other wrong strings in the same files.
--- tools/testing/selftests/damon/_damon_sysfs.py | 2 +- tools/testing/selftests/damon/damos_apply_interval.py | 2 +- tools/testing/selftests/damon/damos_quota_goal.py | 2 +- tools/testing/selftests/damon/drgn_dump_damon_status.py | 2 +- tools/testing/selftests/damon/sysfs.py | 4 ++-- tools/testing/selftests/damon/sysfs.sh | 6 +++--- .../sysfs_update_schemes_tried_regions_wss_estimation.py | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/damon/_damon_sysfs.py +1 -1
@@ -837,7 +837,7 @@ class Kdamond: for goal in scheme.quota.goals: err = goal.stage() if err is not None: - print('commit_schemes_quota_goals failed stagign: %s'% + print('commit_schemes_quota_goals failed staging: %s'% err) exit(1) return write_file(os.path.join(self.sysfs_dir(), 'state'),
tools/testing/selftests/damon/damos_apply_interval.py +1 -1
@@ -56,7 +56,7 @@ def main(): # Because the second scheme was having the apply interval that is ten times # lower than that of the first scheme, the second scheme should be tried # about ten times more frequently than the first scheme. For possible - # timing errors, check if it was at least nine times more freuqnetly tried. + # timing errors, check if it was at least nine times more frequently tried. ratio = nr_tried_stats[1] / nr_tried_stats[0] if ratio < 9: print('%d / %d = %f (< 9)' %
tools/testing/selftests/damon/damos_quota_goal.py +1 -1
@@ -66,7 +66,7 @@ def main(): # effective quota was already minimum that cannot be more reduced if expect_increase is False and last_effective_bytes == 1: continue - print('efective bytes not changed: %d' % goal.effective_bytes) + print('effective bytes not changed: %d' % goal.effective_bytes) exit(1) increased = last_effective_bytes < goal.effective_bytes
tools/testing/selftests/damon/drgn_dump_damon_status.py +1 -1
@@ -163,7 +163,7 @@ def damos_filter_to_dict(damos_filter): int(damos_filter.addr_range.end)] elif type_ == 'target': dict_['target_idx'] = int(damos_filter.target_idx) - elif type_ == 'hugeapge_size': + elif type_ == 'hugepage_size': dict_['sz_range'] = [int(damos_filter.sz_range.min), int(damos_filter.sz_range.max)] return dict_
tools/testing/selftests/damon/sysfs.py +2 -2
@@ -119,7 +119,7 @@ def assert_access_pattern_committed(pattern, dump): 'max_nr_accesses', dump) assert_true(dump['min_age_region'] == pattern.age[0], 'min_age_region', dump) - assert_true(dump['max_age_region'] == pattern.age[1], 'miaxage_region', + assert_true(dump['max_age_region'] == pattern.age[1], 'max_age_region', dump) def assert_scheme_committed(scheme, dump):
@@ -129,7 +129,7 @@ def assert_scheme_committed(scheme, dump): 'cold': 1, 'pageout': 2, 'hugepage': 3, - 'nohugeapge': 4, + 'nohugepage': 4, 'collapse': 5, 'lru_prio': 6, 'lru_deprio': 7,
tools/testing/selftests/damon/sysfs.sh +3 -3
@@ -3,7 +3,7 @@ source _common.sh -# Kselftest frmework requirement - SKIP code is 4. +# Kselftest framework requirement - SKIP code is 4. ksft_skip=4 ensure_write_succ()
@@ -28,7 +28,7 @@ ensure_write_fail() if (echo "$content" > "$file") 2> /dev/null then - echo "writing $content to $file succeed ($fail_reason)" + echo "writing $content to $file succeeded ($reason)" echo "expected failure because $reason" exit 1 fi
@@ -363,7 +363,7 @@ test_context() { context_dir=$1 ensure_dir "$context_dir" "exist" - ensure_file "$context_dir/avail_operations" "exit" 400 + ensure_file "$context_dir/avail_operations" "exist" 400 ensure_file "$context_dir/operations" "exist" 600 ensure_file "$context_dir/addr_unit" "exist" 600 ensure_file "$context_dir/pause" "exist" 600
tools/testing/selftests/damon/sysfs_update_schemes_tried_regions_wss_estimation.py +1 -1
@@ -7,7 +7,7 @@ import time import _damon_sysfs def pass_wss_estimation(sz_region): - # access two regions of given size, 2 seocnds per each region + # access two regions of given size, 2 seconds per each region proc = subprocess.Popen( ['./access_memory', '2', '%d' % sz_region, '2000', 'repeat']) kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
Signature · 1 line
2.43.0
Re: [PATCH 1/5] selftests/damon: prevent cross-context state pollution in DamonCtx
In reply to Kunwu ChanQuoted 10 lines · On Sun, 31 May 2026 17:17:20 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
DamonCtx.__init__()uses mutable default values formonitoring_attrs, targets, and schemes. In Python these are evaluated once at function definition time, so multiple DamonCtx instances can unintentionally share the same lists and DamonAttrs instance.Replace the mutable defaults with None sentinels and initialize the objects when needed.
Nice change, thank you!
Quoted 4 lines
Co-developed-by Wang Lian <[email protected]>Signed-off-by Wang Lian <[email protected]>Signed-off-by Kunwu Chan <[email protected]>
checkpatch.pl complains as below:
WARNING: From:/Signed-off-by: email address mismatch: 'From: Kunwu Chan <[email protected]>' != 'Signed-off-by: Kunwu Chan <[email protected]>'
Other than that,
Thanks,
SJ
[...]
SeongJae Park
maintainerRe: [PATCH 2/5] selftests/damon: fix memcg filter path handling
In reply to Kunwu ChanConventionally, this mail should be sent as a direct reply to the coverletter. But somehow this was sent as a reply to the first patch of the series. Please send all patches of a series as a direct reply to the coverletter from the next time.
Quoted 9 lines · On Sun, 31 May 2026 17:17:21 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
A trailing comma in the
memcg_pathassignment makesmemcg_patha single-element tuple instead of a string.As a result, an invalid value is written to sysfs and memcg filter configuration fails.
Remove the comma.
Good findinng! But, Cheng sent [1] a same patch earlier. I believe Cheng is working on revisioning it. I think we should give Cheng more time for that.
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ
[...]
Re: [PATCH 3/5] selftests/damon/damos_tried_regions: fix expectation output and join TypeError
In reply to Kunwu ChanQuoted 9 lines · On Sun, 31 May 2026 17:17:22 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
The expectation print has wrong operator precedence: '%' binds before the conditional expression, so the else branch prints 'not met' without the prefix 'expectation (>= 14) is'. Add parentheses to fix it.
Also, '\n'.join() on the list of ints raises TypeError; convert to str in the list comprehension.
Nice findings and fixes, thank you!
Quoted 4 lines
Co-developed-by Wang Lian <[email protected]>Signed-off-by Wang Lian <[email protected]>Signed-off-by Kunwu Chan <[email protected]>
checkpatch.pl may warn here like below, and it should be fixed.
WARNING: From:/Signed-off-by: email address mismatch: 'From: Kunwu Chan <[email protected]>' != 'Signed-off-by: Kunwu Chan <[email protected]>'
Other than that,
Thanks,
SJ
[...]
SeongJae Park
maintainerRe: [PATCH 4/5] selftests/damon/damos_tried_regions: handle empty tried regions in early cycles
In reply to Kunwu ChanQuoted 7 lines · On Sun, 31 May 2026 17:17:23 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
The test aborts if the initial aggregation cycles produce zero tried regions. This can happen on slow machines, causing false failures. Skip empty cycles and retry up to 200 times before giving up. Also check that enough samples were collected before computing the 50th percentile.
I agree this will make the test be more reliable. I'm bit concerned if 200 times retry can make the test run too long, though.
Also, could you further elaborate why this can fail on slow machines? That is, DAMON will check the access of 'access_memory_even' process every 5ms. Are you thinking the 5ms is too short for 'access_memory_event' to make the expected access (accessing the 7 regins of 10 MiB size) within? If so, should we increase the sampling interval before retrying?
I also suspect if the unreliable results you seen is due to the fact that DAMON is not flushing TLB, like we discussed before. If that's the case, could we increase the working set size of this test, similar to the wss_estimation test?
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ
[...]
Re: [PATCH 5/5] selftests/damon: fix dead code, skipped checks, and broken lookups
In reply to Kunwu ChanQuoted 9 lines · On Sun, 31 May 2026 17:17:24 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
'
hugeapge_size' indrgn_dump_damon_status.pywas a dead elif branch. $fail_reason insysfs.shwas undefined, silently emptying the error message. 'exit' instead of 'exist' insysfs.shskipped a file existence check. 'nohugeapge' insysfs.pybroke an action dict lookup.Fix other wrong strings in the same files.
Good eyes! Thank you for fixing these!
Quoted 4 lines
Co-developed-by Wang Lian <[email protected]>Signed-off-by Wang Lian <[email protected]>Signed-off-by Kunwu Chan <[email protected]>
Checkpatch.pl will warn as below, and it should be fixed.
WARNING: From:/Signed-off-by: email address mismatch: 'From: Kunwu Chan <[email protected]>' != 'Signed-off-by: Kunwu Chan <[email protected]>'
Other than that,
Thanks,
SJ
[...]
SeongJae Park
maintainerQuoted 5 lines · On Sun, 31 May 2026 16:56:28 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
This series fixes several bugs in the DAMON selftests. Most are trivial but cause tests to silently pass when they shouldn't, or fail prematurely on slow machines.
Thank you for sharing these great changes, Kunwu and Lian!
Quoted 8 lines
Patch 1 fixes mutable default arguments in
DamonCtx.__init__()that cause state to leak between test instances.Patch 2 fixes a trailing comma in DamosFilter that turns
memcg_pathfrom a string into a tuple, silently breaking memcg filter setup.
Quoted 7 lines
Patch 3-4 fix bugs in
damos_tried_regions.py: wrong operator precedence drops the "not met" prefix from failure output, and empty early aggregation cycles cause premature test failure.Patch 5 fixes several wrong strings that produce dead elif branches, skipped file existence checks, and broken dict key lookups.
I left comments to each patch. To summarize,
Patches 1, 3 and 5 look good except signer/author info mismatch.
For patches 2 and 4, I think we need more discussions.
Thanks,
SJ
[...]
Re: [PATCH 1/5] selftests/damon: prevent cross-context state pollution in DamonCtx
In reply to SeongJae ParkQuoted 12 lines · SeongJae Park
Quoted 9 lines · On Sun, 31 May 2026 17:17:20 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
DamonCtx.__init__()uses mutable default values formonitoring_attrs, targets, and schemes. In Python these are evaluated once at function definition time, so multiple DamonCtx instances can unintentionally share the same lists and DamonAttrs instance.
Quoted 5 lines
Quoted 3 lines
Replace the mutable defaults with None sentinels and initialize the objects when needed.
Nice change, thank you!
Thanks, SJ.
Quoted 5 lines
Quoted 5 lines
Co-developed-by Wang Lian <[email protected]>Signed-off-by Wang Lian <[email protected]>Signed-off-by Kunwu Chan <[email protected]>
Quoted 8 lines
checkpatch.pl complains as below:
WARNING: From:/Signed-off-by: email address mismatch: 'From: Kunwu Chan <[email protected]>' != 'Signed-off-by: Kunwu Chan <[email protected]>'
Other than that,
Reviewed-by SeongJae Park <[email protected]>
I'll fix the email mismatch and send a v2 to make the addresses consistent.
I'll also add your Reviewed-by tag.
Thanks,
Kunwu
Quoted 5 lines
Thanks,
SJ[...]
Re: [PATCH 2/5] selftests/damon: fix memcg filter path handling
In reply to SeongJae ParkQuoted 6 lines · SeongJae Park
Conventionally, this mail should be sent as a direct reply to the coverletter. But somehow this was sent as a reply to the first patch of the series. Please send all patches of a series as a direct reply to the coverletter from the next time.
Thanks, SJ.
Sorry about that. I'll make sure future revisions are sent as direct replies to the cover letter.
Quoted 8 lines · On Sun, 31 May 2026 17:17:21 +0800 Kunwu Chan <[email protected]> wrote:
Quoted 7 lines
From: Kunwu Chan <[email protected]>
A trailing comma in the
memcg_pathassignment makesmemcg_patha single-element tuple instead of a string.
Quoted 8 lines
Quoted 5 lines
As a result, an invalid value is written to sysfs and memcg filter configuration fails.
Remove the comma.
Good findinng! But, Cheng sent [1] a same patch earlier. I believe Cheng is working on revisioning it. I think we should give Cheng more time for that.
Yes, I noticed that after sending the series and saw that Cheng had already posted the same fix.
I'll drop this patch from v2 and let Cheng continue with his version.
Thanks,
Kunwu
Quoted 7 lines
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ[...]
Re: [PATCH 4/5] selftests/damon/damos_tried_regions: handle empty tried regions in early cycles
In reply to SeongJae ParkQuoted 21 lines · SeongJae Park
Quoted 9 lines · On Sun, 31 May 2026 17:17:23 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
The test aborts if the initial aggregation cycles produce zero tried regions. This can happen on slow machines, causing false failures. Skip empty cycles and retry up to 200 times before giving up. Also check that enough samples were collected before computing the 50th percentile.
I agree this will make the test be more reliable. I'm bit concerned if 200 times retry can make the test run too long, though.
Also, could you further elaborate why this can fail on slow machines? That is, DAMON will check the access of '
access_memory_even' process every 5ms. Are you thinking the 5ms is too short for 'access_memory_event' to make the expected access (accessing the 7 regins of 10 MiB size) within? If so, should we increase the sampling interval before retrying?
Quoted 4 lines
I also suspect if the unreliable results you seen is due to the fact that DAMON is not flushing TLB, like we discussed before. If that's the case, could we increase the working set size of this test, similar to the
wss_estimationtest?
Thanks, SJ.
Good points.
I don't yet have enough evidence to say whether this is primarily due to scheduling delays, a too-short sampling interval, or effects from not flushing TLB.
I'll investigate the root cause and see if increasing the working set size or adjusting the test configuration may be a cleaner solution than adding retries.
I'll drop this patch from v2 for now and revisit it once I better understand the root cause.
Thanks,
Kunwu
Quoted 7 lines
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ[...]
Quoted 28 lines · SeongJae Park
Quoted 7 lines · On Sun, 31 May 2026 16:56:28 +0800 Kunwu Chan <[email protected]> wrote:
From: Kunwu Chan <[email protected]>
This series fixes several bugs in the DAMON selftests. Most are trivial but cause tests to silently pass when they shouldn't, or fail prematurely on slow machines.
Thank you for sharing these great changes, Kunwu and Lian!
Quoted 8 lines
Patch 1 fixes mutable default arguments in
DamonCtx.__init__()
that cause state to leak between test instances.Patch 2 fixes a trailing comma in DamosFilter that turns
memcg_pathfrom a string into a tuple, silently breaking memcg filter setup.Quoted 8 lines
Patch 3-4 fix bugs in
damos_tried_regions.py: wrong operator precedence drops the "not met" prefix from failure output, and empty early aggregation cycles cause premature test failure.Patch 5 fixes several wrong strings that produce dead elif branches, skipped file existence checks, and broken dict key lookups.
Quoted 6 lines
I left comments to each patch. To summarize,
Patches 1, 3 and 5 look good except signer/author info mismatch.
For patches 2 and 4, I think we need more discussions.
Thanks, SJ.
Noted. I'll fix the signer/author mismatch in v2. As discussed in the individual replies, I'll drop patches 2 and 4 for now and revisit 4 once the underlying issues are better understood.
Thanks,
Kunwu
Quoted 5 lines
Thanks,
SJ[...]