encode_packet_trace.py revision 9706:fbb19814adbc
12086SN/A#!/usr/bin/env python 22086SN/A 32086SN/A# Copyright (c) 2013 ARM Limited 42086SN/A# All rights reserved 52086SN/A# 62086SN/A# The license below extends only to copyright in the software and shall 72086SN/A# not be construed as granting a license to any other intellectual 82086SN/A# property including but not limited to intellectual property relating 92086SN/A# to a hardware implementation of the functionality of the software 102086SN/A# licensed hereunder. You may use the software subject to the license 112086SN/A# terms below provided that you ensure that this notice is replicated 122086SN/A# unmodified and in its entirety in all distributions of the software, 132086SN/A# modified or unmodified, in source code or in binary form. 142086SN/A# 152086SN/A# Redistribution and use in source and binary forms, with or without 162086SN/A# modification, are permitted provided that the following conditions are 172086SN/A# met: redistributions of source code must retain the above copyright 182086SN/A# notice, this list of conditions and the following disclaimer; 192086SN/A# redistributions in binary form must reproduce the above copyright 202086SN/A# notice, this list of conditions and the following disclaimer in the 212086SN/A# documentation and/or other materials provided with the distribution; 222086SN/A# neither the name of the copyright holders nor the names of its 232086SN/A# contributors may be used to endorse or promote products derived from 242086SN/A# this software without specific prior written permission. 252086SN/A# 262086SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 272086SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 282665Ssaidi@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 292665Ssaidi@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 302665Ssaidi@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 312086SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 322086SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 332086SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 342086SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 352086SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 362086SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 372086SN/A# 382086SN/A# Copyright 2008 Google Inc. All rights reserved. 392086SN/A# http://code.google.com/p/protobuf/ 402086SN/A# 412086SN/A# Redistribution and use in source and binary forms, with or without 422086SN/A# modification, are permitted provided that the following conditions are 432086SN/A# met: 442086SN/A# 452086SN/A# * Redistributions of source code must retain the above copyright 462152SN/A# notice, this list of conditions and the following disclaimer. 472152SN/A# * Redistributions in binary form must reproduce the above 482152SN/A# copyright notice, this list of conditions and the following disclaimer 492086SN/A# in the documentation and/or other materials provided with the 502086SN/A# distribution. 512086SN/A# * Neither the name of Google Inc. nor the names of its 522152SN/A# contributors may be used to endorse or promote products derived from 532652Ssaidi@eecs.umich.edu# this software without specific prior written permission. 542650Ssaidi@eecs.umich.edu# 552086SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 562086SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 572086SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 582152SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 592579SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 602458SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 612600SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 622600SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 632209SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 642086SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 652086SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 662152SN/A# 672086SN/A# Authors: Andreas Hansson 682086SN/A# 692152SN/A 702086SN/A# This script is used to migrate ASCII packet traces to the protobuf 712152SN/A# format currently used in gem5. It assumes that protoc has been 722086SN/A# executed and already generated the Python package for the packet 732152SN/A# messages. This can be done manually using: 742152SN/A# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto 752152SN/A# 762152SN/A# The ASCII trace format uses one line per request on the format cmd, 772152SN/A# addr, size, tick. For example: 782152SN/A# r,128,64,4000 792152SN/A# w,232123,64,500000 802152SN/A# This trace reads 64 bytes from decimal address 128 at tick 4000, 812152SN/A# then writes 64 bytes to address 232123 at tick 500000. 822086SN/A# 832086SN/A# This script can of course also be used as a template to convert 84# other trace formats into the gem5 protobuf format 85 86import struct 87import sys 88 89# Import the packet proto definitions. If they are not found, attempt 90# to generate them automatically. This assumes that the script is 91# executed from the gem5 root. 92try: 93 import packet_pb2 94except: 95 print "Did not find packet proto definitions, attempting to generate" 96 from subprocess import call 97 error = call(['protoc', '--python_out=util', '--proto_path=src/proto', 98 'src/proto/packet.proto']) 99 if not error: 100 import packet_pb2 101 print "Generated packet proto definitions" 102 else: 103 print "Failed to import packet proto definitions" 104 exit(-1) 105 106def EncodeVarint(out_file, value): 107 """ 108 The encoding of the Varint32 is copied from 109 google.protobuf.internal.encoder and is only repeated here to 110 avoid depending on the internal functions in the library. 111 """ 112 bits = value & 0x7f 113 value >>= 7 114 while value: 115 out_file.write(struct.pack('<B', 0x80|bits)) 116 bits = value & 0x7f 117 value >>= 7 118 out_file.write(struct.pack('<B', bits)) 119 120def encodeMessage(out_file, message): 121 """ 122 Encoded a message with the length prepended as a 32-bit varint. 123 """ 124 out = message.SerializeToString() 125 EncodeVarint(out_file, len(out)) 126 out_file.write(out) 127 128def main(): 129 if len(sys.argv) != 3: 130 print "Usage: ", sys.argv[0], " <ASCII input> <protobuf output>" 131 exit(-1) 132 133 try: 134 ascii_in = open(sys.argv[1], 'r') 135 except IOError: 136 print "Failed to open ", sys.argv[1], " for reading" 137 exit(-1) 138 139 try: 140 proto_out = open(sys.argv[2], 'wb') 141 except IOError: 142 print "Failed to open ", sys.argv[2], " for writing" 143 exit(-1) 144 145 # Write the magic number in 4-byte Little Endian, similar to what 146 # is done in src/proto/protoio.cc 147 proto_out.write("gem5") 148 149 # Add the packet header 150 header = packet_pb2.PacketHeader() 151 header.obj_id = "Converted ASCII trace " + sys.argv[1] 152 # Assume the default tick rate 153 header.tick_freq = 1000000000 154 encodeMessage(proto_out, header) 155 156 # For each line in the ASCII trace, create a packet message and 157 # write it to the encoded output 158 for line in ascii_in: 159 cmd, addr, size, tick = line.split(',') 160 packet = packet_pb2.Packet() 161 packet.tick = long(tick) 162 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum 163 packet.cmd = 1 if cmd == 'r' else 4 164 packet.addr = long(addr) 165 packet.size = int(size) 166 encodeMessage(proto_out, packet) 167 168 # We're done 169 ascii_in.close() 170 proto_out.close() 171 172if __name__ == "__main__": 173 main() 174