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