blob: 013227955e497b0a2ff0191063fdf7b55309508d [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 Brown595ae1e2012-05-22 14:52:13 -070013import errno, optparse, os, select, subprocess, sys, time, zlib, config
Jamie Gennis92791472012-03-05 17:33:58 -080014
Jamie Gennis7e3783f2012-04-28 13:16:11 -070015# This list is based on the tags in frameworks/native/include/utils/Trace.h.
16trace_tag_bits = {
17 'gfx': 1<<1,
18 'input': 1<<2,
19 'view': 1<<3,
20 'webview': 1<<4,
21 'wm': 1<<5,
22 'am': 1<<6,
Andy Stadler5bd161f2012-05-03 15:31:03 -070023 'sync': 1<<7,
Glenn Kastenb4fa51e2012-05-07 09:19:32 -070024 'audio': 1<<8,
Jamie Gennisb7f480d2012-05-11 04:44:03 -070025 'video': 1<<9,
Jamie Gennis7e3783f2012-04-28 13:16:11 -070026}
27
Jamie Gennis92791472012-03-05 17:33:58 -080028def main():
29 parser = optparse.OptionParser()
30 parser.add_option('-o', dest='output_file', help='write HTML to FILE',
31 default='trace.html', metavar='FILE')
32 parser.add_option('-t', '--time', dest='trace_time', type='int',
33 help='trace for N seconds', metavar='N')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080034 parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int',
35 help='use a trace buffer size of N KB', metavar='N')
36 parser.add_option('-f', '--cpu-freq', dest='trace_cpu_freq', default=False,
37 action='store_true', help='trace CPU frequency changes')
Jamie Gennis415e5d82012-05-07 18:00:11 -070038 parser.add_option('-i', '--cpu-idle', dest='trace_cpu_idle', default=False,
39 action='store_true', help='trace CPU idle events')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080040 parser.add_option('-l', '--cpu-load', dest='trace_cpu_load', default=False,
41 action='store_true', help='trace CPU load')
Andy Stadler5bd161f2012-05-03 15:31:03 -070042 parser.add_option('-s', '--no-cpu-sched', dest='trace_cpu_sched', default=True,
43 action='store_false', help='inhibit tracing CPU ' +
44 'scheduler (allows longer trace times by reducing data ' +
45 'rate into buffer)')
Jamie Gennis92791472012-03-05 17:33:58 -080046 parser.add_option('-w', '--workqueue', dest='trace_workqueue', default=False,
47 action='store_true', help='trace the kernel workqueues')
Jamie Gennis7e3783f2012-04-28 13:16:11 -070048 parser.add_option('--set-tags', dest='set_tags', action='store',
49 help='set the enabled trace tags and exit; set to a ' +
50 'comma separated list of: ' +
51 ', '.join(trace_tag_bits.iterkeys()))
Jeff Brown595ae1e2012-05-22 14:52:13 -070052 parser.add_option('--link-assets', dest='link_assets', default=False,
53 action='store_true', help='link to original CSS or JS resources '
54 'instead of embedding them')
Jamie Gennis92791472012-03-05 17:33:58 -080055 options, args = parser.parse_args()
56
Jamie Gennis7e3783f2012-04-28 13:16:11 -070057 if options.set_tags:
58 flags = 0
59 tags = options.set_tags.split(',')
60 for tag in tags:
61 try:
62 flags |= trace_tag_bits[tag]
63 except KeyError:
64 parser.error('unrecognized tag: %s\nknown tags are: %s' %
65 (tag, ', '.join(trace_tag_bits.iterkeys())))
Jamie Gennis81e9aa72012-05-09 13:57:58 -070066 atrace_args = ['adb', 'shell', 'setprop', 'debug.atrace.tags.enableflags', hex(flags)]
Jamie Gennis7e3783f2012-04-28 13:16:11 -070067 try:
68 subprocess.check_call(atrace_args)
69 except subprocess.CalledProcessError, e:
70 print sys.stderr, 'unable to set tags: %s' % e
71 print '\nSet enabled tags to: %s\n' % ', '.join(tags)
72 print ('You will likely need to restart the Android framework for this to ' +
73 'take effect:\n\n adb shell stop\n adb shell ' +
74 'start\n')
75 return
76
Andy Stadler5bd161f2012-05-03 15:31:03 -070077 atrace_args = ['adb', 'shell', 'atrace', '-z']
Jamie Gennis98ef97d2012-03-07 16:06:53 -080078 if options.trace_cpu_freq:
79 atrace_args.append('-f')
Jamie Gennis415e5d82012-05-07 18:00:11 -070080 if options.trace_cpu_idle:
81 atrace_args.append('-i')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080082 if options.trace_cpu_load:
83 atrace_args.append('-l')
Andy Stadler5bd161f2012-05-03 15:31:03 -070084 if options.trace_cpu_sched:
85 atrace_args.append('-s')
Jamie Gennis92791472012-03-05 17:33:58 -080086 if options.trace_workqueue:
87 atrace_args.append('-w')
88 if options.trace_time is not None:
89 if options.trace_time > 0:
90 atrace_args.extend(['-t', str(options.trace_time)])
91 else:
92 parser.error('the trace time must be a positive number')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080093 if options.trace_buf_size is not None:
94 if options.trace_buf_size > 0:
95 atrace_args.extend(['-b', str(options.trace_buf_size)])
96 else:
97 parser.error('the trace buffer size must be a positive number')
Jamie Gennis92791472012-03-05 17:33:58 -080098
Jamie Gennis4b56a2b2012-04-28 01:06:56 -070099 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
Jeff Brown595ae1e2012-05-22 14:52:13 -0700100
101 if options.link_assets:
102 css = '\n'.join(linked_css_tag % (os.path.join(script_dir, f)) for f in config.css_in_files)
103 js = '\n'.join(linked_js_tag % (os.path.join(script_dir, f)) for f in config.js_in_files)
104 else:
105 css_filename = os.path.join(script_dir, config.css_out_file)
106 js_filename = os.path.join(script_dir, config.js_out_file)
107 css = compiled_css_tag % (open(css_filename).read())
108 js = compiled_js_tag % (open(js_filename).read())
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700109
Jamie Gennis92791472012-03-05 17:33:58 -0800110 html_filename = options.output_file
111 html_file = open(html_filename, 'w')
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700112 html_file.write(html_prefix % (css, js))
Jamie Gennis92791472012-03-05 17:33:58 -0800113
114 trace_started = False
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700115 leftovers = ''
116 adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE,
Jamie Gennis7e3783f2012-04-28 13:16:11 -0700117 stderr=subprocess.PIPE)
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700118 dec = zlib.decompressobj()
Jamie Gennis92791472012-03-05 17:33:58 -0800119 while True:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700120 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
121 if adb.stderr in ready[0]:
122 err = os.read(adb.stderr.fileno(), 4096)
123 sys.stderr.write(err)
124 sys.stderr.flush()
125 if adb.stdout in ready[0]:
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700126 out = leftovers + os.read(adb.stdout.fileno(), 4096)
127 out = out.replace('\r\n', '\n')
128 if out.endswith('\r'):
129 out = out[:-1]
130 leftovers = '\r'
131 else:
132 leftovers = ''
Jamie Gennis92791472012-03-05 17:33:58 -0800133 if not trace_started:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700134 lines = out.splitlines(True)
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700135 out = ''
Jamie Gennis92791472012-03-05 17:33:58 -0800136 for i, line in enumerate(lines):
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700137 if line == 'TRACE:\n':
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700138 sys.stdout.write("downloading trace...")
Jamie Gennis92791472012-03-05 17:33:58 -0800139 sys.stdout.flush()
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700140 out = ''.join(lines[i+1:])
Jamie Gennis92791472012-03-05 17:33:58 -0800141 trace_started = True
142 break
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700143 elif 'TRACE:'.startswith(line) and i == len(lines) - 1:
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700144 leftovers = line + leftovers
Jamie Gennis92791472012-03-05 17:33:58 -0800145 else:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700146 sys.stdout.write(line)
Jamie Gennis92791472012-03-05 17:33:58 -0800147 sys.stdout.flush()
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700148 if len(out) > 0:
149 out = dec.decompress(out)
150 html_out = out.replace('\n', '\\n\\\n')
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700151 if len(html_out) > 0:
152 html_file.write(html_out)
153 result = adb.poll()
Jamie Gennis92791472012-03-05 17:33:58 -0800154 if result is not None:
155 break
156 if result != 0:
157 print sys.stderr, 'adb returned error code %d' % result
158 else:
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700159 html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '')
160 if len(html_out) > 0:
161 html_file.write(html_out)
Jamie Gennis92791472012-03-05 17:33:58 -0800162 html_file.write(html_suffix)
163 html_file.close()
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700164 print " done\n\n wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
Jamie Gennis92791472012-03-05 17:33:58 -0800165
166html_prefix = """<!DOCTYPE HTML>
167<html>
168<head i18n-values="dir:textdirection;">
169<title>Android System Trace</title>
Jeff Brown595ae1e2012-05-22 14:52:13 -0700170%s
171%s
Jamie Gennis92791472012-03-05 17:33:58 -0800172<style>
173 .view {
174 overflow: hidden;
175 position: absolute;
176 top: 0;
177 bottom: 0;
178 left: 0;
179 right: 0;
180 }
181</style>
182</head>
183<body>
184 <div class="view">
185 </div>
186 <script>
187 var linuxPerfData = "\\
188"""
189
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700190html_suffix = """ dummy-0000 [000] 0.0: 0: trace_event_clock_sync: parent_ts=0.0\\n";
Jamie Gennis92791472012-03-05 17:33:58 -0800191 </script>
192</body>
193</html>
194"""
195
Jeff Brown595ae1e2012-05-22 14:52:13 -0700196compiled_css_tag = """<style type="text/css">%s</style>"""
197compiled_js_tag = """<script language="javascript">%s</script>"""
198
199linked_css_tag = """<link rel="stylesheet" href="%s"></link>"""
200linked_js_tag = """<script language="javascript" src="%s"></script>"""
201
Jamie Gennis92791472012-03-05 17:33:58 -0800202if __name__ == '__main__':
203 main()