xref: /linux/tools/testing/selftests/damon/damos_quota.py (revision 55d0969c451159cff86949b38c39171cab962069)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3
4import subprocess
5import time
6
7import _damon_sysfs
8
9def main():
10    # access two 10 MiB memory regions, 2 second per each
11    sz_region = 10 * 1024 * 1024
12    proc = subprocess.Popen(['./access_memory', '2', '%d' % sz_region, '2000'])
13
14    # Set quota up to 1 MiB per 100 ms
15    sz_quota = 1024 * 1024 # 1 MiB
16    quota_reset_interval = 100 # 100 ms
17    kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
18            contexts=[_damon_sysfs.DamonCtx(
19                ops='vaddr',
20                targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
21                schemes=[_damon_sysfs.Damos(
22                    access_pattern=_damon_sysfs.DamosAccessPattern(
23                        # >= 25% access rate, >= 200ms age
24                        nr_accesses=[5, 20], age=[2, 2**64 - 1]),
25                    quota=_damon_sysfs.DamosQuota(
26                        sz=sz_quota, reset_interval_ms=quota_reset_interval)
27                    )] # schemes
28                )] # contexts
29            )]) # kdamonds
30
31    err = kdamonds.start()
32    if err != None:
33        print('kdamond start failed: %s' % err)
34        exit(1)
35
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_tried_bytes()
41        if err != None:
42            print('tried bytes update failed: %s' % err)
43            exit(1)
44        err = kdamonds.kdamonds[0].update_schemes_stats()
45        if err != None:
46            print('stats update failed: %s' % err)
47            exit(1)
48
49        scheme = kdamonds.kdamonds[0].contexts[0].schemes[0]
50        wss_collected.append(scheme.tried_bytes)
51        nr_quota_exceeds = scheme.stats.qt_exceeds
52
53    wss_collected.sort()
54    for wss in wss_collected:
55        if wss > sz_quota:
56            print('quota is not kept: %s > %s' % (wss, sz_quota))
57            print('collected samples are as below')
58            print('\n'.join(['%d' % wss for wss in wss_collected]))
59            exit(1)
60
61    if nr_quota_exceeds < len(wss_collected):
62        print('quota is not always exceeded: %d > %d' %
63              (len(wss_collected), nr_quota_exceeds))
64        exit(1)
65
66if __name__ == '__main__':
67    main()
68