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') 20ap.add_argument('--file', type=argparse.FileType('r'), default=sys.stdin) 21args = ap.parse_args() 22 23Lines = args.file.readlines() 24 25def isfloat(num): 26 try: 27 float(num) 28 return True 29 except ValueError: 30 return False 31 32 33def isint(num): 34 try: 35 int(num) 36 return True 37 except ValueError: 38 return False 39 40def is_counter_value(num): 41 return isfloat(num) or num == '<not counted>' or num == '<not supported>' 42 43def check_json_output(expected_items): 44 checks = { 45 'aggregate-number': lambda x: isfloat(x), 46 'core': lambda x: True, 47 'counter-value': lambda x: is_counter_value(x), 48 'cgroup': lambda x: True, 49 'cpu': lambda x: isint(x), 50 'die': lambda x: True, 51 'event': lambda x: True, 52 'event-runtime': lambda x: isfloat(x), 53 'interval': lambda x: isfloat(x), 54 'metric-unit': lambda x: True, 55 'metric-value': lambda x: isfloat(x), 56 'node': lambda x: True, 57 'pcnt-running': lambda x: isfloat(x), 58 'socket': lambda x: True, 59 'thread': lambda x: True, 60 'unit': lambda x: True, 61 } 62 input = '[\n' + ','.join(Lines) + '\n]' 63 for item in json.loads(input): 64 if expected_items != -1: 65 count = len(item) 66 if count != expected_items and count >= 1 and count <= 4 and 'metric-value' in item: 67 # Events that generate >1 metric may have isolated metric 68 # values and possibly other prefixes like interval, core and 69 # aggregate-number. 70 pass 71 elif count != expected_items: 72 raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}' 73 f' in \'{item}\'') 74 for key, value in item.items(): 75 if key not in checks: 76 raise RuntimeError(f'Unexpected key: key={key} value={value}') 77 if not checks[key](value): 78 raise RuntimeError(f'Check failed for: key={key} value={value}') 79 80 81try: 82 if args.no_args or args.system_wide or args.event: 83 expected_items = 7 84 elif args.interval or args.per_thread or args.system_wide_no_aggr: 85 expected_items = 8 86 elif args.per_core or args.per_socket or args.per_node or args.per_die: 87 expected_items = 9 88 else: 89 # If no option is specified, don't check the number of items. 90 expected_items = -1 91 check_json_output(expected_items) 92except: 93 print('Test failed for input:\n' + '\n'.join(Lines)) 94 raise 95