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 | |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 13 | import errno, optparse, os, select, subprocess, sys, time, zlib |
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, |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 28 | def 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 Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 34 | 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 Gennis | 415e5d8 | 2012-05-07 18:00:11 -0700 | [diff] [blame] | 38 | parser.add_option('-i', '--cpu-idle', dest='trace_cpu_idle', default=False, |
| 39 | action='store_true', help='trace CPU idle events') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 40 | parser.add_option('-l', '--cpu-load', dest='trace_cpu_load', default=False, |
| 41 | action='store_true', help='trace CPU load') |
Andy Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 42 | 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 Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 46 | parser.add_option('-w', '--workqueue', dest='trace_workqueue', default=False, |
| 47 | action='store_true', help='trace the kernel workqueues') |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 48 | 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 Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 52 | options, args = parser.parse_args() |
| 53 | |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 54 | 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 Gennis | 81e9aa7 | 2012-05-09 13:57:58 -0700 | [diff] [blame] | 63 | atrace_args = ['adb', 'shell', 'setprop', 'debug.atrace.tags.enableflags', hex(flags)] |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 64 | 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 Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 74 | atrace_args = ['adb', 'shell', 'atrace', '-z'] |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 75 | if options.trace_cpu_freq: |
| 76 | atrace_args.append('-f') |
Jamie Gennis | 415e5d8 | 2012-05-07 18:00:11 -0700 | [diff] [blame] | 77 | if options.trace_cpu_idle: |
| 78 | atrace_args.append('-i') |
Jamie Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 79 | if options.trace_cpu_load: |
| 80 | atrace_args.append('-l') |
Andy Stadler | 5bd161f | 2012-05-03 15:31:03 -0700 | [diff] [blame] | 81 | if options.trace_cpu_sched: |
| 82 | atrace_args.append('-s') |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 83 | 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 Gennis | 98ef97d | 2012-03-07 16:06:53 -0800 | [diff] [blame] | 90 | 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 Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 95 | |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 96 | 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 Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 102 | html_filename = options.output_file |
| 103 | html_file = open(html_filename, 'w') |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 104 | html_file.write(html_prefix % (css, js)) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 105 | |
| 106 | trace_started = False |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 107 | leftovers = '' |
| 108 | adb = subprocess.Popen(atrace_args, stdout=subprocess.PIPE, |
Jamie Gennis | 7e3783f | 2012-04-28 13:16:11 -0700 | [diff] [blame] | 109 | stderr=subprocess.PIPE) |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 110 | dec = zlib.decompressobj() |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 111 | while True: |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 112 | 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 Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 118 | 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 Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 125 | if not trace_started: |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 126 | lines = out.splitlines(True) |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 127 | out = '' |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 128 | for i, line in enumerate(lines): |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 129 | if line == 'TRACE:\n': |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 130 | sys.stdout.write("downloading trace...") |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 131 | sys.stdout.flush() |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 132 | out = ''.join(lines[i+1:]) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 133 | trace_started = True |
| 134 | break |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 135 | elif 'TRACE:'.startswith(line) and i == len(lines) - 1: |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 136 | leftovers = line + leftovers |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 137 | else: |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 138 | sys.stdout.write(line) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 139 | sys.stdout.flush() |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 140 | if len(out) > 0: |
| 141 | out = dec.decompress(out) |
| 142 | html_out = out.replace('\n', '\\n\\\n') |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 143 | if len(html_out) > 0: |
| 144 | html_file.write(html_out) |
| 145 | result = adb.poll() |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 146 | if result is not None: |
| 147 | break |
| 148 | if result != 0: |
| 149 | print sys.stderr, 'adb returned error code %d' % result |
| 150 | else: |
Jamie Gennis | bf3e616 | 2012-04-28 19:16:49 -0700 | [diff] [blame] | 151 | html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '') |
| 152 | if len(html_out) > 0: |
| 153 | html_file.write(html_out) |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 154 | html_file.write(html_suffix) |
| 155 | html_file.close() |
Jamie Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 156 | 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] | 157 | |
| 158 | html_prefix = """<!DOCTYPE HTML> |
| 159 | <html> |
| 160 | <head i18n-values="dir:textdirection;"> |
| 161 | <title>Android System Trace</title> |
Jamie Gennis | 4b56a2b | 2012-04-28 01:06:56 -0700 | [diff] [blame] | 162 | <style type="text/css">%s</style> |
| 163 | <script language="javascript">%s</script> |
Jamie Gennis | 9279147 | 2012-03-05 17:33:58 -0800 | [diff] [blame] | 164 | <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 Gennis | 1bf4a49 | 2012-03-13 18:07:36 -0700 | [diff] [blame] | 182 | 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] | 183 | </script> |
| 184 | </body> |
| 185 | </html> |
| 186 | """ |
| 187 | |
| 188 | if __name__ == '__main__': |
| 189 | main() |