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 # Pipeline activity viewer for the O3 CPU model.
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
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.
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.
68 def process_trace(trace
, outfile
, cycle_time
, width
, color
, timestamps
,
69 committed_only
, store_completions
, start_tick
, stop_tick
, start_sn
, stop_sn
):
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
81 # Skip lines up to the starting tick
84 line
= trace
.readline()
86 fields
= line
.split(':')
87 if fields
[0] != 'O3PipeView': continue
88 if int(fields
[2]) >= start_tick
: break
91 line
= trace
.readline()
93 fields
= line
.split(':')
94 if fields
[0] != 'O3PipeView': continue
95 if fields
[1] == 'fetch' and int(fields
[5]) >= start_sn
: break
97 line
= trace
.readline()
99 fields
= line
.split(':')
101 # Skip lines up to next instruction fetch
102 while fields
[0] != 'O3PipeView' or fields
[1] != 'fetch':
103 line
= trace
.readline()
105 fields
= line
.split(':')
108 outfile
.write('// f = fetch, d = decode, n = rename, p = dispatch, '
109 'i = issue, c = complete, r = retire')
111 if store_completions
:
112 outfile
.write(', s = store-complete')
113 outfile
.write('\n\n')
115 outfile
.write(' ' + 'timeline'.center(width
) +
116 ' ' + 'tick'.center(15) +
117 ' ' + 'pc.upc'.center(12) +
118 ' ' + 'disasm'.ljust(25) +
119 ' ' + 'seq_num'.center(10))
121 outfile
.write('timestamps'.center(25))
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)
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
)
144 line
= trace
.readline()
146 print_insts(outfile
, cycle_time
, width
, color
, timestamps
, store_completions
, 0)
148 fields
= line
.split(':')
151 #Sorts out instructions according to sequence number
152 def compare_by_sn(a
, b
):
153 return cmp(a
['sn'], b
['sn'])
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
):
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'])
164 # Sorts out and prints instructions in print queue
165 def print_insts(outfile
, cycle_time
, width
, color
, timestamps
, store_completions
, lower_threshold
):
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
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
)
188 # Prints a single instruction
189 def print_inst(outfile
, inst
, cycle_time
, width
, color
, timestamps
, store_completions
):
191 from m5
.util
.terminal
import termcap
193 from m5
.util
.terminal
import no_termcap
as termcap
195 stages
= [{'name': 'fetch',
196 'color': termcap
.Blue
+ termcap
.Reverse
,
199 'color': termcap
.Yellow
+ termcap
.Reverse
,
202 'color': termcap
.Magenta
+ termcap
.Reverse
,
205 'color': termcap
.Green
+ termcap
.Reverse
,
208 'color': termcap
.Red
+ termcap
.Reverse
,
211 'color': termcap
.Cyan
+ termcap
.Reverse
,
214 'color': termcap
.Blue
+ termcap
.Reverse
,
217 if store_completions
:
220 'color': termcap
.Yellow
+ termcap
.Reverse
,
225 time_width
= width
* cycle_time
226 base_tick
= (inst
['fetch'] / time_width
) * time_width
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'])
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
240 num_lines
= ((last_event_time
- base_tick
) / time_width
) + 1
242 curr_color
= termcap
.Normal
244 # This will visually distinguish completed and abandoned intructions.
245 if inst
['retire'] == 0: dot
= '=' # abandoned instruction
246 else: dot
= '.' # completed instruction
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
)
254 for stage_idx
in range(len(stages
)):
255 tick
= inst
[stages
[stage_idx
]['name']]
257 if tick
>= start_tick
and tick
< end_tick
:
258 events
.append((tick
% time_width
,
259 stages
[stage_idx
]['name'],
264 if num_lines
== 1 and events
[0][2] != 0: # event is not fetch
265 curr_color
= stages
[events
[0][2] - 1]['color']
267 if (stages
[event
[2]]['name'] == 'dispatch' and
268 inst
['dispatch'] == inst
['issue']):
270 outfile
.write(curr_color
+ dot
* ((event
[0] / cycle_time
) - pos
))
271 outfile
.write(stages
[event
[2]]['color'] +
272 stages
[event
[2]]['shorthand'])
274 if event
[3] != last_event_time
: # event is not the last one
275 curr_color
= stages
[event
[2]]['color']
277 curr_color
= termcap
.Normal
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) + ') ')
283 outfile
.write('%s.%s %s [%s]' % (
284 inst
['pc'].rjust(10),
286 inst
['disasm'].ljust(25),
287 str(inst
['sn']).rjust(10)))
289 outfile
.write(' f=%s, r=%s' % (inst
['fetch'], inst
['retire']))
292 outfile
.write('...'.center(12) + '\n')
295 def validate_range(my_range
):
296 my_range
= [int(i
) for i
in my_range
.split(':')]
297 if (len(my_range
) != 2 or
299 my_range
[1] > 0 and my_range
[0] >= my_range
[1]):
306 usage
= ('%prog [OPTION]... TRACE_FILE')
307 parser
= optparse
.OptionParser(usage
=usage
)
311 default
=os
.path
.join(os
.getcwd(), 'o3-pipeview.out'),
312 help="output file (default: '%default')")
317 help="tick range (default: '%default'; -1 == inf.)")
322 help="instruction range (default: '%default'; -1 == inf.)")
326 type='int', default
=80,
327 help="timeline width (default: '%default')")
330 action
='store_true', default
=False,
331 help="enable colored output (default: '%default')")
333 '-c', '--cycle-time',
334 type='int', default
=1000,
335 help="CPU cycle time in ticks (default: '%default')")
338 action
='store_true', default
=False,
339 help="print fetch and retire timestamps (default: '%default')")
342 action
='store_true', default
=False,
343 help="display only committed (completed) instructions (default: '%default')")
345 '--store_completions',
346 action
='store_true', default
=False,
347 help="additionally display store completion ticks (default: '%default')")
348 (options
, args
) = parser
.parse_args()
350 parser
.error('incorrect number of arguments')
352 tick_range
= validate_range(options
.tick_range
)
354 parser
.error('invalid range')
356 inst_range
= validate_range(options
.inst_range
)
358 parser
.error('invalid range')
361 print 'Processing trace... ',
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
))
371 if __name__
== '__main__':
372 sys
.path
.append(os
.path
.join(
373 os
.path
.dirname(os
.path
.abspath(__file__
)),
374 '..', 'src', 'python'))