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