Anindita Ghosh | 237a776 | 2020-06-30 10:46:52 +0000 | [diff] [blame] | 1 | #!/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 Ghosh | a3fe557 | 2020-07-03 12:33:08 +0000 | [diff] [blame] | 16 | import argparse |
| 17 | |
Lalit Maganti | cb8e0ff | 2022-01-11 15:36:37 +0000 | [diff] [blame] | 18 | from perfetto.trace_processor import TraceProcessor, TraceProcessorConfig |
Anindita Ghosh | 237a776 | 2020-06-30 10:46:52 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def main(): |
Anindita Ghosh | a3fe557 | 2020-07-03 12:33:08 +0000 | [diff] [blame] | 22 | # Parse arguments passed from command line |
Anindita Ghosh | a3fe557 | 2020-07-03 12:33:08 +0000 | [diff] [blame] | 23 | parser = argparse.ArgumentParser() |
| 24 | parser.add_argument( |
| 25 | "-a", |
| 26 | "--address", |
Anindita Ghosh | 7887c76 | 2020-07-20 16:35:15 +0000 | [diff] [blame] | 27 | help="Address at which trace_processor is being run, e.g. localhost:9001", |
Anindita Ghosh | a3fe557 | 2020-07-03 12:33:08 +0000 | [diff] [blame] | 28 | type=str) |
Anindita Ghosh | 03cef71 | 2020-07-21 18:29:29 +0000 | [diff] [blame] | 29 | parser.add_argument( |
| 30 | "-b", |
| 31 | "--binary", |
| 32 | help="Absolute path to a trace processor binary", |
| 33 | type=str) |
Anindita Ghosh | 7887c76 | 2020-07-20 16:35:15 +0000 | [diff] [blame] | 34 | parser.add_argument("-f", "--file", help="Absolute path to trace", type=str) |
Lalit Maganti | 82a2c04 | 2020-07-06 13:50:33 +0100 | [diff] [blame] | 35 | args = parser.parse_args() |
Anindita Ghosh | a3fe557 | 2020-07-03 12:33:08 +0000 | [diff] [blame] | 36 | |
Lalit Maganti | cb8e0ff | 2022-01-11 15:36:37 +0000 | [diff] [blame] | 37 | config = TraceProcessorConfig(bin_path=args.binary) |
| 38 | |
Anindita Ghosh | 7887c76 | 2020-07-20 16:35:15 +0000 | [diff] [blame] | 39 | # Pass arguments into api to construct the trace processor and load the trace |
Anindita Ghosh | 03cef71 | 2020-07-21 18:29:29 +0000 | [diff] [blame] | 40 | if args.address is None and args.file is None: |
Anindita Ghosh | 7887c76 | 2020-07-20 16:35:15 +0000 | [diff] [blame] | 41 | raise Exception("You must specify an address or a file path to trace") |
Anindita Ghosh | 03cef71 | 2020-07-21 18:29:29 +0000 | [diff] [blame] | 42 | elif args.address is None: |
Lalit Maganti | 5c8cb0f | 2022-04-08 12:43:12 +0000 | [diff] [blame] | 43 | tp = TraceProcessor(trace=args.file, config=config) |
Anindita Ghosh | 03cef71 | 2020-07-21 18:29:29 +0000 | [diff] [blame] | 44 | elif args.file is None: |
Lalit Maganti | cb8e0ff | 2022-01-11 15:36:37 +0000 | [diff] [blame] | 45 | tp = TraceProcessor(addr=args.address, config=config) |
Anindita Ghosh | 7887c76 | 2020-07-20 16:35:15 +0000 | [diff] [blame] | 46 | else: |
Lalit Maganti | 5c8cb0f | 2022-04-08 12:43:12 +0000 | [diff] [blame] | 47 | tp = TraceProcessor(trace=args.file, addr=args.address, config=config) |
Anindita Ghosh | a3fe557 | 2020-07-03 12:33:08 +0000 | [diff] [blame] | 48 | |
Anindita Ghosh | f8ccdc6 | 2020-08-03 20:48:02 +0000 | [diff] [blame] | 49 | # Iterate through QueryResultIterator |
Anindita Ghosh | 3b19f41 | 2020-09-10 22:43:15 +0000 | [diff] [blame] | 50 | res_it = tp.query('select * from slice limit 10') |
Anindita Ghosh | c63cdce | 2020-07-15 20:14:07 +0000 | [diff] [blame] | 51 | for row in res_it: |
| 52 | print(row.name) |
Anindita Ghosh | f8ccdc6 | 2020-08-03 20:48:02 +0000 | [diff] [blame] | 53 | |
| 54 | # Convert QueryResultIterator into a pandas dataframe + iterate. This yields |
| 55 | # the same results as the function above. |
| 56 | try: |
Anindita Ghosh | 3b19f41 | 2020-09-10 22:43:15 +0000 | [diff] [blame] | 57 | res_df = tp.query('select * from slice limit 10').as_pandas_dataframe() |
Anindita Ghosh | f8ccdc6 | 2020-08-03 20:48:02 +0000 | [diff] [blame] | 58 | 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 Ghosh | c63cdce | 2020-07-15 20:14:07 +0000 | [diff] [blame] | 64 | am_metrics = tp.metric(['android_mem']) |
Anindita Ghosh | 7887c76 | 2020-07-20 16:35:15 +0000 | [diff] [blame] | 65 | tp.close() |
Anindita Ghosh | 237a776 | 2020-06-30 10:46:52 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |