blob: 4d89fceb3c7705b724da263b814be29f8fde52b5 [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:
Chris Craik631112b2015-04-28 17:00:17 -0700113 tracer_args = ['adb', 'shell', 'atrace --list_categories']
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800114 expect_trace = False
115 elif options.from_file is not None:
Chris Craik631112b2015-04-28 17:00:17 -0700116 tracer_args = ['cat', options.from_file]
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800117 expect_trace = True
118 else:
Chris Craik631112b2015-04-28 17:00:17 -0700119 atrace_args = ['atrace', '-z']
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800120 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'])
Chris Craik631112b2015-04-28 17:00:17 -0700146 tracer_args = ['adb', 'shell', ' '.join(atrace_args)]
Jeff Brownc6e750f2014-08-15 16:27:54 -0700147
Chris Craik631112b2015-04-28 17:00:17 -0700148 if tracer_args[0] == 'adb':
149 add_adb_serial(tracer_args, options.device_serial)
Glenn Kastena0cfa1d2012-10-08 15:40:30 -0700150
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700151 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
Jeff Brown595ae1e2012-05-22 14:52:13 -0700152
Jamie Gennis92791472012-03-05 17:33:58 -0800153 html_filename = options.output_file
Jamie Gennis92791472012-03-05 17:33:58 -0800154
Chris Craik631112b2015-04-28 17:00:17 -0700155 adb = subprocess.Popen(tracer_args, stdout=subprocess.PIPE,
Jamie Gennis7e3783f2012-04-28 13:16:11 -0700156 stderr=subprocess.PIPE)
Jamie Gennis9623f132013-03-08 14:50:37 -0800157
158 result = None
159 data = []
160
161 # Read the text portion of the output and watch for the 'TRACE:' marker that
162 # indicates the start of the trace data.
163 while result is None:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700164 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
165 if adb.stderr in ready[0]:
166 err = os.read(adb.stderr.fileno(), 4096)
167 sys.stderr.write(err)
168 sys.stderr.flush()
169 if adb.stdout in ready[0]:
Jamie Gennis9623f132013-03-08 14:50:37 -0800170 out = os.read(adb.stdout.fileno(), 4096)
171 parts = out.split('\nTRACE:', 1)
172
173 txt = parts[0].replace('\r', '')
174 if len(parts) == 2:
175 # The '\nTRACE:' match stole the last newline from the text, so add it
176 # back here.
177 txt += '\n'
178 sys.stdout.write(txt)
179 sys.stdout.flush()
180
181 if len(parts) == 2:
182 data.append(parts[1])
183 sys.stdout.write("downloading trace...")
184 sys.stdout.flush()
185 break
186
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700187 result = adb.poll()
Jamie Gennis9623f132013-03-08 14:50:37 -0800188
189 # Read and buffer the data portion of the output.
Jamie Gennis18bb5282013-05-13 15:39:58 -0700190 while True:
Jamie Gennis9623f132013-03-08 14:50:37 -0800191 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
Jamie Gennis18bb5282013-05-13 15:39:58 -0700192 keepReading = False
Jamie Gennis9623f132013-03-08 14:50:37 -0800193 if adb.stderr in ready[0]:
194 err = os.read(adb.stderr.fileno(), 4096)
Jamie Gennis18bb5282013-05-13 15:39:58 -0700195 if len(err) > 0:
196 keepReading = True
197 sys.stderr.write(err)
198 sys.stderr.flush()
Jamie Gennis9623f132013-03-08 14:50:37 -0800199 if adb.stdout in ready[0]:
200 out = os.read(adb.stdout.fileno(), 4096)
Jamie Gennis18bb5282013-05-13 15:39:58 -0700201 if len(out) > 0:
202 keepReading = True
203 data.append(out)
204
205 if result is not None and not keepReading:
206 break
Jamie Gennis9623f132013-03-08 14:50:37 -0800207
208 result = adb.poll()
209
210 if result == 0:
211 if expect_trace:
212 data = ''.join(data)
213
214 # Collapse CRLFs that are added by adb shell.
215 if data.startswith('\r\n'):
216 data = data.replace('\r\n', '\n')
217
218 # Skip the initial newline.
219 data = data[1:]
220
221 if not data:
222 print >> sys.stderr, ('No data was captured. Output file was not ' +
223 'written.')
224 sys.exit(1)
225 else:
226 # Indicate to the user that the data download is complete.
227 print " done\n"
228
Jeff Brownc6e750f2014-08-15 16:27:54 -0700229 # Extract the thread list dumped by ps.
230 threads = {}
231 if options.fix_threads:
Adrian Roos208a55a2015-05-07 15:54:32 -0700232 parts = re.split('USER +PID +PPID +VSIZE +RSS +WCHAN +PC +NAME', data, 1)
Jeff Brownc6e750f2014-08-15 16:27:54 -0700233 if len(parts) == 2:
234 data = parts[0]
235 for line in parts[1].splitlines():
236 cols = line.split(None, 8)
237 if len(cols) == 9:
238 tid = int(cols[1])
239 name = cols[8]
240 threads[tid] = name
241
242 # Decompress and preprocess the data.
243 out = zlib.decompress(data)
244 if options.fix_threads:
245 def repl(m):
246 tid = int(m.group(2))
247 if tid > 0:
248 name = threads.get(tid)
249 if name is None:
250 name = m.group(1)
251 if name == '<...>':
252 name = '<' + str(tid) + '>'
253 threads[tid] = name
254 return name + '-' + m.group(2)
255 else:
256 return m.group(0)
257 out = re.sub(r'^\s*(\S+)-(\d+)', repl, out, flags=re.MULTILINE)
258
Siva Velusamy48ea0762013-07-19 11:03:37 -0700259 html_prefix = read_asset(script_dir, 'prefix.html')
260 html_suffix = read_asset(script_dir, 'suffix.html')
Chris Craik5b5f1462015-04-07 16:41:13 -0700261 trace_viewer_html = read_asset(script_dir, 'systrace_trace_viewer.html')
Siva Velusamy48ea0762013-07-19 11:03:37 -0700262
Jamie Gennis9623f132013-03-08 14:50:37 -0800263 html_file = open(html_filename, 'w')
Chris Craikb122baf2015-03-05 13:58:42 -0800264 html_file.write(
265 html_prefix.replace("{{SYSTRACE_TRACE_VIEWER_HTML}}", trace_viewer_html))
Chris Craik92062442015-04-02 16:30:37 -0700266
Aaron Schulman64a222c2015-04-03 17:53:42 -0700267 html_file.write('<!-- BEGIN TRACE -->\n' +
268 ' <script class="trace-data" type="application/text">\n')
269 html_file.write(out)
270 html_file.write(' </script>\n<!-- END TRACE -->\n')
Chris Craik92062442015-04-02 16:30:37 -0700271
Jamie Gennis9623f132013-03-08 14:50:37 -0800272 html_file.write(html_suffix)
273 html_file.close()
Jamie Gennis66a37682013-07-15 18:29:18 -0700274 print "\n wrote file://%s\n" % os.path.abspath(options.output_file)
Jamie Gennis9623f132013-03-08 14:50:37 -0800275
276 else: # i.e. result != 0
277 print >> sys.stderr, 'adb returned error code %d' % result
278 sys.exit(1)
Jamie Gennis92791472012-03-05 17:33:58 -0800279
Siva Velusamy48ea0762013-07-19 11:03:37 -0700280def read_asset(src_dir, filename):
281 return open(os.path.join(src_dir, filename)).read()
282
Jeff Brown595ae1e2012-05-22 14:52:13 -0700283
Jamie Gennis92791472012-03-05 17:33:58 -0800284if __name__ == '__main__':
285 main()