OLD | NEW |
---|---|
(Empty) | |
1 #! /usr/bin/python | |
2 # | |
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 | |
5 # found in the LICENSE file. | |
6 # | |
7 | |
8 import argparse | |
9 import heapq | |
10 import json | |
11 import matplotlib.colors as colors | |
12 import matplotlib.pyplot as plt | |
13 import numpy as np | |
rmcilroy
2016/04/11 16:35:16
nit - don't use "as" in imports here, I think it j
Stefano Sanfilippo
2016/04/12 11:18:55
Done.
| |
14 import struct | |
15 | |
16 | |
17 __COUNTER_BITS = struct.calcsize("P") * 8 # Size in bits of a pointer | |
18 __COUNTER_MAX = 2**__COUNTER_BITS - 1 | |
19 | |
rmcilroy
2016/04/11 16:35:16
Can you add a couple of unittests (not for the mat
Stefano Sanfilippo
2016/04/12 11:18:55
The two functions that we should test are:
* warn
| |
20 | |
21 def load_counters_data(stream): | |
22 raw_data = json.load(stream) | |
23 labels = sorted(raw_data.keys()) | |
24 counters_matrix = np.empty([len(labels), len(labels)], dtype=int) | |
25 for from_index, from_name in enumerate(labels): | |
26 current_row = raw_data[from_name]; | |
27 for to_index, to_name in enumerate(labels): | |
28 counters_matrix[from_index, to_index] = current_row.get(to_name, 0) | |
29 return labels, counters_matrix | |
30 | |
31 | |
32 def build_name_for_coordinates(labels, coordinates): | |
33 return " -> ".join(labels[c] for c in coordinates) | |
34 | |
35 | |
36 def check_saturated_counters(labels, counters_matrix): | |
rmcilroy
2016/04/11 16:35:16
warn_if_counters_are_saturated
Stefano Sanfilippo
2016/04/12 11:18:55
Done.
| |
37 # np.where() returns two arrays of x and y coordinates, | |
38 # zip them together to get (x,y) tuples. | |
39 saturated_cells = zip(*np.where(counters_matrix == __COUNTER_MAX)) | |
40 for coordinates in saturated_cells: | |
41 name = build_name_for_coordinates(labels, coordinates) | |
42 print "WARNING: counter may have saturated: {}".format(name) | |
43 | |
44 | |
45 def cells_coordinates_content_generator(counters_matrix): | |
46 """Yield tuples (cell_content, (x, y)) for each cell in counters_matrix.""" | |
47 cells_iterator = np.nditer(counters_matrix, flags=['multi_index']) | |
48 while not cells_iterator.finished: | |
49 yield (cells_iterator[0], cells_iterator.multi_index) | |
50 cells_iterator.iternext() | |
51 | |
52 | |
53 def print_top_counters(labels, counters_matrix, limit): | |
54 top_counters = heapq.nlargest( | |
rmcilroy
2016/04/11 16:35:16
As discussed offline, I think this tool would be m
Stefano Sanfilippo
2016/04/12 11:18:55
Done.
| |
55 limit, | |
56 cells_coordinates_content_generator(counters_matrix), | |
57 key=lambda x: x[0] | |
58 ) | |
59 print "Top {} dispatch counters:".format(limit) | |
60 for counter_value, counter_coordinates in top_counters: | |
61 counter_name = build_name_for_coordinates(labels, counter_coordinates) | |
62 print " {:>12} {}".format(counter_value, counter_name) | |
63 | |
64 | |
65 def prepare_data_for_plot(labels, counters_matrix): | |
66 xlabels = labels | |
67 ylabels = list(reversed(xlabels)) | |
68 counters_matrix = np.flipud(counters_matrix) | |
69 return xlabels, ylabels, counters_matrix | |
70 | |
71 | |
72 def plot_handler_to_handler_counters(counters_matrix, xlabels, ylabels, figure, | |
73 axis): | |
74 image = axis.pcolor( | |
75 counters_matrix, | |
76 cmap='jet', | |
77 norm=colors.LogNorm(), | |
78 edgecolor='grey', | |
79 linestyle='dotted', | |
80 linewidth=0.5 | |
81 ) | |
82 | |
83 axis.xaxis.set( | |
84 ticks=np.arange(0.5, len(xlabels)), | |
85 label="From bytecode handler" | |
86 ) | |
87 axis.xaxis.tick_top() | |
88 axis.set_xlim(0, len(xlabels)) | |
89 axis.set_xticklabels(xlabels, rotation='vertical') | |
90 | |
91 axis.yaxis.set( | |
92 ticks=np.arange(0.5, len(ylabels)), | |
93 label="To bytecode handler", | |
94 ticklabels=ylabels | |
95 ) | |
96 axis.set_ylim(0, len(ylabels)) | |
97 | |
98 figure.colorbar( | |
99 image, | |
100 ax=axis, | |
101 fraction=0.01, | |
102 pad=0.01 | |
103 ) | |
104 | |
105 | |
106 def parse_command_line(): | |
107 command_line_parser = argparse.ArgumentParser( | |
108 description="Plot Ignition counters file.", | |
rmcilroy
2016/04/11 16:35:16
Could you add more description of what this tool i
Stefano Sanfilippo
2016/04/12 11:18:55
Done.
| |
109 ) | |
110 command_line_parser.add_argument( | |
111 "--plot_size", "-s", | |
112 metavar="N", | |
113 default=30, | |
114 help="shorter side, in inches, of the output plot", | |
115 ) | |
116 command_line_parser.add_argument( | |
117 "--interactive", "-i", | |
118 action="store_true", | |
119 help="open an interactive viewer, rather than writing to file.", | |
120 ) | |
121 command_line_parser.add_argument( | |
122 "--print_top", "-t", | |
rmcilroy
2016/04/11 16:35:16
I think you need to rename the file since this isn
Stefano Sanfilippo
2016/04/12 11:18:55
Done.
| |
123 metavar="N", | |
124 type=int, | |
125 default=0, | |
126 help="print the top N counters" | |
127 ) | |
rmcilroy
2016/04/11 16:35:16
You should probably have another option for doing
Stefano Sanfilippo
2016/04/12 11:18:55
Done.
| |
128 command_line_parser.add_argument( | |
129 "--output_filename", "-o", | |
130 metavar="<output filename>", | |
131 default="v8.ignition_dispatches_table.svg", | |
132 help=("file to save the plot file to. File type is deduced from the " | |
133 "extension. PDF, SVG, PNG supported") | |
134 ) | |
135 command_line_parser.add_argument( | |
136 "input_filename", | |
137 metavar="<input filename>", | |
138 default="v8.ignition_dispatches_table.json", | |
139 nargs='?', | |
140 help="Ignition counters JSON file", | |
141 ) | |
142 | |
143 return command_line_parser.parse_args() | |
144 | |
145 | |
146 def main(): | |
147 program_options = parse_command_line() | |
148 | |
149 with open(program_options.input_filename) as stream: | |
150 labels, counters_matrix = load_counters_data(stream) | |
151 | |
152 check_saturated_counters(labels, counters_matrix) | |
153 if program_options.print_top: | |
154 print_top_counters(labels, counters_matrix, program_options.print_top) | |
155 | |
156 xlabels, ylabels, counters_matrix = prepare_data_for_plot(labels, | |
157 counters_matrix) | |
158 | |
159 figure, axis = plt.subplots() | |
160 plot_handler_to_handler_counters(counters_matrix, xlabels, ylabels, figure, | |
161 axis) | |
162 | |
163 if program_options.interactive: | |
164 plt.show() | |
165 else: | |
166 figure.set_size_inches(program_options.plot_size, program_options.plot_size) | |
167 plt.savefig(program_options.output_filename) | |
168 | |
169 | |
170 if __name__ == "__main__": | |
171 main() | |
OLD | NEW |