util: Teach the m5 utility's scons how to run unit tests.
[gem5.git] / util / encode_packet_trace.py
1 #!/usr/bin/env python3
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
9 # to a hardware implementation of the functionality of the software
10 # licensed hereunder. You may use the software subject to the license
11 # terms below provided that you ensure that this notice is replicated
12 # unmodified and in its entirety in all distributions of the software,
13 # modified or unmodified, in source code or in binary form.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions are
17 # met: redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer;
19 # redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution;
22 # neither the name of the copyright holders nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
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 # This script is used to migrate ASCII packet traces to the protobuf
39 # format currently used in gem5. It assumes that protoc has been
40 # executed and already generated the Python package for the packet
41 # messages. This can be done manually using:
42 # protoc --python_out=. --proto_path=src/proto src/proto/packet.proto
43 #
44 # The ASCII trace format uses one line per request on the format cmd,
45 # addr, size, tick. For example:
46 # r,128,64,4000
47 # w,232123,64,500000
48 # This trace reads 64 bytes from decimal address 128 at tick 4000,
49 # then writes 64 bytes to address 232123 at tick 500000.
50 #
51 # This script can of course also be used as a template to convert
52 # other trace formats into the gem5 protobuf format
53
54 import protolib
55 import sys
56
57 # Import the packet proto definitions. If they are not found, attempt
58 # to generate them automatically. This assumes that the script is
59 # executed from the gem5 root.
60 try:
61 import packet_pb2
62 except:
63 print("Did not find packet proto definitions, attempting to generate")
64 from subprocess import call
65 error = call(['protoc', '--python_out=util', '--proto_path=src/proto',
66 'src/proto/packet.proto'])
67 if not error:
68 print("Generated packet proto definitions")
69
70 try:
71 import google.protobuf
72 except:
73 print("Please install the Python protobuf module")
74 exit(-1)
75
76 import packet_pb2
77 else:
78 print("Failed to import packet proto definitions")
79 exit(-1)
80
81 def main():
82 if len(sys.argv) != 3:
83 print("Usage: ", sys.argv[0], " <ASCII input> <protobuf output>")
84 exit(-1)
85
86 try:
87 ascii_in = open(sys.argv[1], 'r')
88 except IOError:
89 print("Failed to open ", sys.argv[1], " for reading")
90 exit(-1)
91
92 try:
93 proto_out = open(sys.argv[2], 'wb')
94 except IOError:
95 print("Failed to open ", sys.argv[2], " for writing")
96 exit(-1)
97
98 # Write the magic number in 4-byte Little Endian, similar to what
99 # is done in src/proto/protoio.cc
100 proto_out.write("gem5")
101
102 # Add the packet header
103 header = packet_pb2.PacketHeader()
104 header.obj_id = "Converted ASCII trace " + sys.argv[1]
105 # Assume the default tick rate
106 header.tick_freq = 1000000000000
107 protolib.encodeMessage(proto_out, header)
108
109 # For each line in the ASCII trace, create a packet message and
110 # write it to the encoded output
111 for line in ascii_in:
112 cmd, addr, size, tick = line.split(',')
113 packet = packet_pb2.Packet()
114 packet.tick = int(tick)
115 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum
116 packet.cmd = 1 if cmd == 'r' else 4
117 packet.addr = int(addr)
118 packet.size = int(size)
119 protolib.encodeMessage(proto_out, packet)
120
121 # We're done
122 ascii_in.close()
123 proto_out.close()
124
125 if __name__ == "__main__":
126 main()