3 # stackcollapse.py - format perf samples with one line per distinct call stack
5 # This script's output has two space-separated fields. The first is a semicolon
6 # separated stack including the program name (from the "comm" field) and the
7 # function names from the call stack. The second is a count:
9 # swapper;start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 2
11 # The file is sorted according to the first field.
13 # Input may be created and processed using:
15 # perf record -a -g -F 99 sleep 60
16 # perf script report stackcollapse > out.stacks-folded
18 # (perf script record stackcollapse works too).
21 # Based on Brendan Gregg's stackcollapse-perf.pl script.
25 from collections import defaultdict
26 from optparse import OptionParser, make_option
28 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
29 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
31 from perf_trace_context import *
33 from EventClass import *
35 # command line parsing
38 # formatting options for the bottom entry of the stack
39 make_option("--include-tid", dest="include_tid",
40 action="store_true", default=False,
41 help="include thread id in stack"),
42 make_option("--include-pid", dest="include_pid",
43 action="store_true", default=False,
44 help="include process id in stack"),
45 make_option("--no-comm", dest="include_comm",
46 action="store_false", default=True,
47 help="do not separate stacks according to comm"),
48 make_option("--tidy-java", dest="tidy_java",
49 action="store_true", default=False,
50 help="beautify Java signatures"),
51 make_option("--kernel", dest="annotate_kernel",
52 action="store_true", default=False,
53 help="annotate kernel functions with _[k]")
56 parser = OptionParser(option_list=option_list)
57 (opts, args) = parser.parse_args()
60 parser.error("unexpected command line argument")
61 if opts.include_tid and not opts.include_comm:
62 parser.error("requesting tid but not comm is invalid")
63 if opts.include_pid and not opts.include_comm:
64 parser.error("requesting pid but not comm is invalid")
68 lines = defaultdict(lambda: 0)
70 def process_event(param_dict):
71 def tidy_function_name(sym, dso):
75 sym = sym.replace(';', ':')
77 # the original stackcollapse-perf.pl script gives the
78 # example of converting this:
79 # Lorg/mozilla/javascript/MemberBox;.<init>(Ljava/lang/reflect/Method;)V
81 # org/mozilla/javascript/MemberBox:.init
82 sym = sym.replace('<', '')
83 sym = sym.replace('>', '')
84 if sym[0] == 'L' and sym.find('/'):
87 sym = sym[:sym.index('(')]
91 if opts.annotate_kernel and dso == '[kernel.kallsyms]':
97 if 'callchain' in param_dict:
98 for entry in param_dict['callchain']:
99 entry.setdefault('sym', dict())
100 entry['sym'].setdefault('name', None)
101 entry.setdefault('dso', None)
102 stack.append(tidy_function_name(entry['sym']['name'],
105 param_dict.setdefault('symbol', None)
106 param_dict.setdefault('dso', None)
107 stack.append(tidy_function_name(param_dict['symbol'],
110 if opts.include_comm:
111 comm = param_dict["comm"].replace(' ', '_')
114 comm = comm + sep + str(param_dict['sample']['pid'])
117 comm = comm + sep + str(param_dict['sample']['tid'])
120 stack_string = ';'.join(reversed(stack))
121 lines[stack_string] = lines[stack_string] + 1
127 print "%s %d" % (stack, lines[stack])