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