decode_packet_trace.py revision 10269:82773ace39fa
12914SN/A#!/usr/bin/env python
210713Sandreas.hansson@arm.com
38856SN/A# Copyright (c) 2013-2014 ARM Limited
48856SN/A# All rights reserved
58856SN/A#
68856SN/A# The license below extends only to copyright in the software and shall
78856SN/A# not be construed as granting a license to any other intellectual
88856SN/A# property including but not limited to intellectual property relating
98856SN/A# to a hardware implementation of the functionality of the software
108856SN/A# licensed hereunder.  You may use the software subject to the license
118856SN/A# terms below provided that you ensure that this notice is replicated
128856SN/A# unmodified and in its entirety in all distributions of the software,
138856SN/A# modified or unmodified, in source code or in binary form.
142914SN/A#
152914SN/A# Redistribution and use in source and binary forms, with or without
162914SN/A# modification, are permitted provided that the following conditions are
172914SN/A# met: redistributions of source code must retain the above copyright
182914SN/A# notice, this list of conditions and the following disclaimer;
192914SN/A# redistributions in binary form must reproduce the above copyright
202914SN/A# notice, this list of conditions and the following disclaimer in the
212914SN/A# documentation and/or other materials provided with the distribution;
222914SN/A# neither the name of the copyright holders nor the names of its
232914SN/A# contributors may be used to endorse or promote products derived from
242914SN/A# this software without specific prior written permission.
252914SN/A#
262914SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
272914SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
282914SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
292914SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
302914SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
312914SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
322914SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
332914SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
342914SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
352914SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
362914SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
372914SN/A#
382914SN/A# Authors: Andreas Hansson
392914SN/A
402914SN/A# This script is used to dump protobuf packet traces to ASCII
418856SN/A# format. It assumes that protoc has been executed and already
422914SN/A# generated the Python package for the packet messages. This can
432914SN/A# be done manually using:
4411793Sbrandon.potter@amd.com# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto
4511793Sbrandon.potter@amd.com#
469356Snilay@cs.wisc.edu# The ASCII trace format uses one line per request on the format cmd,
479152Satgutier@umich.edu# addr, size, tick,flags. For example:
488914Sandreas.hansson@arm.com# r,128,64,4000,0
492914SN/A# w,232123,64,500000,0
5011207SBrad.Beckmann@amd.com
5112083Sspwilson2@wisc.eduimport protolib
5211207SBrad.Beckmann@amd.comimport sys
5312083Sspwilson2@wisc.edu
5412083Sspwilson2@wisc.edu# Import the packet proto definitions. If they are not found, attempt
5511207SBrad.Beckmann@amd.com# to generate them automatically. This assumes that the script is
565740SN/A# executed from the gem5 root.
575740SN/Atry:
585740SN/A    import packet_pb2
598914Sandreas.hansson@arm.comexcept:
605740SN/A    print "Did not find packet proto definitions, attempting to generate"
615740SN/A    from subprocess import call
625740SN/A    error = call(['protoc', '--python_out=util', '--proto_path=src/proto',
638914Sandreas.hansson@arm.com                  'src/proto/packet.proto'])
648914Sandreas.hansson@arm.com    if not error:
658914Sandreas.hansson@arm.com        print "Generated packet proto definitions"
668914Sandreas.hansson@arm.com
678914Sandreas.hansson@arm.com        try:
6810713Sandreas.hansson@arm.com            import google.protobuf
698914Sandreas.hansson@arm.com        except:
708914Sandreas.hansson@arm.com            print "Please install Python protobuf module"
718914Sandreas.hansson@arm.com            exit(-1)
724929SN/A
7310713Sandreas.hansson@arm.com        import packet_pb2
7410713Sandreas.hansson@arm.com    else:
7510713Sandreas.hansson@arm.com        print "Failed to import packet proto definitions"
7610713Sandreas.hansson@arm.com        exit(-1)
7710713Sandreas.hansson@arm.com
7810713Sandreas.hansson@arm.comdef main():
7910713Sandreas.hansson@arm.com    if len(sys.argv) != 3:
8010713Sandreas.hansson@arm.com        print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>"
8110713Sandreas.hansson@arm.com        exit(-1)
8210713Sandreas.hansson@arm.com
8310713Sandreas.hansson@arm.com    # Open the file in read mode
8410713Sandreas.hansson@arm.com    proto_in = protolib.openFileRd(sys.argv[1])
8512823Srmk35@cl.cam.ac.uk
863091SN/A    try:
878856SN/A        ascii_out = open(sys.argv[2], 'w')
888856SN/A    except IOError:
8910322Sandreas.hansson@arm.com        print "Failed to open ", sys.argv[2], " for writing"
908856SN/A        exit(-1)
913296SN/A
9210322Sandreas.hansson@arm.com    # Read the magic number in 4-byte Little Endian
938856SN/A    magic_number = proto_in.read(4)
948856SN/A
9512823Srmk35@cl.cam.ac.uk    if magic_number != "gem5":
968856SN/A        print "Unrecognized file", sys.argv[1]
973284SN/A        exit(-1)
984929SN/A
998856SN/A    print "Parsing packet header"
1008856SN/A
1018856SN/A    # Add the packet header
1024490SN/A    header = packet_pb2.PacketHeader()
1033342SN/A    protolib.decodeMessage(proto_in, header)
1044490SN/A
10510722Sstephan.diestelhorst@arm.com    print "Object id:", header.obj_id
1063403SN/A    print "Tick frequency:", header.tick_freq
10710722Sstephan.diestelhorst@arm.com
10810722Sstephan.diestelhorst@arm.com    print "Parsing packets"
10910722Sstephan.diestelhorst@arm.com
11010713Sandreas.hansson@arm.com    num_packets = 0
1119160Sandreas.hansson@arm.com    packet = packet_pb2.Packet()
1129160Sandreas.hansson@arm.com
1134492SN/A    # Decode the packet messages until we hit the end of the file
1149163Sandreas.hansson@arm.com    while protolib.decodeMessage(proto_in, packet):
1159163Sandreas.hansson@arm.com        num_packets += 1
1169163Sandreas.hansson@arm.com        # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum
1179390Sandreas.hansson@arm.com        cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u')
1189390Sandreas.hansson@arm.com        if packet.HasField('pkt_id'):
11911207SBrad.Beckmann@amd.com            ascii_out.write('%s,' % (packet.pkt_id))
1209390Sandreas.hansson@arm.com        if packet.HasField('flags'):
1219390Sandreas.hansson@arm.com            ascii_out.write('%s,%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size,
1229390Sandreas.hansson@arm.com                            packet.flags, packet.tick))
1239390Sandreas.hansson@arm.com        else:
12411195Sandreas.hansson@arm.com            ascii_out.write('%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size,
12511195Sandreas.hansson@arm.com                                           packet.tick))
12610922Sandreas.hansson@arm.com
1274666SN/A    print "Parsed packets:", num_packets
1284666SN/A
1294666SN/A    # We're done
1304666SN/A    ascii_out.close()
13110713Sandreas.hansson@arm.com    proto_in.close()
13210713Sandreas.hansson@arm.com
13310713Sandreas.hansson@arm.comif __name__ == "__main__":
13410713Sandreas.hansson@arm.com    main()
13510713Sandreas.hansson@arm.com