• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
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
29import logging
30import subprocess
31import sys
32import time
33
34from simpleperf_utils import AdbHelper, BaseArgumentParser, get_target_binary_path
35
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.pid:
49        shell_cmd += ' -p ' + args.pid
50    if args.size_limit:
51        shell_cmd += ' --size-limit ' + args.size_limit
52    shell_cmd += ' >/data/local/tmp/simpleperf_output 2>&1'
53    print('shell_cmd: %s' % shell_cmd)
54    subproc = subprocess.Popen([adb.adb_path, 'shell', shell_cmd])
55    # Wait 2 seconds to see if the simpleperf command fails to start.
56    time.sleep(2)
57    if subproc.poll() is None:
58        print('Simpleperf recording has started. Please unplug the usb cable and run the app.')
59        print('After that, run `%s stop` to get recording result.' % sys.argv[0])
60    else:
61        adb.run(['shell', 'cat', '/data/local/tmp/simpleperf_output'])
62        sys.exit(subproc.returncode)
63
64
65def stop_recording(args):
66    adb = AdbHelper()
67    result = adb.run(['shell', 'pidof', 'simpleperf'])
68    if not result:
69        logging.warning('No simpleperf process on device. The recording has ended.')
70    else:
71        adb.run(['shell', 'pkill', '-l', '2', 'simpleperf'])
72        print('Waiting for simpleperf process to finish...')
73        while adb.run(['shell', 'pidof', 'simpleperf']):
74            time.sleep(1)
75    adb.run(['shell', 'cat', '/data/local/tmp/simpleperf_output'])
76    adb.check_run(['pull', '/data/local/tmp/perf.data', args.perf_data_path])
77    print('The recording data has been collected in %s.' % args.perf_data_path)
78
79
80def main():
81    parser = BaseArgumentParser(description=__doc__)
82    subparsers = parser.add_subparsers()
83    start_parser = subparsers.add_parser('start', help='Start recording.')
84    start_parser.add_argument('-r', '--record_options',
85                              default='-e task-clock:u -g',
86                              help="""Set options for `simpleperf record` command.
87                                      Default is `-e task-clock:u -g`.""")
88    start_parser.add_argument('-p', '--app', help="""Profile an Android app, given the package
89                              name. Like `-p com.example.android.myapp`.""")
90    start_parser.add_argument('--pid', help="""Profile an Android app, given the process id.
91                              Like `--pid 918`.""")
92    start_parser.add_argument('--size_limit', type=str,
93                              help="""Stop profiling when recording data reaches
94                                      [size_limit][K|M|G] bytes. Like `--size_limit 1M`.""")
95    start_parser.set_defaults(func=start_recording)
96    stop_parser = subparsers.add_parser('stop', help='Stop recording.')
97    stop_parser.add_argument('-o', '--perf_data_path', default='perf.data', help="""The path to
98                             store profiling data on host. Default is perf.data.""")
99    stop_parser.set_defaults(func=stop_recording)
100    args = parser.parse_args()
101    args.func(args)
102
103
104if __name__ == '__main__':
105    main()
106