encode_packet_trace.py (10065:58bf21ca88de) | encode_packet_trace.py (10107:524afa92d940) |
---|---|
1#!/usr/bin/env python 2 3# Copyright (c) 2013-2014 ARM Limited 4# All rights reserved 5# 6# The license below extends only to copyright in the software and shall 7# not be construed as granting a license to any other intellectual 8# property including but not limited to intellectual property relating --- 21 unchanged lines hidden (view full) --- 30# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37# | 1#!/usr/bin/env python 2 3# Copyright (c) 2013-2014 ARM Limited 4# All rights reserved 5# 6# The license below extends only to copyright in the software and shall 7# not be construed as granting a license to any other intellectual 8# property including but not limited to intellectual property relating --- 21 unchanged lines hidden (view full) --- 30# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37# |
38# Copyright 2008 Google Inc. All rights reserved. 39# http://code.google.com/p/protobuf/ 40# 41# Redistribution and use in source and binary forms, with or without 42# modification, are permitted provided that the following conditions are 43# met: 44# 45# * Redistributions of source code must retain the above copyright 46# notice, this list of conditions and the following disclaimer. 47# * Redistributions in binary form must reproduce the above 48# copyright notice, this list of conditions and the following disclaimer 49# in the documentation and/or other materials provided with the 50# distribution. 51# * Neither the name of Google Inc. nor the names of its 52# contributors may be used to endorse or promote products derived from 53# this software without specific prior written permission. 54# 55# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 56# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 57# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 58# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 59# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 60# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 61# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 62# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 63# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 64# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 65# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 66# | |
67# Authors: Andreas Hansson | 38# Authors: Andreas Hansson |
68# | |
69 70# This script is used to migrate ASCII packet traces to the protobuf 71# format currently used in gem5. It assumes that protoc has been 72# executed and already generated the Python package for the packet 73# messages. This can be done manually using: 74# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto 75# 76# The ASCII trace format uses one line per request on the format cmd, 77# addr, size, tick. For example: 78# r,128,64,4000 79# w,232123,64,500000 80# This trace reads 64 bytes from decimal address 128 at tick 4000, 81# then writes 64 bytes to address 232123 at tick 500000. 82# 83# This script can of course also be used as a template to convert 84# other trace formats into the gem5 protobuf format 85 | 39 40# This script is used to migrate ASCII packet traces to the protobuf 41# format currently used in gem5. It assumes that protoc has been 42# executed and already generated the Python package for the packet 43# messages. This can be done manually using: 44# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto 45# 46# The ASCII trace format uses one line per request on the format cmd, 47# addr, size, tick. For example: 48# r,128,64,4000 49# w,232123,64,500000 50# This trace reads 64 bytes from decimal address 128 at tick 4000, 51# then writes 64 bytes to address 232123 at tick 500000. 52# 53# This script can of course also be used as a template to convert 54# other trace formats into the gem5 protobuf format 55 |
86import struct | 56import protolib |
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: --- 10 unchanged lines hidden (view full) --- 105 print "Please install the Python protobuf module" 106 exit(-1) 107 108 import packet_pb2 109 else: 110 print "Failed to import packet proto definitions" 111 exit(-1) 112 | 57import sys 58 59# Import the packet proto definitions. If they are not found, attempt 60# to generate them automatically. This assumes that the script is 61# executed from the gem5 root. 62try: 63 import packet_pb2 64except: --- 10 unchanged lines hidden (view full) --- 75 print "Please install the Python protobuf module" 76 exit(-1) 77 78 import packet_pb2 79 else: 80 print "Failed to import packet proto definitions" 81 exit(-1) 82 |
113def EncodeVarint(out_file, value): 114 """ 115 The encoding of the Varint32 is copied from 116 google.protobuf.internal.encoder and is only repeated here to 117 avoid depending on the internal functions in the library. 118 """ 119 bits = value & 0x7f 120 value >>= 7 121 while value: 122 out_file.write(struct.pack('<B', 0x80|bits)) 123 bits = value & 0x7f 124 value >>= 7 125 out_file.write(struct.pack('<B', bits)) 126 127def encodeMessage(out_file, message): 128 """ 129 Encoded a message with the length prepended as a 32-bit varint. 130 """ 131 out = message.SerializeToString() 132 EncodeVarint(out_file, len(out)) 133 out_file.write(out) 134 | |
135def main(): 136 if len(sys.argv) != 3: 137 print "Usage: ", sys.argv[0], " <ASCII input> <protobuf output>" 138 exit(-1) 139 140 try: 141 ascii_in = open(sys.argv[1], 'r') 142 except IOError: --- 10 unchanged lines hidden (view full) --- 153 # is done in src/proto/protoio.cc 154 proto_out.write("gem5") 155 156 # Add the packet header 157 header = packet_pb2.PacketHeader() 158 header.obj_id = "Converted ASCII trace " + sys.argv[1] 159 # Assume the default tick rate 160 header.tick_freq = 1000000000 | 83def main(): 84 if len(sys.argv) != 3: 85 print "Usage: ", sys.argv[0], " <ASCII input> <protobuf output>" 86 exit(-1) 87 88 try: 89 ascii_in = open(sys.argv[1], 'r') 90 except IOError: --- 10 unchanged lines hidden (view full) --- 101 # is done in src/proto/protoio.cc 102 proto_out.write("gem5") 103 104 # Add the packet header 105 header = packet_pb2.PacketHeader() 106 header.obj_id = "Converted ASCII trace " + sys.argv[1] 107 # Assume the default tick rate 108 header.tick_freq = 1000000000 |
161 encodeMessage(proto_out, header) | 109 protolib.encodeMessage(proto_out, header) |
162 163 # For each line in the ASCII trace, create a packet message and 164 # write it to the encoded output 165 for line in ascii_in: 166 cmd, addr, size, tick = line.split(',') 167 packet = packet_pb2.Packet() 168 packet.tick = long(tick) 169 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum 170 packet.cmd = 1 if cmd == 'r' else 4 171 packet.addr = long(addr) 172 packet.size = int(size) | 110 111 # For each line in the ASCII trace, create a packet message and 112 # write it to the encoded output 113 for line in ascii_in: 114 cmd, addr, size, tick = line.split(',') 115 packet = packet_pb2.Packet() 116 packet.tick = long(tick) 117 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum 118 packet.cmd = 1 if cmd == 'r' else 4 119 packet.addr = long(addr) 120 packet.size = int(size) |
173 encodeMessage(proto_out, packet) | 121 protolib.encodeMessage(proto_out, packet) |
174 175 # We're done 176 ascii_in.close() 177 proto_out.close() 178 179if __name__ == "__main__": 180 main() | 122 123 # We're done 124 ascii_in.close() 125 proto_out.close() 126 127if __name__ == "__main__": 128 main() |