| 1 | |
| 2 | |
| 3 |
|
| 4 | import os
|
| 5 | import subprocess
|
| 6 | import time
|
| 7 |
|
| 8 | import _damon_sysfs
|
| 9 |
|
| 10 | def main():
|
| 11 | |
| 12 | |
| 13 | sz_region = 10 * 1024 * 1024
|
| 14 | proc = subprocess.Popen(
|
| 15 | ['./access_memory', '1', '%d' % sz_region, '60000', 'repeat'])
|
| 16 |
|
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | kdamond = _damon_sysfs.Kdamond(
|
| 22 | refresh_ms=100,
|
| 23 | contexts=[_damon_sysfs.DamonCtx(
|
| 24 | ops='vaddr',
|
| 25 | targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
|
| 26 | schemes=[_damon_sysfs.Damos(action='stat')],
|
| 27 | )])
|
| 28 | kdamonds = _damon_sysfs.Kdamonds([kdamond])
|
| 29 |
|
| 30 | err = kdamonds.start()
|
| 31 | if err is not None:
|
| 32 | |
| 33 | |
| 34 | if not os.path.exists(os.path.join(kdamond.sysfs_dir(), 'refresh_ms')):
|
| 35 | proc.terminate()
|
| 36 | proc.wait()
|
| 37 | print('kdamond has no refresh_ms file; skipping')
|
| 38 | exit(_damon_sysfs.ksft_skip)
|
| 39 | proc.terminate()
|
| 40 | proc.wait()
|
| 41 | print('kdamond start failed: %s' % err)
|
| 42 | exit(1)
|
| 43 |
|
| 44 | scheme = kdamond.contexts[0].schemes[0]
|
| 45 | nr_tried_path = os.path.join(scheme.sysfs_dir(), 'stats', 'nr_tried')
|
| 46 |
|
| 47 | try:
|
| 48 | |
| 49 | |
| 50 | |
| 51 | nr_tried = 0
|
| 52 | deadline = time.monotonic() + 10
|
| 53 | while time.monotonic() < deadline:
|
| 54 | if proc.poll() is not None:
|
| 55 | print('the access_memory target exited unexpectedly')
|
| 56 | exit(1)
|
| 57 | content, err = _damon_sysfs.read_file(nr_tried_path)
|
| 58 | if err is not None:
|
| 59 | print('reading %s failed: %s' % (nr_tried_path, err))
|
| 60 | exit(1)
|
| 61 | nr_tried = int(content)
|
| 62 | if nr_tried > 0:
|
| 63 | break
|
| 64 | time.sleep(0.1)
|
| 65 | finally:
|
| 66 | kdamonds.stop()
|
| 67 | proc.terminate()
|
| 68 | proc.wait()
|
| 69 |
|
| 70 | if nr_tried == 0:
|
| 71 | print('refresh_ms did not auto-update the schemes stats')
|
| 72 | exit(1)
|
| 73 |
|
| 74 | if __name__ == '__main__':
|
| 75 | main()
|