1#!/usr/bin/env python3 2# 3# Copyright (C) 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18"""report_sample.py: report samples in the same format as `perf script`. 19""" 20 21import sys 22from simpleperf_report_lib import ReportLib 23from simpleperf_utils import BaseArgumentParser, flatten_arg_list, ReportLibOptions 24from typing import List, Set, Optional 25 26 27def report_sample( 28 record_file: str, 29 symfs_dir: str, 30 kallsyms_file: str, 31 show_tracing_data: bool, 32 header: bool, 33 report_lib_options: ReportLibOptions): 34 """ read record_file, and print each sample""" 35 lib = ReportLib() 36 37 lib.ShowIpForUnknownSymbol() 38 if symfs_dir is not None: 39 lib.SetSymfs(symfs_dir) 40 if record_file is not None: 41 lib.SetRecordFile(record_file) 42 if kallsyms_file is not None: 43 lib.SetKallsymsFile(kallsyms_file) 44 lib.SetReportOptions(report_lib_options) 45 46 if header: 47 print("# ========") 48 print("# cmdline : %s" % lib.GetRecordCmd()) 49 print("# arch : %s" % lib.GetArch()) 50 for k, v in lib.MetaInfo().items(): 51 print('# %s : %s' % (k, v.replace('\n', ' '))) 52 print("# ========") 53 print("#") 54 55 while True: 56 sample = lib.GetNextSample() 57 if sample is None: 58 lib.Close() 59 break 60 event = lib.GetEventOfCurrentSample() 61 symbol = lib.GetSymbolOfCurrentSample() 62 callchain = lib.GetCallChainOfCurrentSample() 63 64 sec = sample.time // 1000000000 65 usec = (sample.time - sec * 1000000000) // 1000 66 print('%s\t%d/%d [%03d] %d.%06d: %d %s:' % (sample.thread_comm, 67 sample.pid, sample.tid, sample.cpu, sec, 68 usec, sample.period, event.name)) 69 print('\t%16x %s (%s)' % (sample.ip, symbol.symbol_name, symbol.dso_name)) 70 for i in range(callchain.nr): 71 entry = callchain.entries[i] 72 print('\t%16x %s (%s)' % (entry.ip, entry.symbol.symbol_name, entry.symbol.dso_name)) 73 if show_tracing_data: 74 data = lib.GetTracingDataOfCurrentSample() 75 if data: 76 print('\ttracing data:') 77 for key, value in data.items(): 78 print('\t\t%s : %s' % (key, value)) 79 print('') 80 81 82def main(): 83 parser = BaseArgumentParser(description='Report samples in perf.data.') 84 parser.add_argument('--symfs', 85 help='Set the path to find binaries with symbols and debug info.') 86 parser.add_argument('--kallsyms', help='Set the path to find kernel symbols.') 87 parser.add_argument('-i', '--record_file', nargs='?', default='perf.data', 88 help='Default is perf.data.') 89 parser.add_argument('--show_tracing_data', action='store_true', help='print tracing data.') 90 parser.add_argument('--header', action='store_true', 91 help='Show metadata header, like perf script --header') 92 parser.add_argument('-o', '--output_file', default='', help=""" 93 The path of the generated report. Default is stdout.""") 94 parser.add_report_lib_options() 95 args = parser.parse_args() 96 # If the output file has been set, redirect stdout. 97 if args.output_file != '' and args.output_file != '-': 98 sys.stdout = open(file=args.output_file, mode='w') 99 report_sample( 100 record_file=args.record_file, 101 symfs_dir=args.symfs, 102 kallsyms_file=args.kallsyms, 103 show_tracing_data=args.show_tracing_data, 104 header=args.header, 105 report_lib_options=args.report_lib_options) 106 107 108if __name__ == '__main__': 109 main() 110