xref: /linux/tools/testing/selftests/damon/sysfs.py (revision 4ece01897627ddeefcede4ac709cd99763994dc4)
1*4ece0189SSeongJae Park#!/usr/bin/env python3
2*4ece0189SSeongJae Park# SPDX-License-Identifier: GPL-2.0
3*4ece0189SSeongJae Park
4*4ece0189SSeongJae Parkimport json
5*4ece0189SSeongJae Parkimport os
6*4ece0189SSeongJae Parkimport subprocess
7*4ece0189SSeongJae Park
8*4ece0189SSeongJae Parkimport _damon_sysfs
9*4ece0189SSeongJae Park
10*4ece0189SSeongJae Parkdef dump_damon_status_dict(pid):
11*4ece0189SSeongJae Park    file_dir = os.path.dirname(os.path.abspath(__file__))
12*4ece0189SSeongJae Park    dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py')
13*4ece0189SSeongJae Park    rc = subprocess.call(['drgn', dump_script, pid, 'damon_dump_output'],
14*4ece0189SSeongJae Park                         stderr=subprocess.DEVNULL)
15*4ece0189SSeongJae Park    if rc != 0:
16*4ece0189SSeongJae Park        return None, 'drgn fail'
17*4ece0189SSeongJae Park    try:
18*4ece0189SSeongJae Park        with open('damon_dump_output', 'r') as f:
19*4ece0189SSeongJae Park            return json.load(f), None
20*4ece0189SSeongJae Park    except Exception as e:
21*4ece0189SSeongJae Park        return None, 'json.load fail (%s)' % e
22*4ece0189SSeongJae Park
23*4ece0189SSeongJae Parkdef main():
24*4ece0189SSeongJae Park    kdamonds = _damon_sysfs.Kdamonds(
25*4ece0189SSeongJae Park            [_damon_sysfs.Kdamond(contexts=[_damon_sysfs.DamonCtx()])])
26*4ece0189SSeongJae Park    err = kdamonds.start()
27*4ece0189SSeongJae Park    if err is not None:
28*4ece0189SSeongJae Park        print('kdamond start failed: %s' % err)
29*4ece0189SSeongJae Park        exit(1)
30*4ece0189SSeongJae Park
31*4ece0189SSeongJae Park    status, err = dump_damon_status_dict(kdamonds.kdamonds[0].pid)
32*4ece0189SSeongJae Park    if err is not None:
33*4ece0189SSeongJae Park        print(err)
34*4ece0189SSeongJae Park        exit(1)
35*4ece0189SSeongJae Park
36*4ece0189SSeongJae Park    if len(status['contexts']) != 1:
37*4ece0189SSeongJae Park        print('number of contexts: %d' % len(status['contexts']))
38*4ece0189SSeongJae Park        exit(1)
39*4ece0189SSeongJae Park    kdamonds.stop()
40*4ece0189SSeongJae Park
41*4ece0189SSeongJae Parkif __name__ == '__main__':
42*4ece0189SSeongJae Park    main()
43