blob: ffdd9ace4025039f92f9518675aad49adaff0eba [file] [log] [blame]
Anindita Ghosh237a7762020-06-30 10:46:52 +00001#!/usr/bin/env python3
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Anindita Ghosha3fe5572020-07-03 12:33:08 +000016import argparse
17
Lalit Maganticb8e0ff2022-01-11 15:36:37 +000018from perfetto.trace_processor import TraceProcessor, TraceProcessorConfig
Anindita Ghosh237a7762020-06-30 10:46:52 +000019
20
21def main():
Anindita Ghosha3fe5572020-07-03 12:33:08 +000022 # Parse arguments passed from command line
Anindita Ghosha3fe5572020-07-03 12:33:08 +000023 parser = argparse.ArgumentParser()
24 parser.add_argument(
25 "-a",
26 "--address",
Anindita Ghosh7887c762020-07-20 16:35:15 +000027 help="Address at which trace_processor is being run, e.g. localhost:9001",
Anindita Ghosha3fe5572020-07-03 12:33:08 +000028 type=str)
Anindita Ghosh03cef712020-07-21 18:29:29 +000029 parser.add_argument(
30 "-b",
31 "--binary",
32 help="Absolute path to a trace processor binary",
33 type=str)
Anindita Ghosh7887c762020-07-20 16:35:15 +000034 parser.add_argument("-f", "--file", help="Absolute path to trace", type=str)
Lalit Maganti82a2c042020-07-06 13:50:33 +010035 args = parser.parse_args()
Anindita Ghosha3fe5572020-07-03 12:33:08 +000036
Lalit Maganticb8e0ff2022-01-11 15:36:37 +000037 config = TraceProcessorConfig(bin_path=args.binary)
38
Anindita Ghosh7887c762020-07-20 16:35:15 +000039 # Pass arguments into api to construct the trace processor and load the trace
Anindita Ghosh03cef712020-07-21 18:29:29 +000040 if args.address is None and args.file is None:
Anindita Ghosh7887c762020-07-20 16:35:15 +000041 raise Exception("You must specify an address or a file path to trace")
Anindita Ghosh03cef712020-07-21 18:29:29 +000042 elif args.address is None:
Lalit Maganti5c8cb0f2022-04-08 12:43:12 +000043 tp = TraceProcessor(trace=args.file, config=config)
Anindita Ghosh03cef712020-07-21 18:29:29 +000044 elif args.file is None:
Lalit Maganticb8e0ff2022-01-11 15:36:37 +000045 tp = TraceProcessor(addr=args.address, config=config)
Anindita Ghosh7887c762020-07-20 16:35:15 +000046 else:
Lalit Maganti5c8cb0f2022-04-08 12:43:12 +000047 tp = TraceProcessor(trace=args.file, addr=args.address, config=config)
Anindita Ghosha3fe5572020-07-03 12:33:08 +000048
Anindita Ghoshf8ccdc62020-08-03 20:48:02 +000049 # Iterate through QueryResultIterator
Anindita Ghosh3b19f412020-09-10 22:43:15 +000050 res_it = tp.query('select * from slice limit 10')
Anindita Ghoshc63cdce2020-07-15 20:14:07 +000051 for row in res_it:
52 print(row.name)
Anindita Ghoshf8ccdc62020-08-03 20:48:02 +000053
54 # Convert QueryResultIterator into a pandas dataframe + iterate. This yields
55 # the same results as the function above.
56 try:
Anindita Ghosh3b19f412020-09-10 22:43:15 +000057 res_df = tp.query('select * from slice limit 10').as_pandas_dataframe()
Anindita Ghoshf8ccdc62020-08-03 20:48:02 +000058 for index, row in res_df.iterrows():
59 print(row['name'])
60 except Exception:
61 pass
62
63 # Call another function on the loaded trace
Anindita Ghoshc63cdce2020-07-15 20:14:07 +000064 am_metrics = tp.metric(['android_mem'])
Anindita Ghosh7887c762020-07-20 16:35:15 +000065 tp.close()
Anindita Ghosh237a7762020-06-30 10:46:52 +000066
67
68if __name__ == "__main__":
69 main()