decode_inst_dep_trace.py revision 13540:da30e62884ee
16019Shines@cs.fsu.edu#!/usr/bin/env python2.7
27189Sgblack@eecs.umich.edu
37189Sgblack@eecs.umich.edu# Copyright (c) 2013 - 2015 ARM Limited
47189Sgblack@eecs.umich.edu# All rights reserved
57189Sgblack@eecs.umich.edu#
67189Sgblack@eecs.umich.edu# The license below extends only to copyright in the software and shall
77189Sgblack@eecs.umich.edu# not be construed as granting a license to any other intellectual
87189Sgblack@eecs.umich.edu# property including but not limited to intellectual property relating
97189Sgblack@eecs.umich.edu# to a hardware implementation of the functionality of the software
107189Sgblack@eecs.umich.edu# licensed hereunder.  You may use the software subject to the license
117189Sgblack@eecs.umich.edu# terms below provided that you ensure that this notice is replicated
127189Sgblack@eecs.umich.edu# unmodified and in its entirety in all distributions of the software,
137189Sgblack@eecs.umich.edu# modified or unmodified, in source code or in binary form.
146019Shines@cs.fsu.edu#
156019Shines@cs.fsu.edu# Redistribution and use in source and binary forms, with or without
166019Shines@cs.fsu.edu# modification, are permitted provided that the following conditions are
176019Shines@cs.fsu.edu# met: redistributions of source code must retain the above copyright
186019Shines@cs.fsu.edu# notice, this list of conditions and the following disclaimer;
196019Shines@cs.fsu.edu# redistributions in binary form must reproduce the above copyright
206019Shines@cs.fsu.edu# notice, this list of conditions and the following disclaimer in the
216019Shines@cs.fsu.edu# documentation and/or other materials provided with the distribution;
226019Shines@cs.fsu.edu# neither the name of the copyright holders nor the names of its
236019Shines@cs.fsu.edu# contributors may be used to endorse or promote products derived from
246019Shines@cs.fsu.edu# this software without specific prior written permission.
256019Shines@cs.fsu.edu#
266019Shines@cs.fsu.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
276019Shines@cs.fsu.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
286019Shines@cs.fsu.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
296019Shines@cs.fsu.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
306019Shines@cs.fsu.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
316019Shines@cs.fsu.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
326019Shines@cs.fsu.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
336019Shines@cs.fsu.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
346019Shines@cs.fsu.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
356019Shines@cs.fsu.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
366019Shines@cs.fsu.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
376019Shines@cs.fsu.edu#
386019Shines@cs.fsu.edu# Copyright 2008 Google Inc.  All rights reserved.
396019Shines@cs.fsu.edu# http://code.google.com/p/protobuf/
406019Shines@cs.fsu.edu#
416735Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
426735Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
436019Shines@cs.fsu.edu# met:
446019Shines@cs.fsu.edu#
456019Shines@cs.fsu.edu#     * Redistributions of source code must retain the above copyright
466019Shines@cs.fsu.edu# notice, this list of conditions and the following disclaimer.
476019Shines@cs.fsu.edu#     * Redistributions in binary form must reproduce the above
487362Sgblack@eecs.umich.edu# copyright notice, this list of conditions and the following disclaimer
496735Sgblack@eecs.umich.edu# in the documentation and/or other materials provided with the
508229Snate@binkert.org# distribution.
516735Sgblack@eecs.umich.edu#     * Neither the name of Google Inc. nor the names of its
526019Shines@cs.fsu.edu# contributors may be used to endorse or promote products derived from
536019Shines@cs.fsu.edu# this software without specific prior written permission.
546019Shines@cs.fsu.edu#
556019Shines@cs.fsu.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
566019Shines@cs.fsu.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
576019Shines@cs.fsu.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
586735Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
596019Shines@cs.fsu.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
607362Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
616019Shines@cs.fsu.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
626019Shines@cs.fsu.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
636735Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
646735Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
656019Shines@cs.fsu.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
667362Sgblack@eecs.umich.edu#
677362Sgblack@eecs.umich.edu# Authors: Radhika Jagtap
687362Sgblack@eecs.umich.edu#
697362Sgblack@eecs.umich.edu
707362Sgblack@eecs.umich.edu# This script is used to dump protobuf traces of the instruction dependency
717362Sgblack@eecs.umich.edu# graph to ASCII format.
727362Sgblack@eecs.umich.edu#
737362Sgblack@eecs.umich.edu# The ASCII trace format uses one line per instruction with the format
747362Sgblack@eecs.umich.edu# instruction sequence number, (optional) pc, (optional) weight, type
757362Sgblack@eecs.umich.edu# (optional) flags, (optional) phys addr, (optional) size, comp delay,
767362Sgblack@eecs.umich.edu# (repeated) order dependencies comma-separated, and (repeated) register
777362Sgblack@eecs.umich.edu# dependencies comma-separated.
787362Sgblack@eecs.umich.edu#
797595SGene.Wu@arm.com# examples:
807362Sgblack@eecs.umich.edu# seq_num,[pc],[weight,]type,[p_addr,size,flags,]comp_delay:[rob_dep]:
817404SAli.Saidi@ARM.com# [reg_dep]
827362Sgblack@eecs.umich.edu# 1,35652,1,COMP,8500::
837404SAli.Saidi@ARM.com# 2,35656,1,COMP,0:,1:
847362Sgblack@eecs.umich.edu# 3,35660,1,LOAD,1748752,4,74,500:,2:
857362Sgblack@eecs.umich.edu# 4,35660,1,COMP,0:,3:
867362Sgblack@eecs.umich.edu# 5,35664,1,COMP,3000::,4
877362Sgblack@eecs.umich.edu# 6,35666,1,STORE,1748752,4,74,1000:,3:,4,5
887404SAli.Saidi@ARM.com# 7,35666,1,COMP,3000::,4
897404SAli.Saidi@ARM.com# 8,35670,1,STORE,1748748,4,74,0:,6,3:,7
907611SGene.Wu@arm.com# 9,35670,1,COMP,500::,7
917611SGene.Wu@arm.com
927611SGene.Wu@arm.comimport protolib
937611SGene.Wu@arm.comimport sys
947611SGene.Wu@arm.com
957850SMatt.Horsnell@arm.com# Import the packet proto definitions. If they are not found, attempt
967850SMatt.Horsnell@arm.com# to generate them automatically. This assumes that the script is
977362Sgblack@eecs.umich.edu# executed from the gem5 root.
987362Sgblack@eecs.umich.edutry:
996735Sgblack@eecs.umich.edu    import inst_dep_record_pb2
1006735Sgblack@eecs.umich.eduexcept:
1016735Sgblack@eecs.umich.edu    print "Did not find proto definition, attempting to generate"
1026735Sgblack@eecs.umich.edu    from subprocess import call
1036735Sgblack@eecs.umich.edu    error = call(['protoc', '--python_out=util', '--proto_path=src/proto',
1046735Sgblack@eecs.umich.edu                  'src/proto/inst_dep_record.proto'])
1056735Sgblack@eecs.umich.edu    if not error:
1066735Sgblack@eecs.umich.edu        import inst_dep_record_pb2
1076735Sgblack@eecs.umich.edu        print "Generated proto definitions for instruction dependency record"
1086735Sgblack@eecs.umich.edu    else:
1096735Sgblack@eecs.umich.edu        print "Failed to import proto definitions"
1106735Sgblack@eecs.umich.edu        exit(-1)
1116019Shines@cs.fsu.edu
1127678Sgblack@eecs.umich.edudef main():
1137678Sgblack@eecs.umich.edu    if len(sys.argv) != 3:
1146019Shines@cs.fsu.edu        print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>"
1156735Sgblack@eecs.umich.edu        exit(-1)
1166735Sgblack@eecs.umich.edu
1176735Sgblack@eecs.umich.edu    # Open the file on read mode
1186735Sgblack@eecs.umich.edu    proto_in = protolib.openFileRd(sys.argv[1])
1196735Sgblack@eecs.umich.edu
1206735Sgblack@eecs.umich.edu    try:
1216735Sgblack@eecs.umich.edu        ascii_out = open(sys.argv[2], 'w')
1226019Shines@cs.fsu.edu    except IOError:
1236019Shines@cs.fsu.edu        print "Failed to open ", sys.argv[2], " for writing"
1246735Sgblack@eecs.umich.edu        exit(-1)
1257362Sgblack@eecs.umich.edu
1266019Shines@cs.fsu.edu    # Read the magic number in 4-byte Little Endian
1276735Sgblack@eecs.umich.edu    magic_number = proto_in.read(4)
1286735Sgblack@eecs.umich.edu
1296735Sgblack@eecs.umich.edu    if magic_number != "gem5":
1306019Shines@cs.fsu.edu        print "Unrecognized file"
1316735Sgblack@eecs.umich.edu        exit(-1)
1326735Sgblack@eecs.umich.edu
1336735Sgblack@eecs.umich.edu    print "Parsing packet header"
1346735Sgblack@eecs.umich.edu
1356735Sgblack@eecs.umich.edu    # Add the packet header
1366735Sgblack@eecs.umich.edu    header = inst_dep_record_pb2.InstDepRecordHeader()
1376735Sgblack@eecs.umich.edu    protolib.decodeMessage(proto_in, header)
1386735Sgblack@eecs.umich.edu
1396019Shines@cs.fsu.edu    print "Object id:", header.obj_id
1406019Shines@cs.fsu.edu    print "Tick frequency:", header.tick_freq
1417400SAli.Saidi@ARM.com
1427400SAli.Saidi@ARM.com    print "Parsing packets"
1437400SAli.Saidi@ARM.com
1447400SAli.Saidi@ARM.com    print "Creating enum value,name lookup from proto"
1457678Sgblack@eecs.umich.edu    enumNames = {}
1467678Sgblack@eecs.umich.edu    desc = inst_dep_record_pb2.InstDepRecord.DESCRIPTOR
1477400SAli.Saidi@ARM.com    for namestr, valdesc in desc.enum_values_by_name.items():
1487400SAli.Saidi@ARM.com        print '\t', valdesc.number, namestr
1497400SAli.Saidi@ARM.com        enumNames[valdesc.number] = namestr
1507400SAli.Saidi@ARM.com
1517189Sgblack@eecs.umich.edu    num_packets = 0
1527362Sgblack@eecs.umich.edu    num_regdeps = 0
1537189Sgblack@eecs.umich.edu    num_robdeps = 0
1547189Sgblack@eecs.umich.edu    packet = inst_dep_record_pb2.InstDepRecord()
1557189Sgblack@eecs.umich.edu
1567189Sgblack@eecs.umich.edu    # Decode the packet messages until we hit the end of the file
1577189Sgblack@eecs.umich.edu    while protolib.decodeMessage(proto_in, packet):
1587189Sgblack@eecs.umich.edu        num_packets += 1
1597640Sgblack@eecs.umich.edu
1607189Sgblack@eecs.umich.edu        # Write to file the seq num
1617189Sgblack@eecs.umich.edu        ascii_out.write('%s' % (packet.seq_num))
1627189Sgblack@eecs.umich.edu        # Write to file the pc of the instruction, default is 0
1637189Sgblack@eecs.umich.edu        if packet.HasField('pc'):
1647640Sgblack@eecs.umich.edu            ascii_out.write(',%s' % (packet.pc))
1657640Sgblack@eecs.umich.edu        else:
1667640Sgblack@eecs.umich.edu            ascii_out.write(',0')
1677640Sgblack@eecs.umich.edu        # Write to file the weight, default is 1
1687189Sgblack@eecs.umich.edu        if packet.HasField('weight'):
1697189Sgblack@eecs.umich.edu            ascii_out.write(',%s' % (packet.weight))
1707189Sgblack@eecs.umich.edu        else:
1717678Sgblack@eecs.umich.edu            ascii_out.write(',1')
1727678Sgblack@eecs.umich.edu        # Write to file the type of the record
1737189Sgblack@eecs.umich.edu        try:
1747189Sgblack@eecs.umich.edu            ascii_out.write(',%s' % enumNames[packet.type])
1757189Sgblack@eecs.umich.edu        except KeyError:
1767362Sgblack@eecs.umich.edu            print "Seq. num", packet.seq_num, "has unsupported type", \
1777197Sgblack@eecs.umich.edu                packet.type
1787197Sgblack@eecs.umich.edu            exit(-1)
1797197Sgblack@eecs.umich.edu
1807197Sgblack@eecs.umich.edu
1817197Sgblack@eecs.umich.edu        # Write to file if it has the optional fields physical addr, size,
1827197Sgblack@eecs.umich.edu        # flags
1837197Sgblack@eecs.umich.edu        if packet.HasField('p_addr'):
1847197Sgblack@eecs.umich.edu            ascii_out.write(',%s' % (packet.p_addr))
1857197Sgblack@eecs.umich.edu        if packet.HasField('size'):
1867678Sgblack@eecs.umich.edu            ascii_out.write(',%s' % (packet.size))
1877678Sgblack@eecs.umich.edu        if packet.HasField('flags'):
1887197Sgblack@eecs.umich.edu            ascii_out.write(',%s' % (packet.flags))
1897197Sgblack@eecs.umich.edu
1907362Sgblack@eecs.umich.edu        # Write to file the comp delay
1917362Sgblack@eecs.umich.edu        ascii_out.write(',%s' % (packet.comp_delay))
1927362Sgblack@eecs.umich.edu
1937362Sgblack@eecs.umich.edu        # Write to file the repeated field order dependency
1947362Sgblack@eecs.umich.edu        ascii_out.write(':')
1957362Sgblack@eecs.umich.edu        if packet.rob_dep:
1967362Sgblack@eecs.umich.edu            num_robdeps += 1
1977362Sgblack@eecs.umich.edu            for dep in packet.rob_dep:
1987362Sgblack@eecs.umich.edu                ascii_out.write(',%s' % dep)
1997362Sgblack@eecs.umich.edu        # Write to file the repeated field register dependency
2007362Sgblack@eecs.umich.edu        ascii_out.write(':')
2017362Sgblack@eecs.umich.edu        if packet.reg_dep:
2027362Sgblack@eecs.umich.edu            num_regdeps += 1 # No. of packets with atleast 1 register dependency
2037362Sgblack@eecs.umich.edu            for dep in packet.reg_dep:
2047362Sgblack@eecs.umich.edu                ascii_out.write(',%s' % dep)
2057362Sgblack@eecs.umich.edu        # New line
2067362Sgblack@eecs.umich.edu        ascii_out.write('\n')
2077678Sgblack@eecs.umich.edu
2087678Sgblack@eecs.umich.edu    print "Parsed packets:", num_packets
2097362Sgblack@eecs.umich.edu    print "Packets with at least 1 reg dep:", num_regdeps
2107362Sgblack@eecs.umich.edu    print "Packets with at least 1 rob dep:", num_robdeps
2117362Sgblack@eecs.umich.edu
2127362Sgblack@eecs.umich.edu    # We're done
2137362Sgblack@eecs.umich.edu    ascii_out.close()
2147362Sgblack@eecs.umich.edu    proto_in.close()
2157362Sgblack@eecs.umich.edu
2167362Sgblack@eecs.umich.eduif __name__ == "__main__":
2177362Sgblack@eecs.umich.edu    main()
2187362Sgblack@eecs.umich.edu