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 21 22from perfetto.experimental.slice_breakdown import compute_breakdown 23from perfetto.experimental.slice_breakdown import compute_breakdown_for_startup 24from perfetto.trace_processor import TraceProcessor 25from perfetto.trace_processor import TraceProcessorConfig 26 27 28def compute_breakdown_wrapper(args): 29 config = TraceProcessorConfig(bin_path=args.shell_path, verbose=args.verbose) 30 with TraceProcessor(trace=args.file, config=config) as tp: 31 if args.startup_bounds: 32 breakdown = compute_breakdown_for_startup(tp, args.startup_package, 33 args.process_name) 34 else: 35 breakdown = compute_breakdown(tp, args.start_ts, args.end_ts, 36 args.process_name) 37 return breakdown 38 39 40def main(): 41 parser = argparse.ArgumentParser() 42 parser.add_argument('--file', required=True) 43 parser.add_argument('--shell-path', default=None) 44 parser.add_argument('--start-ts', default=None) 45 parser.add_argument('--end-ts', default=None) 46 parser.add_argument('--startup-bounds', action='store_true', default=False) 47 parser.add_argument('--startup-package', default=None) 48 parser.add_argument('--process-name', default=None) 49 parser.add_argument('--verbose', action='store_true', default=False) 50 parser.add_argument('--out-csv', required=True) 51 args = parser.parse_args() 52 53 if (args.start_ts or args.end_ts) and args.startup_bounds: 54 print("Cannot specify --start-ts or --end-ts and --startup-bounds") 55 return 1 56 57 if args.startup_package and not args.startup_bounds: 58 print("Must specify --startup-bounds if --startup-package is specified") 59 return 1 60 61 breakdown = compute_breakdown_wrapper(args) 62 63 if args.out_csv: 64 diff_csv = breakdown.to_csv(index=False) 65 if args.out_csv == '-': 66 sys.stdout.write(diff_csv) 67 else: 68 with open(args.out_csv, 'w') as out: 69 out.write(diff_csv) 70 71 return 0 72 73 74if __name__ == '__main__': 75 exit(main()) 76