^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) # stackcollapse.py - format perf samples with one line per distinct call stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # This script's output has two space-separated fields. The first is a semicolon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # separated stack including the program name (from the "comm" field) and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # function names from the call stack. The second is a count:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # swapper;start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # The file is sorted according to the first field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # Input may be created and processed using:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # perf record -a -g -F 99 sleep 60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) # perf script report stackcollapse > out.stacks-folded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) # (perf script record stackcollapse works too).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # Written by Paolo Bonzini <pbonzini@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # Based on Brendan Gregg's stackcollapse-perf.pl script.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) from __future__ import print_function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) import os
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) import sys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) from collections import defaultdict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) from optparse import OptionParser, make_option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) sys.path.append(os.environ['PERF_EXEC_PATH'] + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) from perf_trace_context import *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) from Core import *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) from EventClass import *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # command line parsing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) option_list = [
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) # formatting options for the bottom entry of the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) make_option("--include-tid", dest="include_tid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) action="store_true", default=False,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) help="include thread id in stack"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) make_option("--include-pid", dest="include_pid",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) action="store_true", default=False,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) help="include process id in stack"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) make_option("--no-comm", dest="include_comm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) action="store_false", default=True,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) help="do not separate stacks according to comm"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) make_option("--tidy-java", dest="tidy_java",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) action="store_true", default=False,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) help="beautify Java signatures"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) make_option("--kernel", dest="annotate_kernel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) action="store_true", default=False,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) help="annotate kernel functions with _[k]")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) parser = OptionParser(option_list=option_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) (opts, args) = parser.parse_args()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if len(args) != 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) parser.error("unexpected command line argument")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if opts.include_tid and not opts.include_comm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) parser.error("requesting tid but not comm is invalid")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if opts.include_pid and not opts.include_comm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) parser.error("requesting pid but not comm is invalid")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) # event handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) lines = defaultdict(lambda: 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) def process_event(param_dict):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) def tidy_function_name(sym, dso):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if sym is None:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sym = '[unknown]'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) sym = sym.replace(';', ':')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if opts.tidy_java:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) # the original stackcollapse-perf.pl script gives the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) # example of converting this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) # Lorg/mozilla/javascript/MemberBox;.<init>(Ljava/lang/reflect/Method;)V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) # to this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) # org/mozilla/javascript/MemberBox:.init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) sym = sym.replace('<', '')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) sym = sym.replace('>', '')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if sym[0] == 'L' and sym.find('/'):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sym = sym[1:]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) sym = sym[:sym.index('(')]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) except ValueError:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if opts.annotate_kernel and dso == '[kernel.kallsyms]':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return sym + '_[k]'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return sym
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) stack = list()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if 'callchain' in param_dict:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) for entry in param_dict['callchain']:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) entry.setdefault('sym', dict())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) entry['sym'].setdefault('name', None)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) entry.setdefault('dso', None)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) stack.append(tidy_function_name(entry['sym']['name'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) entry['dso']))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) param_dict.setdefault('symbol', None)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) param_dict.setdefault('dso', None)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) stack.append(tidy_function_name(param_dict['symbol'],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) param_dict['dso']))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if opts.include_comm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) comm = param_dict["comm"].replace(' ', '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) sep = "-"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if opts.include_pid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) comm = comm + sep + str(param_dict['sample']['pid'])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sep = "/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if opts.include_tid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) comm = comm + sep + str(param_dict['sample']['tid'])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) stack.append(comm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) stack_string = ';'.join(reversed(stack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) lines[stack_string] = lines[stack_string] + 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) def trace_end():
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) list = sorted(lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) for stack in list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) print("%s %d" % (stack, lines[stack]))