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 not 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 bytecodes 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 dispatch pairs 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 """ |
41 | 44 |
42 __COUNTER_BITS = struct.calcsize("P") * 8 # Size in bits of a pointer | 45 __COUNTER_BITS = struct.calcsize("P") * 8 # Size in bits of a pointer |
43 __COUNTER_MAX = 2**__COUNTER_BITS - 1 | 46 __COUNTER_MAX = 2**__COUNTER_BITS - 1 |
44 | 47 |
45 | 48 |
46 def warn_if_counter_may_have_saturated(dispatches_table): | 49 def warn_if_counter_may_have_saturated(dispatches_table): |
47 for source, counters_from_source in dispatches_table.items(): | 50 for source, counters_from_source in dispatches_table.items(): |
48 for destination, counter in counters_from_source.items(): | 51 for destination, counter in counters_from_source.items(): |
49 if counter == __COUNTER_MAX: | 52 if counter == __COUNTER_MAX: |
50 print "WARNING: {} -> {} may have saturated.".format(source, | 53 print "WARNING: {} -> {} may have saturated.".format(source, |
51 destination) | 54 destination) |
52 | 55 |
53 | 56 |
54 def find_top_counters(dispatches_table, top_count): | 57 def find_top_bytecode_dispatch_pairs(dispatches_table, top_count): |
55 def flattened_counters_generator(): | 58 def flattened_counters_generator(): |
56 for source, counters_from_source in dispatches_table.items(): | 59 for source, counters_from_source in dispatches_table.items(): |
57 for destination, counter in counters_from_source.items(): | 60 for destination, counter in counters_from_source.items(): |
58 yield source, destination, counter | 61 yield source, destination, counter |
59 | 62 |
60 return heapq.nlargest(top_count, flattened_counters_generator(), | 63 return heapq.nlargest(top_count, flattened_counters_generator(), |
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_bytecode_dispatch_pairs(dispatches_table, top_count): |
65 top_counters = find_top_counters(dispatches_table, top_count) | 68 top_bytecode_dispatch_pairs = ( |
66 print "Top {} dispatch counters:".format(top_count) | 69 find_top_bytecode_dispatch_pairs(dispatches_table, top_count)) |
67 for source, destination, counter in top_counters: | 70 print "Top {} bytecode dispatch pairs:".format(top_count) |
| 71 for source, destination, counter in top_bytecode_dispatch_pairs: |
68 print "{:>12d}\t{} -> {}".format(counter, source, destination) | 72 print "{:>12d}\t{} -> {}".format(counter, source, destination) |
69 | 73 |
70 | 74 |
| 75 def find_top_bytecodes(dispatches_table): |
| 76 top_bytecodes = [] |
| 77 for bytecode, counters_from_bytecode in dispatches_table.items(): |
| 78 top_bytecodes.append((bytecode, sum(counters_from_bytecode.values()))) |
| 79 top_bytecodes.sort(key=lambda x: x[1], reverse=True) |
| 80 return top_bytecodes |
| 81 |
| 82 |
| 83 def print_top_bytecodes(dispatches_table): |
| 84 top_bytecodes = find_top_bytecodes(dispatches_table) |
| 85 print "Top bytecodes:" |
| 86 for bytecode, counter in top_bytecodes: |
| 87 print "{:>12d}\t{}".format(counter, bytecode) |
| 88 |
| 89 |
71 def build_counters_matrix(dispatches_table): | 90 def build_counters_matrix(dispatches_table): |
72 labels = sorted(dispatches_table.keys()) | 91 labels = sorted(dispatches_table.keys()) |
73 | 92 |
74 counters_matrix = numpy.empty([len(labels), len(labels)], dtype=int) | 93 counters_matrix = numpy.empty([len(labels), len(labels)], dtype=int) |
75 for from_index, from_name in enumerate(labels): | 94 for from_index, from_name in enumerate(labels): |
76 current_row = dispatches_table[from_name]; | 95 current_row = dispatches_table[from_name]; |
77 for to_index, to_name in enumerate(labels): | 96 for to_index, to_name in enumerate(labels): |
78 counters_matrix[from_index, to_index] = current_row.get(to_name, 0) | 97 counters_matrix[from_index, to_index] = current_row.get(to_name, 0) |
79 | 98 |
80 # Reverse y axis for a nicer appearance | 99 # Reverse y axis for a nicer appearance |
81 xlabels = labels | 100 xlabels = labels |
82 ylabels = list(reversed(xlabels)) | 101 ylabels = list(reversed(xlabels)) |
83 counters_matrix = numpy.flipud(counters_matrix) | 102 counters_matrix = numpy.flipud(counters_matrix) |
84 | 103 |
85 return counters_matrix, xlabels, ylabels | 104 return counters_matrix, xlabels, ylabels |
86 | 105 |
87 | 106 |
88 def plot_dispatches_table(dispatches_table, figure, axis): | 107 def plot_dispatches_table(dispatches_table, figure, axis): |
89 counters_matrix, xlabels, ylabels = build_counters_matrix(dispatches_table) | 108 counters_matrix, xlabels, ylabels = build_counters_matrix(dispatches_table) |
90 | 109 |
91 image = axis.pcolor( | 110 image = axis.pcolor( |
92 counters_matrix, | 111 counters_matrix, |
93 cmap='jet', | 112 cmap="jet", |
94 norm=colors.LogNorm(), | 113 norm=colors.LogNorm(), |
95 edgecolor='grey', | 114 edgecolor="grey", |
96 linestyle='dotted', | 115 linestyle="dotted", |
97 linewidth=0.5 | 116 linewidth=0.5 |
98 ) | 117 ) |
99 | 118 |
100 axis.xaxis.set( | 119 axis.xaxis.set( |
101 ticks=numpy.arange(0.5, len(xlabels)), | 120 ticks=numpy.arange(0.5, len(xlabels)), |
102 label="From bytecode handler" | 121 label="From bytecode handler" |
103 ) | 122 ) |
104 axis.xaxis.tick_top() | 123 axis.xaxis.tick_top() |
105 axis.set_xlim(0, len(xlabels)) | 124 axis.set_xlim(0, len(xlabels)) |
106 axis.set_xticklabels(xlabels, rotation='vertical') | 125 axis.set_xticklabels(xlabels, rotation="vertical") |
107 | 126 |
108 axis.yaxis.set( | 127 axis.yaxis.set( |
109 ticks=numpy.arange(0.5, len(ylabels)), | 128 ticks=numpy.arange(0.5, len(ylabels)), |
110 label="To bytecode handler", | 129 label="To bytecode handler", |
111 ticklabels=ylabels | 130 ticklabels=ylabels |
112 ) | 131 ) |
113 axis.set_ylim(0, len(ylabels)) | 132 axis.set_ylim(0, len(ylabels)) |
114 | 133 |
115 figure.colorbar( | 134 figure.colorbar( |
116 image, | 135 image, |
117 ax=axis, | 136 ax=axis, |
118 fraction=0.01, | 137 fraction=0.01, |
119 pad=0.01 | 138 pad=0.01 |
120 ) | 139 ) |
121 | 140 |
122 | 141 |
123 def parse_command_line(): | 142 def parse_command_line(): |
124 command_line_parser = argparse.ArgumentParser( | 143 command_line_parser = argparse.ArgumentParser( |
125 formatter_class=argparse.RawDescriptionHelpFormatter, | 144 formatter_class=argparse.RawDescriptionHelpFormatter, |
126 description=__DESCRIPTION, | 145 description=__DESCRIPTION, |
127 epilog=__HELP_EPILOGUE | 146 epilog=__HELP_EPILOGUE |
128 ) | 147 ) |
129 command_line_parser.add_argument( | 148 command_line_parser.add_argument( |
130 "--plot_size", "-s", | 149 "--plot-size", "-s", |
131 metavar="N", | 150 metavar="N", |
132 default=30, | 151 default=30, |
133 help="shorter side, in inches, of the output plot (default 30)" | 152 help="shorter side in inches of the output plot (default 30)" |
134 ) | 153 ) |
135 command_line_parser.add_argument( | 154 command_line_parser.add_argument( |
136 "--plot", "-p", | 155 "--plot", "-p", |
137 action="store_true", | 156 action="store_true", |
138 help="plot dispatches table heatmap" | 157 help="plot dispatch pairs heatmap" |
139 ) | 158 ) |
140 command_line_parser.add_argument( | 159 command_line_parser.add_argument( |
141 "--interactive", "-i", | 160 "--interactive", "-i", |
142 action="store_true", | 161 action="store_true", |
143 help="open an interactive viewer, rather than writing to file" | 162 help="open the heatmap in an interactive viewer, instead of writing to file" |
144 ) | 163 ) |
145 command_line_parser.add_argument( | 164 command_line_parser.add_argument( |
146 "--top_count", "-t", | 165 "--top-bytecode-dispatch-pairs", "-t", |
| 166 action="store_true", |
| 167 help="print the top bytecode dispatch pairs" |
| 168 ) |
| 169 command_line_parser.add_argument( |
| 170 "--top-bytecode-dispatch-pairs-number", "-n", |
147 metavar="N", | 171 metavar="N", |
148 type=int, | 172 type=int, |
149 default=10, | 173 default=10, |
150 help="print the top N counters (default 10)" | 174 help="print N top bytecode dispatch pairs when running with -t (default 10)" |
151 ) | 175 ) |
152 command_line_parser.add_argument( | 176 command_line_parser.add_argument( |
153 "--output_filename", "-o", | 177 "--output-filename", "-o", |
154 metavar="<output filename>", | 178 metavar="<output filename>", |
155 default="v8.ignition_dispatches_table.svg", | 179 default="v8.ignition_dispatches_table.svg", |
156 help=("file to save the plot file to. File type is deduced from the " | 180 help=("file to save the plot file to. File type is deduced from the " |
157 "extension. PDF, SVG, PNG supported") | 181 "extension. PDF, SVG, PNG supported") |
158 ) | 182 ) |
159 command_line_parser.add_argument( | 183 command_line_parser.add_argument( |
160 "input_filename", | 184 "input_filename", |
161 metavar="<input filename>", | 185 metavar="<input filename>", |
162 default="v8.ignition_dispatches_table.json", | 186 default="v8.ignition_dispatches_table.json", |
163 nargs='?', | 187 nargs='?', |
(...skipping 14 matching lines...) Expand all Loading... |
178 if program_options.plot: | 202 if program_options.plot: |
179 figure, axis = pyplot.subplots() | 203 figure, axis = pyplot.subplots() |
180 plot_dispatches_table(dispatches_table, figure, axis) | 204 plot_dispatches_table(dispatches_table, figure, axis) |
181 | 205 |
182 if program_options.interactive: | 206 if program_options.interactive: |
183 pyplot.show() | 207 pyplot.show() |
184 else: | 208 else: |
185 figure.set_size_inches(program_options.plot_size, | 209 figure.set_size_inches(program_options.plot_size, |
186 program_options.plot_size) | 210 program_options.plot_size) |
187 pyplot.savefig(program_options.output_filename) | 211 pyplot.savefig(program_options.output_filename) |
| 212 elif program_options.top_bytecode_dispatch_pairs: |
| 213 print_top_bytecode_dispatch_pairs( |
| 214 dispatches_table, program_options.top_bytecode_dispatch_pairs_number) |
188 else: | 215 else: |
189 print_top_counters(dispatches_table, program_options.top_count) | 216 print_top_bytecodes(dispatches_table) |
190 | 217 |
191 | 218 |
192 if __name__ == "__main__": | 219 if __name__ == "__main__": |
193 main() | 220 main() |
OLD | NEW |