Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: build/android/adb_profile_chrome.py

Issue 25044004: android: Reimplement adb_profile_chrome in Python (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nits picked. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « build/android/adb_profile_chrome ('k') | build/android/pylib/android_commands.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Copyright 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import gzip
8 import logging
9 import optparse
10 import os
11 import pexpect
12 import re
13 import sys
14 import time
15
16 from pylib import android_commands
17 from pylib import constants
18
19
20 def _GetSupportedBrowsers():
21 # Add aliases for backwards compatibility.
22 supported_browsers = {
23 'stable': constants.PACKAGE_INFO['chrome_stable'],
24 'beta': constants.PACKAGE_INFO['chrome_beta'],
25 'dev': constants.PACKAGE_INFO['chrome_dev'],
26 'build': constants.PACKAGE_INFO['chrome'],
27 }
28 supported_browsers.update(constants.PACKAGE_INFO)
29 unsupported_browsers = ['content_browsertests', 'gtest', 'legacy_browser']
30 for browser in unsupported_browsers:
31 del supported_browsers[browser]
32 return supported_browsers
33
34
35 def _StartTracing(adb, package_info, categories, continuous):
36 adb.BroadcastIntent(package_info.package, 'GPU_PROFILER_START',
37 '-e categories "%s"' % categories,
38 '-e continuous' if continuous else '')
39
40
41 def _StopTracing(adb, package_info):
42 adb.BroadcastIntent(package_info.package, 'GPU_PROFILER_STOP')
43
44
45 def _GetLatestTraceFileName(adb, check_for_multiple_traces=True):
46 # Chrome logs two different messages related to tracing:
47 #
48 # 1. "Logging performance trace to file [...]"
49 # 2. "Profiler finished. Results are in [...]"
50 #
51 # The first one is printed when tracing starts and the second one indicates
52 # that the trace file is ready to be downloaded.
53 #
54 # We have to look for both of these messages to make sure we get the results
55 # from the latest trace and that the trace file is complete.
56 trace_start_re = re.compile(r'Logging performance trace to file')
57 trace_finish_re = re.compile(r'Profiler finished[.] Results are in (.*)[.]')
58
59 start_timeout = 5
60 start_match = None
61 finish_match = None
62
63 while True:
64 try:
65 start_match = adb.WaitForLogMatch(trace_start_re, None,
66 timeout=start_timeout)
67 except pexpect.TIMEOUT:
68 if start_match:
69 break
70 raise RuntimeError('Trace start marker not found. Is the correct version '
71 'of the browser running?')
72 finish_match = adb.WaitForLogMatch(trace_finish_re, None, timeout=120)
73 if not check_for_multiple_traces:
74 break
75 # Now that we've found one trace file, switch to polling for the rest of the
76 # log to see if there are more.
77 start_timeout = 1
78 return finish_match.group(1)
79
80
81 def _DownloadTrace(adb, trace_file):
82 trace_file = trace_file.replace('/storage/emulated/0/', '/sdcard/')
83 host_file = os.path.abspath(os.path.basename(trace_file))
84 adb.PullFileFromDevice(trace_file, host_file)
85 return host_file
86
87
88 def _CompressFile(host_file):
89 compressed_file = host_file + '.gz'
90 with gzip.open(compressed_file, 'wb') as out:
91 with open(host_file, 'rb') as input_file:
92 out.write(input_file.read())
93 os.unlink(host_file)
94 return compressed_file
95
96
97 def _PrintMessage(heading, eol='\n'):
98 sys.stdout.write(heading + eol)
99 sys.stdout.flush()
100
101
102 def _DownloadLatestTrace(adb, compress, check_for_multiple_traces=True):
103 _PrintMessage('Downloading trace...', eol='')
104 trace_file = _GetLatestTraceFileName(adb, check_for_multiple_traces)
105 host_file = _DownloadTrace(adb, trace_file)
106 if compress:
107 host_file = _CompressFile(host_file)
108 _PrintMessage('done')
109 _PrintMessage('Trace written to %s' % host_file)
110
111
112 def _CaptureAndDownloadTimedTrace(adb, package_info, categories, interval,
113 continuous, compress):
114 adb.StartMonitoringLogcat(clear=False)
115 adb.SyncLogCat()
116
117 try:
118 _PrintMessage('Capturing %d-second trace. Press Ctrl-C to stop early...' \
119 % interval, eol='')
120 _StartTracing(adb, package_info, categories, continuous)
121 time.sleep(interval)
122 except KeyboardInterrupt:
123 _PrintMessage('\nInterrupted, stopping...', eol='')
124 _StopTracing(adb, package_info)
125 _PrintMessage('done')
126
127 # Wait a bit for the browser to finish writing the trace file.
128 time.sleep(interval / 4 + 1)
129
130 _DownloadLatestTrace(adb, compress, check_for_multiple_traces=False)
131
132
133 def _ComputeCategories(options):
134 categories = [options.categories]
135 if options.trace_cc:
136 categories.append('disabled-by-default-cc.debug*')
137 if options.trace_gpu:
138 categories.append('disabled-by-default-gpu.debug*')
139 return ",".join(categories)
140
141
142 def main():
143 parser = optparse.OptionParser(description='Record about://tracing profiles '
144 'from Android browsers. See http://dev.'
145 'chromium.org/developers/how-tos/trace-event-'
146 'profiling-tool for detailed instructions for '
147 'profiling.')
148 manual_options = optparse.OptionGroup(parser, 'Manual tracing')
149 manual_options.add_option('--start', help='Start tracing.',
150 action='store_true')
151 manual_options.add_option('--stop', help='Stop tracing.',
152 action='store_true')
153 manual_options.add_option('-d', '--download', help='Download latest trace.',
154 action='store_true')
155 parser.add_option_group(manual_options)
156
157 auto_options = optparse.OptionGroup(parser, 'Automated tracing')
158 auto_options.add_option('-t', '--time', help='Profile for N seconds and '
159 'download the resulting trace.', metavar='N',
160 type='float')
161 parser.add_option_group(auto_options)
162
163 categories = optparse.OptionGroup(parser, 'Trace categories')
164 categories.add_option('-c', '--categories', help='Select categories to trace '
165 'with comma-delimited wildcards, e.g., '
166 '"*", "cat1*,-cat1a". Default is "*".', default='*')
167 categories.add_option('--trace-cc', help='Enable extra trace categories for '
168 'compositor frame viewer data.', action='store_true')
169 categories.add_option('--trace-gpu', help='Enable extra trace categories for '
170 'GPU data.', action='store_true')
171 parser.add_option_group(categories)
172
173 parser.add_option('-o', '--output', help='Save profile output to file. '
174 'Default is "/sdcard/Download/chrome-profile-results-*".')
175 parser.add_option('--continuous', help='Using the trace buffer as a ring '
176 'buffer, continuously profile until stopped.',
177 action='store_true')
178 browsers = sorted(_GetSupportedBrowsers().keys())
179 parser.add_option('-b', '--browser', help='Select among installed browsers. '
180 'One of ' + ', '.join(browsers) + ', "stable" is used by '
181 'default.', type='choice', choices=browsers,
182 default='stable')
183 parser.add_option('-v', '--verbose', help='Verbose logging.',
184 action='store_true')
185 parser.add_option('-z', '--compress', help='Compress the resulting trace '
186 'with gzip. ', action='store_true')
187 options, args = parser.parse_args()
188
189 if not any([options.start, options.stop, options.time, options.download]):
190 _PrintMessage('One of start/stop/download/time should be specified.')
191 return 1
192
193 if options.verbose:
194 logging.getLogger().setLevel(logging.DEBUG)
195
196 adb = android_commands.AndroidCommands()
197 categories = _ComputeCategories(options)
198 package_info = _GetSupportedBrowsers()[options.browser]
199
200 if options.start:
201 _StartTracing(adb, package_info, categories, options.continuous)
202 elif options.stop:
203 _StopTracing(adb, package_info)
204 elif options.download:
205 _DownloadLatestTrace(adb, options.compress)
206 elif options.time:
207 _CaptureAndDownloadTimedTrace(adb, package_info, categories, options.time,
208 options.continuous, options.compress)
209
210
211 if __name__ == '__main__':
212 sys.exit(main())
OLDNEW
« no previous file with comments | « build/android/adb_profile_chrome ('k') | build/android/pylib/android_commands.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698