o3-pipeview.py (9188:b91e4bec7a76) | o3-pipeview.py (9252:f350fac86d0f) |
---|---|
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 --- 28 unchanged lines hidden (view full) --- 37# 38# Authors: Giacomo Gabrielli 39 40# Pipeline activity viewer for the O3 CPU model. 41 42import optparse 43import os 44import sys | 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 --- 28 unchanged lines hidden (view full) --- 37# 38# Authors: Giacomo Gabrielli 39 40# Pipeline activity viewer for the O3 CPU model. 41 42import optparse 43import os 44import sys |
45import copy |
|
45 | 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} |
|
46 47def process_trace(trace, outfile, cycle_time, width, color, timestamps, | 69 70def process_trace(trace, outfile, cycle_time, width, color, timestamps, |
48 start_tick, stop_tick, start_sn, stop_sn): | 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 |
49 line = None 50 fields = None | 80 line = None 81 fields = None |
51 # Skip lines up to region of interest | 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 |
52 if start_tick != 0: 53 while True: | 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 |
|
54 line = trace.readline() 55 if not line: return 56 fields = line.split(':') | 94 line = trace.readline() 95 if not line: return 96 fields = line.split(':') |
57 if fields[0] != 'O3PipeView': continue 58 if int(fields[2]) >= start_tick: break 59 elif start_sn != 0: | 97 98 # Skip lines up to the starting sequence number 99 if start_sn != 0: |
60 while True: | 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 |
|
61 line = trace.readline() 62 if not line: return 63 fields = line.split(':') | 105 line = trace.readline() 106 if not line: return 107 fields = line.split(':') |
64 if fields[0] != 'O3PipeView': continue 65 if fields[1] == 'fetch' and int(fields[5]) >= start_sn: break 66 else: 67 line = trace.readline() 68 if not line: return 69 fields = line.split(':') | 108 |
70 # Skip lines up to next instruction fetch 71 while fields[0] != 'O3PipeView' or fields[1] != 'fetch': 72 line = trace.readline() 73 if not line: return 74 fields = line.split(':') | 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 |
|
75 # Print header 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)) 83 if timestamps: 84 outfile.write('timestamps'.center(25)) 85 outfile.write('\n') | 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 |
|
86 # Region of interest 87 curr_inst = {} 88 while True: 89 if fields[0] == 'O3PipeView': 90 curr_inst[fields[1]] = int(fields[2]) 91 if fields[1] == 'fetch': | 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': |
92 if ((stop_tick > 0 and int(fields[2]) > stop_tick) or 93 (stop_sn > 0 and int(fields[5]) > stop_sn)): | 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) |
94 return 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': | 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': |
99 print_inst(outfile, curr_inst, cycle_time, width, color, 100 timestamps) | 141 queue_inst(outfile, curr_inst, cycle_time, width, color, timestamps) |
101 line = trace.readline() 102 if not line: return 103 fields = line.split(':') 104 105 | 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 |
|
106def print_inst(outfile, inst, cycle_time, width, color, timestamps): 107 if color: 108 from m5.util.terminal import termcap 109 else: 110 from m5.util.terminal import no_termcap as termcap 111 # Pipeline stages 112 stages = [{'name': 'fetch', 113 'color': termcap.Blue + termcap.Reverse, --- 11 unchanged lines hidden (view full) --- 125 'color': termcap.Red + termcap.Reverse, 126 'shorthand': 'i'}, 127 {'name': 'complete', 128 'color': termcap.Cyan + termcap.Reverse, 129 'shorthand': 'c'}, 130 {'name': 'retire', 131 'color': termcap.Blue + termcap.Reverse, 132 'shorthand': 'r'}] | 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, --- 11 unchanged lines hidden (view full) --- 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 |
|
133 # Print | 213 # Print |
214 |
|
134 time_width = width * cycle_time 135 base_tick = (inst['fetch'] / time_width) * time_width | 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 |
|
136 # Timeline shorter then time_width is printed in compact form where 137 # the print continues at the start of the same line. | 223 # Timeline shorter then time_width is printed in compact form where 224 # the print continues at the start of the same line. |
138 if ((inst['retire'] - inst['fetch']) < time_width): | 225 if ((last_event_time - inst['fetch']) < time_width): |
139 num_lines = 1 # compact form 140 else: | 226 num_lines = 1 # compact form 227 else: |
141 num_lines = ((inst['retire'] - base_tick) / time_width) + 1 | 228 num_lines = ((last_event_time - base_tick) / time_width) + 1 |
142 143 curr_color = termcap.Normal | 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 |
|
144 for i in range(num_lines): 145 start_tick = base_tick + i * time_width 146 end_tick = start_tick + time_width 147 if num_lines == 1: # compact form 148 end_tick += (inst['fetch'] - base_tick) 149 events = [] 150 for stage_idx in range(len(stages)): 151 tick = inst[stages[stage_idx]['name']] | 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']] |
152 if tick >= start_tick and tick < end_tick: 153 events.append((tick % time_width, 154 stages[stage_idx]['name'], 155 stage_idx)) | 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)) |
156 events.sort() 157 outfile.write('[') 158 pos = 0 159 if num_lines == 1 and events[0][2] != 0: # event is not fetch 160 curr_color = stages[events[0][2] - 1]['color'] 161 for event in events: 162 if (stages[event[2]]['name'] == 'dispatch' and 163 inst['dispatch'] == inst['issue']): 164 continue | 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 |
165 outfile.write(curr_color + '.' * ((event[0] / cycle_time) - pos)) | 258 outfile.write(curr_color + dot * ((event[0] / cycle_time) - pos)) |
166 outfile.write(stages[event[2]]['color'] + 167 stages[event[2]]['shorthand']) | 259 outfile.write(stages[event[2]]['color'] + 260 stages[event[2]]['shorthand']) |
168 if event[2] != len(stages) - 1: # event is not retire | 261 262 if event[3] != last_event_time: # event is not the last one |
169 curr_color = stages[event[2]]['color'] 170 else: 171 curr_color = termcap.Normal | 263 curr_color = stages[event[2]]['color'] 264 else: 265 curr_color = termcap.Normal |
266 |
|
172 pos = (event[0] / cycle_time) + 1 | 267 pos = (event[0] / cycle_time) + 1 |
173 outfile.write(curr_color + '.' * (width - pos) + termcap.Normal + | 268 outfile.write(curr_color + dot * (width - pos) + termcap.Normal + |
174 ']-(' + str(base_tick + i * time_width).rjust(15) + ') ') 175 if i == 0: 176 outfile.write('%s.%s %s [%s]' % ( 177 inst['pc'].rjust(10), 178 inst['upc'], 179 inst['disasm'].ljust(25), 180 str(inst['sn']).rjust(15))) 181 if timestamps: --- 43 unchanged lines hidden (view full) --- 225 parser.add_option( 226 '-c', '--cycle-time', 227 type='int', default=1000, 228 help="CPU cycle time in ticks (default: '%default')") 229 parser.add_option( 230 '--timestamps', 231 action='store_true', default=False, 232 help="print fetch and retire timestamps (default: '%default')") | 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: --- 43 unchanged lines hidden (view full) --- 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')") |
|
233 (options, args) = parser.parse_args() 234 if len(args) != 1: 235 parser.error('incorrect number of arguments') 236 sys.exit(1) 237 tick_range = validate_range(options.tick_range) 238 if not tick_range: 239 parser.error('invalid range') 240 sys.exit(1) 241 inst_range = validate_range(options.inst_range) 242 if not inst_range: 243 parser.error('invalid range') 244 sys.exit(1) 245 # Process trace 246 print 'Processing trace... ', 247 with open(args[0], 'r') as trace: 248 with open(options.outfile, 'w') as out: 249 process_trace(trace, out, options.cycle_time, options.width, 250 options.color, options.timestamps, | 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, |
251 *(tick_range + inst_range)) | 350 options.only_committed, *(tick_range + inst_range)) |
252 print 'done!' 253 254 255if __name__ == '__main__': 256 sys.path.append(os.path.join( 257 os.path.dirname(os.path.abspath(__file__)), 258 '..', 'src', 'python')) 259 main() | 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() |