1#! /usr/bin/env python2.7 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# Authors: Giacomo Gabrielli 39 40# Pipeline activity viewer for the O3 CPU model. 41 42import optparse 43import os 44import sys 45import copy 46 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 50# 'min_threshold'. 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. 53insts = { 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. 68} 69 70def process_trace(trace, outfile, cycle_time, width, color, timestamps, 71 committed_only, store_completions, start_tick, stop_tick, start_sn, stop_sn): 72 global insts 73 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 80 line = None 81 fields = None 82 83 # Skip lines up to the starting tick 84 if start_tick != 0: 85 while True: 86 line = trace.readline() 87 if not line: return 88 fields = line.split(':') 89 if fields[0] != 'O3PipeView': continue 90 if int(fields[2]) >= start_tick: break 91 elif start_sn != 0: 92 while True: 93 line = trace.readline() 94 if not line: return 95 fields = line.split(':') 96 if fields[0] != 'O3PipeView': continue 97 if fields[1] == 'fetch' and int(fields[5]) >= start_sn: break 98 else: 99 line = trace.readline() 100 if not line: return 101 fields = line.split(':') 102 103 # Skip lines up to next instruction fetch 104 while fields[0] != 'O3PipeView' or fields[1] != 'fetch': 105 line = trace.readline() 106 if not line: return 107 fields = line.split(':') 108 109 # Print header 110 outfile.write('// f = fetch, d = decode, n = rename, p = dispatch, ' 111 'i = issue, c = complete, r = retire') 112 113 if store_completions: 114 outfile.write(', s = store-complete') 115 outfile.write('\n\n') 116 117 outfile.write(' ' + 'timeline'.center(width) + 118 ' ' + 'tick'.center(15) + 119 ' ' + 'pc.upc'.center(12) + 120 ' ' + 'disasm'.ljust(25) + 121 ' ' + 'seq_num'.center(10)) 122 if timestamps: 123 outfile.write('timestamps'.center(25)) 124 outfile.write('\n') 125 126 # Region of interest 127 curr_inst = {} 128 while True: 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) 135 return 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) 145 146 line = trace.readline() 147 if not line: 148 print_insts(outfile, cycle_time, width, color, timestamps, store_completions, 0) 149 return 150 fields = line.split(':') 151 152 153#Sorts out instructions according to sequence number 154def compare_by_sn(a, b): 155 return cmp(a['sn'], b['sn']) 156 157# Puts new instruction into the print queue. 158# Sorts out and prints instructions when their number reaches threshold value 159def queue_inst(outfile, inst, cycle_time, width, color, timestamps, store_completions): 160 global insts 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']) 165 166# Sorts out and prints instructions in print queue 167def print_insts(outfile, cycle_time, width, color, timestamps, store_completions, lower_threshold): 168 global insts 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 185 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) 189 190# Prints a single instruction 191def print_inst(outfile, inst, cycle_time, width, color, timestamps, store_completions): 192 if color: 193 from m5.util.terminal import termcap 194 else: 195 from m5.util.terminal import no_termcap as termcap 196 # Pipeline stages 197 stages = [{'name': 'fetch', 198 'color': termcap.Blue + termcap.Reverse, 199 'shorthand': 'f'}, 200 {'name': 'decode', 201 'color': termcap.Yellow + termcap.Reverse, 202 'shorthand': 'd'}, 203 {'name': 'rename', 204 'color': termcap.Magenta + termcap.Reverse, 205 'shorthand': 'n'}, 206 {'name': 'dispatch', 207 'color': termcap.Green + termcap.Reverse, 208 'shorthand': 'p'}, 209 {'name': 'issue', 210 'color': termcap.Red + termcap.Reverse, 211 'shorthand': 'i'}, 212 {'name': 'complete', 213 'color': termcap.Cyan + termcap.Reverse, 214 'shorthand': 'c'}, 215 {'name': 'retire', 216 'color': termcap.Blue + termcap.Reverse, 217 'shorthand': 'r'} 218 ] 219 if store_completions: 220 stages.append( 221 {'name': 'store', 222 'color': termcap.Yellow + termcap.Reverse, 223 'shorthand': 's'}) 224 225 # Print 226 227 time_width = width * cycle_time 228 base_tick = (inst['fetch'] / time_width) * time_width 229 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']) 236 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 241 else: 242 num_lines = ((last_event_time - base_tick) / time_width) + 1 243 244 curr_color = termcap.Normal 245 246 # This will visually distinguish completed and abandoned intructions. 247 if inst['retire'] == 0: dot = '=' # abandoned instruction 248 else: dot = '.' # completed instruction 249 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) 255 events = [] 256 for stage_idx in range(len(stages)): 257 tick = inst[stages[stage_idx]['name']] 258 if tick != 0: 259 if tick >= start_tick and tick < end_tick: 260 events.append((tick % time_width, 261 stages[stage_idx]['name'], 262 stage_idx, tick)) 263 events.sort() 264 outfile.write('[') 265 pos = 0 266 if num_lines == 1 and events[0][2] != 0: # event is not fetch 267 curr_color = stages[events[0][2] - 1]['color'] 268 for event in events: 269 if (stages[event[2]]['name'] == 'dispatch' and 270 inst['dispatch'] == inst['issue']): 271 continue 272 outfile.write(curr_color + dot * ((event[0] / cycle_time) - pos)) 273 outfile.write(stages[event[2]]['color'] + 274 stages[event[2]]['shorthand']) 275 276 if event[3] != last_event_time: # event is not the last one 277 curr_color = stages[event[2]]['color'] 278 else: 279 curr_color = termcap.Normal 280 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) + ') ') 284 if i == 0: 285 outfile.write('%s.%s %s [%s]' % ( 286 inst['pc'].rjust(10), 287 inst['upc'], 288 inst['disasm'].ljust(25), 289 str(inst['sn']).rjust(10))) 290 if timestamps: 291 outfile.write(' f=%s, r=%s' % (inst['fetch'], inst['retire'])) 292 outfile.write('\n') 293 else: 294 outfile.write('...'.center(12) + '\n') 295 296 297def validate_range(my_range): 298 my_range = [int(i) for i in my_range.split(':')] 299 if (len(my_range) != 2 or 300 my_range[0] < 0 or 301 my_range[1] > 0 and my_range[0] >= my_range[1]): 302 return None 303 return my_range 304 305 306def main(): 307 # Parse options 308 usage = ('%prog [OPTION]... TRACE_FILE') 309 parser = optparse.OptionParser(usage=usage) 310 parser.add_option( 311 '-o', 312 dest='outfile', 313 default=os.path.join(os.getcwd(), 'o3-pipeview.out'), 314 help="output file (default: '%default')") 315 parser.add_option( 316 '-t', 317 dest='tick_range', 318 default='0:-1', 319 help="tick range (default: '%default'; -1 == inf.)") 320 parser.add_option( 321 '-i', 322 dest='inst_range', 323 default='0:-1', 324 help="instruction range (default: '%default'; -1 == inf.)") 325 parser.add_option( 326 '-w', 327 dest='width', 328 type='int', default=80, 329 help="timeline width (default: '%default')") 330 parser.add_option( 331 '--color', 332 action='store_true', default=False, 333 help="enable colored output (default: '%default')") 334 parser.add_option( 335 '-c', '--cycle-time', 336 type='int', default=1000, 337 help="CPU cycle time in ticks (default: '%default')") 338 parser.add_option( 339 '--timestamps', 340 action='store_true', default=False, 341 help="print fetch and retire timestamps (default: '%default')") 342 parser.add_option( 343 '--only_committed', 344 action='store_true', default=False, 345 help="display only committed (completed) instructions (default: '%default')") 346 parser.add_option( 347 '--store_completions', 348 action='store_true', default=False, 349 help="additionally display store completion ticks (default: '%default')") 350 (options, args) = parser.parse_args() 351 if len(args) != 1: 352 parser.error('incorrect number of arguments') 353 sys.exit(1) 354 tick_range = validate_range(options.tick_range) 355 if not tick_range: 356 parser.error('invalid range') 357 sys.exit(1) 358 inst_range = validate_range(options.inst_range) 359 if not inst_range: 360 parser.error('invalid range') 361 sys.exit(1) 362 # Process trace 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)) 370 print 'done!' 371 372 373if __name__ == '__main__': 374 sys.path.append(os.path.join( 375 os.path.dirname(os.path.abspath(__file__)), 376 '..', 'src', 'python')) 377 main() 378