Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 3 | # 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 Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 6 | |
| 7 | """Android system-wide tracing utility. |
| 8 | |
| 9 | This is a tool for capturing a trace that includes data from both userland and |
| 10 | the kernel. It creates an HTML file for visualizing the trace. |
| 11 | """ |
| 12 | |
Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 13 | import errno, optparse, os, select, subprocess, sys, time, zlib, config |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 14 | |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 15 | # This list is based on the tags in frameworks/native/include/utils/Trace.h. |
| 16 | trace_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 Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 23 | 'sync': 1<<7, |
Glenn Kasten | b4fa51e | 2012-05-07 09:19:32 -0700 | [diff] [blame] | 24 | 'audio': 1<<8, |
Jamie Gennis | b7f480d | 2012-05-11 04:44:03 -0700 | [diff] [blame] | 25 | 'video': 1<<9, |
Eino-Ville Talvala | da77298 | 2012-05-31 15:49:57 -0700 | [diff] [blame^] | 26 | 'camera': 1<<10, |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 29 | def main(): |
| 30 | parser = optparse.OptionParser() |
| 31 | parser.add_option('-o', dest='output_file', help='write HTML to FILE', |
| 32 | default='trace.html', metavar='FILE') |
| 33 | parser.add_option('-t', '--time', dest='trace_time', type='int', |
| 34 | help='trace for N seconds', metavar='N') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 35 | parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int', |
| 36 | help='use a trace buffer size of N KB', metavar='N') |
Jeff Brown | a4d8b28 | 2012-05-22 18:56:09 -0700 | [diff] [blame] | 37 | parser.add_option('-d', '--disk', dest='trace_disk', default=False, |
| 38 | action='store_true', help='trace disk I/O') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 39 | parser.add_option('-f', '--cpu-freq', dest='trace_cpu_freq', default=False, |
| 40 | action='store_true', help='trace CPU frequency changes') |
Jamie Gennis | 415e5d8 | 2012-05-07 18:00:11 -0700 | [diff] [blame] | 41 | parser.add_option('-i', '--cpu-idle', dest='trace_cpu_idle', default=False, |
| 42 | action='store_true', help='trace CPU idle events') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 43 | parser.add_option('-l', '--cpu-load', dest='trace_cpu_load', default=False, |
| 44 | action='store_true', help='trace CPU load') |
Andy Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 45 | parser.add_option('-s', '--no-cpu-sched', dest='trace_cpu_sched', default=True, |
| 46 | action='store_false', help='inhibit tracing CPU ' + |
| 47 | 'scheduler (allows longer trace times by reducing data ' + |
| 48 | 'rate into buffer)') |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 49 | parser.add_option('-w', '--workqueue', dest='trace_workqueue', default=False, |
| 50 | action='store_true', help='trace the kernel workqueues') |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 51 | parser.add_option('--set-tags', dest='set_tags', action='store', |
| 52 | help='set the enabled trace tags and exit; set to a ' + |
| 53 | 'comma separated list of: ' + |
| 54 | ', '.join(trace_tag_bits.iterkeys())) |
Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 55 | parser.add_option('--link-assets', dest='link_assets', default=False, |
| 56 | action='store_true', help='link to original CSS or JS resources ' |
| 57 | 'instead of embedding them') |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 58 | options, args = parser.parse_args() |
| 59 | |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 60 | if options.set_tags: |
| 61 | flags = 0 |
| 62 | tags = options.set_tags.split(',') |
| 63 | for tag in tags: |
| 64 | try: |
| 65 | flags |= trace_tag_bits[tag] |
| 66 | except KeyError: |
| 67 | parser.error('unrecognized tag: %s\nknown tags are: %s' % |
| 68 | (tag, ', '.join(trace_tag_bits.iterkeys()))) |
Jamie Gennis | 81e9aa7 | 2012-05-09 13:57:58 -0700 | [diff] [blame] | 69 | atrace_args = ['adb', 'shell', 'setprop', 'debug.atrace.tags.enableflags', hex(flags)] |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 70 | try: |
| 71 | subprocess.check_call(atrace_args) |
| 72 | except subprocess.CalledProcessError, e: |
| 73 | print sys.stderr, 'unable to set tags: %s' % e |
| 74 | print '\nSet enabled tags to: %s\n' % ', '.join(tags) |
| 75 | print ('You will likely need to restart the Android framework for this to ' + |
| 76 | 'take effect:\n\n adb shell stop\n adb shell ' + |
| 77 | 'start\n') |
| 78 | return |
| 79 | |
Andy Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 80 | atrace_args = ['adb', 'shell', 'atrace', '-z'] |
Jeff Brown | a4d8b28 | 2012-05-22 18:56:09 -0700 | [diff] [blame] | 81 | if options.trace_disk: |
| 82 | atrace_args.append('-d') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 83 | if options.trace_cpu_freq: |
| 84 | atrace_args.append('-f') |
Jamie Gennis | 415e5d8 | 2012-05-07 18:00:11 -0700 | [diff] [blame] | 85 | if options.trace_cpu_idle: |
| 86 | atrace_args.append('-i') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 87 | if options.trace_cpu_load: |
| 88 | atrace_args.append('-l') |
Andy Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 89 | if options.trace_cpu_sched: |
| 90 | atrace_args.append('-s') |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 91 | if options.trace_workqueue: |
| 92 | atrace_args.append('-w') |
| 93 | if options.trace_time is not None: |
| 94 | if options.trace_time > 0: |
| 95 | atrace_args.extend(['-t', str(options.trace_time)]) |
| 96 | else: |
| 97 | parser.error('the trace time must be a positive number') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 98 | if options.trace_buf_size is not None: |
| 99 | if options.trace_buf_size > 0: |
| 100 | atrace_args.extend(['-b', str(options.trace_buf_size)]) |
| 101 | else: |
| 102 | parser.error('the trace buffer size must be a positive number') |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 103 | |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 104 | script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 105 | |
| 106 | if options.link_assets: |
| 107 | css = '\n'.join(linked_css_tag % (os.path.join(script_dir, f)) for f in config.css_in_files) |
| 108 | js = '\n'.join(linked_js_tag % (os.path.join(script_dir, f)) for f in config.js_in_files) |
| 109 | else: |
| 110 | css_filename = os.path.join(script_dir, config.css_out_file) |
| 111 | js_filename = os.path.join(script_dir, config.js_out_file) |
| 112 | css = compiled_css_tag % (open(css_filename).read()) |
| 113 | js = compiled_js_tag % (open(js_filename).read()) |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 114 | |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 115 | html_filename = options.output_file |
| 116 | html_file = open(html_filename, 'w') |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 117 | html_file.write(html_prefix % (css, js)) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 118 | |
| 119 | trace_started = False |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 120 | leftovers = '' |
| 121 | adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE, |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 122 | stderr=subprocess.PIPE) |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 123 | dec = zlib.decompressobj() |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 124 | while True: |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 125 | ready = select.select([adb.stdout, adb.stderr], [], [adb.stdout, adb.stderr]) |
| 126 | if adb.stderr in ready[0]: |
| 127 | err = os.read(adb.stderr.fileno(), 4096) |
| 128 | sys.stderr.write(err) |
| 129 | sys.stderr.flush() |
| 130 | if adb.stdout in ready[0]: |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 131 | out = leftovers + os.read(adb.stdout.fileno(), 4096) |
| 132 | out = out.replace('\r\n', '\n') |
| 133 | if out.endswith('\r'): |
| 134 | out = out[:-1] |
| 135 | leftovers = '\r' |
| 136 | else: |
| 137 | leftovers = '' |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 138 | if not trace_started: |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 139 | lines = out.splitlines(True) |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 140 | out = '' |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 141 | for i, line in enumerate(lines): |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 142 | if line == 'TRACE:\n': |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 143 | sys.stdout.write("downloading trace...") |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 144 | sys.stdout.flush() |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 145 | out = ''.join(lines[i+1:]) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 146 | trace_started = True |
| 147 | break |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 148 | elif 'TRACE:'.startswith(line) and i == len(lines) - 1: |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 149 | leftovers = line + leftovers |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 150 | else: |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 151 | sys.stdout.write(line) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 152 | sys.stdout.flush() |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 153 | if len(out) > 0: |
| 154 | out = dec.decompress(out) |
| 155 | html_out = out.replace('\n', '\\n\\\n') |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 156 | if len(html_out) > 0: |
| 157 | html_file.write(html_out) |
| 158 | result = adb.poll() |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 159 | if result is not None: |
| 160 | break |
| 161 | if result != 0: |
| 162 | print sys.stderr, 'adb returned error code %d' % result |
| 163 | else: |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 164 | html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '') |
| 165 | if len(html_out) > 0: |
| 166 | html_file.write(html_out) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 167 | html_file.write(html_suffix) |
| 168 | html_file.close() |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 169 | print " done\n\n wrote file://%s/%s\n" % (os.getcwd(), options.output_file) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 170 | |
| 171 | html_prefix = """<!DOCTYPE HTML> |
| 172 | <html> |
| 173 | <head i18n-values="dir:textdirection;"> |
| 174 | <title>Android System Trace</title> |
Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 175 | %s |
| 176 | %s |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 177 | <style> |
| 178 | .view { |
| 179 | overflow: hidden; |
| 180 | position: absolute; |
| 181 | top: 0; |
| 182 | bottom: 0; |
| 183 | left: 0; |
| 184 | right: 0; |
| 185 | } |
| 186 | </style> |
| 187 | </head> |
| 188 | <body> |
| 189 | <div class="view"> |
| 190 | </div> |
| 191 | <script> |
| 192 | var linuxPerfData = "\\ |
| 193 | """ |
| 194 | |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 195 | html_suffix = """ dummy-0000 [000] 0.0: 0: trace_event_clock_sync: parent_ts=0.0\\n"; |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 196 | </script> |
| 197 | </body> |
| 198 | </html> |
| 199 | """ |
| 200 | |
Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 201 | compiled_css_tag = """<style type="text/css">%s</style>""" |
| 202 | compiled_js_tag = """<script language="javascript">%s</script>""" |
| 203 | |
| 204 | linked_css_tag = """<link rel="stylesheet" href="%s"></link>""" |
| 205 | linked_js_tag = """<script language="javascript" src="%s"></script>""" |
| 206 | |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 207 | if __name__ == '__main__': |
| 208 | main() |