fedafd449a9584623ee67cde6b3636285322094e
1 #! /usr/bin/env python2.7
3 # Copyright (c) 2011 ARM Limited
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.
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.
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.
38 # Authors: Giacomo Gabrielli
40 # Pipeline activity viewer for the O3 CPU model.
47 # Temporary storage for instructions. The queue is filled in out-of-order
48 # until it reaches 'max_threshold' number of instructions. It is then
49 # sorted out and instructions are printed out until their number drops to
51 # It is assumed that the instructions are not out of order for more then
52 # 'min_threshold' places - otherwise they will appear out of order.
54 'queue': [] , # Instructions to print.
55 'max_threshold':2000, # Instructions are sorted out and printed when
56 # their number reaches this threshold.
57 'min_threshold':1000, # Printing stops when this number is reached.
58 'sn_start':0, # The first instruction seq. number to be printed.
59 'sn_stop':0, # The last instruction seq. number to be printed.
60 'tick_start':0, # The first tick to be printed
61 'tick_stop':0, # The last tick to be printed
62 'tick_drift':2000, # Used to calculate the start and the end of main
63 # loop. We assume here that the instructions are not
64 # out of order for more then 2000 CPU ticks,
65 # otherwise the print may not start/stop
66 # at the time specified by tick_start/stop.
67 'only_committed':0, # Set if only committed instructions are printed.
70 def process_trace(trace
, outfile
, cycle_time
, width
, color
, timestamps
,
71 committed_only
, store_completions
, start_tick
, stop_tick
, start_sn
, stop_sn
):
74 insts
['sn_start'] = start_sn
75 insts
['sn_stop'] = stop_sn
76 insts
['tick_start'] = start_tick
77 insts
['tick_stop'] = stop_tick
78 insts
['tick_drift'] = insts
['tick_drift'] * cycle_time
79 insts
['only_committed'] = committed_only
83 # Skip lines up to the starting tick
86 line
= trace
.readline()
88 fields
= line
.split(':')
89 if fields
[0] != 'O3PipeView': continue
90 if int(fields
[2]) >= start_tick
: break
93 line
= trace
.readline()
95 fields
= line
.split(':')
96 if fields
[0] != 'O3PipeView': continue
97 if fields
[1] == 'fetch' and int(fields
[5]) >= start_sn
: break
99 line
= trace
.readline()
101 fields
= line
.split(':')
103 # Skip lines up to next instruction fetch
104 while fields
[0] != 'O3PipeView' or fields
[1] != 'fetch':
105 line
= trace
.readline()
107 fields
= line
.split(':')
110 outfile
.write('// f = fetch, d = decode, n = rename, p = dispatch, '
111 'i = issue, c = complete, r = retire')
113 if store_completions
:
114 outfile
.write(', s = store-complete')
115 outfile
.write('\n\n')
117 outfile
.write(' ' + 'timeline'.center(width
) +
118 ' ' + 'tick'.center(15) +
119 ' ' + 'pc.upc'.center(12) +
120 ' ' + 'disasm'.ljust(25) +
121 ' ' + 'seq_num'.center(10))
123 outfile
.write('timestamps'.center(25))
129 if fields
[0] == 'O3PipeView':
130 curr_inst
[fields
[1]] = int(fields
[2])
131 if fields
[1] == 'fetch':
132 if ((stop_tick
> 0 and int(fields
[2]) > stop_tick
+insts
['tick_drift']) or
133 (stop_sn
> 0 and int(fields
[5]) > (stop_sn
+insts
['max_threshold']))):
134 print_insts(outfile
, cycle_time
, width
, color
, timestamps
, 0)
136 (curr_inst
['pc'], curr_inst
['upc']) = fields
[3:5]
137 curr_inst
['sn'] = int(fields
[5])
138 curr_inst
['disasm'] = ' '.join(fields
[6][:-1].split())
139 elif fields
[1] == 'retire':
140 if curr_inst
['retire'] == 0:
141 curr_inst
['disasm'] = '-----' + curr_inst
['disasm']
142 if store_completions
:
143 curr_inst
[fields
[3]] = int(fields
[4])
144 queue_inst(outfile
, curr_inst
, cycle_time
, width
, color
, timestamps
, store_completions
)
146 line
= trace
.readline()
148 print_insts(outfile
, cycle_time
, width
, color
, timestamps
, store_completions
, 0)
150 fields
= line
.split(':')
153 #Sorts out instructions according to sequence number
154 def compare_by_sn(a
, b
):
155 return cmp(a
['sn'], b
['sn'])
157 # Puts new instruction into the print queue.
158 # Sorts out and prints instructions when their number reaches threshold value
159 def queue_inst(outfile
, inst
, cycle_time
, width
, color
, timestamps
, store_completions
):
161 l_copy
= copy
.deepcopy(inst
)
162 insts
['queue'].append(l_copy
)
163 if len(insts
['queue']) > insts
['max_threshold']:
164 print_insts(outfile
, cycle_time
, width
, color
, timestamps
, store_completions
, insts
['min_threshold'])
166 # Sorts out and prints instructions in print queue
167 def print_insts(outfile
, cycle_time
, width
, color
, timestamps
, store_completions
, lower_threshold
):
169 insts
['queue'].sort(compare_by_sn
)
170 while len(insts
['queue']) > lower_threshold
:
171 print_item
=insts
['queue'].pop(0)
172 # As the instructions are processed out of order the main loop starts
173 # earlier then specified by start_sn/tick and finishes later then what
174 # is defined in stop_sn/tick.
175 # Therefore, here we have to filter out instructions that reside out of
176 # the specified boundaries.
177 if (insts
['sn_start'] > 0 and print_item
['sn'] < insts
['sn_start']):
178 continue; # earlier then the starting sequence number
179 if (insts
['sn_stop'] > 0 and print_item
['sn'] > insts
['sn_stop']):
180 continue; # later then the ending sequence number
181 if (insts
['tick_start'] > 0 and print_item
['fetch'] < insts
['tick_start']):
182 continue; # earlier then the starting tick number
183 if (insts
['tick_stop'] > 0 and print_item
['fetch'] > insts
['tick_stop']):
184 continue; # later then the ending tick number
186 if (insts
['only_committed'] != 0 and print_item
['retire'] == 0):
187 continue; # retire is set to zero if it hasn't been completed
188 print_inst(outfile
, print_item
, cycle_time
, width
, color
, timestamps
, store_completions
)
190 # Prints a single instruction
191 def print_inst(outfile
, inst
, cycle_time
, width
, color
, timestamps
, store_completions
):
193 from m5
.util
.terminal
import termcap
195 from m5
.util
.terminal
import no_termcap
as termcap
197 stages
= [{'name': 'fetch',
198 'color': termcap
.Blue
+ termcap
.Reverse
,
201 'color': termcap
.Yellow
+ termcap
.Reverse
,
204 'color': termcap
.Magenta
+ termcap
.Reverse
,
207 'color': termcap
.Green
+ termcap
.Reverse
,
210 'color': termcap
.Red
+ termcap
.Reverse
,
213 'color': termcap
.Cyan
+ termcap
.Reverse
,
216 'color': termcap
.Blue
+ termcap
.Reverse
,
219 if store_completions
:
222 'color': termcap
.Yellow
+ termcap
.Reverse
,
227 time_width
= width
* cycle_time
228 base_tick
= (inst
['fetch'] / time_width
) * time_width
230 # Find out the time of the last event - it may not
231 # be 'retire' if the instruction is not comlpeted.
232 last_event_time
= max(inst
['fetch'], inst
['decode'],inst
['rename'],
233 inst
['dispatch'],inst
['issue'], inst
['complete'], inst
['retire'])
234 if store_completions
:
235 last_event_time
= max(last_event_time
, inst
['store'])
237 # Timeline shorter then time_width is printed in compact form where
238 # the print continues at the start of the same line.
239 if ((last_event_time
- inst
['fetch']) < time_width
):
240 num_lines
= 1 # compact form
242 num_lines
= ((last_event_time
- base_tick
) / time_width
) + 1
244 curr_color
= termcap
.Normal
246 # This will visually distinguish completed and abandoned intructions.
247 if inst
['retire'] == 0: dot
= '=' # abandoned instruction
248 else: dot
= '.' # completed instruction
250 for i
in range(num_lines
):
251 start_tick
= base_tick
+ i
* time_width
252 end_tick
= start_tick
+ time_width
253 if num_lines
== 1: # compact form
254 end_tick
+= (inst
['fetch'] - base_tick
)
256 for stage_idx
in range(len(stages
)):
257 tick
= inst
[stages
[stage_idx
]['name']]
259 if tick
>= start_tick
and tick
< end_tick
:
260 events
.append((tick
% time_width
,
261 stages
[stage_idx
]['name'],
266 if num_lines
== 1 and events
[0][2] != 0: # event is not fetch
267 curr_color
= stages
[events
[0][2] - 1]['color']
269 if (stages
[event
[2]]['name'] == 'dispatch' and
270 inst
['dispatch'] == inst
['issue']):
272 outfile
.write(curr_color
+ dot
* ((event
[0] / cycle_time
) - pos
))
273 outfile
.write(stages
[event
[2]]['color'] +
274 stages
[event
[2]]['shorthand'])
276 if event
[3] != last_event_time
: # event is not the last one
277 curr_color
= stages
[event
[2]]['color']
279 curr_color
= termcap
.Normal
281 pos
= (event
[0] / cycle_time
) + 1
282 outfile
.write(curr_color
+ dot
* (width
- pos
) + termcap
.Normal
+
283 ']-(' + str(base_tick
+ i
* time_width
).rjust(15) + ') ')
285 outfile
.write('%s.%s %s [%s]' % (
286 inst
['pc'].rjust(10),
288 inst
['disasm'].ljust(25),
289 str(inst
['sn']).rjust(10)))
291 outfile
.write(' f=%s, r=%s' % (inst
['fetch'], inst
['retire']))
294 outfile
.write('...'.center(12) + '\n')
297 def validate_range(my_range
):
298 my_range
= [int(i
) for i
in my_range
.split(':')]
299 if (len(my_range
) != 2 or
301 my_range
[1] > 0 and my_range
[0] >= my_range
[1]):
308 usage
= ('%prog [OPTION]... TRACE_FILE')
309 parser
= optparse
.OptionParser(usage
=usage
)
313 default
=os
.path
.join(os
.getcwd(), 'o3-pipeview.out'),
314 help="output file (default: '%default')")
319 help="tick range (default: '%default'; -1 == inf.)")
324 help="instruction range (default: '%default'; -1 == inf.)")
328 type='int', default
=80,
329 help="timeline width (default: '%default')")
332 action
='store_true', default
=False,
333 help="enable colored output (default: '%default')")
335 '-c', '--cycle-time',
336 type='int', default
=1000,
337 help="CPU cycle time in ticks (default: '%default')")
340 action
='store_true', default
=False,
341 help="print fetch and retire timestamps (default: '%default')")
344 action
='store_true', default
=False,
345 help="display only committed (completed) instructions (default: '%default')")
347 '--store_completions',
348 action
='store_true', default
=False,
349 help="additionally display store completion ticks (default: '%default')")
350 (options
, args
) = parser
.parse_args()
352 parser
.error('incorrect number of arguments')
354 tick_range
= validate_range(options
.tick_range
)
356 parser
.error('invalid range')
358 inst_range
= validate_range(options
.inst_range
)
360 parser
.error('invalid range')
363 print 'Processing trace... ',
364 with
open(args
[0], 'r') as trace
:
365 with
open(options
.outfile
, 'w') as out
:
366 process_trace(trace
, out
, options
.cycle_time
, options
.width
,
367 options
.color
, options
.timestamps
,
368 options
.only_committed
, options
.store_completions
,
369 *(tick_range
+ inst_range
))
373 if __name__
== '__main__':
374 sys
.path
.append(os
.path
.join(
375 os
.path
.dirname(os
.path
.abspath(__file__
)),
376 '..', 'src', 'python'))