OLD | NEW |
---|---|
1 #! /usr/bin/python | 1 #! /usr/bin/python |
2 # | 2 # |
3 # Copyright 2016 the V8 project authors. All rights reserved. | 3 # Copyright 2016 the V8 project authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 # | 6 # |
7 | 7 |
8 import argparse | 8 import argparse |
9 import heapq | 9 import heapq |
10 import json | 10 import json |
11 from matplotlib import colors | 11 from matplotlib import colors |
12 from matplotlib import pyplot | 12 from matplotlib import pyplot |
13 import numpy | 13 import numpy |
14 import struct | 14 import struct |
15 | 15 |
16 | 16 |
17 __DESCRIPTION = """ | 17 __DESCRIPTION = """ |
18 Process v8.ignition_dispatches_counters.json and list top counters, | 18 Process v8.ignition_dispatches_counters.json and list top counters, |
19 or plot a dispatch heatmap. | 19 or plot a dispatch heatmap. |
20 | |
21 Please note that those handlers that may or will never dispatch | |
22 (e.g. Return or Throw) do not show up in the results. | |
20 """ | 23 """ |
21 | 24 |
22 | 25 |
23 __HELP_EPILOGUE = """ | 26 __HELP_EPILOGUE = """ |
24 examples: | 27 examples: |
25 # Print the top 10 counters, reading from default filename | 28 # Print the hottest dispatch counters in descending order, reading from |
26 # v8.ignition_dispatches_counters.json (default mode) | 29 # default filename v8.ignition_dispatches_counters.json (default mode) |
27 $ tools/ignition/bytecode_dispatches_report.py | 30 $ tools/ignition/bytecode_dispatches_report.py |
28 | 31 |
29 # Print the top 15 counters reading from data.json | 32 # Print the hottest 15 bytecode handlers reading from data.json |
30 $ tools/ignition/bytecode_dispatches_report.py -t 15 data.json | 33 $ tools/ignition/bytecode_dispatches_report.py -t -n 15 data.json |
31 | 34 |
32 # Save heatmap to default filename v8.ignition_dispatches_counters.svg | 35 # Save heatmap to default filename v8.ignition_dispatches_counters.svg |
33 $ tools/ignition/bytecode_dispatches_report.py -p | 36 $ tools/ignition/bytecode_dispatches_report.py -p |
34 | 37 |
35 # Save heatmap to filename data.svg | 38 # Save heatmap to filename data.svg |
36 $ tools/ignition/bytecode_dispatches_report.py -p -o data.svg | 39 $ tools/ignition/bytecode_dispatches_report.py -p -o data.svg |
37 | 40 |
38 # Open the heatmap in an interactive viewer | 41 # Open the heatmap in an interactive viewer |
39 $ tools/ignition/bytecode_dispatches_report.py -p -i | 42 $ tools/ignition/bytecode_dispatches_report.py -p -i |
40 """ | 43 """ |
(...skipping 20 matching lines...) Expand all Loading... | |
61 key=lambda x: x[2]) | 64 key=lambda x: x[2]) |
62 | 65 |
63 | 66 |
64 def print_top_counters(dispatches_table, top_count): | 67 def print_top_counters(dispatches_table, top_count): |
65 top_counters = find_top_counters(dispatches_table, top_count) | 68 top_counters = find_top_counters(dispatches_table, top_count) |
66 print "Top {} dispatch counters:".format(top_count) | 69 print "Top {} dispatch counters:".format(top_count) |
67 for source, destination, counter in top_counters: | 70 for source, destination, counter in top_counters: |
68 print "{:>12d}\t{} -> {}".format(counter, source, destination) | 71 print "{:>12d}\t{} -> {}".format(counter, source, destination) |
69 | 72 |
70 | 73 |
74 def find_top_bytecodes(dispatches_table): | |
75 top_bytecodes = [] | |
76 for bytecode, counters_from_bytecode in dispatches_table.items(): | |
77 top_bytecodes.append((bytecode, sum(counters_from_bytecode.values()))) | |
78 top_bytecodes.sort(key=lambda x: x[1], reverse=True) | |
79 return top_bytecodes | |
80 | |
81 | |
82 def print_top_bytecodes(dispatches_table): | |
83 top_bytecodes = find_top_bytecodes(dispatches_table) | |
84 print "Top bytecodes:" | |
85 for bytecode, counter in top_bytecodes: | |
86 print "{:>12d}\t{}".format(counter, bytecode) | |
87 | |
88 | |
71 def build_counters_matrix(dispatches_table): | 89 def build_counters_matrix(dispatches_table): |
72 labels = sorted(dispatches_table.keys()) | 90 labels = sorted(dispatches_table.keys()) |
73 | 91 |
74 counters_matrix = numpy.empty([len(labels), len(labels)], dtype=int) | 92 counters_matrix = numpy.empty([len(labels), len(labels)], dtype=int) |
75 for from_index, from_name in enumerate(labels): | 93 for from_index, from_name in enumerate(labels): |
76 current_row = dispatches_table[from_name]; | 94 current_row = dispatches_table[from_name]; |
77 for to_index, to_name in enumerate(labels): | 95 for to_index, to_name in enumerate(labels): |
78 counters_matrix[from_index, to_index] = current_row.get(to_name, 0) | 96 counters_matrix[from_index, to_index] = current_row.get(to_name, 0) |
79 | 97 |
80 # Reverse y axis for a nicer appearance | 98 # Reverse y axis for a nicer appearance |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 ) | 138 ) |
121 | 139 |
122 | 140 |
123 def parse_command_line(): | 141 def parse_command_line(): |
124 command_line_parser = argparse.ArgumentParser( | 142 command_line_parser = argparse.ArgumentParser( |
125 formatter_class=argparse.RawDescriptionHelpFormatter, | 143 formatter_class=argparse.RawDescriptionHelpFormatter, |
126 description=__DESCRIPTION, | 144 description=__DESCRIPTION, |
127 epilog=__HELP_EPILOGUE | 145 epilog=__HELP_EPILOGUE |
128 ) | 146 ) |
129 command_line_parser.add_argument( | 147 command_line_parser.add_argument( |
130 "--plot_size", "-s", | 148 "--plot-size", "-s", |
131 metavar="N", | 149 metavar="N", |
132 default=30, | 150 default=30, |
133 help="shorter side, in inches, of the output plot (default 30)" | 151 help="shorter side in inches of the output plot (default 30)" |
134 ) | 152 ) |
135 command_line_parser.add_argument( | 153 command_line_parser.add_argument( |
136 "--plot", "-p", | 154 "--plot", "-p", |
137 action="store_true", | 155 action="store_true", |
138 help="plot dispatches table heatmap" | 156 help="plot dispatches table heatmap" |
139 ) | 157 ) |
140 command_line_parser.add_argument( | 158 command_line_parser.add_argument( |
141 "--interactive", "-i", | 159 "--interactive", "-i", |
142 action="store_true", | 160 action="store_true", |
143 help="open an interactive viewer, rather than writing to file" | 161 help="open an interactive viewer, instead of writing to file" |
144 ) | 162 ) |
145 command_line_parser.add_argument( | 163 command_line_parser.add_argument( |
146 "--top_count", "-t", | 164 "--top-counters", "-t", |
rmcilroy
2016/04/19 11:18:19
This is still not a good name - what do counters m
Stefano Sanfilippo
2016/04/19 13:10:33
Done.
| |
165 action="store_true", | |
166 help="print the top counters" | |
167 ) | |
168 command_line_parser.add_argument( | |
169 "--top-counters-number", "-n", | |
147 metavar="N", | 170 metavar="N", |
148 type=int, | 171 type=int, |
149 default=10, | 172 default=10, |
150 help="print the top N counters (default 10)" | 173 help="print N top counters when running with -t (default 10)" |
151 ) | 174 ) |
152 command_line_parser.add_argument( | 175 command_line_parser.add_argument( |
153 "--output_filename", "-o", | 176 "--output-filename", "-o", |
154 metavar="<output filename>", | 177 metavar="<output filename>", |
155 default="v8.ignition_dispatches_table.svg", | 178 default="v8.ignition_dispatches_table.svg", |
156 help=("file to save the plot file to. File type is deduced from the " | 179 help=("file to save the plot file to. File type is deduced from the " |
157 "extension. PDF, SVG, PNG supported") | 180 "extension. PDF, SVG, PNG supported") |
158 ) | 181 ) |
159 command_line_parser.add_argument( | 182 command_line_parser.add_argument( |
160 "input_filename", | 183 "input_filename", |
161 metavar="<input filename>", | 184 metavar="<input filename>", |
162 default="v8.ignition_dispatches_table.json", | 185 default="v8.ignition_dispatches_table.json", |
163 nargs='?', | 186 nargs='?', |
(...skipping 14 matching lines...) Expand all Loading... | |
178 if program_options.plot: | 201 if program_options.plot: |
179 figure, axis = pyplot.subplots() | 202 figure, axis = pyplot.subplots() |
180 plot_dispatches_table(dispatches_table, figure, axis) | 203 plot_dispatches_table(dispatches_table, figure, axis) |
181 | 204 |
182 if program_options.interactive: | 205 if program_options.interactive: |
183 pyplot.show() | 206 pyplot.show() |
184 else: | 207 else: |
185 figure.set_size_inches(program_options.plot_size, | 208 figure.set_size_inches(program_options.plot_size, |
186 program_options.plot_size) | 209 program_options.plot_size) |
187 pyplot.savefig(program_options.output_filename) | 210 pyplot.savefig(program_options.output_filename) |
211 elif program_options.top_counters: | |
212 print_top_counters(dispatches_table, program_options.top_counters_number) | |
188 else: | 213 else: |
189 print_top_counters(dispatches_table, program_options.top_count) | 214 print_top_bytecodes(dispatches_table) |
190 | 215 |
191 | 216 |
192 if __name__ == "__main__": | 217 if __name__ == "__main__": |
193 main() | 218 main() |
OLD | NEW |