encode_inst_dep_trace.py revision 11250
111250Sradhika.jagtap@ARM.com#!/usr/bin/env python
211250Sradhika.jagtap@ARM.com
311250Sradhika.jagtap@ARM.com# Copyright (c) 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 ASCII traces of the instruction dependency
7111250Sradhika.jagtap@ARM.com# graph to protobuf 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], " <ASCII input> <protobuf output>"
11211250Sradhika.jagtap@ARM.com        exit(-1)
11311250Sradhika.jagtap@ARM.com
11411250Sradhika.jagtap@ARM.com    # Open the file in write mode
11511250Sradhika.jagtap@ARM.com    proto_out = open(sys.argv[2], 'wb')
11611250Sradhika.jagtap@ARM.com
11711250Sradhika.jagtap@ARM.com    # Open the file in read mode
11811250Sradhika.jagtap@ARM.com    try:
11911250Sradhika.jagtap@ARM.com        ascii_in = open(sys.argv[1], 'r')
12011250Sradhika.jagtap@ARM.com    except IOError:
12111250Sradhika.jagtap@ARM.com        print "Failed to open ", sys.argv[1], " for reading"
12211250Sradhika.jagtap@ARM.com        exit(-1)
12311250Sradhika.jagtap@ARM.com
12411250Sradhika.jagtap@ARM.com    # Write the magic number in 4-byte Little Endian, similar to what
12511250Sradhika.jagtap@ARM.com    # is done in src/proto/protoio.cc
12611250Sradhika.jagtap@ARM.com    proto_out.write("gem5")
12711250Sradhika.jagtap@ARM.com
12811250Sradhika.jagtap@ARM.com    # Add the packet header
12911250Sradhika.jagtap@ARM.com    header = inst_dep_record_pb2.InstDepRecordHeader()
13011250Sradhika.jagtap@ARM.com    header.obj_id = "Converted ASCII trace " + sys.argv[1]
13111250Sradhika.jagtap@ARM.com    # Assume the default tick rate
13211250Sradhika.jagtap@ARM.com    header.tick_freq = 1000000000
13311250Sradhika.jagtap@ARM.com    header.window_size = 120
13411250Sradhika.jagtap@ARM.com    protolib.encodeMessage(proto_out, header)
13511250Sradhika.jagtap@ARM.com
13611250Sradhika.jagtap@ARM.com    num_records = 0
13711250Sradhika.jagtap@ARM.com    # For each line in the ASCII trace, create a packet message and
13811250Sradhika.jagtap@ARM.com    # write it to the encoded output
13911250Sradhika.jagtap@ARM.com    for line in ascii_in:
14011250Sradhika.jagtap@ARM.com        inst_info_str, rob_dep_str, reg_dep_str = (line.strip()).split(':')
14111250Sradhika.jagtap@ARM.com        inst_info_list = inst_info_str.split(',')
14211250Sradhika.jagtap@ARM.com        dep_record = inst_dep_record_pb2.InstDepRecord()
14311250Sradhika.jagtap@ARM.com
14411250Sradhika.jagtap@ARM.com        dep_record.seq_num = long(inst_info_list[0])
14511250Sradhika.jagtap@ARM.com        dep_record.pc = long(inst_info_list[1])
14611250Sradhika.jagtap@ARM.com        dep_record.weight = long(inst_info_list[2])
14711250Sradhika.jagtap@ARM.com        dep_record.load = True if inst_info_list[3] == 'True' else  False
14811250Sradhika.jagtap@ARM.com        dep_record.store = True if inst_info_list[4] == 'True' else  False
14911250Sradhika.jagtap@ARM.com
15011250Sradhika.jagtap@ARM.com        # If the instruction is a load or store record the addr, size flags
15111250Sradhika.jagtap@ARM.com        # in addition to recording the computation delay
15211250Sradhika.jagtap@ARM.com        if dep_record.load or dep_record.store:
15311250Sradhika.jagtap@ARM.com            addr, size, flags, comp_delay = inst_info_list[5:9]
15411250Sradhika.jagtap@ARM.com            dep_record.addr = long(addr)
15511250Sradhika.jagtap@ARM.com            dep_record.size = int(size)
15611250Sradhika.jagtap@ARM.com            dep_record.flags = int(flags)
15711250Sradhika.jagtap@ARM.com            dep_record.comp_delay = long(comp_delay)
15811250Sradhika.jagtap@ARM.com        elif not dep_record.load and not dep_record.store:
15911250Sradhika.jagtap@ARM.com            comp_delay = inst_info_list[4]
16011250Sradhika.jagtap@ARM.com            dep_record.comp_delay = long(comp_delay)
16111250Sradhika.jagtap@ARM.com        else:
16211250Sradhika.jagtap@ARM.com            print "Fatal:", seq_num, "is both load and store"
16311250Sradhika.jagtap@ARM.com            exit(1)
16411250Sradhika.jagtap@ARM.com
16511250Sradhika.jagtap@ARM.com        # Parse the register and order dependencies both of which are
16611250Sradhika.jagtap@ARM.com        # repeated fields. An empty list is valid.
16711250Sradhika.jagtap@ARM.com        rob_deps = rob_dep_str.strip().split(',')
16811250Sradhika.jagtap@ARM.com        for a_dep in rob_deps:
16911250Sradhika.jagtap@ARM.com            # if the string is empty, split(',') returns 1 item: ''
17011250Sradhika.jagtap@ARM.com            # if the string is ",4", split(',') returns 2 items: '', '4'
17111250Sradhika.jagtap@ARM.com            # long('') gives error, so check if the item is non-empty
17211250Sradhika.jagtap@ARM.com            if a_dep:
17311250Sradhika.jagtap@ARM.com                dep_record.rob_dep.append(long(a_dep))
17411250Sradhika.jagtap@ARM.com
17511250Sradhika.jagtap@ARM.com        reg_deps = reg_dep_str.split(',')
17611250Sradhika.jagtap@ARM.com        for a_dep in reg_deps:
17711250Sradhika.jagtap@ARM.com            if a_dep:
17811250Sradhika.jagtap@ARM.com                dep_record.reg_dep.append(long(a_dep))
17911250Sradhika.jagtap@ARM.com
18011250Sradhika.jagtap@ARM.com        protolib.encodeMessage(proto_out, dep_record)
18111250Sradhika.jagtap@ARM.com        num_records += 1
18211250Sradhika.jagtap@ARM.com
18311250Sradhika.jagtap@ARM.com    print "Converted", num_records, "records."
18411250Sradhika.jagtap@ARM.com    # We're done
18511250Sradhika.jagtap@ARM.com    ascii_in.close()
18611250Sradhika.jagtap@ARM.com    proto_out.close()
18711250Sradhika.jagtap@ARM.com
18811250Sradhika.jagtap@ARM.comif __name__ == "__main__":
18911250Sradhika.jagtap@ARM.com    main()
190