Path: blob/master/tools/testing/selftests/damon/damos_quota_goal.py
29268 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.023import subprocess4import time56import _damon_sysfs78def main():9# access two 10 MiB memory regions, 2 second per each10sz_region = 10 * 1024 * 102411proc = subprocess.Popen(['./access_memory', '2', '%d' % sz_region, '2000'])1213goal = _damon_sysfs.DamosQuotaGoal(14metric=_damon_sysfs.qgoal_metric_user_input, target_value=10000)15kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(16contexts=[_damon_sysfs.DamonCtx(17ops='vaddr',18targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],19schemes=[_damon_sysfs.Damos(20action='stat',21quota=_damon_sysfs.DamosQuota(22goals=[goal], reset_interval_ms=100),23)] # schemes24)] # contexts25)]) # kdamonds2627err = kdamonds.start()28if err != None:29print('kdamond start failed: %s' % err)30exit(1)3132score_values_to_test = [0, 15000, 5000, 18000]33while proc.poll() == None:34if len(score_values_to_test) == 0:35time.sleep(0.1)36continue3738goal.current_value = score_values_to_test.pop(0)39expect_increase = goal.current_value < goal.target_value4041err = kdamonds.kdamonds[0].commit_schemes_quota_goals()42if err is not None:43print('commit_schemes_quota_goals failed: %s' % err)44exit(1)4546err = kdamonds.kdamonds[0].update_schemes_effective_quotas()47if err is not None:48print('before-update_schemes_effective_quotas failed: %s' % err)49exit(1)50last_effective_bytes = goal.effective_bytes5152time.sleep(0.5)5354err = kdamonds.kdamonds[0].update_schemes_effective_quotas()55if err is not None:56print('after-update_schemes_effective_quotas failed: %s' % err)57exit(1)5859print('score: %s, effective quota: %d -> %d (%.3fx)' % (60goal.current_value, last_effective_bytes, goal.effective_bytes,61goal.effective_bytes / last_effective_bytes62if last_effective_bytes != 0 else -1.0))6364if last_effective_bytes == goal.effective_bytes:65# effective quota was already minimum that cannot be more reduced66if expect_increase is False and last_effective_bytes == 1:67continue68print('efective bytes not changed: %d' % goal.effective_bytes)69exit(1)7071increased = last_effective_bytes < goal.effective_bytes72if expect_increase != increased:73print('expectation of increase (%s) != increased (%s)' %74(expect_increase, increased))75exit(1)76last_effective_bytes = goal.effective_bytes7778if __name__ == '__main__':79main()808182