blob: 98a143ff9b221028d149bcf95057b4eedeee5302 [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
Jamie Gennisbf3e6162012-04-28 19:16:49 -070013import errno, optparse, os, select, subprocess, sys, time, zlib
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()))
Jamie Gennis92791472012-03-05 17:33:58 -080052 options, args = parser.parse_args()
53
Jamie Gennis7e3783f2012-04-28 13:16:11 -070054 if options.set_tags:
55 flags = 0
56 tags = options.set_tags.split(',')
57 for tag in tags:
58 try:
59 flags |= trace_tag_bits[tag]
60 except KeyError:
61 parser.error('unrecognized tag: %s\nknown tags are: %s' %
62 (tag, ', '.join(trace_tag_bits.iterkeys())))
Jamie Gennis81e9aa72012-05-09 13:57:58 -070063 atrace_args = ['adb', 'shell', 'setprop', 'debug.atrace.tags.enableflags', hex(flags)]
Jamie Gennis7e3783f2012-04-28 13:16:11 -070064 try:
65 subprocess.check_call(atrace_args)
66 except subprocess.CalledProcessError, e:
67 print sys.stderr, 'unable to set tags: %s' % e
68 print '\nSet enabled tags to: %s\n' % ', '.join(tags)
69 print ('You will likely need to restart the Android framework for this to ' +
70 'take effect:\n\n adb shell stop\n adb shell ' +
71 'start\n')
72 return
73
Andy Stadler5bd161f2012-05-03 15:31:03 -070074 atrace_args = ['adb', 'shell', 'atrace', '-z']
Jamie Gennis98ef97d2012-03-07 16:06:53 -080075 if options.trace_cpu_freq:
76 atrace_args.append('-f')
Jamie Gennis415e5d82012-05-07 18:00:11 -070077 if options.trace_cpu_idle:
78 atrace_args.append('-i')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080079 if options.trace_cpu_load:
80 atrace_args.append('-l')
Andy Stadler5bd161f2012-05-03 15:31:03 -070081 if options.trace_cpu_sched:
82 atrace_args.append('-s')
Jamie Gennis92791472012-03-05 17:33:58 -080083 if options.trace_workqueue:
84 atrace_args.append('-w')
85 if options.trace_time is not None:
86 if options.trace_time > 0:
87 atrace_args.extend(['-t', str(options.trace_time)])
88 else:
89 parser.error('the trace time must be a positive number')
Jamie Gennis98ef97d2012-03-07 16:06:53 -080090 if options.trace_buf_size is not None:
91 if options.trace_buf_size > 0:
92 atrace_args.extend(['-b', str(options.trace_buf_size)])
93 else:
94 parser.error('the trace buffer size must be a positive number')
Jamie Gennis92791472012-03-05 17:33:58 -080095
Jamie Gennis4b56a2b2012-04-28 01:06:56 -070096 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
97 css_filename = os.path.join(script_dir, 'style.css')
98 js_filename = os.path.join(script_dir, 'script.js')
99 css = open(css_filename).read()
100 js = open(js_filename).read()
101
Jamie Gennis92791472012-03-05 17:33:58 -0800102 html_filename = options.output_file
103 html_file = open(html_filename, 'w')
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700104 html_file.write(html_prefix % (css, js))
Jamie Gennis92791472012-03-05 17:33:58 -0800105
106 trace_started = False
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700107 leftovers = ''
108 adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE,
Jamie Gennis7e3783f2012-04-28 13:16:11 -0700109 stderr=subprocess.PIPE)
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700110 dec = zlib.decompressobj()
Jamie Gennis92791472012-03-05 17:33:58 -0800111 while True:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700112 ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
113 if adb.stderr in ready[0]:
114 err = os.read(adb.stderr.fileno(), 4096)
115 sys.stderr.write(err)
116 sys.stderr.flush()
117 if adb.stdout in ready[0]:
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700118 out = leftovers + os.read(adb.stdout.fileno(), 4096)
119 out = out.replace('\r\n', '\n')
120 if out.endswith('\r'):
121 out = out[:-1]
122 leftovers = '\r'
123 else:
124 leftovers = ''
Jamie Gennis92791472012-03-05 17:33:58 -0800125 if not trace_started:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700126 lines = out.splitlines(True)
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700127 out = ''
Jamie Gennis92791472012-03-05 17:33:58 -0800128 for i, line in enumerate(lines):
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700129 if line == 'TRACE:\n':
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700130 sys.stdout.write("downloading trace...")
Jamie Gennis92791472012-03-05 17:33:58 -0800131 sys.stdout.flush()
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700132 out = ''.join(lines[i+1:])
Jamie Gennis92791472012-03-05 17:33:58 -0800133 trace_started = True
134 break
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700135 elif 'TRACE:'.startswith(line) and i == len(lines) - 1:
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700136 leftovers = line + leftovers
Jamie Gennis92791472012-03-05 17:33:58 -0800137 else:
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700138 sys.stdout.write(line)
Jamie Gennis92791472012-03-05 17:33:58 -0800139 sys.stdout.flush()
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700140 if len(out) > 0:
141 out = dec.decompress(out)
142 html_out = out.replace('\n', '\\n\\\n')
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700143 if len(html_out) > 0:
144 html_file.write(html_out)
145 result = adb.poll()
Jamie Gennis92791472012-03-05 17:33:58 -0800146 if result is not None:
147 break
148 if result != 0:
149 print sys.stderr, 'adb returned error code %d' % result
150 else:
Jamie Gennisbf3e6162012-04-28 19:16:49 -0700151 html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '')
152 if len(html_out) > 0:
153 html_file.write(html_out)
Jamie Gennis92791472012-03-05 17:33:58 -0800154 html_file.write(html_suffix)
155 html_file.close()
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700156 print " done\n\n wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
Jamie Gennis92791472012-03-05 17:33:58 -0800157
158html_prefix = """<!DOCTYPE HTML>
159<html>
160<head i18n-values="dir:textdirection;">
161<title>Android System Trace</title>
Jamie Gennis4b56a2b2012-04-28 01:06:56 -0700162<style type="text/css">%s</style>
163<script language="javascript">%s</script>
Jamie Gennis92791472012-03-05 17:33:58 -0800164<style>
165 .view {
166 overflow: hidden;
167 position: absolute;
168 top: 0;
169 bottom: 0;
170 left: 0;
171 right: 0;
172 }
173</style>
174</head>
175<body>
176 <div class="view">
177 </div>
178 <script>
179 var linuxPerfData = "\\
180"""
181
Jamie Gennis1bf4a492012-03-13 18:07:36 -0700182html_suffix = """ dummy-0000 [000] 0.0: 0: trace_event_clock_sync: parent_ts=0.0\\n";
Jamie Gennis92791472012-03-05 17:33:58 -0800183 </script>
184</body>
185</html>
186"""
187
188if __name__ == '__main__':
189 main()