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 16import argparse 17 18from perfetto.trace_processor import TraceProcessor, TraceProcessorConfig 19 20 21def main(): 22 # Parse arguments passed from command line 23 parser = argparse.ArgumentParser() 24 parser.add_argument( 25 "-a", 26 "--address", 27 help="Address at which trace_processor is being run, e.g. localhost:9001", 28 type=str) 29 parser.add_argument( 30 "-b", 31 "--binary", 32 help="Absolute path to a trace processor binary", 33 type=str) 34 parser.add_argument("-f", "--file", help="Absolute path to trace", type=str) 35 args = parser.parse_args() 36 37 config = TraceProcessorConfig(bin_path=args.binary) 38 39 # Pass arguments into api to construct the trace processor and load the trace 40 if args.address is None and args.file is None: 41 raise Exception("You must specify an address or a file path to trace") 42 elif args.address is None: 43 tp = TraceProcessor(trace=args.file, config=config) 44 elif args.file is None: 45 tp = TraceProcessor(addr=args.address, config=config) 46 else: 47 tp = TraceProcessor(trace=args.file, addr=args.address, config=config) 48 49 # Iterate through QueryResultIterator 50 res_it = tp.query('select * from slice limit 10') 51 for row in res_it: 52 print(row.name) 53 54 # Convert QueryResultIterator into a pandas dataframe + iterate. This yields 55 # the same results as the function above. 56 try: 57 res_df = tp.query('select * from slice limit 10').as_pandas_dataframe() 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 64 am_metrics = tp.metric(['android_mem']) 65 tp.close() 66 67 68if __name__ == "__main__": 69 main() 70