blob: 14c226dc6f3173d8d6c72156f820d46575146aff [file] [log] [blame]
#!/usr/bin/env python
#
# Copyright 2012, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""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 optparse, os, subprocess, sys
def main():
parser = optparse.OptionParser()
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('-f', '--cpu-freq', dest='trace_cpu_freq', default=False,
action='store_true', help='trace CPU frequency changes')
parser.add_option('-l', '--cpu-load', dest='trace_cpu_load', default=False,
action='store_true', help='trace CPU load')
parser.add_option('-w', '--workqueue', dest='trace_workqueue', default=False,
action='store_true', help='trace the kernel workqueues')
options, args = parser.parse_args()
atrace_args = ['adb', 'shell', 'atrace', '-s']
if options.trace_cpu_freq:
atrace_args.append('-f')
if options.trace_cpu_load:
atrace_args.append('-l')
if options.trace_workqueue:
atrace_args.append('-w')
if options.trace_time is not None:
if options.trace_time > 0:
atrace_args.extend(['-t', str(options.trace_time)])
else:
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)])
else:
parser.error('the trace buffer size must be a positive number')
html_filename = options.output_file
html_file = open(html_filename, 'w')
html_file.write(html_prefix)
trace_started = False
popen = subprocess.Popen(atrace_args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
while True:
stdout, stderr = popen.communicate()
if len(stderr) > 0:
print sys.stderr, stderr
break
if len(stdout) > 0:
if not trace_started:
lines = stdout.splitlines()
for i, line in enumerate(lines):
if line == 'TRACE:':
print "downloading trace...",
sys.stdout.flush()
stdout = '\n'.join(lines[i+1:])
trace_started = True
break
else:
print line
sys.stdout.flush()
html_stdout = stdout.replace('\n', '\\n\\\n').replace('\r', '')
if len(html_stdout) > 0:
html_file.write(html_stdout)
result = popen.poll()
if result is not None:
break
if result != 0:
print sys.stderr, 'adb returned error code %d' % result
else:
html_file.write(html_suffix)
html_file.close()
print "done\n\n wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
html_prefix = """<!DOCTYPE HTML>
<html>
<head i18n-values="dir:textdirection;">
<title>Android System Trace</title>
<link rel="stylesheet" href="http://www.corp.google.com/~jgennis/android_tracing/viewer/timeline.css">
<link rel="stylesheet" href="http://www.corp.google.com/~jgennis/android_tracing/viewer/timeline_view.css">
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/shared/js/cr.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/shared/js/cr/event_target.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/shared/js/cr/ui.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/shared/js/util.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/timeline_model.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/linux_perf_importer.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/trace_event_importer.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/sorted_array_utils.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/measuring_stick.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/timeline.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/timeline_track.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/timeline_view.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/fast_rect_renderer.js"></script>
<script src="http://www.corp.google.com/~jgennis/android_tracing/0.1/viewer/test_utils.js"></script>
<style>
.view {
overflow: hidden;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div class="view">
</div>
<script>
var linuxPerfData = "\\
"""
html_suffix = """\\n\\
dummy-0000 [000] 0.0: 0: trace_event_clock_sync: parent_ts=0.0\\n";
var timelineViewEl;
function onLoad() {
reload();
}
function reload() {
if (!linuxPerfData)
return;
var m = new tracing.TimelineModel();
m.importEvents("[]", true, [linuxPerfData]);
timelineViewEl = document.querySelector('.view');
cr.ui.decorate(timelineViewEl, tracing.TimelineView);
timelineViewEl.model = m;
timelineViewEl.tabIndex = 1;
timelineViewEl.timeline.focusElement = timelineViewEl;
}
document.addEventListener('DOMContentLoaded', onLoad);
</script>
</body>
</html>
"""
if __name__ == '__main__':
main()