blob: 41d0cba5d9bfb95c561d9c90eeffd5d4adebbf18 [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Android system-wide tracing utility.
This is a tool for capturing a trace that includes data from both userland and
the kernel. It creates an HTML file for visualizing the trace.
import errno, optparse, os, select, subprocess, sys, time, zlib
flattened_css_file = 'style.css'
flattened_js_file = 'script.js'
class OptionParserIgnoreErrors(optparse.OptionParser):
def error(self, msg):
def exit(self):
def print_usage(self):
def print_help(self):
def print_version(self):
def get_device_sdk_version():
getprop_args = ['adb', 'shell', 'getprop', '']
parser = OptionParserIgnoreErrors()
parser.add_option('-e', '--serial', dest='device_serial', type='string')
options, args = parser.parse_args()
if options.device_serial is not None:
getprop_args[1:1] = ['-s', options.device_serial]
version = subprocess.check_output(getprop_args)
return int(version)
def add_adb_serial(command, serial):
if serial != None:
command.insert(1, serial)
command.insert(1, '-s')
def main():
device_sdk_version = get_device_sdk_version()
if device_sdk_version < 18:
legacy_script = os.path.join(os.path.dirname(sys.argv[0]), '')
os.execv(legacy_script, sys.argv)
usage = "Usage: %prog [options] [category1 [category2 ...]]"
desc = "Example: %prog -b 32768 -t 15 gfx input view sched freq"
parser = optparse.OptionParser(usage=usage, description=desc)
parser.add_option('-o', dest='output_file', help='write HTML to FILE',
default='trace.html', metavar='FILE')
parser.add_option('-t', '--time', dest='trace_time', type='int',
help='trace for N seconds', metavar='N')
parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int',
help='use a trace buffer size of N KB', metavar='N')
parser.add_option('-k', '--ktrace', dest='kfuncs', action='store',
help='specify a comma-separated list of kernel functions to trace')
parser.add_option('-l', '--list-categories', dest='list_categories', default=False,
action='store_true', help='list the available categories and exit')
parser.add_option('-a', '--app', dest='app_name', default=None, type='string',
action='store', help='enable application-level tracing for comma-separated ' +
'list of app cmdlines')
parser.add_option('--link-assets', dest='link_assets', default=False,
action='store_true', help='link to original CSS or JS resources '
'instead of embedding them')
parser.add_option('--from-file', dest='from_file', action='store',
help='read the trace from a file (compressed) rather than running a live trace')
parser.add_option('--asset-dir', dest='asset_dir', default='trace-viewer',
type='string', help='')
parser.add_option('-e', '--serial', dest='device_serial', type='string',
help='adb device serial number')
options, args = parser.parse_args()
if options.list_categories:
atrace_args = ['adb', 'shell', 'atrace', '--list_categories']
expect_trace = False
elif options.from_file is not None:
atrace_args = ['cat', options.from_file]
expect_trace = True
atrace_args = ['adb', 'shell', 'atrace', '-z']
expect_trace = True
if options.trace_time is not None:
if options.trace_time > 0:
atrace_args.extend(['-t', str(options.trace_time)])
parser.error('the trace time must be a positive number')
if options.trace_buf_size is not None:
if options.trace_buf_size > 0:
atrace_args.extend(['-b', str(options.trace_buf_size)])
parser.error('the trace buffer size must be a positive number')
if options.app_name is not None:
atrace_args.extend(['-a', options.app_name])
if options.kfuncs is not None:
atrace_args.extend(['-k', options.kfuncs])
if atrace_args[0] == 'adb':
add_adb_serial(atrace_args, options.device_serial)
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
if options.link_assets:
src_dir = os.path.join(script_dir, options.asset_dir, 'src')
build_dir = os.path.join(script_dir, options.asset_dir, 'build')
js_files, js_flattenizer, css_files = get_assets(src_dir, build_dir)
css = '\n'.join(linked_css_tag % (os.path.join(src_dir, f)) for f in css_files)
js = '<script language="javascript">\n%s</script>\n' % js_flattenizer
js += '\n'.join(linked_js_tag % (os.path.join(src_dir, f)) for f in js_files)
css_filename = os.path.join(script_dir, flattened_css_file)
js_filename = os.path.join(script_dir, flattened_js_file)
css = compiled_css_tag % (open(css_filename).read())
js = compiled_js_tag % (open(js_filename).read())
html_filename = options.output_file
adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE,
result = None
data = []
# Read the text portion of the output and watch for the 'TRACE:' marker that
# indicates the start of the trace data.
while result is None:
ready =[adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
if adb.stderr in ready[0]:
err =, 4096)
if adb.stdout in ready[0]:
out =, 4096)
parts = out.split('\nTRACE:', 1)
txt = parts[0].replace('\r', '')
if len(parts) == 2:
# The '\nTRACE:' match stole the last newline from the text, so add it
# back here.
txt += '\n'
if len(parts) == 2:
sys.stdout.write("downloading trace...")
result = adb.poll()
# Read and buffer the data portion of the output.
while True:
ready =[adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr])
keepReading = False
if adb.stderr in ready[0]:
err =, 4096)
if len(err) > 0:
keepReading = True
if adb.stdout in ready[0]:
out =, 4096)
if len(out) > 0:
keepReading = True
if result is not None and not keepReading:
result = adb.poll()
if result == 0:
if expect_trace:
data = ''.join(data)
# Collapse CRLFs that are added by adb shell.
if data.startswith('\r\n'):
data = data.replace('\r\n', '\n')
# Skip the initial newline.
data = data[1:]
if not data:
print >> sys.stderr, ('No data was captured. Output file was not ' +
# Indicate to the user that the data download is complete.
print " done\n"
html_file = open(html_filename, 'w')
html_file.write(html_prefix % (css, js))
size = 4096
dec = zlib.decompressobj()
for chunk in (data[i:i+size] for i in xrange(0, len(data), size)):
decoded_chunk = dec.decompress(chunk)
html_chunk = decoded_chunk.replace('\n', '\\n\\\n')
html_out = dec.flush().replace('\n', '\\n\\\n')
print "\n wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
else: # i.e. result != 0
print >> sys.stderr, 'adb returned error code %d' % result
def get_assets(src_dir, build_dir):
gen = __import__('generate_standalone_timeline_view', {}, {})
parse_deps = __import__('parse_deps', {}, {})
filenames = gen._get_input_filenames()
load_sequence = parse_deps.calc_load_sequence(filenames, src_dir)
js_files = []
js_flattenizer = "window.FLATTENED = {};\n"
css_files = []
for module in load_sequence:
js_files.append(os.path.relpath(module.filename, src_dir))
js_flattenizer += "window.FLATTENED['%s'] = true;\n" %
for style_sheet in module.style_sheets:
css_files.append(os.path.relpath(style_sheet.filename, src_dir))
return (js_files, js_flattenizer, css_files)
html_prefix = """<!DOCTYPE HTML>
<head i18n-values="dir:textdirection;">
<meta charset="utf-8"/>
<title>Android System Trace</title>
<script language="javascript">
document.addEventListener('DOMContentLoaded', function() {
if (!linuxPerfData)
var m = new tracing.Model(linuxPerfData);
var timelineViewEl = document.querySelector('.view');
tracing.ui.decorate(timelineViewEl, tracing.TimelineView);
timelineViewEl.model = m;
timelineViewEl.tabIndex = 1;
timelineViewEl.timeline.focusElement = timelineViewEl;
.view {
overflow: hidden;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
<div class="view">
<!-- BEGIN TRACE -->
var linuxPerfData = "\\
html_suffix = """\\n";
<!-- END TRACE -->
compiled_css_tag = """<style type="text/css">%s</style>"""
compiled_js_tag = """<script language="javascript">%s</script>"""
linked_css_tag = """<link rel="stylesheet" href="%s"></link>"""
linked_js_tag = """<script language="javascript" src="%s"></script>"""
if __name__ == '__main__':