xref: /linux/tools/testing/selftests/damon/damos_quota_goal.py (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*f1c07c0aSSeongJae Park#!/usr/bin/env python3
2*f1c07c0aSSeongJae Park# SPDX-License-Identifier: GPL-2.0
3*f1c07c0aSSeongJae Park
4*f1c07c0aSSeongJae Parkimport subprocess
5*f1c07c0aSSeongJae Parkimport time
6*f1c07c0aSSeongJae Park
7*f1c07c0aSSeongJae Parkimport _damon_sysfs
8*f1c07c0aSSeongJae Park
9*f1c07c0aSSeongJae Parkdef main():
10*f1c07c0aSSeongJae Park    # access two 10 MiB memory regions, 2 second per each
11*f1c07c0aSSeongJae Park    sz_region = 10 * 1024 * 1024
12*f1c07c0aSSeongJae Park    proc = subprocess.Popen(['./access_memory', '2', '%d' % sz_region, '2000'])
13*f1c07c0aSSeongJae Park
14*f1c07c0aSSeongJae Park    goal = _damon_sysfs.DamosQuotaGoal(
15*f1c07c0aSSeongJae Park            metric=_damon_sysfs.qgoal_metric_user_input, target_value=10000)
16*f1c07c0aSSeongJae Park    kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
17*f1c07c0aSSeongJae Park            contexts=[_damon_sysfs.DamonCtx(
18*f1c07c0aSSeongJae Park                ops='vaddr',
19*f1c07c0aSSeongJae Park                targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
20*f1c07c0aSSeongJae Park                schemes=[_damon_sysfs.Damos(
21*f1c07c0aSSeongJae Park                    action='stat',
22*f1c07c0aSSeongJae Park                    quota=_damon_sysfs.DamosQuota(
23*f1c07c0aSSeongJae Park                        goals=[goal], reset_interval_ms=100),
24*f1c07c0aSSeongJae Park                    )] # schemes
25*f1c07c0aSSeongJae Park                )] # contexts
26*f1c07c0aSSeongJae Park            )]) # kdamonds
27*f1c07c0aSSeongJae Park
28*f1c07c0aSSeongJae Park    err = kdamonds.start()
29*f1c07c0aSSeongJae Park    if err != None:
30*f1c07c0aSSeongJae Park        print('kdamond start failed: %s' % err)
31*f1c07c0aSSeongJae Park        exit(1)
32*f1c07c0aSSeongJae Park
33*f1c07c0aSSeongJae Park    score_values_to_test = [0, 15000, 5000, 18000]
34*f1c07c0aSSeongJae Park    while proc.poll() == None:
35*f1c07c0aSSeongJae Park        if len(score_values_to_test) == 0:
36*f1c07c0aSSeongJae Park            time.sleep(0.1)
37*f1c07c0aSSeongJae Park            continue
38*f1c07c0aSSeongJae Park
39*f1c07c0aSSeongJae Park        goal.current_value = score_values_to_test.pop(0)
40*f1c07c0aSSeongJae Park        expect_increase = goal.current_value < goal.target_value
41*f1c07c0aSSeongJae Park
42*f1c07c0aSSeongJae Park        err = kdamonds.kdamonds[0].commit_schemes_quota_goals()
43*f1c07c0aSSeongJae Park        if err is not None:
44*f1c07c0aSSeongJae Park            print('commit_schemes_quota_goals failed: %s' % err)
45*f1c07c0aSSeongJae Park            exit(1)
46*f1c07c0aSSeongJae Park
47*f1c07c0aSSeongJae Park        err = kdamonds.kdamonds[0].update_schemes_effective_quotas()
48*f1c07c0aSSeongJae Park        if err is not None:
49*f1c07c0aSSeongJae Park            print('before-update_schemes_effective_quotas failed: %s' % err)
50*f1c07c0aSSeongJae Park            exit(1)
51*f1c07c0aSSeongJae Park        last_effective_bytes = goal.effective_bytes
52*f1c07c0aSSeongJae Park
53*f1c07c0aSSeongJae Park        time.sleep(0.5)
54*f1c07c0aSSeongJae Park
55*f1c07c0aSSeongJae Park        err = kdamonds.kdamonds[0].update_schemes_effective_quotas()
56*f1c07c0aSSeongJae Park        if err is not None:
57*f1c07c0aSSeongJae Park            print('after-update_schemes_effective_quotas failed: %s' % err)
58*f1c07c0aSSeongJae Park            exit(1)
59*f1c07c0aSSeongJae Park
60*f1c07c0aSSeongJae Park        print('score: %s, effective quota: %d -> %d (%.3fx)' % (
61*f1c07c0aSSeongJae Park            goal.current_value, last_effective_bytes, goal.effective_bytes,
62*f1c07c0aSSeongJae Park            goal.effective_bytes / last_effective_bytes
63*f1c07c0aSSeongJae Park            if last_effective_bytes != 0 else -1.0))
64*f1c07c0aSSeongJae Park
65*f1c07c0aSSeongJae Park        if last_effective_bytes == goal.effective_bytes:
66*f1c07c0aSSeongJae Park            print('efective bytes not changed: %d' % goal.effective_bytes)
67*f1c07c0aSSeongJae Park            exit(1)
68*f1c07c0aSSeongJae Park
69*f1c07c0aSSeongJae Park        increased = last_effective_bytes < goal.effective_bytes
70*f1c07c0aSSeongJae Park        if expect_increase != increased:
71*f1c07c0aSSeongJae Park            print('expectation of increase (%s) != increased (%s)' %
72*f1c07c0aSSeongJae Park                  (expect_increase, increased))
73*f1c07c0aSSeongJae Park            exit(1)
74*f1c07c0aSSeongJae Park        last_effective_bytes = goal.effective_bytes
75*f1c07c0aSSeongJae Park
76*f1c07c0aSSeongJae Parkif __name__ == '__main__':
77*f1c07c0aSSeongJae Park    main()
78