1#!/usr/bin/python 2# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 3# Basic sanity check of perf JSON output as specified in the man page. 4 5import argparse 6import sys 7import json 8 9ap = argparse.ArgumentParser() 10ap.add_argument('--no-args', action='store_true') 11ap.add_argument('--interval', action='store_true') 12ap.add_argument('--system-wide-no-aggr', action='store_true') 13ap.add_argument('--system-wide', action='store_true') 14ap.add_argument('--event', action='store_true') 15ap.add_argument('--per-core', action='store_true') 16ap.add_argument('--per-thread', action='store_true') 17ap.add_argument('--per-die', action='store_true') 18ap.add_argument('--per-node', action='store_true') 19ap.add_argument('--per-socket', action='store_true') 20args = ap.parse_args() 21 22Lines = sys.stdin.readlines() 23 24def isfloat(num): 25 try: 26 float(num) 27 return True 28 except ValueError: 29 return False 30 31 32def isint(num): 33 try: 34 int(num) 35 return True 36 except ValueError: 37 return False 38 39def is_counter_value(num): 40 return isfloat(num) or num == '<not counted>' or num == '<not supported>' 41 42def check_json_output(expected_items): 43 checks = { 44 'aggregate-number': lambda x: isfloat(x), 45 'core': lambda x: True, 46 'counter-value': lambda x: is_counter_value(x), 47 'cgroup': lambda x: True, 48 'cpu': lambda x: isint(x), 49 'die': lambda x: True, 50 'event': lambda x: True, 51 'event-runtime': lambda x: isfloat(x), 52 'interval': lambda x: isfloat(x), 53 'metric-unit': lambda x: True, 54 'metric-value': lambda x: isfloat(x), 55 'node': lambda x: True, 56 'pcnt-running': lambda x: isfloat(x), 57 'socket': lambda x: True, 58 'thread': lambda x: True, 59 'unit': lambda x: True, 60 } 61 input = '[\n' + ','.join(Lines) + '\n]' 62 for item in json.loads(input): 63 if expected_items != -1: 64 count = len(item) 65 if count != expected_items and count >= 1 and count <= 4 and 'metric-value' in item: 66 # Events that generate >1 metric may have isolated metric 67 # values and possibly other prefixes like interval, core and 68 # aggregate-number. 69 pass 70 elif count != expected_items: 71 raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}' 72 f' in \'{item}\'') 73 for key, value in item.items(): 74 if key not in checks: 75 raise RuntimeError(f'Unexpected key: key={key} value={value}') 76 if not checks[key](value): 77 raise RuntimeError(f'Check failed for: key={key} value={value}') 78 79 80try: 81 if args.no_args or args.system_wide or args.event: 82 expected_items = 7 83 elif args.interval or args.per_thread or args.system_wide_no_aggr: 84 expected_items = 8 85 elif args.per_core or args.per_socket or args.per_node or args.per_die: 86 expected_items = 9 87 else: 88 # If no option is specified, don't check the number of items. 89 expected_items = -1 90 check_json_output(expected_items) 91except: 92 print('Test failed for input:\n' + '\n'.join(Lines)) 93 raise 94