encode_packet_trace.py revision 9616
112027Sjungma@eit.uni-kl.de#!/usr/bin/env python
212027Sjungma@eit.uni-kl.de
312027Sjungma@eit.uni-kl.de# Copyright (c) 2013 ARM Limited
412027Sjungma@eit.uni-kl.de# All rights reserved
512027Sjungma@eit.uni-kl.de#
612027Sjungma@eit.uni-kl.de# The license below extends only to copyright in the software and shall
712027Sjungma@eit.uni-kl.de# not be construed as granting a license to any other intellectual
812027Sjungma@eit.uni-kl.de# property including but not limited to intellectual property relating
912027Sjungma@eit.uni-kl.de# to a hardware implementation of the functionality of the software
1012027Sjungma@eit.uni-kl.de# licensed hereunder.  You may use the software subject to the license
1112027Sjungma@eit.uni-kl.de# terms below provided that you ensure that this notice is replicated
1212027Sjungma@eit.uni-kl.de# unmodified and in its entirety in all distributions of the software,
1312027Sjungma@eit.uni-kl.de# modified or unmodified, in source code or in binary form.
1412027Sjungma@eit.uni-kl.de#
1512027Sjungma@eit.uni-kl.de# Redistribution and use in source and binary forms, with or without
1612027Sjungma@eit.uni-kl.de# modification, are permitted provided that the following conditions are
1712027Sjungma@eit.uni-kl.de# met: redistributions of source code must retain the above copyright
1812027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer;
1912027Sjungma@eit.uni-kl.de# redistributions in binary form must reproduce the above copyright
2012027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer in the
2112027Sjungma@eit.uni-kl.de# documentation and/or other materials provided with the distribution;
2212027Sjungma@eit.uni-kl.de# neither the name of the copyright holders nor the names of its
2312027Sjungma@eit.uni-kl.de# contributors may be used to endorse or promote products derived from
2412027Sjungma@eit.uni-kl.de# this software without specific prior written permission.
2512027Sjungma@eit.uni-kl.de#
2612027Sjungma@eit.uni-kl.de# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2712027Sjungma@eit.uni-kl.de# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2812027Sjungma@eit.uni-kl.de# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2912027Sjungma@eit.uni-kl.de# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3012027Sjungma@eit.uni-kl.de# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3112027Sjungma@eit.uni-kl.de# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3212027Sjungma@eit.uni-kl.de# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3312027Sjungma@eit.uni-kl.de# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3412027Sjungma@eit.uni-kl.de# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3512027Sjungma@eit.uni-kl.de# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3612027Sjungma@eit.uni-kl.de# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3712027Sjungma@eit.uni-kl.de#
3812027Sjungma@eit.uni-kl.de# Copyright 2008 Google Inc.  All rights reserved.
3912027Sjungma@eit.uni-kl.de# http://code.google.com/p/protobuf/
4012027Sjungma@eit.uni-kl.de#
4112027Sjungma@eit.uni-kl.de# Redistribution and use in source and binary forms, with or without
4212027Sjungma@eit.uni-kl.de# modification, are permitted provided that the following conditions are
4312027Sjungma@eit.uni-kl.de# met:
4412027Sjungma@eit.uni-kl.de#
4512027Sjungma@eit.uni-kl.de#     * Redistributions of source code must retain the above copyright
4612027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer.
4712027Sjungma@eit.uni-kl.de#     * Redistributions in binary form must reproduce the above
4812027Sjungma@eit.uni-kl.de# copyright notice, this list of conditions and the following disclaimer
4912027Sjungma@eit.uni-kl.de# in the documentation and/or other materials provided with the
5012027Sjungma@eit.uni-kl.de# distribution.
5112027Sjungma@eit.uni-kl.de#     * Neither the name of Google Inc. nor the names of its
5212027Sjungma@eit.uni-kl.de# contributors may be used to endorse or promote products derived from
5312027Sjungma@eit.uni-kl.de# this software without specific prior written permission.
5412027Sjungma@eit.uni-kl.de#
5512027Sjungma@eit.uni-kl.de# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5612027Sjungma@eit.uni-kl.de# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5712027Sjungma@eit.uni-kl.de# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
5812027Sjungma@eit.uni-kl.de# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
5912027Sjungma@eit.uni-kl.de# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6012027Sjungma@eit.uni-kl.de# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
6112027Sjungma@eit.uni-kl.de# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6212027Sjungma@eit.uni-kl.de# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6312027Sjungma@eit.uni-kl.de# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6412027Sjungma@eit.uni-kl.de# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
6512027Sjungma@eit.uni-kl.de# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6612027Sjungma@eit.uni-kl.de#
6712027Sjungma@eit.uni-kl.de# Authors: Andreas Hansson
6812027Sjungma@eit.uni-kl.de#
6912027Sjungma@eit.uni-kl.de
7012027Sjungma@eit.uni-kl.de# This script is used to migrate ASCII packet traces to the protobuf
7112027Sjungma@eit.uni-kl.de# format currently used in gem5. It assumes that protoc has been
7212027Sjungma@eit.uni-kl.de# executed and already generated the Python package for the packet
7312027Sjungma@eit.uni-kl.de# messages. This can be done manually using:
7412027Sjungma@eit.uni-kl.de# protoc --python_out=. --proto_path=src/proto src/proto/packet.proto
7512027Sjungma@eit.uni-kl.de#
7612027Sjungma@eit.uni-kl.de# The ASCII trace format uses one line per request on the format cmd,
7712027Sjungma@eit.uni-kl.de# addr, size, tick. For example:
7812027Sjungma@eit.uni-kl.de# r,128,64,4000
7912027Sjungma@eit.uni-kl.de# w,232123,64,500000
8012027Sjungma@eit.uni-kl.de# This trace reads 64 bytes from decimal address 128 at tick 4000,
8112027Sjungma@eit.uni-kl.de# then writes 64 bytes to address 232123 at tick 500000.
8212027Sjungma@eit.uni-kl.de#
8312027Sjungma@eit.uni-kl.de# This script can of course also be used as a template to convert
8412027Sjungma@eit.uni-kl.de# other trace formats into the gem5 protobuf format
8512027Sjungma@eit.uni-kl.de
8612027Sjungma@eit.uni-kl.deimport struct
8712027Sjungma@eit.uni-kl.deimport sys
8812027Sjungma@eit.uni-kl.deimport packet_pb2
8912027Sjungma@eit.uni-kl.de
9012027Sjungma@eit.uni-kl.dedef EncodeVarint(out_file, value):
9112027Sjungma@eit.uni-kl.de  """
9212027Sjungma@eit.uni-kl.de  The encoding of the Varint32 is copied from
9312027Sjungma@eit.uni-kl.de  google.protobuf.internal.encoder and is only repeated here to
9412027Sjungma@eit.uni-kl.de  avoid depending on the internal functions in the library.
9512027Sjungma@eit.uni-kl.de  """
9612027Sjungma@eit.uni-kl.de  bits = value & 0x7f
9712027Sjungma@eit.uni-kl.de  value >>= 7
9812027Sjungma@eit.uni-kl.de  while value:
9912027Sjungma@eit.uni-kl.de    out_file.write(struct.pack('<B', 0x80|bits))
10012027Sjungma@eit.uni-kl.de    bits = value & 0x7f
10112027Sjungma@eit.uni-kl.de    value >>= 7
10212027Sjungma@eit.uni-kl.de  out_file.write(struct.pack('<B', bits))
10312027Sjungma@eit.uni-kl.de
10412027Sjungma@eit.uni-kl.dedef encodeMessage(out_file, message):
10512027Sjungma@eit.uni-kl.de    """
10612027Sjungma@eit.uni-kl.de    Encoded a message with the length prepended as a 32-bit varint.
10712027Sjungma@eit.uni-kl.de    """
10812027Sjungma@eit.uni-kl.de    out = message.SerializeToString()
10912027Sjungma@eit.uni-kl.de    EncodeVarint(out_file, len(out))
11012027Sjungma@eit.uni-kl.de    out_file.write(out)
11112027Sjungma@eit.uni-kl.de
11212027Sjungma@eit.uni-kl.dedef main():
11312027Sjungma@eit.uni-kl.de    if len(sys.argv) != 3:
11412027Sjungma@eit.uni-kl.de        print "Usage: ", sys.argv[0], " <ASCII input> <protobuf output>"
11512027Sjungma@eit.uni-kl.de        exit(-1)
11612027Sjungma@eit.uni-kl.de
11712027Sjungma@eit.uni-kl.de    try:
11812027Sjungma@eit.uni-kl.de        ascii_in = open(sys.argv[1], 'r')
11912027Sjungma@eit.uni-kl.de    except IOError:
12012027Sjungma@eit.uni-kl.de        print "Failed to open ", sys.argv[1], " for reading"
12112027Sjungma@eit.uni-kl.de        exit(-1)
12212027Sjungma@eit.uni-kl.de
12312027Sjungma@eit.uni-kl.de    try:
12412027Sjungma@eit.uni-kl.de        proto_out = open(sys.argv[2], 'wb')
12512027Sjungma@eit.uni-kl.de    except IOError:
12612027Sjungma@eit.uni-kl.de        print "Failed to open ", sys.argv[2], " for writing"
12712027Sjungma@eit.uni-kl.de        exit(-1)
12812027Sjungma@eit.uni-kl.de
12912027Sjungma@eit.uni-kl.de    # Write the magic number in 4-byte Little Endian, similar to what
13012027Sjungma@eit.uni-kl.de    # is done in src/proto/protoio.cc
13112027Sjungma@eit.uni-kl.de    proto_out.write("gem5")
13212027Sjungma@eit.uni-kl.de
13312027Sjungma@eit.uni-kl.de    # Add the packet header
13412027Sjungma@eit.uni-kl.de    header = packet_pb2.PacketHeader()
13512027Sjungma@eit.uni-kl.de    header.obj_id = "Converted ASCII trace " + sys.argv[1]
13612027Sjungma@eit.uni-kl.de    # Assume the default tick rate
13712027Sjungma@eit.uni-kl.de    header.tick_freq = 1000000000
13812027Sjungma@eit.uni-kl.de    encodeMessage(proto_out, header)
13912027Sjungma@eit.uni-kl.de
14012027Sjungma@eit.uni-kl.de    # For each line in the ASCII trace, create a packet message and
14112027Sjungma@eit.uni-kl.de    # write it to the encoded output
14212027Sjungma@eit.uni-kl.de    for line in ascii_in:
14312027Sjungma@eit.uni-kl.de        cmd, addr, size, tick = line.split(',')
14412027Sjungma@eit.uni-kl.de        packet = packet_pb2.Packet()
14512027Sjungma@eit.uni-kl.de        packet.tick = long(tick)
14612027Sjungma@eit.uni-kl.de        # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum
14712027Sjungma@eit.uni-kl.de        packet.cmd = 1 if cmd == 'r' else 4
14812027Sjungma@eit.uni-kl.de        packet.addr = long(addr)
14912027Sjungma@eit.uni-kl.de        packet.size = int(size)
15012027Sjungma@eit.uni-kl.de        encodeMessage(proto_out, packet)
15112027Sjungma@eit.uni-kl.de
15212027Sjungma@eit.uni-kl.de    # We're done
15312027Sjungma@eit.uni-kl.de    ascii_in.close()
154    proto_out.close()
155
156if __name__ == "__main__":
157    main()
158