decode_inst_dep_trace.py revision 11250
111250Sradhika.jagtap@ARM.com#!/usr/bin/env python 211250Sradhika.jagtap@ARM.com 311250Sradhika.jagtap@ARM.com# Copyright (c) 2013 - 2015 ARM Limited 411250Sradhika.jagtap@ARM.com# All rights reserved 511250Sradhika.jagtap@ARM.com# 611250Sradhika.jagtap@ARM.com# The license below extends only to copyright in the software and shall 711250Sradhika.jagtap@ARM.com# not be construed as granting a license to any other intellectual 811250Sradhika.jagtap@ARM.com# property including but not limited to intellectual property relating 911250Sradhika.jagtap@ARM.com# to a hardware implementation of the functionality of the software 1011250Sradhika.jagtap@ARM.com# licensed hereunder. You may use the software subject to the license 1111250Sradhika.jagtap@ARM.com# terms below provided that you ensure that this notice is replicated 1211250Sradhika.jagtap@ARM.com# unmodified and in its entirety in all distributions of the software, 1311250Sradhika.jagtap@ARM.com# modified or unmodified, in source code or in binary form. 1411250Sradhika.jagtap@ARM.com# 1511250Sradhika.jagtap@ARM.com# Redistribution and use in source and binary forms, with or without 1611250Sradhika.jagtap@ARM.com# modification, are permitted provided that the following conditions are 1711250Sradhika.jagtap@ARM.com# met: redistributions of source code must retain the above copyright 1811250Sradhika.jagtap@ARM.com# notice, this list of conditions and the following disclaimer; 1911250Sradhika.jagtap@ARM.com# redistributions in binary form must reproduce the above copyright 2011250Sradhika.jagtap@ARM.com# notice, this list of conditions and the following disclaimer in the 2111250Sradhika.jagtap@ARM.com# documentation and/or other materials provided with the distribution; 2211250Sradhika.jagtap@ARM.com# neither the name of the copyright holders nor the names of its 2311250Sradhika.jagtap@ARM.com# contributors may be used to endorse or promote products derived from 2411250Sradhika.jagtap@ARM.com# this software without specific prior written permission. 2511250Sradhika.jagtap@ARM.com# 2611250Sradhika.jagtap@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2711250Sradhika.jagtap@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2811250Sradhika.jagtap@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2911250Sradhika.jagtap@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3011250Sradhika.jagtap@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3111250Sradhika.jagtap@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3211250Sradhika.jagtap@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3311250Sradhika.jagtap@ARM.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3411250Sradhika.jagtap@ARM.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3511250Sradhika.jagtap@ARM.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3611250Sradhika.jagtap@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3711250Sradhika.jagtap@ARM.com# 3811250Sradhika.jagtap@ARM.com# Copyright 2008 Google Inc. All rights reserved. 3911250Sradhika.jagtap@ARM.com# http://code.google.com/p/protobuf/ 4011250Sradhika.jagtap@ARM.com# 4111250Sradhika.jagtap@ARM.com# Redistribution and use in source and binary forms, with or without 4211250Sradhika.jagtap@ARM.com# modification, are permitted provided that the following conditions are 4311250Sradhika.jagtap@ARM.com# met: 4411250Sradhika.jagtap@ARM.com# 4511250Sradhika.jagtap@ARM.com# * Redistributions of source code must retain the above copyright 4611250Sradhika.jagtap@ARM.com# notice, this list of conditions and the following disclaimer. 4711250Sradhika.jagtap@ARM.com# * Redistributions in binary form must reproduce the above 4811250Sradhika.jagtap@ARM.com# copyright notice, this list of conditions and the following disclaimer 4911250Sradhika.jagtap@ARM.com# in the documentation and/or other materials provided with the 5011250Sradhika.jagtap@ARM.com# distribution. 5111250Sradhika.jagtap@ARM.com# * Neither the name of Google Inc. nor the names of its 5211250Sradhika.jagtap@ARM.com# contributors may be used to endorse or promote products derived from 5311250Sradhika.jagtap@ARM.com# this software without specific prior written permission. 5411250Sradhika.jagtap@ARM.com# 5511250Sradhika.jagtap@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 5611250Sradhika.jagtap@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 5711250Sradhika.jagtap@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 5811250Sradhika.jagtap@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 5911250Sradhika.jagtap@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 6011250Sradhika.jagtap@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 6111250Sradhika.jagtap@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 6211250Sradhika.jagtap@ARM.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 6311250Sradhika.jagtap@ARM.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 6411250Sradhika.jagtap@ARM.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 6511250Sradhika.jagtap@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6611250Sradhika.jagtap@ARM.com# 6711250Sradhika.jagtap@ARM.com# Authors: Radhika Jagtap 6811250Sradhika.jagtap@ARM.com# 6911250Sradhika.jagtap@ARM.com 7011250Sradhika.jagtap@ARM.com# This script is used to dump protobuf traces of the instruction dependency 7111250Sradhika.jagtap@ARM.com# graph to ASCII format. 7211250Sradhika.jagtap@ARM.com# 7311250Sradhika.jagtap@ARM.com# The ASCII trace format uses one line per instruction with the format 7411250Sradhika.jagtap@ARM.com# instruction sequence number, (optional) pc, (optional) weight, load, store, 7511250Sradhika.jagtap@ARM.com# (optional) flags, (optional) addr, (optional) size, comp delay, 7611250Sradhika.jagtap@ARM.com# (repeated) order dependencies comma-separated, and (repeated) register 7711250Sradhika.jagtap@ARM.com# dependencies comma-separated. 7811250Sradhika.jagtap@ARM.com# 7911250Sradhika.jagtap@ARM.com# examples: 8011250Sradhika.jagtap@ARM.com# seq_num,[pc],[weight,]load,store,[address,size,flags,]comp_delay:[rob_dep]: 8111250Sradhika.jagtap@ARM.com# [reg_dep] 8211250Sradhika.jagtap@ARM.com# 1,1,False,False,8500:: 8311250Sradhika.jagtap@ARM.com# 2,1,False,False,1000:,1: 8411250Sradhika.jagtap@ARM.com# 3,1,True,False,831248,4,74,500:,2: 8511250Sradhika.jagtap@ARM.com# 4,1,False,False,0:,2: 8611250Sradhika.jagtap@ARM.com# 5,1,False,False,500::,4 8711250Sradhika.jagtap@ARM.com# 6,1,False,True,831248,4,74,1000:,3:,4,5 8811250Sradhika.jagtap@ARM.com 8911250Sradhika.jagtap@ARM.comimport protolib 9011250Sradhika.jagtap@ARM.comimport sys 9111250Sradhika.jagtap@ARM.com 9211250Sradhika.jagtap@ARM.com# Import the packet proto definitions. If they are not found, attempt 9311250Sradhika.jagtap@ARM.com# to generate them automatically. This assumes that the script is 9411250Sradhika.jagtap@ARM.com# executed from the gem5 root. 9511250Sradhika.jagtap@ARM.comtry: 9611250Sradhika.jagtap@ARM.com import inst_dep_record_pb2 9711250Sradhika.jagtap@ARM.comexcept: 9811250Sradhika.jagtap@ARM.com print "Did not find proto definition, attempting to generate" 9911250Sradhika.jagtap@ARM.com from subprocess import call 10011250Sradhika.jagtap@ARM.com error = call(['protoc', '--python_out=util', '--proto_path=src/proto', 10111250Sradhika.jagtap@ARM.com 'src/proto/inst_dep_record.proto']) 10211250Sradhika.jagtap@ARM.com if not error: 10311250Sradhika.jagtap@ARM.com import inst_dep_record_pb2 10411250Sradhika.jagtap@ARM.com print "Generated proto definitions for instruction dependency record" 10511250Sradhika.jagtap@ARM.com else: 10611250Sradhika.jagtap@ARM.com print "Failed to import proto definitions" 10711250Sradhika.jagtap@ARM.com exit(-1) 10811250Sradhika.jagtap@ARM.com 10911250Sradhika.jagtap@ARM.comdef main(): 11011250Sradhika.jagtap@ARM.com if len(sys.argv) != 3: 11111250Sradhika.jagtap@ARM.com print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" 11211250Sradhika.jagtap@ARM.com exit(-1) 11311250Sradhika.jagtap@ARM.com 11411250Sradhika.jagtap@ARM.com # Open the file on read mode 11511250Sradhika.jagtap@ARM.com proto_in = protolib.openFileRd(sys.argv[1]) 11611250Sradhika.jagtap@ARM.com 11711250Sradhika.jagtap@ARM.com try: 11811250Sradhika.jagtap@ARM.com ascii_out = open(sys.argv[2], 'w') 11911250Sradhika.jagtap@ARM.com except IOError: 12011250Sradhika.jagtap@ARM.com print "Failed to open ", sys.argv[2], " for writing" 12111250Sradhika.jagtap@ARM.com exit(-1) 12211250Sradhika.jagtap@ARM.com 12311250Sradhika.jagtap@ARM.com # Read the magic number in 4-byte Little Endian 12411250Sradhika.jagtap@ARM.com magic_number = proto_in.read(4) 12511250Sradhika.jagtap@ARM.com 12611250Sradhika.jagtap@ARM.com if magic_number != "gem5": 12711250Sradhika.jagtap@ARM.com print "Unrecognized file" 12811250Sradhika.jagtap@ARM.com exit(-1) 12911250Sradhika.jagtap@ARM.com 13011250Sradhika.jagtap@ARM.com print "Parsing packet header" 13111250Sradhika.jagtap@ARM.com 13211250Sradhika.jagtap@ARM.com # Add the packet header 13311250Sradhika.jagtap@ARM.com header = inst_dep_record_pb2.InstDepRecordHeader() 13411250Sradhika.jagtap@ARM.com protolib.decodeMessage(proto_in, header) 13511250Sradhika.jagtap@ARM.com 13611250Sradhika.jagtap@ARM.com print "Object id:", header.obj_id 13711250Sradhika.jagtap@ARM.com print "Tick frequency:", header.tick_freq 13811250Sradhika.jagtap@ARM.com 13911250Sradhika.jagtap@ARM.com print "Parsing packets" 14011250Sradhika.jagtap@ARM.com 14111250Sradhika.jagtap@ARM.com num_packets = 0 14211250Sradhika.jagtap@ARM.com num_regdeps = 0 14311250Sradhika.jagtap@ARM.com num_robdeps = 0 14411250Sradhika.jagtap@ARM.com packet = inst_dep_record_pb2.InstDepRecord() 14511250Sradhika.jagtap@ARM.com 14611250Sradhika.jagtap@ARM.com # Decode the packet messages until we hit the end of the file 14711250Sradhika.jagtap@ARM.com while protolib.decodeMessage(proto_in, packet): 14811250Sradhika.jagtap@ARM.com num_packets += 1 14911250Sradhika.jagtap@ARM.com 15011250Sradhika.jagtap@ARM.com # Write to file the seq num 15111250Sradhika.jagtap@ARM.com ascii_out.write('%s' % (packet.seq_num)) 15211250Sradhika.jagtap@ARM.com # Write to file the pc of the instruction, default is 0 15311250Sradhika.jagtap@ARM.com if packet.HasField('pc'): 15411250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % (packet.pc)) 15511250Sradhika.jagtap@ARM.com else: 15611250Sradhika.jagtap@ARM.com ascii_out.write(',0') 15711250Sradhika.jagtap@ARM.com # Write to file the weight, default is 1 15811250Sradhika.jagtap@ARM.com if packet.HasField('weight'): 15911250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % (packet.weight)) 16011250Sradhika.jagtap@ARM.com else: 16111250Sradhika.jagtap@ARM.com ascii_out.write(',1') 16211250Sradhika.jagtap@ARM.com # Write to file if it is a load and if it is a store 16311250Sradhika.jagtap@ARM.com ascii_out.write(',%s,%s' % (packet.load, packet.store)) 16411250Sradhika.jagtap@ARM.com 16511250Sradhika.jagtap@ARM.com # Write to file if it has the optional fields addr, size, flags 16611250Sradhika.jagtap@ARM.com if packet.HasField('addr'): 16711250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % (packet.addr)) 16811250Sradhika.jagtap@ARM.com if packet.HasField('size'): 16911250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % (packet.size)) 17011250Sradhika.jagtap@ARM.com if packet.HasField('flags'): 17111250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % (packet.flags)) 17211250Sradhika.jagtap@ARM.com 17311250Sradhika.jagtap@ARM.com # Write to file the comp delay 17411250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % (packet.comp_delay)) 17511250Sradhika.jagtap@ARM.com 17611250Sradhika.jagtap@ARM.com # Write to file the repeated field order dependency 17711250Sradhika.jagtap@ARM.com ascii_out.write(':') 17811250Sradhika.jagtap@ARM.com if packet.rob_dep: 17911250Sradhika.jagtap@ARM.com num_robdeps += 1 18011250Sradhika.jagtap@ARM.com for dep in packet.rob_dep: 18111250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % dep) 18211250Sradhika.jagtap@ARM.com # Write to file the repeated field register dependency 18311250Sradhika.jagtap@ARM.com ascii_out.write(':') 18411250Sradhika.jagtap@ARM.com if packet.reg_dep: 18511250Sradhika.jagtap@ARM.com num_regdeps += 1 # No. of packets with atleast 1 register dependency 18611250Sradhika.jagtap@ARM.com for dep in packet.reg_dep: 18711250Sradhika.jagtap@ARM.com ascii_out.write(',%s' % dep) 18811250Sradhika.jagtap@ARM.com # New line 18911250Sradhika.jagtap@ARM.com ascii_out.write('\n') 19011250Sradhika.jagtap@ARM.com 19111250Sradhika.jagtap@ARM.com print "Parsed packets:", num_packets 19211250Sradhika.jagtap@ARM.com print "Packets with at least 1 reg dep:", num_regdeps 19311250Sradhika.jagtap@ARM.com print "Packets with at least 1 rob dep:", num_robdeps 19411250Sradhika.jagtap@ARM.com 19511250Sradhika.jagtap@ARM.com # We're done 19611250Sradhika.jagtap@ARM.com ascii_out.close() 19711250Sradhika.jagtap@ARM.com proto_in.close() 19811250Sradhika.jagtap@ARM.com 19911250Sradhika.jagtap@ARM.comif __name__ == "__main__": 20011250Sradhika.jagtap@ARM.com main() 201