~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/shell/lib/perf_json_output_lint.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php