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