1#!/usr/bin/env python 2# 3# Copyright (C) 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18""" 19 Support profiling without usb connection in below steps: 20 1. With usb connection, start simpleperf recording. 21 2. Unplug the usb cable and play the app you want to profile, while the process of 22 simpleperf keeps running and collecting samples. 23 3. Replug the usb cable, stop simpleperf recording and pull recording file on host. 24 25 Note that recording is stopped once the app is killed. So if you restart the app 26 during profiling time, simpleperf only records the first running. 27""" 28 29from __future__ import print_function 30import argparse 31import subprocess 32import sys 33import time 34 35from utils import AdbHelper, get_target_binary_path, log_warning 36 37def start_recording(args): 38 adb = AdbHelper() 39 device_arch = adb.get_device_arch() 40 simpleperf_binary = get_target_binary_path(device_arch, 'simpleperf') 41 adb.check_run(['push', simpleperf_binary, '/data/local/tmp']) 42 adb.check_run(['shell', 'chmod', 'a+x', '/data/local/tmp/simpleperf']) 43 adb.check_run(['shell', 'rm', '-rf', '/data/local/tmp/perf.data', 44 '/data/local/tmp/simpleperf_output']) 45 shell_cmd = 'cd /data/local/tmp && nohup ./simpleperf record ' + args.record_options 46 if args.app: 47 shell_cmd += ' --app ' + args.app 48 if args.size_limit: 49 shell_cmd += ' --size-limit ' + args.size_limit 50 shell_cmd += ' >/data/local/tmp/simpleperf_output 2>&1' 51 print('shell_cmd: %s' % shell_cmd) 52 subproc = subprocess.Popen([adb.adb_path, 'shell', shell_cmd]) 53 # Wait 2 seconds to see if the simpleperf command fails to start. 54 time.sleep(2) 55 if subproc.poll() is None: 56 print('Simpleperf recording has started. Please unplug the usb cable and run the app.') 57 print('After that, run `%s stop` to get recording result.' % sys.argv[0]) 58 else: 59 adb.run(['shell', 'cat', '/data/local/tmp/simpleperf_output']) 60 sys.exit(subproc.returncode) 61 62def stop_recording(args): 63 adb = AdbHelper() 64 result = adb.run(['shell', 'pidof', 'simpleperf']) 65 if not result: 66 log_warning('No simpleperf process on device. The recording has ended.') 67 else: 68 adb.run(['shell', 'pkill', '-l', '2', 'simpleperf']) 69 print('Waiting for simpleperf process to finish...') 70 while adb.run(['shell', 'pidof', 'simpleperf']): 71 time.sleep(1) 72 adb.run(['shell', 'cat', '/data/local/tmp/simpleperf_output']) 73 adb.check_run(['pull', '/data/local/tmp/perf.data', args.perf_data_path]) 74 print('The recording data has been collected in %s.' % args.perf_data_path) 75 76def main(): 77 parser = argparse.ArgumentParser(description=__doc__, 78 formatter_class=argparse.RawDescriptionHelpFormatter) 79 subparsers = parser.add_subparsers() 80 start_parser = subparsers.add_parser('start', help='Start recording.') 81 start_parser.add_argument('-r', '--record_options', 82 default='-e task-clock:u -g', 83 help="""Set options for `simpleperf record` command. 84 Default is `-e task-clock:u -g`.""") 85 start_parser.add_argument('-p', '--app', help="""Profile an Android app, given the package 86 name. Like `-p com.example.android.myapp`.""") 87 start_parser.add_argument('--size_limit', type=str, 88 help="""Stop profiling when recording data reaches 89 [size_limit][K|M|G] bytes. Like `--size_limit 1M`.""") 90 start_parser.set_defaults(func=start_recording) 91 stop_parser = subparsers.add_parser('stop', help='Stop recording.') 92 stop_parser.add_argument('-o', '--perf_data_path', default='perf.data', help="""The path to 93 store profiling data on host. Default is perf.data.""") 94 stop_parser.set_defaults(func=stop_recording) 95 args = parser.parse_args() 96 args.func(args) 97 98if __name__ == '__main__': 99 main() 100