damon_nr_regions.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. import subprocess
  4. import time
  5. import _damon_sysfs
  6. def test_nr_regions(real_nr_regions, min_nr_regions, max_nr_regions):
  7. '''
  8. Create process of the given 'real_nr_regions' regions, monitor it using
  9. DAMON with given '{min,max}_nr_regions' monitoring parameter.
  10. Exit with non-zero return code if the given {min,max}_nr_regions is not
  11. kept.
  12. '''
  13. sz_region = 10 * 1024 * 1024
  14. proc = subprocess.Popen(['./access_memory_even', '%d' % real_nr_regions,
  15. '%d' % sz_region])
  16. # stat every monitored regions
  17. kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
  18. contexts=[_damon_sysfs.DamonCtx(
  19. monitoring_attrs=_damon_sysfs.DamonAttrs(
  20. min_nr_regions=min_nr_regions,
  21. max_nr_regions=max_nr_regions),
  22. ops='vaddr',
  23. targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
  24. schemes=[_damon_sysfs.Damos(action='stat',
  25. )] # schemes
  26. )] # contexts
  27. )]) # kdamonds
  28. err = kdamonds.start()
  29. if err is not None:
  30. proc.terminate()
  31. print('kdamond start failed: %s' % err)
  32. exit(1)
  33. collected_nr_regions = []
  34. while proc.poll() is None:
  35. time.sleep(0.1)
  36. err = kdamonds.kdamonds[0].update_schemes_tried_regions()
  37. if err is not None:
  38. proc.terminate()
  39. print('tried regions update failed: %s' % err)
  40. exit(1)
  41. scheme = kdamonds.kdamonds[0].contexts[0].schemes[0]
  42. if scheme.tried_regions is None:
  43. proc.terminate()
  44. print('tried regions is not collected')
  45. exit(1)
  46. nr_tried_regions = len(scheme.tried_regions)
  47. if nr_tried_regions <= 0:
  48. proc.terminate()
  49. print('tried regions is not created')
  50. exit(1)
  51. collected_nr_regions.append(nr_tried_regions)
  52. if len(collected_nr_regions) > 10:
  53. break
  54. proc.terminate()
  55. kdamonds.stop()
  56. test_name = 'nr_regions test with %d/%d/%d real/min/max nr_regions' % (
  57. real_nr_regions, min_nr_regions, max_nr_regions)
  58. collected_nr_regions.sort()
  59. if (collected_nr_regions[0] < min_nr_regions or
  60. collected_nr_regions[-1] > max_nr_regions):
  61. print('fail %s' % test_name)
  62. print('number of regions that collected are:')
  63. for nr in collected_nr_regions:
  64. print(nr)
  65. exit(1)
  66. print('pass %s ' % test_name)
  67. def main():
  68. # test min_nr_regions larger than real nr regions
  69. test_nr_regions(10, 20, 100)
  70. # test max_nr_regions smaller than real nr regions
  71. test_nr_regions(15, 3, 10)
  72. # test online-tuned max_nr_regions that smaller than real nr regions
  73. sz_region = 10 * 1024 * 1024
  74. proc = subprocess.Popen(['./access_memory_even', '14', '%d' % sz_region])
  75. # stat every monitored regions
  76. kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
  77. contexts=[_damon_sysfs.DamonCtx(
  78. monitoring_attrs=_damon_sysfs.DamonAttrs(
  79. min_nr_regions=10, max_nr_regions=1000),
  80. ops='vaddr',
  81. targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
  82. schemes=[_damon_sysfs.Damos(action='stat',
  83. )] # schemes
  84. )] # contexts
  85. )]) # kdamonds
  86. err = kdamonds.start()
  87. if err is not None:
  88. proc.terminate()
  89. print('kdamond start failed: %s' % err)
  90. exit(1)
  91. # wait until the real regions are found
  92. time.sleep(3)
  93. attrs = kdamonds.kdamonds[0].contexts[0].monitoring_attrs
  94. attrs.min_nr_regions = 3
  95. attrs.max_nr_regions = 7
  96. attrs.update_us = 100000
  97. err = kdamonds.kdamonds[0].commit()
  98. if err is not None:
  99. proc.terminate()
  100. print('commit failed: %s' % err)
  101. exit(1)
  102. # wait for next merge operation is executed
  103. time.sleep(0.3)
  104. err = kdamonds.kdamonds[0].update_schemes_tried_regions()
  105. if err is not None:
  106. proc.terminate()
  107. print('tried regions update failed: %s' % err)
  108. exit(1)
  109. scheme = kdamonds.kdamonds[0].contexts[0].schemes[0]
  110. if scheme.tried_regions is None:
  111. proc.terminate()
  112. print('tried regions is not collected')
  113. exit(1)
  114. nr_tried_regions = len(scheme.tried_regions)
  115. if nr_tried_regions <= 0:
  116. proc.terminate()
  117. print('tried regions is not created')
  118. exit(1)
  119. proc.terminate()
  120. if nr_tried_regions > 7:
  121. print('fail online-tuned max_nr_regions: %d > 7' % nr_tried_regions)
  122. exit(1)
  123. print('pass online-tuned max_nr_regions')
  124. if __name__ == '__main__':
  125. main()