decode_packet_trace.py revision 10269
14120Sgblack@eecs.umich.edu#!/usr/bin/env python 24120Sgblack@eecs.umich.edu 34120Sgblack@eecs.umich.edu# Copyright (c) 2013-2014 ARM Limited 44120Sgblack@eecs.umich.edu# All rights reserved 57087Snate@binkert.org# 67087Snate@binkert.org# The license below extends only to copyright in the software and shall 77087Snate@binkert.org# not be construed as granting a license to any other intellectual 87087Snate@binkert.org# property including but not limited to intellectual property relating 97087Snate@binkert.org# to a hardware implementation of the functionality of the software 107087Snate@binkert.org# licensed hereunder. You may use the software subject to the license 117087Snate@binkert.org# terms below provided that you ensure that this notice is replicated 127087Snate@binkert.org# unmodified and in its entirety in all distributions of the software, 134120Sgblack@eecs.umich.edu# modified or unmodified, in source code or in binary form. 147087Snate@binkert.org# 157087Snate@binkert.org# Redistribution and use in source and binary forms, with or without 167087Snate@binkert.org# modification, are permitted provided that the following conditions are 177087Snate@binkert.org# met: redistributions of source code must retain the above copyright 187087Snate@binkert.org# notice, this list of conditions and the following disclaimer; 197087Snate@binkert.org# redistributions in binary form must reproduce the above copyright 207087Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 217087Snate@binkert.org# documentation and/or other materials provided with the distribution; 224120Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its 237087Snate@binkert.org# contributors may be used to endorse or promote products derived from 244120Sgblack@eecs.umich.edu# this software without specific prior written permission. 254120Sgblack@eecs.umich.edu# 264120Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 274120Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 284120Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 294120Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 304120Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 314120Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 324120Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 334120Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 344120Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 354120Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 364120Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 374120Sgblack@eecs.umich.edu# 384120Sgblack@eecs.umich.edu# Authors: Andreas Hansson 394120Sgblack@eecs.umich.edu 404120Sgblack@eecs.umich.edu# This script is used to dump protobuf packet traces to ASCII 414120Sgblack@eecs.umich.edu# format. It assumes that protoc has been executed and already 424120Sgblack@eecs.umich.edu# generated the Python package for the packet messages. This can 435124Sgblack@eecs.umich.edu# be done manually using: 445237Sgblack@eecs.umich.edu# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto 455124Sgblack@eecs.umich.edu# 4610687SAndreas.Sandberg@ARM.com# The ASCII trace format uses one line per request on the format cmd, 475124Sgblack@eecs.umich.edu# addr, size, tick,flags. For example: 488953Sgblack@eecs.umich.edu# r,128,64,4000,0 495086Sgblack@eecs.umich.edu# w,232123,64,500000,0 506022Sgblack@eecs.umich.edu 515086Sgblack@eecs.umich.eduimport protolib 525086Sgblack@eecs.umich.eduimport sys 535086Sgblack@eecs.umich.edu 545086Sgblack@eecs.umich.edu# Import the packet proto definitions. If they are not found, attempt 555086Sgblack@eecs.umich.edu# to generate them automatically. This assumes that the script is 565245Sgblack@eecs.umich.edu# executed from the gem5 root. 575245Sgblack@eecs.umich.edutry: 585358Sgblack@eecs.umich.edu import packet_pb2 595086Sgblack@eecs.umich.eduexcept: 605124Sgblack@eecs.umich.edu print "Did not find packet proto definitions, attempting to generate" 615895Sgblack@eecs.umich.edu from subprocess import call 625236Sgblack@eecs.umich.edu error = call(['protoc', '--python_out=util', '--proto_path=src/proto', 635360Sgblack@eecs.umich.edu 'src/proto/packet.proto']) 645360Sgblack@eecs.umich.edu if not error: 655357Sgblack@eecs.umich.edu print "Generated packet proto definitions" 665237Sgblack@eecs.umich.edu 675124Sgblack@eecs.umich.edu try: 685245Sgblack@eecs.umich.edu import google.protobuf 695124Sgblack@eecs.umich.edu except: 705124Sgblack@eecs.umich.edu print "Please install Python protobuf module" 715086Sgblack@eecs.umich.edu exit(-1) 7211175Sandreas.hansson@arm.com 7310194SGeoffrey.Blake@arm.com import packet_pb2 745124Sgblack@eecs.umich.edu else: 755124Sgblack@eecs.umich.edu print "Failed to import packet proto definitions" 765357Sgblack@eecs.umich.edu exit(-1) 775357Sgblack@eecs.umich.edu 785360Sgblack@eecs.umich.edudef main(): 795360Sgblack@eecs.umich.edu if len(sys.argv) != 3: 805360Sgblack@eecs.umich.edu print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" 815360Sgblack@eecs.umich.edu exit(-1) 828752Sgblack@eecs.umich.edu 835236Sgblack@eecs.umich.edu # Open the file in read mode 847912Shestness@cs.utexas.edu proto_in = protolib.openFileRd(sys.argv[1]) 857912Shestness@cs.utexas.edu 865236Sgblack@eecs.umich.edu try: 8711175Sandreas.hansson@arm.com ascii_out = open(sys.argv[2], 'w') 885242Sgblack@eecs.umich.edu except IOError: 899423SAndreas.Sandberg@arm.com print "Failed to open ", sys.argv[2], " for writing" 905242Sgblack@eecs.umich.edu exit(-1) 9111175Sandreas.hansson@arm.com 925242Sgblack@eecs.umich.edu # Read the magic number in 4-byte Little Endian 935124Sgblack@eecs.umich.edu magic_number = proto_in.read(4) 949818Snilay@cs.wisc.edu 955124Sgblack@eecs.umich.edu if magic_number != "gem5": 9610905Sandreas.sandberg@arm.com print "Unrecognized file", sys.argv[1] 975124Sgblack@eecs.umich.edu exit(-1) 985124Sgblack@eecs.umich.edu 995124Sgblack@eecs.umich.edu print "Parsing packet header" 1008953Sgblack@eecs.umich.edu 1018953Sgblack@eecs.umich.edu # Add the packet header 1028953Sgblack@eecs.umich.edu header = packet_pb2.PacketHeader() 10312140Sswapnilster@gmail.com protolib.decodeMessage(proto_in, header) 10412140Sswapnilster@gmail.com 10512140Sswapnilster@gmail.com print "Object id:", header.obj_id 10612140Sswapnilster@gmail.com print "Tick frequency:", header.tick_freq 10712140Sswapnilster@gmail.com 10812140Sswapnilster@gmail.com print "Parsing packets" 1096141Sgblack@eecs.umich.edu 1106141Sgblack@eecs.umich.edu num_packets = 0 1115895Sgblack@eecs.umich.edu packet = packet_pb2.Packet() 1126023Snate@binkert.org 1135895Sgblack@eecs.umich.edu # Decode the packet messages until we hit the end of the file 1145140Sgblack@eecs.umich.edu while protolib.decodeMessage(proto_in, packet): 1155124Sgblack@eecs.umich.edu num_packets += 1 1165245Sgblack@eecs.umich.edu # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum 1178953Sgblack@eecs.umich.edu cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u') 1188953Sgblack@eecs.umich.edu if packet.HasField('pkt_id'): 1198953Sgblack@eecs.umich.edu ascii_out.write('%s,' % (packet.pkt_id)) 1208953Sgblack@eecs.umich.edu if packet.HasField('flags'): 1218953Sgblack@eecs.umich.edu ascii_out.write('%s,%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size, 1228953Sgblack@eecs.umich.edu packet.flags, packet.tick)) 1238953Sgblack@eecs.umich.edu else: 1248953Sgblack@eecs.umich.edu ascii_out.write('%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size, 12512406Sgabeblack@google.com packet.tick)) 12612406Sgabeblack@google.com 12712406Sgabeblack@google.com print "Parsed packets:", num_packets 12812406Sgabeblack@google.com 12912406Sgabeblack@google.com # We're done 1305245Sgblack@eecs.umich.edu ascii_out.close() 1319738Sandreas@sandberg.pp.se proto_in.close() 1329738Sandreas@sandberg.pp.se 1339738Sandreas@sandberg.pp.seif __name__ == "__main__": 1349738Sandreas@sandberg.pp.se main() 1359738Sandreas@sandberg.pp.se