1#!/usr/bin/env python3 2# SPDX-License-Identifier: GPL-2.0 3 4import json 5import os 6import subprocess 7 8import _damon_sysfs 9 10def dump_damon_status_dict(pid): 11 file_dir = os.path.dirname(os.path.abspath(__file__)) 12 dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py') 13 rc = subprocess.call(['drgn', dump_script, pid, 'damon_dump_output'], 14 stderr=subprocess.DEVNULL) 15 if rc != 0: 16 return None, 'drgn fail' 17 try: 18 with open('damon_dump_output', 'r') as f: 19 return json.load(f), None 20 except Exception as e: 21 return None, 'json.load fail (%s)' % e 22 23def fail(expectation, status): 24 print('unexpected %s' % expectation) 25 print(json.dumps(status, indent=4)) 26 exit(1) 27 28def main(): 29 kdamonds = _damon_sysfs.Kdamonds( 30 [_damon_sysfs.Kdamond( 31 contexts=[_damon_sysfs.DamonCtx( 32 targets=[_damon_sysfs.DamonTarget(pid=-1)], 33 schemes=[_damon_sysfs.Damos()], 34 )])]) 35 err = kdamonds.start() 36 if err is not None: 37 print('kdamond start failed: %s' % err) 38 exit(1) 39 40 status, err = dump_damon_status_dict(kdamonds.kdamonds[0].pid) 41 if err is not None: 42 print(err) 43 exit(1) 44 45 if len(status['contexts']) != 1: 46 fail('number of contexts', status) 47 48 ctx = status['contexts'][0] 49 attrs = ctx['attrs'] 50 if attrs['sample_interval'] != 5000: 51 fail('sample interval', status) 52 if attrs['aggr_interval'] != 100000: 53 fail('aggr interval', status) 54 if attrs['ops_update_interval'] != 1000000: 55 fail('ops updte interval', status) 56 57 if attrs['intervals_goal'] != { 58 'access_bp': 0, 'aggrs': 0, 59 'min_sample_us': 0, 'max_sample_us': 0}: 60 fail('intervals goal') 61 62 if attrs['min_nr_regions'] != 10: 63 fail('min_nr_regions') 64 if attrs['max_nr_regions'] != 1000: 65 fail('max_nr_regions') 66 67 if ctx['adaptive_targets'] != [ 68 { 'pid': 0, 'nr_regions': 0, 'regions_list': []}]: 69 fail('adaptive targets', status) 70 71 if len(ctx['schemes']) != 1: 72 fail('number of schemes', status) 73 74 scheme = ctx['schemes'][0] 75 if scheme['pattern'] != { 76 'min_sz_region': 0, 77 'max_sz_region': 2**64 - 1, 78 'min_nr_accesses': 0, 79 'max_nr_accesses': 2**32 - 1, 80 'min_age_region': 0, 81 'max_age_region': 2**32 - 1, 82 }: 83 fail('damos pattern', status) 84 if scheme['action'] != 9: # stat 85 fail('damos action', status) 86 if scheme['apply_interval_us'] != 0: 87 fail('damos apply interval', status) 88 if scheme['target_nid'] != -1: 89 fail('damos target nid', status) 90 91 if scheme['quota'] != { 92 'reset_interval': 0, 93 'ms': 0, 94 'sz': 0, 95 'goals': [], 96 'esz': 0, 97 'weight_sz': 0, 98 'weight_nr_accesses': 0, 99 'weight_age': 0, 100 }: 101 fail('damos quota', status) 102 103 if scheme['wmarks'] != { 104 'metric': 0, 105 'interval': 0, 106 'high': 0, 107 'mid': 0, 108 'low': 0, 109 }: 110 fail('damos wmarks', status) 111 112 kdamonds.stop() 113 114if __name__ == '__main__': 115 main() 116