File Index
Files and directories
Sign in
FILE

perf_json_output_lint.py

File preview with syntax highlighting.

linux-6.19.14/tools/perf/tests/shell/lib/perf_json_output_lint.py PYTHON
  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 is_metric_value(num):
 47  return isfloat(num) or num == 'none'
 48
 49def check_json_output(expected_items):
 50  checks = {
 51      'counters': lambda x: isfloat(x),
 52      'core': lambda x: True,
 53      'counter-value': lambda x: is_counter_value(x),
 54      'cgroup': lambda x: True,
 55      'cpu': lambda x: isint(x),
 56      'cache': lambda x: True,
 57      'cluster': lambda x: True,
 58      'die': lambda x: True,
 59      'event': lambda x: True,
 60      'event-runtime': lambda x: isfloat(x),
 61      'interval': lambda x: isfloat(x),
 62      'metric-unit': lambda x: True,
 63      'metric-value': lambda x: is_metric_value(x),
 64      'metric-threshold': lambda x: x in ['unknown', 'good', 'less good', 'nearly bad', 'bad'],
 65      'metricgroup': lambda x: True,
 66      'node': lambda x: True,
 67      'pcnt-running': lambda x: isfloat(x),
 68      'socket': lambda x: True,
 69      'thread': lambda x: True,
 70      'unit': lambda x: True,
 71  }
 72  input = '[\n' + ','.join(Lines) + '\n]'
 73  for item in json.loads(input):
 74    if expected_items != -1:
 75      count = len(item)
 76      if count not in expected_items and count >= 1 and count <= 7 and 'metric-value' in item:
 77        # Events that generate >1 metric may have isolated metric
 78        # values and possibly other prefixes like interval, core,
 79        # counters, or event-runtime/pcnt-running from multiplexing.
 80        pass
 81      elif count not in expected_items and count >= 1 and count <= 5 and 'metricgroup' in item:
 82        pass
 83      elif count - 1 in expected_items and 'metric-threshold' in item:
 84          pass
 85      elif count in expected_items and 'insn per cycle' in item:
 86          pass
 87      elif count not in expected_items:
 88        raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}'
 89                           f' in \'{item}\'')
 90    for key, value in item.items():
 91      if key not in checks:
 92        if args.metric_only:
 93          continue
 94        raise RuntimeError(f'Unexpected key: key={key} value={value}')
 95      if not checks[key](value):
 96        raise RuntimeError(f'Check failed for: key={key} value={value}')
 97
 98
 99try:
100  if args.no_args or args.system_wide or args.event:
101    expected_items = [5, 7]
102  elif args.interval or args.per_thread or args.system_wide_no_aggr:
103    expected_items = [6, 8]
104  elif args.per_core or args.per_socket or args.per_node or args.per_die or args.per_cluster or args.per_cache:
105    expected_items = [7, 9]
106  elif args.metric_only:
107    expected_items = [1, 2]
108  else:
109    # If no option is specified, don't check the number of items.
110    expected_items = -1
111  check_json_output(expected_items)
112except:
113  print('Test failed for input:\n' + '\n'.join(Lines))
114  raise
~/ Read-only file preview.