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 def process_trace(trace
, outfile
, cycle_time
, width
, color
, timestamps
,
48 start_tick
, stop_tick
, start_sn
, stop_sn
):
51 # Skip lines up to region of interest
54 line
= trace
.readline()
56 fields
= line
.split(':')
57 if fields
[0] != 'O3PipeView': continue
58 if int(fields
[2]) >= start_tick
: break
61 line
= trace
.readline()
63 fields
= line
.split(':')
64 if fields
[0] != 'O3PipeView': continue
65 if fields
[1] == 'fetch' and int(fields
[5]) >= start_sn
: break
67 line
= trace
.readline()
69 fields
= line
.split(':')
70 # Skip lines up to next instruction fetch
71 while fields
[0] != 'O3PipeView' or fields
[1] != 'fetch':
72 line
= trace
.readline()
74 fields
= line
.split(':')
76 outfile
.write('// f = fetch, d = decode, n = rename, p = dispatch, '
77 'i = issue, c = complete, r = retire\n\n')
78 outfile
.write(' ' + 'timeline'.center(width
) +
79 ' ' + 'tick'.center(15) +
80 ' ' + 'pc.upc'.center(12) +
81 ' ' + 'disasm'.ljust(25) +
82 ' ' + 'seq_num'.center(15))
84 outfile
.write('timestamps'.center(25))
89 if fields
[0] == 'O3PipeView':
90 curr_inst
[fields
[1]] = int(fields
[2])
91 if fields
[1] == 'fetch':
92 if ((stop_tick
> 0 and int(fields
[2]) > stop_tick
) or
93 (stop_sn
> 0 and int(fields
[5]) > stop_sn
)):
95 (curr_inst
['pc'], curr_inst
['upc']) = fields
[3:5]
96 curr_inst
['sn'] = int(fields
[5])
97 curr_inst
['disasm'] = ' '.join(fields
[6][:-1].split())
98 elif fields
[1] == 'retire':
99 print_inst(outfile
, curr_inst
, cycle_time
, width
, color
,
101 line
= trace
.readline()
103 fields
= line
.split(':')
106 def print_inst(outfile
, inst
, cycle_time
, width
, color
, timestamps
):
108 from m5
.util
.terminal
import termcap
110 from m5
.util
.terminal
import no_termcap
as termcap
112 stages
= [{'name': 'fetch',
113 'color': termcap
.Blue
+ termcap
.Reverse
,
116 'color': termcap
.Yellow
+ termcap
.Reverse
,
119 'color': termcap
.Magenta
+ termcap
.Reverse
,
122 'color': termcap
.Green
+ termcap
.Reverse
,
125 'color': termcap
.Red
+ termcap
.Reverse
,
128 'color': termcap
.Cyan
+ termcap
.Reverse
,
131 'color': termcap
.Blue
+ termcap
.Reverse
,
134 time_width
= width
* cycle_time
135 base_tick
= (inst
['fetch'] / time_width
) * time_width
136 num_lines
= ((inst
['retire'] - inst
['fetch']) / time_width
) + 1
137 curr_color
= termcap
.Normal
138 for i
in range(num_lines
):
139 start_tick
= base_tick
+ i
* time_width
140 end_tick
= start_tick
+ time_width
141 if num_lines
== 1: # compact form
142 end_tick
+= (inst
['fetch'] - base_tick
)
144 for stage_idx
in range(len(stages
)):
145 tick
= inst
[stages
[stage_idx
]['name']]
146 if tick
>= start_tick
and tick
< end_tick
:
147 events
.append((tick
% time_width
,
148 stages
[stage_idx
]['name'],
153 if num_lines
== 1 and events
[0][2] != 0: # event is not fetch
154 curr_color
= stages
[events
[0][2] - 1]['color']
156 if (stages
[event
[2]]['name'] == 'dispatch' and
157 inst
['dispatch'] == inst
['issue']):
159 outfile
.write(curr_color
+ '.' * ((event
[0] / cycle_time
) - pos
))
160 outfile
.write(stages
[event
[2]]['color'] +
161 stages
[event
[2]]['shorthand'])
162 if event
[2] != len(stages
) - 1: # event is not retire
163 curr_color
= stages
[event
[2]]['color']
165 curr_color
= termcap
.Normal
166 pos
= (event
[0] / cycle_time
) + 1
167 outfile
.write(curr_color
+ '.' * (width
- pos
) + termcap
.Normal
+
168 ']-(' + str(base_tick
+ i
* time_width
).rjust(15) + ') ')
170 outfile
.write('%s.%s %s [%s]' % (
171 inst
['pc'].rjust(10),
173 inst
['disasm'].ljust(25),
174 str(inst
['sn']).rjust(15)))
176 outfile
.write(' f=%s, r=%s' % (inst
['fetch'], inst
['retire']))
179 outfile
.write('...'.center(12) + '\n')
182 def validate_range(my_range
):
183 my_range
= [int(i
) for i
in my_range
.split(':')]
184 if (len(my_range
) != 2 or
186 my_range
[1] > 0 and my_range
[0] >= my_range
[1]):
193 usage
= ('%prog [OPTION]... TRACE_FILE')
194 parser
= optparse
.OptionParser(usage
=usage
)
198 default
=os
.path
.join(os
.getcwd(), 'o3-pipeview.out'),
199 help="output file (default: '%default')")
204 help="tick range (default: '%default'; -1 == inf.)")
209 help="instruction range (default: '%default'; -1 == inf.)")
213 type='int', default
=80,
214 help="timeline width (default: '%default')")
217 action
='store_true', default
=False,
218 help="enable colored output (default: '%default')")
220 '-c', '--cycle-time',
221 type='int', default
=1000,
222 help="CPU cycle time in ticks (default: '%default')")
225 action
='store_true', default
=False,
226 help="print fetch and retire timestamps (default: '%default')")
227 (options
, args
) = parser
.parse_args()
229 parser
.error('incorrect number of arguments')
231 tick_range
= validate_range(options
.tick_range
)
233 parser
.error('invalid range')
235 inst_range
= validate_range(options
.inst_range
)
237 parser
.error('invalid range')
240 print 'Processing trace... ',
241 with
open(args
[0], 'r') as trace
:
242 with
open(options
.outfile
, 'w') as out
:
243 process_trace(trace
, out
, options
.cycle_time
, options
.width
,
244 options
.color
, options
.timestamps
,
245 *(tick_range
+ inst_range
))
249 if __name__
== '__main__':
250 sys
.path
.append(os
.path
.join(
251 os
.path
.dirname(os
.path
.abspath(__file__
)),
252 '..', 'src', 'python'))