decode_packet_trace.py revision 9617:c503acb59e51
13520Sgblack@eecs.umich.edu#!/usr/bin/env python 23520Sgblack@eecs.umich.edu 33520Sgblack@eecs.umich.edu# Copyright (c) 2013 ARM Limited 43520Sgblack@eecs.umich.edu# All rights reserved 53520Sgblack@eecs.umich.edu# 63520Sgblack@eecs.umich.edu# The license below extends only to copyright in the software and shall 73520Sgblack@eecs.umich.edu# not be construed as granting a license to any other intellectual 83520Sgblack@eecs.umich.edu# property including but not limited to intellectual property relating 93520Sgblack@eecs.umich.edu# to a hardware implementation of the functionality of the software 103520Sgblack@eecs.umich.edu# licensed hereunder. You may use the software subject to the license 113520Sgblack@eecs.umich.edu# terms below provided that you ensure that this notice is replicated 123520Sgblack@eecs.umich.edu# unmodified and in its entirety in all distributions of the software, 133520Sgblack@eecs.umich.edu# modified or unmodified, in source code or in binary form. 143520Sgblack@eecs.umich.edu# 153520Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without 163520Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are 173520Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright 183520Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer; 193520Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright 203520Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the 213520Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution; 223520Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its 233520Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from 243520Sgblack@eecs.umich.edu# this software without specific prior written permission. 253520Sgblack@eecs.umich.edu# 263520Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 273520Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 283520Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 293520Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 303520Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 313520Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 323520Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 333520Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 343520Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 353520Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 363520Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 374103Ssaidi@eecs.umich.edu# 385647Sgblack@eecs.umich.edu# Copyright 2008 Google Inc. All rights reserved. 393520Sgblack@eecs.umich.edu# http://code.google.com/p/protobuf/ 408232Snate@binkert.org# 418232Snate@binkert.org# Redistribution and use in source and binary forms, with or without 425647Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are 435647Sgblack@eecs.umich.edu# met: 443520Sgblack@eecs.umich.edu# 455565Snate@binkert.org# * Redistributions of source code must retain the above copyright 465565Snate@binkert.org# notice, this list of conditions and the following disclaimer. 475647Sgblack@eecs.umich.edu# * Redistributions in binary form must reproduce the above 483520Sgblack@eecs.umich.edu# copyright notice, this list of conditions and the following disclaimer 495565Snate@binkert.org# in the documentation and/or other materials provided with the 505565Snate@binkert.org# distribution. 515565Snate@binkert.org# * Neither the name of Google Inc. nor the names of its 525565Snate@binkert.org# contributors may be used to endorse or promote products derived from 535810Sgblack@eecs.umich.edu# this software without specific prior written permission. 545565Snate@binkert.org# 555565Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 565565Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 575565Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 585565Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 595565Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 605647Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 615647Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 625647Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 635647Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 645647Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 655647Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 665647Sgblack@eecs.umich.edu# 675647Sgblack@eecs.umich.edu# Authors: Andreas Hansson 685810Sgblack@eecs.umich.edu# 693520Sgblack@eecs.umich.edu 705565Snate@binkert.org# This script is used to dump protobuf packet traces to ASCII 715565Snate@binkert.org# format. It assumes that protoc has been executed and already 725565Snate@binkert.org# generated the Python package for the packet messages. This can 735565Snate@binkert.org# be done manually using: 743520Sgblack@eecs.umich.edu# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto 755565Snate@binkert.org# 765810Sgblack@eecs.umich.edu# The ASCII trace format uses one line per request on the format cmd, 775810Sgblack@eecs.umich.edu# addr, size, tick,flags. For example: 785810Sgblack@eecs.umich.edu# r,128,64,4000,0 795810Sgblack@eecs.umich.edu# w,232123,64,500000,0 805810Sgblack@eecs.umich.edu 815810Sgblack@eecs.umich.eduimport struct 825565Snate@binkert.orgimport sys 835565Snate@binkert.orgimport packet_pb2 845565Snate@binkert.org 853520Sgblack@eecs.umich.edudef DecodeVarint(in_file): 865565Snate@binkert.org """ 875565Snate@binkert.org The decoding of the Varint32 is copied from 883520Sgblack@eecs.umich.edu google.protobuf.internal.decoder and is only repeated here to 895565Snate@binkert.org avoid depending on the internal functions in the library. If the 905565Snate@binkert.org end of file is reached, return (0, 0). 913520Sgblack@eecs.umich.edu """ 925565Snate@binkert.org result = 0 935565Snate@binkert.org shift = 0 945565Snate@binkert.org pos = 0 953520Sgblack@eecs.umich.edu # Use a 32-bit mask 965565Snate@binkert.org mask = 0xffffffff 975565Snate@binkert.org while 1: 985565Snate@binkert.org c = in_file.read(1) 995565Snate@binkert.org if len(c) == 0: 1003520Sgblack@eecs.umich.edu return (0, 0) 1015568Snate@binkert.org b = struct.unpack('<B', c)[0] 1025565Snate@binkert.org result |= ((b & 0x7f) << shift) 1033520Sgblack@eecs.umich.edu pos += 1 1045565Snate@binkert.org if not (b & 0x80): 1055565Snate@binkert.org if result > 0x7fffffffffffffff: 1063520Sgblack@eecs.umich.edu result -= (1 << 64) 1075565Snate@binkert.org result |= ~mask 1085565Snate@binkert.org else: 1095565Snate@binkert.org result &= mask 1105565Snate@binkert.org return (result, pos) 1113520Sgblack@eecs.umich.edu shift += 7 1125565Snate@binkert.org if shift >= 64: 1135704Snate@binkert.org raise IOError('Too many bytes when decoding varint.') 1145565Snate@binkert.org 1155565Snate@binkert.orgdef decodeMessage(in_file, message): 1163520Sgblack@eecs.umich.edu """ 1175565Snate@binkert.org Attempt to read a message from the file and decode it. Return 1185565Snate@binkert.org False if no message could be read. 1195565Snate@binkert.org """ 1203520Sgblack@eecs.umich.edu try: 1215565Snate@binkert.org size, pos = DecodeVarint(in_file) 1225565Snate@binkert.org if size == 0: 1235565Snate@binkert.org return False 1245565Snate@binkert.org buf = in_file.read(size) 1255565Snate@binkert.org message.ParseFromString(buf) 1265565Snate@binkert.org return True 1273520Sgblack@eecs.umich.edu except IOError: 1285565Snate@binkert.org return False 1295565Snate@binkert.org 1305565Snate@binkert.orgdef main(): 1315565Snate@binkert.org if len(sys.argv) != 3: 1325565Snate@binkert.org print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" 1335565Snate@binkert.org exit(-1) 1343520Sgblack@eecs.umich.edu 1355565Snate@binkert.org try: 1365704Snate@binkert.org proto_in = open(sys.argv[1], 'rb') 1375565Snate@binkert.org except IOError: 1387720Sgblack@eecs.umich.edu print "Failed to open ", sys.argv[1], " for reading" 1395565Snate@binkert.org exit(-1) 1403520Sgblack@eecs.umich.edu 1415565Snate@binkert.org try: 1425565Snate@binkert.org ascii_out = open(sys.argv[2], 'w') 1435565Snate@binkert.org except IOError: 1446227Snate@binkert.org print "Failed to open ", sys.argv[2], " for writing" 1456227Snate@binkert.org exit(-1) 1463521Sgblack@eecs.umich.edu 1475565Snate@binkert.org # Read the magic number in 4-byte Little Endian 1485565Snate@binkert.org magic_number = proto_in.read(4) 1493520Sgblack@eecs.umich.edu 1505565Snate@binkert.org if magic_number != "gem5": 1516227Snate@binkert.org print "Unrecognized file" 1525565Snate@binkert.org exit(-1) 1535565Snate@binkert.org 1545565Snate@binkert.org print "Parsing packet header" 1555565Snate@binkert.org 1565565Snate@binkert.org # Add the packet header 1573520Sgblack@eecs.umich.edu header = packet_pb2.PacketHeader() 1583520Sgblack@eecs.umich.edu decodeMessage(proto_in, header) 1593520Sgblack@eecs.umich.edu 1603520Sgblack@eecs.umich.edu print "Object id:", header.obj_id 1615565Snate@binkert.org print "Tick frequency:", header.tick_freq 1625565Snate@binkert.org 1636227Snate@binkert.org print "Parsing packets" 1645565Snate@binkert.org 1655565Snate@binkert.org num_packets = 0 1665565Snate@binkert.org ignored_flags = False 1675565Snate@binkert.org packet = packet_pb2.Packet() 1685565Snate@binkert.org 1695565Snate@binkert.org # Decode the packet messages until we hit the end of the file 1705565Snate@binkert.org while decodeMessage(proto_in, packet): 1713633Sktlim@umich.edu num_packets += 1 1723633Sktlim@umich.edu # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum 1735565Snate@binkert.org cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u') 1745565Snate@binkert.org if packet.HasField('flags'): 1755565Snate@binkert.org # Currently not printing flags 1765565Snate@binkert.org ignored_flags = True 1775565Snate@binkert.org ascii_out.write('%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size, 1785565Snate@binkert.org packet.tick)) 1795565Snate@binkert.org 1805565Snate@binkert.org print "Parsed packets:", num_packets 1815565Snate@binkert.org if ignored_flags: 1825565Snate@binkert.org print "Encountered packet flags that were ignored" 1834103Ssaidi@eecs.umich.edu 1845565Snate@binkert.org # We're done 1854103Ssaidi@eecs.umich.edu ascii_out.close() 1865565Snate@binkert.org proto_in.close() 1875565Snate@binkert.org 1885565Snate@binkert.orgif __name__ == "__main__": 1895565Snate@binkert.org main() 1905565Snate@binkert.org