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 --- 50 unchanged lines hidden (view full) --- 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 --- 4 unchanged lines hidden (view full) --- 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'}, --- 9 unchanged lines hidden (view full) --- 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 --- 33 unchanged lines hidden (view full) --- 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): --- 40 unchanged lines hidden (view full) --- 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() |