decode_packet_trace.py revision 12213:e0083524df0c
112841Sgabeblack@google.com#!/usr/bin/env python2
212841Sgabeblack@google.com
312841Sgabeblack@google.com# Copyright (c) 2013-2014 ARM Limited
412841Sgabeblack@google.com# All rights reserved
512841Sgabeblack@google.com#
612841Sgabeblack@google.com# The license below extends only to copyright in the software and shall
712841Sgabeblack@google.com# not be construed as granting a license to any other intellectual
812841Sgabeblack@google.com# property including but not limited to intellectual property relating
912841Sgabeblack@google.com# to a hardware implementation of the functionality of the software
1012841Sgabeblack@google.com# licensed hereunder.  You may use the software subject to the license
1112841Sgabeblack@google.com# terms below provided that you ensure that this notice is replicated
1212841Sgabeblack@google.com# unmodified and in its entirety in all distributions of the software,
1312841Sgabeblack@google.com# modified or unmodified, in source code or in binary form.
1412841Sgabeblack@google.com#
1512841Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
1612841Sgabeblack@google.com# modification, are permitted provided that the following conditions are
1712841Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
1812841Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
1912841Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
2012841Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
2112841Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
2212841Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
2312841Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
2412841Sgabeblack@google.com# this software without specific prior written permission.
2512841Sgabeblack@google.com#
2612841Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2712841Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2812841Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2912841Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3012841Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3112841Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3212841Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3312841Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3412841Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3512841Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3613201Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3713201Sgabeblack@google.com#
3812841Sgabeblack@google.com# Authors: Andreas Hansson
3912841Sgabeblack@google.com
4012841Sgabeblack@google.com# This script is used to dump protobuf packet traces to ASCII
4113201Sgabeblack@google.com# format.
4212841Sgabeblack@google.com
4313201Sgabeblack@google.comimport os
4412841Sgabeblack@google.comimport protolib
4512841Sgabeblack@google.comimport subprocess
4612841Sgabeblack@google.comimport sys
4712841Sgabeblack@google.com
4813201Sgabeblack@google.comutil_dir = os.path.dirname(os.path.realpath(__file__))
4912841Sgabeblack@google.com# Make sure the proto definitions are up to date.
5013201Sgabeblack@google.comsubprocess.check_call(['make', '--quiet', '-C', util_dir, 'packet_pb2.py'])
5112841Sgabeblack@google.comimport packet_pb2
5212841Sgabeblack@google.com
5312841Sgabeblack@google.comdef main():
5412841Sgabeblack@google.com    if len(sys.argv) != 3:
5512841Sgabeblack@google.com        print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>"
5613201Sgabeblack@google.com        exit(-1)
5712841Sgabeblack@google.com
5813201Sgabeblack@google.com    # Open the file in read mode
5912841Sgabeblack@google.com    proto_in = protolib.openFileRd(sys.argv[1])
6012841Sgabeblack@google.com
6112841Sgabeblack@google.com    try:
6212841Sgabeblack@google.com        ascii_out = open(sys.argv[2], 'w')
6312841Sgabeblack@google.com    except IOError:
6413201Sgabeblack@google.com        print "Failed to open ", sys.argv[2], " for writing"
6512841Sgabeblack@google.com        exit(-1)
6613201Sgabeblack@google.com
6712841Sgabeblack@google.com    # Read the magic number in 4-byte Little Endian
6812841Sgabeblack@google.com    magic_number = proto_in.read(4)
6912841Sgabeblack@google.com
7012841Sgabeblack@google.com    if magic_number != "gem5":
7113201Sgabeblack@google.com        print "Unrecognized file", sys.argv[1]
7212841Sgabeblack@google.com        exit(-1)
7313201Sgabeblack@google.com
7412841Sgabeblack@google.com    print "Parsing packet header"
7512841Sgabeblack@google.com
7612841Sgabeblack@google.com    # Add the packet header
7712841Sgabeblack@google.com    header = packet_pb2.PacketHeader()
78    protolib.decodeMessage(proto_in, header)
79
80    print "Object id:", header.obj_id
81    print "Tick frequency:", header.tick_freq
82
83    for id_string in header.id_strings:
84        print 'Master id %d: %s' % (id_string.key, id_string.value)
85
86    print "Parsing packets"
87
88    num_packets = 0
89    packet = packet_pb2.Packet()
90
91    # Decode the packet messages until we hit the end of the file
92    while protolib.decodeMessage(proto_in, packet):
93        num_packets += 1
94        # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum
95        cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u')
96        if packet.HasField('pkt_id'):
97            ascii_out.write('%s,' % (packet.pkt_id))
98        if packet.HasField('flags'):
99            ascii_out.write('%s,%s,%s,%s,%s' % (cmd, packet.addr, packet.size,
100                            packet.flags, packet.tick))
101        else:
102            ascii_out.write('%s,%s,%s,%s' % (cmd, packet.addr, packet.size,
103                                           packet.tick))
104        if packet.HasField('pc'):
105            ascii_out.write(',%s\n' % (packet.pc))
106        else:
107            ascii_out.write('\n')
108
109    print "Parsed packets:", num_packets
110
111    # We're done
112    ascii_out.close()
113    proto_in.close()
114
115if __name__ == "__main__":
116    main()
117