xref: /aosp_15_r20/system/extras/simpleperf/scripts/report_sample.py (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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 GetReportLib
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 = GetReportLib(record_file)
36
37    lib.ShowIpForUnknownSymbol()
38    if symfs_dir is not None:
39        lib.SetSymfs(symfs_dir)
40    if kallsyms_file is not None:
41        lib.SetKallsymsFile(kallsyms_file)
42    lib.SetReportOptions(report_lib_options)
43
44    if header:
45        print("# ========")
46        print("# cmdline : %s" % lib.GetRecordCmd())
47        print("# arch : %s" % lib.GetArch())
48        for k, v in lib.MetaInfo().items():
49            print('# %s : %s' % (k, v.replace('\n', ' ')))
50        print("# ========")
51        print("#")
52
53    while True:
54        sample = lib.GetNextSample()
55        if sample is None:
56            lib.Close()
57            break
58        event = lib.GetEventOfCurrentSample()
59        symbol = lib.GetSymbolOfCurrentSample()
60        callchain = lib.GetCallChainOfCurrentSample()
61
62        sec = sample.time // 1000000000
63        usec = (sample.time - sec * 1000000000) // 1000
64        print('%s\t%d/%d [%03d] %d.%06d: %d %s:' % (sample.thread_comm,
65                                                    sample.pid, sample.tid, sample.cpu, sec,
66                                                    usec, sample.period, event.name))
67        print('\t%16x %s (%s)' % (sample.ip, symbol.symbol_name, symbol.dso_name))
68        for i in range(callchain.nr):
69            entry = callchain.entries[i]
70            print('\t%16x %s (%s)' % (entry.ip, entry.symbol.symbol_name, entry.symbol.dso_name))
71        if show_tracing_data:
72            data = lib.GetTracingDataOfCurrentSample()
73            if data:
74                print('\ttracing data:')
75                for key, value in data.items():
76                    print('\t\t%s : %s' % (key, value))
77        print('')
78
79
80def main():
81    parser = BaseArgumentParser(description='Report samples in perf.data.')
82    parser.add_argument('--symfs',
83                        help='Set the path to find binaries with symbols and debug info.')
84    parser.add_argument('--kallsyms', help='Set the path to find kernel symbols.')
85    parser.add_argument('-i', '--record_file', nargs='?', default='perf.data',
86                        help='Default is perf.data.')
87    parser.add_argument('--show_tracing_data', action='store_true', help='print tracing data.')
88    parser.add_argument('--header', action='store_true',
89                        help='Show metadata header, like perf script --header')
90    parser.add_argument('-o', '--output_file', default='', help="""
91        The path of the generated report.  Default is stdout.""")
92    parser.add_report_lib_options()
93    args = parser.parse_args()
94    # If the output file has been set, redirect stdout.
95    if args.output_file != '' and args.output_file != '-':
96        sys.stdout = open(file=args.output_file, mode='w')
97    report_sample(
98        record_file=args.record_file,
99        symfs_dir=args.symfs,
100        kallsyms_file=args.kallsyms,
101        show_tracing_data=args.show_tracing_data,
102        header=args.header,
103        report_lib_options=args.report_lib_options)
104
105
106if __name__ == '__main__':
107    main()
108