util: Add decode and encode scripts for elastic traces
[gem5.git] / util / decode_inst_dep_trace.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2013 - 2015 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 # 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: Radhika Jagtap
68 #
69
70 # This script is used to dump protobuf traces of the instruction dependency
71 # graph to ASCII format.
72 #
73 # The ASCII trace format uses one line per instruction with the format
74 # instruction sequence number, (optional) pc, (optional) weight, load, store,
75 # (optional) flags, (optional) addr, (optional) size, comp delay,
76 # (repeated) order dependencies comma-separated, and (repeated) register
77 # dependencies comma-separated.
78 #
79 # examples:
80 # seq_num,[pc],[weight,]load,store,[address,size,flags,]comp_delay:[rob_dep]:
81 # [reg_dep]
82 # 1,1,False,False,8500::
83 # 2,1,False,False,1000:,1:
84 # 3,1,True,False,831248,4,74,500:,2:
85 # 4,1,False,False,0:,2:
86 # 5,1,False,False,500::,4
87 # 6,1,False,True,831248,4,74,1000:,3:,4,5
88
89 import protolib
90 import sys
91
92 # Import the packet proto definitions. If they are not found, attempt
93 # to generate them automatically. This assumes that the script is
94 # executed from the gem5 root.
95 try:
96 import inst_dep_record_pb2
97 except:
98 print "Did not find proto definition, attempting to generate"
99 from subprocess import call
100 error = call(['protoc', '--python_out=util', '--proto_path=src/proto',
101 'src/proto/inst_dep_record.proto'])
102 if not error:
103 import inst_dep_record_pb2
104 print "Generated proto definitions for instruction dependency record"
105 else:
106 print "Failed to import proto definitions"
107 exit(-1)
108
109 def main():
110 if len(sys.argv) != 3:
111 print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>"
112 exit(-1)
113
114 # Open the file on read mode
115 proto_in = protolib.openFileRd(sys.argv[1])
116
117 try:
118 ascii_out = open(sys.argv[2], 'w')
119 except IOError:
120 print "Failed to open ", sys.argv[2], " for writing"
121 exit(-1)
122
123 # Read the magic number in 4-byte Little Endian
124 magic_number = proto_in.read(4)
125
126 if magic_number != "gem5":
127 print "Unrecognized file"
128 exit(-1)
129
130 print "Parsing packet header"
131
132 # Add the packet header
133 header = inst_dep_record_pb2.InstDepRecordHeader()
134 protolib.decodeMessage(proto_in, header)
135
136 print "Object id:", header.obj_id
137 print "Tick frequency:", header.tick_freq
138
139 print "Parsing packets"
140
141 num_packets = 0
142 num_regdeps = 0
143 num_robdeps = 0
144 packet = inst_dep_record_pb2.InstDepRecord()
145
146 # Decode the packet messages until we hit the end of the file
147 while protolib.decodeMessage(proto_in, packet):
148 num_packets += 1
149
150 # Write to file the seq num
151 ascii_out.write('%s' % (packet.seq_num))
152 # Write to file the pc of the instruction, default is 0
153 if packet.HasField('pc'):
154 ascii_out.write(',%s' % (packet.pc))
155 else:
156 ascii_out.write(',0')
157 # Write to file the weight, default is 1
158 if packet.HasField('weight'):
159 ascii_out.write(',%s' % (packet.weight))
160 else:
161 ascii_out.write(',1')
162 # Write to file if it is a load and if it is a store
163 ascii_out.write(',%s,%s' % (packet.load, packet.store))
164
165 # Write to file if it has the optional fields addr, size, flags
166 if packet.HasField('addr'):
167 ascii_out.write(',%s' % (packet.addr))
168 if packet.HasField('size'):
169 ascii_out.write(',%s' % (packet.size))
170 if packet.HasField('flags'):
171 ascii_out.write(',%s' % (packet.flags))
172
173 # Write to file the comp delay
174 ascii_out.write(',%s' % (packet.comp_delay))
175
176 # Write to file the repeated field order dependency
177 ascii_out.write(':')
178 if packet.rob_dep:
179 num_robdeps += 1
180 for dep in packet.rob_dep:
181 ascii_out.write(',%s' % dep)
182 # Write to file the repeated field register dependency
183 ascii_out.write(':')
184 if packet.reg_dep:
185 num_regdeps += 1 # No. of packets with atleast 1 register dependency
186 for dep in packet.reg_dep:
187 ascii_out.write(',%s' % dep)
188 # New line
189 ascii_out.write('\n')
190
191 print "Parsed packets:", num_packets
192 print "Packets with at least 1 reg dep:", num_regdeps
193 print "Packets with at least 1 rob dep:", num_robdeps
194
195 # We're done
196 ascii_out.close()
197 proto_in.close()
198
199 if __name__ == "__main__":
200 main()