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