util: Teach the m5 utility's scons how to run unit tests.
[gem5.git] / util / o3-pipeview.py
1 #! /usr/bin/env python3
2
3 # Copyright (c) 2011 ARM Limited
4 # All rights reserved
5 #
6 # The license below extends only to copyright in the software and shall
7 # not be construed as granting a license to any other intellectual
8 # property including but not limited to intellectual property relating
9 # to a hardware implementation of the functionality of the software
10 # licensed hereunder. You may use the software subject to the license
11 # terms below provided that you ensure that this notice is replicated
12 # unmodified and in its entirety in all distributions of the software,
13 # modified or unmodified, in source code or in binary form.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions are
17 # met: redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer;
19 # redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution;
22 # neither the name of the copyright holders nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 # Pipeline activity viewer for the O3 CPU model.
39
40 import optparse
41 import os
42 import sys
43 import copy
44
45 # Temporary storage for instructions. The queue is filled in out-of-order
46 # until it reaches 'max_threshold' number of instructions. It is then
47 # sorted out and instructions are printed out until their number drops to
48 # 'min_threshold'.
49 # It is assumed that the instructions are not out of order for more then
50 # 'min_threshold' places - otherwise they will appear out of order.
51 insts = {
52 'queue': [] , # Instructions to print.
53 'max_threshold':2000, # Instructions are sorted out and printed when
54 # their number reaches this threshold.
55 'min_threshold':1000, # Printing stops when this number is reached.
56 'sn_start':0, # The first instruction seq. number to be printed.
57 'sn_stop':0, # The last instruction seq. number to be printed.
58 'tick_start':0, # The first tick to be printed
59 'tick_stop':0, # The last tick to be printed
60 'tick_drift':2000, # Used to calculate the start and the end of main
61 # loop. We assume here that the instructions are not
62 # out of order for more then 2000 CPU ticks,
63 # otherwise the print may not start/stop
64 # at the time specified by tick_start/stop.
65 'only_committed':0, # Set if only committed instructions are printed.
66 }
67
68 def process_trace(trace, outfile, cycle_time, width, color, timestamps,
69 committed_only, store_completions, start_tick, stop_tick, start_sn, stop_sn):
70 global insts
71
72 insts['sn_start'] = start_sn
73 insts['sn_stop'] = stop_sn
74 insts['tick_start'] = start_tick
75 insts['tick_stop'] = stop_tick
76 insts['tick_drift'] = insts['tick_drift'] * cycle_time
77 insts['only_committed'] = committed_only
78 line = None
79 fields = None
80
81 # Skip lines up to the starting tick
82 if start_tick != 0:
83 while True:
84 line = trace.readline()
85 if not line: return
86 fields = line.split(':')
87 if fields[0] != 'O3PipeView': continue
88 if int(fields[2]) >= start_tick: break
89 elif start_sn != 0:
90 while True:
91 line = trace.readline()
92 if not line: return
93 fields = line.split(':')
94 if fields[0] != 'O3PipeView': continue
95 if fields[1] == 'fetch' and int(fields[5]) >= start_sn: break
96 else:
97 line = trace.readline()
98 if not line: return
99 fields = line.split(':')
100
101 # Skip lines up to next instruction fetch
102 while fields[0] != 'O3PipeView' or fields[1] != 'fetch':
103 line = trace.readline()
104 if not line: return
105 fields = line.split(':')
106
107 # Print header
108 outfile.write('// f = fetch, d = decode, n = rename, p = dispatch, '
109 'i = issue, c = complete, r = retire')
110
111 if store_completions:
112 outfile.write(', s = store-complete')
113 outfile.write('\n\n')
114
115 outfile.write(' ' + 'timeline'.center(width) +
116 ' ' + 'tick'.center(15) +
117 ' ' + 'pc.upc'.center(12) +
118 ' ' + 'disasm'.ljust(25) +
119 ' ' + 'seq_num'.center(10))
120 if timestamps:
121 outfile.write('timestamps'.center(25))
122 outfile.write('\n')
123
124 # Region of interest
125 curr_inst = {}
126 while True:
127 if fields[0] == 'O3PipeView':
128 curr_inst[fields[1]] = int(fields[2])
129 if fields[1] == 'fetch':
130 if ((stop_tick > 0 and int(fields[2]) > stop_tick+insts['tick_drift']) or
131 (stop_sn > 0 and int(fields[5]) > (stop_sn+insts['max_threshold']))):
132 print_insts(outfile, cycle_time, width, color, timestamps, 0)
133 return
134 (curr_inst['pc'], curr_inst['upc']) = fields[3:5]
135 curr_inst['sn'] = int(fields[5])
136 curr_inst['disasm'] = ' '.join(fields[6][:-1].split())
137 elif fields[1] == 'retire':
138 if curr_inst['retire'] == 0:
139 curr_inst['disasm'] = '-----' + curr_inst['disasm']
140 if store_completions:
141 curr_inst[fields[3]] = int(fields[4])
142 queue_inst(outfile, curr_inst, cycle_time, width, color, timestamps, store_completions)
143
144 line = trace.readline()
145 if not line:
146 print_insts(outfile, cycle_time, width, color, timestamps, store_completions, 0)
147 return
148 fields = line.split(':')
149
150
151 #Sorts out instructions according to sequence number
152 def compare_by_sn(a, b):
153 return cmp(a['sn'], b['sn'])
154
155 # Puts new instruction into the print queue.
156 # Sorts out and prints instructions when their number reaches threshold value
157 def queue_inst(outfile, inst, cycle_time, width, color, timestamps, store_completions):
158 global insts
159 l_copy = copy.deepcopy(inst)
160 insts['queue'].append(l_copy)
161 if len(insts['queue']) > insts['max_threshold']:
162 print_insts(outfile, cycle_time, width, color, timestamps, store_completions, insts['min_threshold'])
163
164 # Sorts out and prints instructions in print queue
165 def print_insts(outfile, cycle_time, width, color, timestamps, store_completions, lower_threshold):
166 global insts
167 insts['queue'].sort(compare_by_sn)
168 while len(insts['queue']) > lower_threshold:
169 print_item=insts['queue'].pop(0)
170 # As the instructions are processed out of order the main loop starts
171 # earlier then specified by start_sn/tick and finishes later then what
172 # is defined in stop_sn/tick.
173 # Therefore, here we have to filter out instructions that reside out of
174 # the specified boundaries.
175 if (insts['sn_start'] > 0 and print_item['sn'] < insts['sn_start']):
176 continue; # earlier then the starting sequence number
177 if (insts['sn_stop'] > 0 and print_item['sn'] > insts['sn_stop']):
178 continue; # later then the ending sequence number
179 if (insts['tick_start'] > 0 and print_item['fetch'] < insts['tick_start']):
180 continue; # earlier then the starting tick number
181 if (insts['tick_stop'] > 0 and print_item['fetch'] > insts['tick_stop']):
182 continue; # later then the ending tick number
183
184 if (insts['only_committed'] != 0 and print_item['retire'] == 0):
185 continue; # retire is set to zero if it hasn't been completed
186 print_inst(outfile, print_item, cycle_time, width, color, timestamps, store_completions)
187
188 # Prints a single instruction
189 def print_inst(outfile, inst, cycle_time, width, color, timestamps, store_completions):
190 if color:
191 from m5.util.terminal import termcap
192 else:
193 from m5.util.terminal import no_termcap as termcap
194 # Pipeline stages
195 stages = [{'name': 'fetch',
196 'color': termcap.Blue + termcap.Reverse,
197 'shorthand': 'f'},
198 {'name': 'decode',
199 'color': termcap.Yellow + termcap.Reverse,
200 'shorthand': 'd'},
201 {'name': 'rename',
202 'color': termcap.Magenta + termcap.Reverse,
203 'shorthand': 'n'},
204 {'name': 'dispatch',
205 'color': termcap.Green + termcap.Reverse,
206 'shorthand': 'p'},
207 {'name': 'issue',
208 'color': termcap.Red + termcap.Reverse,
209 'shorthand': 'i'},
210 {'name': 'complete',
211 'color': termcap.Cyan + termcap.Reverse,
212 'shorthand': 'c'},
213 {'name': 'retire',
214 'color': termcap.Blue + termcap.Reverse,
215 'shorthand': 'r'}
216 ]
217 if store_completions:
218 stages.append(
219 {'name': 'store',
220 'color': termcap.Yellow + termcap.Reverse,
221 'shorthand': 's'})
222
223 # Print
224
225 time_width = width * cycle_time
226 base_tick = (inst['fetch'] / time_width) * time_width
227
228 # Find out the time of the last event - it may not
229 # be 'retire' if the instruction is not comlpeted.
230 last_event_time = max(inst['fetch'], inst['decode'],inst['rename'],
231 inst['dispatch'],inst['issue'], inst['complete'], inst['retire'])
232 if store_completions:
233 last_event_time = max(last_event_time, inst['store'])
234
235 # Timeline shorter then time_width is printed in compact form where
236 # the print continues at the start of the same line.
237 if ((last_event_time - inst['fetch']) < time_width):
238 num_lines = 1 # compact form
239 else:
240 num_lines = ((last_event_time - base_tick) / time_width) + 1
241
242 curr_color = termcap.Normal
243
244 # This will visually distinguish completed and abandoned intructions.
245 if inst['retire'] == 0: dot = '=' # abandoned instruction
246 else: dot = '.' # completed instruction
247
248 for i in range(num_lines):
249 start_tick = base_tick + i * time_width
250 end_tick = start_tick + time_width
251 if num_lines == 1: # compact form
252 end_tick += (inst['fetch'] - base_tick)
253 events = []
254 for stage_idx in range(len(stages)):
255 tick = inst[stages[stage_idx]['name']]
256 if tick != 0:
257 if tick >= start_tick and tick < end_tick:
258 events.append((tick % time_width,
259 stages[stage_idx]['name'],
260 stage_idx, tick))
261 events.sort()
262 outfile.write('[')
263 pos = 0
264 if num_lines == 1 and events[0][2] != 0: # event is not fetch
265 curr_color = stages[events[0][2] - 1]['color']
266 for event in events:
267 if (stages[event[2]]['name'] == 'dispatch' and
268 inst['dispatch'] == inst['issue']):
269 continue
270 outfile.write(curr_color + dot * ((event[0] / cycle_time) - pos))
271 outfile.write(stages[event[2]]['color'] +
272 stages[event[2]]['shorthand'])
273
274 if event[3] != last_event_time: # event is not the last one
275 curr_color = stages[event[2]]['color']
276 else:
277 curr_color = termcap.Normal
278
279 pos = (event[0] / cycle_time) + 1
280 outfile.write(curr_color + dot * (width - pos) + termcap.Normal +
281 ']-(' + str(base_tick + i * time_width).rjust(15) + ') ')
282 if i == 0:
283 outfile.write('%s.%s %s [%s]' % (
284 inst['pc'].rjust(10),
285 inst['upc'],
286 inst['disasm'].ljust(25),
287 str(inst['sn']).rjust(10)))
288 if timestamps:
289 outfile.write(' f=%s, r=%s' % (inst['fetch'], inst['retire']))
290 outfile.write('\n')
291 else:
292 outfile.write('...'.center(12) + '\n')
293
294
295 def validate_range(my_range):
296 my_range = [int(i) for i in my_range.split(':')]
297 if (len(my_range) != 2 or
298 my_range[0] < 0 or
299 my_range[1] > 0 and my_range[0] >= my_range[1]):
300 return None
301 return my_range
302
303
304 def main():
305 # Parse options
306 usage = ('%prog [OPTION]... TRACE_FILE')
307 parser = optparse.OptionParser(usage=usage)
308 parser.add_option(
309 '-o',
310 dest='outfile',
311 default=os.path.join(os.getcwd(), 'o3-pipeview.out'),
312 help="output file (default: '%default')")
313 parser.add_option(
314 '-t',
315 dest='tick_range',
316 default='0:-1',
317 help="tick range (default: '%default'; -1 == inf.)")
318 parser.add_option(
319 '-i',
320 dest='inst_range',
321 default='0:-1',
322 help="instruction range (default: '%default'; -1 == inf.)")
323 parser.add_option(
324 '-w',
325 dest='width',
326 type='int', default=80,
327 help="timeline width (default: '%default')")
328 parser.add_option(
329 '--color',
330 action='store_true', default=False,
331 help="enable colored output (default: '%default')")
332 parser.add_option(
333 '-c', '--cycle-time',
334 type='int', default=1000,
335 help="CPU cycle time in ticks (default: '%default')")
336 parser.add_option(
337 '--timestamps',
338 action='store_true', default=False,
339 help="print fetch and retire timestamps (default: '%default')")
340 parser.add_option(
341 '--only_committed',
342 action='store_true', default=False,
343 help="display only committed (completed) instructions (default: '%default')")
344 parser.add_option(
345 '--store_completions',
346 action='store_true', default=False,
347 help="additionally display store completion ticks (default: '%default')")
348 (options, args) = parser.parse_args()
349 if len(args) != 1:
350 parser.error('incorrect number of arguments')
351 sys.exit(1)
352 tick_range = validate_range(options.tick_range)
353 if not tick_range:
354 parser.error('invalid range')
355 sys.exit(1)
356 inst_range = validate_range(options.inst_range)
357 if not inst_range:
358 parser.error('invalid range')
359 sys.exit(1)
360 # Process trace
361 print('Processing trace... ', end=' ')
362 with open(args[0], 'r') as trace:
363 with open(options.outfile, 'w') as out:
364 process_trace(trace, out, options.cycle_time, options.width,
365 options.color, options.timestamps,
366 options.only_committed, options.store_completions,
367 *(tick_range + inst_range))
368 print('done!')
369
370
371 if __name__ == '__main__':
372 sys.path.append(os.path.join(
373 os.path.dirname(os.path.abspath(__file__)),
374 '..', 'src', 'python'))
375 main()