damos_apply_interval.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. import subprocess
  4. import time
  5. import _damon_sysfs
  6. def main():
  7. # access two 10 MiB memory regions, 2 second per each
  8. sz_region = 10 * 1024 * 1024
  9. proc = subprocess.Popen(['./access_memory', '2', '%d' % sz_region, '2000'])
  10. # Set quota up to 1 MiB per 100 ms
  11. kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
  12. contexts=[_damon_sysfs.DamonCtx(
  13. ops='vaddr',
  14. targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
  15. schemes=[
  16. _damon_sysfs.Damos(
  17. access_pattern=_damon_sysfs.DamosAccessPattern(
  18. # >= 25% access rate, >= 200ms age
  19. nr_accesses=[5, 20], age=[2, 2**64 - 1]),
  20. # aggregation interval (100 ms) is used
  21. apply_interval_us=0),
  22. # use 10ms apply interval
  23. _damon_sysfs.Damos(
  24. access_pattern=_damon_sysfs.DamosAccessPattern(
  25. # >= 25% access rate, >= 200ms age
  26. nr_accesses=[5, 20], age=[2, 2**64 - 1]),
  27. # explicitly set 10 ms apply interval
  28. apply_interval_us=10 * 1000)
  29. ] # schemes
  30. )] # contexts
  31. )]) # kdamonds
  32. err = kdamonds.start()
  33. if err != None:
  34. print('kdamond start failed: %s' % err)
  35. exit(1)
  36. wss_collected = []
  37. nr_quota_exceeds = 0
  38. while proc.poll() == None:
  39. time.sleep(0.1)
  40. err = kdamonds.kdamonds[0].update_schemes_stats()
  41. if err != None:
  42. print('stats update failed: %s' % err)
  43. exit(1)
  44. schemes = kdamonds.kdamonds[0].contexts[0].schemes
  45. nr_tried_stats = [s.stats.nr_tried for s in schemes]
  46. if nr_tried_stats[0] == 0 or nr_tried_stats[1] == 0:
  47. print('scheme(s) are not tried')
  48. exit(1)
  49. # Because the second scheme was having the apply interval that is ten times
  50. # lower than that of the first scheme, the second scheme should be tried
  51. # about ten times more frequently than the first scheme. For possible
  52. # timing errors, check if it was at least nine times more freuqnetly tried.
  53. ratio = nr_tried_stats[1] / nr_tried_stats[0]
  54. if ratio < 9:
  55. print('%d / %d = %f (< 9)' %
  56. (nr_tried_stats[1], nr_tried_stats[0], ratio))
  57. exit(1)
  58. if __name__ == '__main__':
  59. main()