Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/python
0002 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
0003 # Basic sanity check of perf JSON output as specified in the man page.
0004 
0005 import argparse
0006 import sys
0007 import json
0008 
0009 ap = argparse.ArgumentParser()
0010 ap.add_argument('--no-args', action='store_true')
0011 ap.add_argument('--interval', action='store_true')
0012 ap.add_argument('--system-wide-no-aggr', action='store_true')
0013 ap.add_argument('--system-wide', action='store_true')
0014 ap.add_argument('--event', action='store_true')
0015 ap.add_argument('--per-core', action='store_true')
0016 ap.add_argument('--per-thread', action='store_true')
0017 ap.add_argument('--per-die', action='store_true')
0018 ap.add_argument('--per-node', action='store_true')
0019 ap.add_argument('--per-socket', action='store_true')
0020 args = ap.parse_args()
0021 
0022 Lines = sys.stdin.readlines()
0023 
0024 def isfloat(num):
0025   try:
0026     float(num)
0027     return True
0028   except ValueError:
0029     return False
0030 
0031 
0032 def isint(num):
0033   try:
0034     int(num)
0035     return True
0036   except ValueError:
0037     return False
0038 
0039 def is_counter_value(num):
0040   return isfloat(num) or num == '<not counted>' or num == '<not supported>'
0041 
0042 def check_json_output(expected_items):
0043   if expected_items != -1:
0044     for line in Lines:
0045       if 'failed' not in line:
0046         count = 0
0047         count = line.count(',')
0048         if count != expected_items and count >= 1 and count <= 3 and 'metric-value' in line:
0049           # Events that generate >1 metric may have isolated metric
0050           # values and possibly other prefixes like interval, core and
0051           # aggregate-number.
0052           continue
0053         if count != expected_items:
0054           raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}'
0055                              f' in \'{line}\'')
0056   checks = {
0057       'aggregate-number': lambda x: isfloat(x),
0058       'core': lambda x: True,
0059       'counter-value': lambda x: is_counter_value(x),
0060       'cgroup': lambda x: True,
0061       'cpu': lambda x: isint(x),
0062       'die': lambda x: True,
0063       'event': lambda x: True,
0064       'event-runtime': lambda x: isfloat(x),
0065       'interval': lambda x: isfloat(x),
0066       'metric-unit': lambda x: True,
0067       'metric-value': lambda x: isfloat(x),
0068       'node': lambda x: True,
0069       'pcnt-running': lambda x: isfloat(x),
0070       'socket': lambda x: True,
0071       'thread': lambda x: True,
0072       'unit': lambda x: True,
0073   }
0074   input = '[\n' + ','.join(Lines) + '\n]'
0075   for item in json.loads(input):
0076     for key, value in item.items():
0077       if key not in checks:
0078         raise RuntimeError(f'Unexpected key: key={key} value={value}')
0079       if not checks[key](value):
0080         raise RuntimeError(f'Check failed for: key={key} value={value}')
0081 
0082 
0083 try:
0084   if args.no_args or args.system_wide or args.event:
0085     expected_items = 6
0086   elif args.interval or args.per_thread or args.system_wide_no_aggr:
0087     expected_items = 7
0088   elif args.per_core or args.per_socket or args.per_node or args.per_die:
0089     expected_items = 8
0090   else:
0091     # If no option is specified, don't check the number of items.
0092     expected_items = -1
0093   check_json_output(expected_items)
0094 except:
0095   print('Test failed for input:\n' + '\n'.join(Lines))
0096   raise