o3-pipeview.py revision 9252
1#! /usr/bin/env python 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, 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 # Read the first line 84 line = trace.readline() 85 if not line: return 86 fields = line.split(':') 87 88 # Skip lines up to the starting tick 89 if start_tick != 0: 90 while True: 91 if fields[0] != 'O3PipeView': continue 92 if (int(fields[2]) > 0 and 93 int(fields[2]) >= start_tick-insts['tick_drift']): break 94 line = trace.readline() 95 if not line: return 96 fields = line.split(':') 97 98 # Skip lines up to the starting sequence number 99 if start_sn != 0: 100 while True: 101 if fields[0] != 'O3PipeView': continue 102 if (fields[1] == 'fetch' and 103 int(fields[5]) >= (start_sn-insts['max_threshold'])): 104 break 105 line = trace.readline() 106 if not line: return 107 fields = line.split(':') 108 109 # Skip lines up to next instruction fetch 110 while fields[0] != 'O3PipeView' or fields[1] != 'fetch': 111 line = trace.readline() 112 if not line: return 113 fields = line.split(':') 114 115 # Print header 116 outfile.write('// f = fetch, d = decode, n = rename, p = dispatch, ' 117 'i = issue, c = complete, r = retire\n\n') 118 outfile.write(' ' + 'timeline'.center(width) + 119 ' ' + 'tick'.center(15) + 120 ' ' + 'pc.upc'.center(12) + 121 ' ' + 'disasm'.ljust(25) + 122 ' ' + 'seq_num'.center(15)) 123 if timestamps: 124 outfile.write('timestamps'.center(25)) 125 outfile.write('\n') 126 127 # Region of interest 128 curr_inst = {} 129 while True: 130 if fields[0] == 'O3PipeView': 131 curr_inst[fields[1]] = int(fields[2]) 132 if fields[1] == 'fetch': 133 if ((stop_tick > 0 and int(fields[2]) > stop_tick+insts['tick_drift']) or 134 (stop_sn > 0 and int(fields[5]) > (stop_sn+insts['max_threshold']))): 135 print_insts(outfile, cycle_time, width, color, timestamps, 0) 136 return 137 (curr_inst['pc'], curr_inst['upc']) = fields[3:5] 138 curr_inst['sn'] = int(fields[5]) 139 curr_inst['disasm'] = ' '.join(fields[6][:-1].split()) 140 elif fields[1] == 'retire': 141 queue_inst(outfile, curr_inst, cycle_time, width, color, timestamps) 142 line = trace.readline() 143 if not line: return 144 fields = line.split(':') 145 146 147#Sorts out instructions according to sequence number 148def compare_by_sn(a, b): 149 return cmp(a['sn'], b['sn']) 150 151# Puts new instruction into the print queue. 152# Sorts out and prints instructions when their number reaches threshold value 153def queue_inst(outfile, inst, cycle_time, width, color, timestamps): 154 global insts 155 l_copy = copy.deepcopy(inst) 156 insts['queue'].append(l_copy) 157 if len(insts['queue']) > insts['max_threshold']: 158 print_insts(outfile, cycle_time, width, color, timestamps, insts['min_threshold']) 159 160# Sorts out and prints instructions in print queue 161def print_insts(outfile, cycle_time, width, color, timestamps, lower_threshold): 162 global insts 163 insts['queue'].sort(compare_by_sn) 164 while len(insts['queue']) > lower_threshold: 165 print_item=insts['queue'].pop(0) 166 # As the instructions are processed out of order the main loop starts 167 # earlier then specified by start_sn/tick and finishes later then what 168 # is defined in stop_sn/tick. 169 # Therefore, here we have to filter out instructions that reside out of 170 # the specified boundaries. 171 if (insts['sn_start'] > 0 and print_item['sn'] < insts['sn_start']): 172 continue; # earlier then the starting sequence number 173 if (insts['sn_stop'] > 0 and print_item['sn'] > insts['sn_stop']): 174 continue; # later then the ending sequence number 175 if (insts['tick_start'] > 0 and print_item['fetch'] < insts['tick_start']): 176 continue; # earlier then the starting tick number 177 if (insts['tick_stop'] > 0 and print_item['fetch'] > insts['tick_stop']): 178 continue; # later then the ending tick number 179 180 if (insts['only_committed'] != 0 and print_item['retire'] == 0): 181 continue; # retire is set to zero if it hasn't been completed 182 print_inst(outfile, print_item, cycle_time, width, color, timestamps) 183 184# Prints a single instruction 185def print_inst(outfile, inst, cycle_time, width, color, timestamps): 186 if color: 187 from m5.util.terminal import termcap 188 else: 189 from m5.util.terminal import no_termcap as termcap 190 # Pipeline stages 191 stages = [{'name': 'fetch', 192 'color': termcap.Blue + termcap.Reverse, 193 'shorthand': 'f'}, 194 {'name': 'decode', 195 'color': termcap.Yellow + termcap.Reverse, 196 'shorthand': 'd'}, 197 {'name': 'rename', 198 'color': termcap.Magenta + termcap.Reverse, 199 'shorthand': 'n'}, 200 {'name': 'dispatch', 201 'color': termcap.Green + termcap.Reverse, 202 'shorthand': 'p'}, 203 {'name': 'issue', 204 'color': termcap.Red + termcap.Reverse, 205 'shorthand': 'i'}, 206 {'name': 'complete', 207 'color': termcap.Cyan + termcap.Reverse, 208 'shorthand': 'c'}, 209 {'name': 'retire', 210 'color': termcap.Blue + termcap.Reverse, 211 'shorthand': 'r'}] 212 213 # Print 214 215 time_width = width * cycle_time 216 base_tick = (inst['fetch'] / time_width) * time_width 217 218 # Find out the time of the last event - it may not 219 # be 'retire' if the instruction is not comlpeted. 220 last_event_time = max(inst['fetch'], inst['decode'],inst['rename'], 221 inst['dispatch'],inst['issue'], inst['complete'], inst['retire']) 222 223 # Timeline shorter then time_width is printed in compact form where 224 # the print continues at the start of the same line. 225 if ((last_event_time - inst['fetch']) < time_width): 226 num_lines = 1 # compact form 227 else: 228 num_lines = ((last_event_time - base_tick) / time_width) + 1 229 230 curr_color = termcap.Normal 231 232 # This will visually distinguish completed and abandoned intructions. 233 if inst['retire'] == 0: dot = '=' # abandoned instruction 234 else: dot = '.' # completed instruction 235 236 for i in range(num_lines): 237 start_tick = base_tick + i * time_width 238 end_tick = start_tick + time_width 239 if num_lines == 1: # compact form 240 end_tick += (inst['fetch'] - base_tick) 241 events = [] 242 for stage_idx in range(len(stages)): 243 tick = inst[stages[stage_idx]['name']] 244 if tick != 0: 245 if tick >= start_tick and tick < end_tick: 246 events.append((tick % time_width, 247 stages[stage_idx]['name'], 248 stage_idx, tick)) 249 events.sort() 250 outfile.write('[') 251 pos = 0 252 if num_lines == 1 and events[0][2] != 0: # event is not fetch 253 curr_color = stages[events[0][2] - 1]['color'] 254 for event in events: 255 if (stages[event[2]]['name'] == 'dispatch' and 256 inst['dispatch'] == inst['issue']): 257 continue 258 outfile.write(curr_color + dot * ((event[0] / cycle_time) - pos)) 259 outfile.write(stages[event[2]]['color'] + 260 stages[event[2]]['shorthand']) 261 262 if event[3] != last_event_time: # event is not the last one 263 curr_color = stages[event[2]]['color'] 264 else: 265 curr_color = termcap.Normal 266 267 pos = (event[0] / cycle_time) + 1 268 outfile.write(curr_color + dot * (width - pos) + termcap.Normal + 269 ']-(' + str(base_tick + i * time_width).rjust(15) + ') ') 270 if i == 0: 271 outfile.write('%s.%s %s [%s]' % ( 272 inst['pc'].rjust(10), 273 inst['upc'], 274 inst['disasm'].ljust(25), 275 str(inst['sn']).rjust(15))) 276 if timestamps: 277 outfile.write(' f=%s, r=%s' % (inst['fetch'], inst['retire'])) 278 outfile.write('\n') 279 else: 280 outfile.write('...'.center(12) + '\n') 281 282 283def validate_range(my_range): 284 my_range = [int(i) for i in my_range.split(':')] 285 if (len(my_range) != 2 or 286 my_range[0] < 0 or 287 my_range[1] > 0 and my_range[0] >= my_range[1]): 288 return None 289 return my_range 290 291 292def main(): 293 # Parse options 294 usage = ('%prog [OPTION]... TRACE_FILE') 295 parser = optparse.OptionParser(usage=usage) 296 parser.add_option( 297 '-o', 298 dest='outfile', 299 default=os.path.join(os.getcwd(), 'o3-pipeview.out'), 300 help="output file (default: '%default')") 301 parser.add_option( 302 '-t', 303 dest='tick_range', 304 default='0:-1', 305 help="tick range (default: '%default'; -1 == inf.)") 306 parser.add_option( 307 '-i', 308 dest='inst_range', 309 default='0:-1', 310 help="instruction range (default: '%default'; -1 == inf.)") 311 parser.add_option( 312 '-w', 313 dest='width', 314 type='int', default=80, 315 help="timeline width (default: '%default')") 316 parser.add_option( 317 '--color', 318 action='store_true', default=False, 319 help="enable colored output (default: '%default')") 320 parser.add_option( 321 '-c', '--cycle-time', 322 type='int', default=1000, 323 help="CPU cycle time in ticks (default: '%default')") 324 parser.add_option( 325 '--timestamps', 326 action='store_true', default=False, 327 help="print fetch and retire timestamps (default: '%default')") 328 parser.add_option( 329 '--only_committed', 330 action='store_true', default=False, 331 help="display only committed (completed) instructions (default: '%default')") 332 (options, args) = parser.parse_args() 333 if len(args) != 1: 334 parser.error('incorrect number of arguments') 335 sys.exit(1) 336 tick_range = validate_range(options.tick_range) 337 if not tick_range: 338 parser.error('invalid range') 339 sys.exit(1) 340 inst_range = validate_range(options.inst_range) 341 if not inst_range: 342 parser.error('invalid range') 343 sys.exit(1) 344 # Process trace 345 print 'Processing trace... ', 346 with open(args[0], 'r') as trace: 347 with open(options.outfile, 'w') as out: 348 process_trace(trace, out, options.cycle_time, options.width, 349 options.color, options.timestamps, 350 options.only_committed, *(tick_range + inst_range)) 351 print 'done!' 352 353 354if __name__ == '__main__': 355 sys.path.append(os.path.join( 356 os.path.dirname(os.path.abspath(__file__)), 357 '..', 'src', 'python')) 358 main() 359