1#!/usr/bin/env python3 2# Copyright (C) 2021 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""" Given a trace file, gives the self-time of userspace slices broken 16down by process, thread and thread state. 17""" 18 19import argparse 20import sys 21import os 22 23PYTHON_DIR = os.path.join( 24 os.path.dirname( 25 os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "python") 26sys.path.append(os.path.join(PYTHON_DIR)) 27 28from perfetto.experimental.slice_breakdown import compute_breakdown 29from perfetto.experimental.slice_breakdown import compute_breakdown_for_startup 30from perfetto.trace_processor import TraceProcessor 31from perfetto.trace_processor import TraceProcessorConfig 32 33 34def compute_breakdown_wrapper(args): 35 config = TraceProcessorConfig(bin_path=args.shell_path, verbose=args.verbose) 36 with TraceProcessor(trace=args.file, config=config) as tp: 37 if args.startup_bounds: 38 breakdown = compute_breakdown_for_startup(tp, args.startup_package, 39 args.process_name) 40 else: 41 breakdown = compute_breakdown(tp, args.start_ts, args.end_ts, 42 args.process_name) 43 return breakdown 44 45 46def main(): 47 parser = argparse.ArgumentParser() 48 parser.add_argument('--file', required=True) 49 parser.add_argument('--shell-path', default=None) 50 parser.add_argument('--start-ts', default=None) 51 parser.add_argument('--end-ts', default=None) 52 parser.add_argument('--startup-bounds', action='store_true', default=False) 53 parser.add_argument('--startup-package', default=None) 54 parser.add_argument('--process-name', default=None) 55 parser.add_argument('--verbose', action='store_true', default=False) 56 parser.add_argument('--out-csv', required=True) 57 args = parser.parse_args() 58 59 if (args.start_ts or args.end_ts) and args.startup_bounds: 60 print("Cannot specify --start-ts or --end-ts and --startup-bounds") 61 return 1 62 63 if args.startup_package and not args.startup_bounds: 64 print("Must specify --startup-bounds if --startup-package is specified") 65 return 1 66 67 breakdown = compute_breakdown_wrapper(args) 68 69 if args.out_csv: 70 diff_csv = breakdown.to_csv(index=False) 71 if args.out_csv == '-': 72 sys.stdout.write(diff_csv) 73 else: 74 with open(args.out_csv, 'w') as out: 75 out.write(diff_csv) 76 77 return 0 78 79 80if __name__ == '__main__': 81 exit(main()) 82