blob: 8fd861f47bd12bcfca66ccb07a348c0f3310868d [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 Craikb122baf2015-03-05 13:58:42 -080015flattened_html_file = 'systrace_trace_viewer.html'
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
Jamie Gennis92791472012-03-05 17:33:58 -080062def main():
Jamie Gennis664f21b2013-06-03 16:40:54 -070063 device_sdk_version = get_device_sdk_version()
64 if device_sdk_version < 18:
65 legacy_script = os.path.join(os.path.dirname(sys.argv[0]), 'systrace-legacy.py')
66 os.execv(legacy_script, sys.argv)
67
Jamie Gennisfe4c5942012-11-18 18:15:22 -080068 usage = "Usage: %prog [options] [category1 [category2 ...]]"
69 desc = "Example: %prog -b 32768 -t 15 gfx input view sched freq"
70 parser = optparse.OptionParser(usage=usage, description=desc)
Jamie Gennis92791472012-03-05 17:33:58 -080071 parser.add_option('-o', dest='output_file', help='write HTML to FILE',
72 default='trace.html', metavar='FILE')
73 parser.add_option('-t', '--time', dest='trace_time', type='int',
74 help='trace for N seconds', metavar='N')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080075 parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int',
76 help='use a trace buffer size of N KB', metavar='N')
Jamie Gennis553ec562012-11-20 17:45:49 -080077 parser.add_option('-k', '--ktrace', dest='kfuncs', action='store',
78 help='specify a comma-separated list of kernel functions to trace')
Jamie Gennisfe4c5942012-11-18 18:15:22 -080079 parser.add_option('-l', '--list-categories', dest='list_categories', default=False,
80 action='store_true', help='list the available categories and exit')
Jamie Gennisb9a5fc82013-03-27 19:55:09 -070081 parser.add_option('-a', '--app', dest='app_name', default=None, type='string',
82 action='store', help='enable application-level tracing for comma-separated ' +
83 'list of app cmdlines')
Jeff Brownc6e750f2014-08-15 16:27:54 -070084 parser.add_option('--no-fix-threads', dest='fix_threads', default=True,
85 action='store_false', help='don\'t fix missing or truncated thread names')
Jamie Gennisfe4c5942012-11-18 18:15:22 -080086
Jeff Brown595ae1e2012-05-22 14:52:13 -070087 parser.add_option('--link-assets', dest='link_assets', default=False,
88 action='store_true', help='link to original CSS or JS resources '
89 'instead of embedding them')
Glenn Kastena0cfa1d2012-10-08 15:40:30 -070090 parser.add_option('--from-file', dest='from_file', action='store',
Xia Wang340d7722013-02-15 12:38:09 -080091 help='read the trace from a file (compressed) rather than running a live trace')
Jamie Gennis2da489c2012-09-19 18:06:29 -070092 parser.add_option('--asset-dir', dest='asset_dir', default='trace-viewer',
93 type='string', help='')
Keun young Parkde427be2012-08-30 15:17:13 -070094 parser.add_option('-e', '--serial', dest='device_serial', type='string',
95 help='adb device serial number')
Jamie Gennisfe4c5942012-11-18 18:15:22 -080096
Jamie Gennis92791472012-03-05 17:33:58 -080097 options, args = parser.parse_args()
98
Chris Craikb122baf2015-03-05 13:58:42 -080099 if options.link_assets or options.asset_dir != 'trace-viewer':
100 parser.error('--link-assets and --asset-dir is deprecated.')
101
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800102 if options.list_categories:
103 atrace_args = ['adb', 'shell', 'atrace', '--list_categories']
104 expect_trace = False
105 elif options.from_file is not None:
Glenn Kastena0cfa1d2012-10-08 15:40:30 -0700106 atrace_args = ['cat', options.from_file]
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800107 expect_trace = True
108 else:
109 atrace_args = ['adb', 'shell', 'atrace', '-z']
110 expect_trace = True
111
112 if options.trace_time is not None:
113 if options.trace_time > 0:
114 atrace_args.extend(['-t', str(options.trace_time)])
115 else:
116 parser.error('the trace time must be a positive number')
Jamie Gennisb9a5fc82013-03-27 19:55:09 -0700117
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800118 if options.trace_buf_size is not None:
119 if options.trace_buf_size > 0:
120 atrace_args.extend(['-b', str(options.trace_buf_size)])
121 else:
122 parser.error('the trace buffer size must be a positive number')
123
Jamie Gennisb9a5fc82013-03-27 19:55:09 -0700124 if options.app_name is not None:
125 atrace_args.extend(['-a', options.app_name])
126
Jamie Gennis553ec562012-11-20 17:45:49 -0800127 if options.kfuncs is not None:
128 atrace_args.extend(['-k', options.kfuncs])
129
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800130 atrace_args.extend(args)
131
Jeff Brownc6e750f2014-08-15 16:27:54 -0700132 if options.fix_threads:
133 atrace_args.extend([';', 'ps', '-t'])
134
Jamie Gennisfe4c5942012-11-18 18:15:22 -0800135 if atrace_args[0] == 'adb':
136 add_adb_serial(atrace_args, options.device_serial)
Glenn Kastena0cfa1d2012-10-08 15:40:30 -0700137
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700138 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
Jeff Brown595ae1e2012-05-22 14:52:13 -0700139
Chris Craikb122baf2015-03-05 13:58:42 -0800140 with open(os.path.join(script_dir, flattened_html_file), 'r') as f:
141 trace_viewer_html = f.read()
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700142
Jamie Gennis92791472012-03-05 17:33:58 -0800143 html_filename = options.output_file
Jamie Gennis92791472012-03-05 17:33:58 -0800144
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700145 adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE,
Jamie Gennis7e3783f2012-04-28 13:16:11 -0700146 stderr=subprocess.PIPE)
Jamie Gennis9623f132013-03-08 14:50:37 -0800147
148 result = None
149 data = []
150
151 # Read the text portion of the output and watch for the 'TRACE:' marker that
152 # indicates the start of the trace data.
153 while result is None:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700154 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
155 if adb.stderr in ready[0]:
156 err = os.read(adb.stderr.fileno(), 4096)
157 sys.stderr.write(err)
158 sys.stderr.flush()
159 if adb.stdout in ready[0]:
Jamie Gennis9623f132013-03-08 14:50:37 -0800160 out = os.read(adb.stdout.fileno(), 4096)
161 parts = out.split('\nTRACE:', 1)
162
163 txt = parts[0].replace('\r', '')
164 if len(parts) == 2:
165 # The '\nTRACE:' match stole the last newline from the text, so add it
166 # back here.
167 txt += '\n'
168 sys.stdout.write(txt)
169 sys.stdout.flush()
170
171 if len(parts) == 2:
172 data.append(parts[1])
173 sys.stdout.write("downloading trace...")
174 sys.stdout.flush()
175 break
176
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700177 result = adb.poll()
Jamie Gennis9623f132013-03-08 14:50:37 -0800178
179 # Read and buffer the data portion of the output.
Jamie Gennis18bb5282013-05-13 15:39:58 -0700180 while True:
Jamie Gennis9623f132013-03-08 14:50:37 -0800181 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
Jamie Gennis18bb5282013-05-13 15:39:58 -0700182 keepReading = False
Jamie Gennis9623f132013-03-08 14:50:37 -0800183 if adb.stderr in ready[0]:
184 err = os.read(adb.stderr.fileno(), 4096)
Jamie Gennis18bb5282013-05-13 15:39:58 -0700185 if len(err) > 0:
186 keepReading = True
187 sys.stderr.write(err)
188 sys.stderr.flush()
Jamie Gennis9623f132013-03-08 14:50:37 -0800189 if adb.stdout in ready[0]:
190 out = os.read(adb.stdout.fileno(), 4096)
Jamie Gennis18bb5282013-05-13 15:39:58 -0700191 if len(out) > 0:
192 keepReading = True
193 data.append(out)
194
195 if result is not None and not keepReading:
196 break
Jamie Gennis9623f132013-03-08 14:50:37 -0800197
198 result = adb.poll()
199
200 if result == 0:
201 if expect_trace:
202 data = ''.join(data)
203
204 # Collapse CRLFs that are added by adb shell.
205 if data.startswith('\r\n'):
206 data = data.replace('\r\n', '\n')
207
208 # Skip the initial newline.
209 data = data[1:]
210
211 if not data:
212 print >> sys.stderr, ('No data was captured. Output file was not ' +
213 'written.')
214 sys.exit(1)
215 else:
216 # Indicate to the user that the data download is complete.
217 print " done\n"
218
Jeff Brownc6e750f2014-08-15 16:27:54 -0700219 # Extract the thread list dumped by ps.
220 threads = {}
221 if options.fix_threads:
222 parts = data.split('USER PID PPID VSIZE RSS WCHAN PC NAME', 1)
223 if len(parts) == 2:
224 data = parts[0]
225 for line in parts[1].splitlines():
226 cols = line.split(None, 8)
227 if len(cols) == 9:
228 tid = int(cols[1])
229 name = cols[8]
230 threads[tid] = name
231
232 # Decompress and preprocess the data.
233 out = zlib.decompress(data)
234 if options.fix_threads:
235 def repl(m):
236 tid = int(m.group(2))
237 if tid > 0:
238 name = threads.get(tid)
239 if name is None:
240 name = m.group(1)
241 if name == '<...>':
242 name = '<' + str(tid) + '>'
243 threads[tid] = name
244 return name + '-' + m.group(2)
245 else:
246 return m.group(0)
247 out = re.sub(r'^\s*(\S+)-(\d+)', repl, out, flags=re.MULTILINE)
248
Siva Velusamy48ea0762013-07-19 11:03:37 -0700249 html_prefix = read_asset(script_dir, 'prefix.html')
250 html_suffix = read_asset(script_dir, 'suffix.html')
251
Jamie Gennis9623f132013-03-08 14:50:37 -0800252 html_file = open(html_filename, 'w')
Chris Craikb122baf2015-03-05 13:58:42 -0800253 html_file.write(
254 html_prefix.replace("{{SYSTRACE_TRACE_VIEWER_HTML}}", trace_viewer_html))
Jeff Brownc6e750f2014-08-15 16:27:54 -0700255 html_out = out.replace('\n', '\\n\\\n')
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700256 html_file.write(html_out)
Jamie Gennis9623f132013-03-08 14:50:37 -0800257 html_file.write(html_suffix)
258 html_file.close()
Jamie Gennis66a37682013-07-15 18:29:18 -0700259 print "\n wrote file://%s\n" % os.path.abspath(options.output_file)
Jamie Gennis9623f132013-03-08 14:50:37 -0800260
261 else: # i.e. result != 0
262 print >> sys.stderr, 'adb returned error code %d' % result
263 sys.exit(1)
Jamie Gennis92791472012-03-05 17:33:58 -0800264
Siva Velusamy48ea0762013-07-19 11:03:37 -0700265def read_asset(src_dir, filename):
266 return open(os.path.join(src_dir, filename)).read()
267
Jeff Brown595ae1e2012-05-22 14:52:13 -0700268
Jamie Gennis92791472012-03-05 17:33:58 -0800269if __name__ == '__main__':
270 main()