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 21from simpleperf_report_lib import ReportLib 22from simpleperf_utils import BaseArgumentParser, flatten_arg_list, ReportLibOptions 23from typing import List, Set, Optional 24 25 26def report_sample( 27 record_file: str, 28 symfs_dir: str, 29 kallsyms_file: str, 30 show_tracing_data: bool, 31 header: bool, 32 report_lib_options: ReportLibOptions): 33 """ read record_file, and print each sample""" 34 lib = ReportLib() 35 36 lib.ShowIpForUnknownSymbol() 37 if symfs_dir is not None: 38 lib.SetSymfs(symfs_dir) 39 if record_file is not None: 40 lib.SetRecordFile(record_file) 41 if kallsyms_file is not None: 42 lib.SetKallsymsFile(kallsyms_file) 43 lib.SetReportOptions(report_lib_options) 44 45 if header: 46 print("# ========") 47 print("# cmdline : %s" % lib.GetRecordCmd()) 48 print("# arch : %s" % lib.GetArch()) 49 for k, v in lib.MetaInfo().items(): 50 print('# %s : %s' % (k, v.replace('\n', ' '))) 51 print("# ========") 52 print("#") 53 54 while True: 55 sample = lib.GetNextSample() 56 if sample is None: 57 lib.Close() 58 break 59 event = lib.GetEventOfCurrentSample() 60 symbol = lib.GetSymbolOfCurrentSample() 61 callchain = lib.GetCallChainOfCurrentSample() 62 63 sec = sample.time // 1000000000 64 usec = (sample.time - sec * 1000000000) // 1000 65 print('%s\t%d/%d [%03d] %d.%06d: %d %s:' % (sample.thread_comm, 66 sample.pid, sample.tid, sample.cpu, sec, 67 usec, sample.period, event.name)) 68 print('\t%16x %s (%s)' % (sample.ip, symbol.symbol_name, symbol.dso_name)) 69 for i in range(callchain.nr): 70 entry = callchain.entries[i] 71 print('\t%16x %s (%s)' % (entry.ip, entry.symbol.symbol_name, entry.symbol.dso_name)) 72 if show_tracing_data: 73 data = lib.GetTracingDataOfCurrentSample() 74 if data: 75 print('\ttracing data:') 76 for key, value in data.items(): 77 print('\t\t%s : %s' % (key, value)) 78 print('') 79 80 81def main(): 82 parser = BaseArgumentParser(description='Report samples in perf.data.') 83 parser.add_argument('--symfs', 84 help='Set the path to find binaries with symbols and debug info.') 85 parser.add_argument('--kallsyms', help='Set the path to find kernel symbols.') 86 parser.add_argument('-i', '--record_file', nargs='?', default='perf.data', 87 help='Default is perf.data.') 88 parser.add_argument('--show_tracing_data', action='store_true', help='print tracing data.') 89 parser.add_argument('--header', action='store_true', 90 help='Show metadata header, like perf script --header') 91 parser.add_report_lib_options() 92 args = parser.parse_args() 93 report_sample( 94 record_file=args.record_file, 95 symfs_dir=args.symfs, 96 kallsyms_file=args.kallsyms, 97 show_tracing_data=args.show_tracing_data, 98 header=args.header, 99 report_lib_options=args.report_lib_options) 100 101 102if __name__ == '__main__': 103 main() 104