| 1 | |
| 2 | |
| 3 |
|
| 4 | import json
|
| 5 | import os
|
| 6 | import subprocess
|
| 7 | import sys
|
| 8 |
|
| 9 | import _damon_sysfs
|
| 10 |
|
| 11 | def dump_damon_status_dict(pid):
|
| 12 | try:
|
| 13 | subprocess.check_output(['which', 'drgn'], stderr=subprocess.DEVNULL)
|
| 14 | except:
|
| 15 | return None, 'drgn not found'
|
| 16 | file_dir = os.path.dirname(os.path.abspath(__file__))
|
| 17 | dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py')
|
| 18 | rc = subprocess.call(['drgn', dump_script, pid, 'damon_dump_output'],
|
| 19 | stderr=subprocess.DEVNULL)
|
| 20 |
|
| 21 | if rc != 0:
|
| 22 | return None, f'drgn fail: return code({rc})'
|
| 23 | try:
|
| 24 | with open('damon_dump_output', 'r') as f:
|
| 25 | return json.load(f), None
|
| 26 | except Exception as e:
|
| 27 | return None, 'json.load fail (%s)' % e
|
| 28 |
|
| 29 | def main():
|
| 30 | kdamonds = _damon_sysfs.Kdamonds(
|
| 31 | [_damon_sysfs.Kdamond(
|
| 32 | contexts=[_damon_sysfs.DamonCtx(
|
| 33 | schemes=[_damon_sysfs.Damos(
|
| 34 | ops_filters=[
|
| 35 | _damon_sysfs.DamosFilter(
|
| 36 | type_='anon',
|
| 37 | matching=True,
|
| 38 | allow=True,
|
| 39 | )
|
| 40 | ]
|
| 41 | )],
|
| 42 | )])]
|
| 43 | )
|
| 44 |
|
| 45 | err = kdamonds.start()
|
| 46 | if err is not None:
|
| 47 | print('kdamond start failed: %s' % err)
|
| 48 | exit(1)
|
| 49 |
|
| 50 | before_commit_status, err = \
|
| 51 | dump_damon_status_dict(kdamonds.kdamonds[0].pid)
|
| 52 | if err is not None:
|
| 53 | print('before-commit status dump failed: %s' % err)
|
| 54 | exit(1)
|
| 55 |
|
| 56 | kdamonds.kdamonds[0].commit()
|
| 57 |
|
| 58 | after_commit_status, err = \
|
| 59 | dump_damon_status_dict(kdamonds.kdamonds[0].pid)
|
| 60 | if err is not None:
|
| 61 | print('after-commit status dump failed: %s' % err)
|
| 62 | exit(1)
|
| 63 |
|
| 64 | if before_commit_status != after_commit_status:
|
| 65 | print(f'before: {json.dumps(before_commit_status, indent=2)}')
|
| 66 | print(f'after: {json.dumps(after_commit_status, indent=2)}')
|
| 67 | exit(1)
|
| 68 |
|
| 69 | kdamonds.stop()
|
| 70 |
|
| 71 | if __name__ == '__main__':
|
| 72 | main()
|