blob: 6d77e9c37655b109b4f3a94529704f299e957e7a [file] [log] [blame]
Jamie Gennis92791472012-03-05 17:33:58 -08001#!/usr/bin/env python
2
Jamie Gennis4b56a2b2012-04-28 01:06:56 -07003# Copyright (c) 2011 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.
Jamie Gennis92791472012-03-05 17:33:58 -08006
7"""Android system-wide tracing utility.
8
9This is a tool for capturing a trace that includes data from both userland and
10the kernel. It creates an HTML file for visualizing the trace.
11"""
12
Jeff Brownc6e750f2014-08-15 16:27:54 -070013import errno, optparse, os, re, select, subprocess, sys, time, zlib
Jamie Gennis92791472012-03-05 17:33:58 -080014
Chris Craik5b5f1462015-04-07 16:41:13 -070015default_categories = 'sched gfx view dalvik webview input disk am wm'.split()
Jamie Gennis2da489c2012-09-19 18:06:29 -070016
Jamie Gennis664f21b2013-06-03 16:40:54 -070017class OptionParserIgnoreErrors(optparse.OptionParser):
18 def error(self, msg):
19 pass
20
21 def exit(self):
22 pass
23
24 def print_usage(self):
25 pass
26
27 def print_help(self):
28 pass
29
30 def print_version(self):
31 pass
32
33def get_device_sdk_version():
34 getprop_args = ['adb', 'shell', 'getprop', 'ro.build.version.sdk']
35
36 parser = OptionParserIgnoreErrors()
37 parser.add_option('-e', '--serial', dest='device_serial', type='string')
38 options, args = parser.parse_args()
39 if options.device_serial is not None:
40 getprop_args[1:1] = ['-s', options.device_serial]
41
Chris Craikb122baf2015-03-05 13:58:42 -080042 try:
43 adb = subprocess.Popen(getprop_args, stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE)
45 except OSError:
46 print 'Missing adb?'
47 sys.exit(1)
Jamie Gennisc2a6cae2013-06-04 16:17:30 -070048 out, err = adb.communicate()
49 if adb.returncode != 0:
50 print >> sys.stderr, 'Error querying device SDK-version:'
51 print >> sys.stderr, err
52 sys.exit(1)
Jamie Gennis664f21b2013-06-03 16:40:54 -070053
Jamie Gennisc2a6cae2013-06-04 16:17:30 -070054 version = int(out)
55 return version
Jamie Gennis664f21b2013-06-03 16:40:54 -070056
Keun young Parkde427be2012-08-30 15:17:13 -070057def add_adb_serial(command, serial):
Jamie Gennis66a37682013-07-15 18:29:18 -070058 if serial is not None:
Keun young Parkde427be2012-08-30 15:17:13 -070059 command.insert(1, serial)
60 command.insert(1, '-s')
61
Chris Craik5b5f1462015-04-07 16:41:13 -070062def get_default_categories():
63 list_command = ['adb', 'shell', 'atrace', '--list_categories']
64 try:
65 categories_output = subprocess.check_output(list_command)
66 categories = [c.split('-')[0].strip() for c in categories_output.splitlines()]
67
68 return [c for c in categories if c in default_categories]
69 except:
70 return []
71
Jamie Gennis92791472012-03-05 17:33:58 -080072def main():
Jamie Gennis664f21b2013-06-03 16:40:54 -070073 device_sdk_version = get_device_sdk_version()
74 if device_sdk_version < 18:
75 legacy_script = os.path.join(os.path.dirname(sys.argv[0]), 'systrace-legacy.py')
76 os.execv(legacy_script, sys.argv)
77
Jamie Gennisfe4c5942012-11-18 18:15:22 -080078 usage = "Usage: %prog [options] [category1 [category2 ...]]"
79 desc = "Example: %prog -b 32768 -t 15 gfx input view sched freq"
80 parser = optparse.OptionParser(usage=usage, description=desc)
Jamie Gennis92791472012-03-05 17:33:58 -080081 parser.add_option('-o', dest='output_file', help='write HTML to FILE',
82 default='trace.html', metavar='FILE')
83 parser.add_option('-t', '--time', dest='trace_time', type='int',
84 help='trace for N seconds', metavar='N')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080085 parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int',
86 help='use a trace buffer size of N KB', metavar='N')
Jamie Gennis553ec562012-11-20 17:45:49 -080087 parser.add_option('-k', '--ktrace', dest='kfuncs', action='store',
88 help='specify a comma-separated list of kernel functions to trace')
Jamie Gennisfe4c5942012-11-18 18:15:22 -080089 parser.add_option('-l', '--list-categories', dest='list_categories', default=False,
90 action='store_true', help='list the available categories and exit')
Jamie Gennisb9a5fc82013-03-27 19:55:09 -070091 parser.add_option('-a', '--app', dest='app_name', default=None, type='string',
92 action='store', help='enable application-level tracing for comma-separated ' +
93 'list of app cmdlines')
Jeff Brownc6e750f2014-08-15 16:27:54 -070094 parser.add_option('--no-fix-threads', dest='fix_threads', default=True,
95 action='store_false', help='don\'t fix missing or truncated thread names')
Jamie Gennisfe4c5942012-11-18 18:15:22 -080096
Jeff Brown595ae1e2012-05-22 14:52:13 -070097 parser.add_option('--link-assets', dest='link_assets', default=False,
98 action='store_true', help='link to original CSS or JS resources '
99 'instead of embedding them')
Glenn Kastena0cfa1d2012-10-08 15:40:30 -0700100 parser.add_option('--from-file', dest='from_file', action='store',
Xia Wang340d7722013-02-15 12:38:09 -0800101 help='read the trace from a file (compressed) rather than running a live trace')
Jamie Gennis2da489c2012-09-19 18:06:29 -0700102 parser.add_option('--asset-dir', dest='asset_dir', default='trace-viewer',
103 type='string', help='')
Keun young Parkde427be2012-08-30 15:17:13 -0700104 parser.add_option('-e', '--serial', dest='device_serial', type='string',
105 help='adb device serial number')
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800106
Chris Craik5b5f1462015-04-07 16:41:13 -0700107 options, categories = parser.parse_args()
Jamie Gennis92791472012-03-05 17:33:58 -0800108
Chris Craikb122baf2015-03-05 13:58:42 -0800109 if options.link_assets or options.asset_dir != 'trace-viewer':
110 parser.error('--link-assets and --asset-dir is deprecated.')
111
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800112 if options.list_categories:
113 atrace_args = ['adb', 'shell', 'atrace', '--list_categories']
114 expect_trace = False
115 elif options.from_file is not None:
Glenn Kastena0cfa1d2012-10-08 15:40:30 -0700116 atrace_args = ['cat', options.from_file]
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800117 expect_trace = True
118 else:
119 atrace_args = ['adb', 'shell', 'atrace', '-z']
120 expect_trace = True
121
122 if options.trace_time is not None:
123 if options.trace_time > 0:
124 atrace_args.extend(['-t', str(options.trace_time)])
125 else:
126 parser.error('the trace time must be a positive number')
Jamie Gennisb9a5fc82013-03-27 19:55:09 -0700127
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800128 if options.trace_buf_size is not None:
129 if options.trace_buf_size > 0:
130 atrace_args.extend(['-b', str(options.trace_buf_size)])
131 else:
132 parser.error('the trace buffer size must be a positive number')
133
Jamie Gennisb9a5fc82013-03-27 19:55:09 -0700134 if options.app_name is not None:
135 atrace_args.extend(['-a', options.app_name])
136
Jamie Gennis553ec562012-11-20 17:45:49 -0800137 if options.kfuncs is not None:
138 atrace_args.extend(['-k', options.kfuncs])
139
Chris Craik5b5f1462015-04-07 16:41:13 -0700140 if not categories:
141 categories = get_default_categories()
142 atrace_args.extend(categories)
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800143
Jeff Brownc6e750f2014-08-15 16:27:54 -0700144 if options.fix_threads:
145 atrace_args.extend([';', 'ps', '-t'])
146
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800147 if atrace_args[0] == 'adb':
148 add_adb_serial(atrace_args, options.device_serial)
Glenn Kastena0cfa1d2012-10-08 15:40:30 -0700149
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700150 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
Jeff Brown595ae1e2012-05-22 14:52:13 -0700151
Jamie Gennis92791472012-03-05 17:33:58 -0800152 html_filename = options.output_file
Jamie Gennis92791472012-03-05 17:33:58 -0800153
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700154 adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE,
Jamie Gennis7e3783f2012-04-28 13:16:11 -0700155 stderr=subprocess.PIPE)
Jamie Gennis9623f132013-03-08 14:50:37 -0800156
157 result = None
158 data = []
159
160 # Read the text portion of the output and watch for the 'TRACE:' marker that
161 # indicates the start of the trace data.
162 while result is None:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700163 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
164 if adb.stderr in ready[0]:
165 err = os.read(adb.stderr.fileno(), 4096)
166 sys.stderr.write(err)
167 sys.stderr.flush()
168 if adb.stdout in ready[0]:
Jamie Gennis9623f132013-03-08 14:50:37 -0800169 out = os.read(adb.stdout.fileno(), 4096)
170 parts = out.split('\nTRACE:', 1)
171
172 txt = parts[0].replace('\r', '')
173 if len(parts) == 2:
174 # The '\nTRACE:' match stole the last newline from the text, so add it
175 # back here.
176 txt += '\n'
177 sys.stdout.write(txt)
178 sys.stdout.flush()
179
180 if len(parts) == 2:
181 data.append(parts[1])
182 sys.stdout.write("downloading trace...")
183 sys.stdout.flush()
184 break
185
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700186 result = adb.poll()
Jamie Gennis9623f132013-03-08 14:50:37 -0800187
188 # Read and buffer the data portion of the output.
Jamie Gennis18bb5282013-05-13 15:39:58 -0700189 while True:
Jamie Gennis9623f132013-03-08 14:50:37 -0800190 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
Jamie Gennis18bb5282013-05-13 15:39:58 -0700191 keepReading = False
Jamie Gennis9623f132013-03-08 14:50:37 -0800192 if adb.stderr in ready[0]:
193 err = os.read(adb.stderr.fileno(), 4096)
Jamie Gennis18bb5282013-05-13 15:39:58 -0700194 if len(err) > 0:
195 keepReading = True
196 sys.stderr.write(err)
197 sys.stderr.flush()
Jamie Gennis9623f132013-03-08 14:50:37 -0800198 if adb.stdout in ready[0]:
199 out = os.read(adb.stdout.fileno(), 4096)
Jamie Gennis18bb5282013-05-13 15:39:58 -0700200 if len(out) > 0:
201 keepReading = True
202 data.append(out)
203
204 if result is not None and not keepReading:
205 break
Jamie Gennis9623f132013-03-08 14:50:37 -0800206
207 result = adb.poll()
208
209 if result == 0:
210 if expect_trace:
211 data = ''.join(data)
212
213 # Collapse CRLFs that are added by adb shell.
214 if data.startswith('\r\n'):
215 data = data.replace('\r\n', '\n')
216
217 # Skip the initial newline.
218 data = data[1:]
219
220 if not data:
221 print >> sys.stderr, ('No data was captured. Output file was not ' +
222 'written.')
223 sys.exit(1)
224 else:
225 # Indicate to the user that the data download is complete.
226 print " done\n"
227
Jeff Brownc6e750f2014-08-15 16:27:54 -0700228 # Extract the thread list dumped by ps.
229 threads = {}
230 if options.fix_threads:
231 parts = data.split('USER PID PPID VSIZE RSS WCHAN PC NAME', 1)
232 if len(parts) == 2:
233 data = parts[0]
234 for line in parts[1].splitlines():
235 cols = line.split(None, 8)
236 if len(cols) == 9:
237 tid = int(cols[1])
238 name = cols[8]
239 threads[tid] = name
240
241 # Decompress and preprocess the data.
242 out = zlib.decompress(data)
243 if options.fix_threads:
244 def repl(m):
245 tid = int(m.group(2))
246 if tid > 0:
247 name = threads.get(tid)
248 if name is None:
249 name = m.group(1)
250 if name == '<...>':
251 name = '<' + str(tid) + '>'
252 threads[tid] = name
253 return name + '-' + m.group(2)
254 else:
255 return m.group(0)
256 out = re.sub(r'^\s*(\S+)-(\d+)', repl, out, flags=re.MULTILINE)
257
Siva Velusamy48ea0762013-07-19 11:03:37 -0700258 html_prefix = read_asset(script_dir, 'prefix.html')
259 html_suffix = read_asset(script_dir, 'suffix.html')
Chris Craik5b5f1462015-04-07 16:41:13 -0700260 trace_viewer_html = read_asset(script_dir, 'systrace_trace_viewer.html')
Siva Velusamy48ea0762013-07-19 11:03:37 -0700261
Jamie Gennis9623f132013-03-08 14:50:37 -0800262 html_file = open(html_filename, 'w')
Chris Craikb122baf2015-03-05 13:58:42 -0800263 html_file.write(
264 html_prefix.replace("{{SYSTRACE_TRACE_VIEWER_HTML}}", trace_viewer_html))
Chris Craik92062442015-04-02 16:30:37 -0700265
266 # format newlines and double quotes
267 # for embedding in double-quoted JS string
268 html_file.write(out.replace('\n', '\\n\\\n').replace('\"', '\\\"'))
269
Jamie Gennis9623f132013-03-08 14:50:37 -0800270 html_file.write(html_suffix)
271 html_file.close()
Jamie Gennis66a37682013-07-15 18:29:18 -0700272 print "\n wrote file://%s\n" % os.path.abspath(options.output_file)
Jamie Gennis9623f132013-03-08 14:50:37 -0800273
274 else: # i.e. result != 0
275 print >> sys.stderr, 'adb returned error code %d' % result
276 sys.exit(1)
Jamie Gennis92791472012-03-05 17:33:58 -0800277
Siva Velusamy48ea0762013-07-19 11:03:37 -0700278def read_asset(src_dir, filename):
279 return open(os.path.join(src_dir, filename)).read()
280
Jeff Brown595ae1e2012-05-22 14:52:13 -0700281
Jamie Gennis92791472012-03-05 17:33:58 -0800282if __name__ == '__main__':
283 main()