1 #!/usr/bin/env python2.7
3 # Copyright (c) 2013 - 2015 ARM Limited
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.
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.
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.
38 # Copyright 2008 Google Inc. All rights reserved.
39 # http://code.google.com/p/protobuf/
41 # Redistribution and use in source and binary forms, with or without
42 # modification, are permitted provided that the following conditions are
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
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.
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.
67 # Authors: Radhika Jagtap
70 # This script is used to dump protobuf traces of the instruction dependency
71 # graph to ASCII format.
73 # The ASCII trace format uses one line per instruction with the format
74 # instruction sequence number, (optional) pc, (optional) weight, type
75 # (optional) flags, (optional) phys addr, (optional) size, comp delay,
76 # (repeated) order dependencies comma-separated, and (repeated) register
77 # dependencies comma-separated.
80 # seq_num,[pc],[weight,]type,[p_addr,size,flags,]comp_delay:[rob_dep]:
82 # 1,35652,1,COMP,8500::
83 # 2,35656,1,COMP,0:,1:
84 # 3,35660,1,LOAD,1748752,4,74,500:,2:
85 # 4,35660,1,COMP,0:,3:
86 # 5,35664,1,COMP,3000::,4
87 # 6,35666,1,STORE,1748752,4,74,1000:,3:,4,5
88 # 7,35666,1,COMP,3000::,4
89 # 8,35670,1,STORE,1748748,4,74,0:,6,3:,7
90 # 9,35670,1,COMP,500::,7
95 # Import the packet proto definitions. If they are not found, attempt
96 # to generate them automatically. This assumes that the script is
97 # executed from the gem5 root.
99 import inst_dep_record_pb2
101 print "Did not find proto definition, attempting to generate"
102 from subprocess
import call
103 error
= call(['protoc', '--python_out=util', '--proto_path=src/proto',
104 'src/proto/inst_dep_record.proto'])
106 import inst_dep_record_pb2
107 print "Generated proto definitions for instruction dependency record"
109 print "Failed to import proto definitions"
113 if len(sys
.argv
) != 3:
114 print "Usage: ", sys
.argv
[0], " <protobuf input> <ASCII output>"
117 # Open the file on read mode
118 proto_in
= protolib
.openFileRd(sys
.argv
[1])
121 ascii_out
= open(sys
.argv
[2], 'w')
123 print "Failed to open ", sys
.argv
[2], " for writing"
126 # Read the magic number in 4-byte Little Endian
127 magic_number
= proto_in
.read(4)
129 if magic_number
!= "gem5":
130 print "Unrecognized file"
133 print "Parsing packet header"
135 # Add the packet header
136 header
= inst_dep_record_pb2
.InstDepRecordHeader()
137 protolib
.decodeMessage(proto_in
, header
)
139 print "Object id:", header
.obj_id
140 print "Tick frequency:", header
.tick_freq
142 print "Parsing packets"
144 print "Creating enum value,name lookup from proto"
146 desc
= inst_dep_record_pb2
.InstDepRecord
.DESCRIPTOR
147 for namestr
, valdesc
in desc
.enum_values_by_name
.items():
148 print '\t', valdesc
.number
, namestr
149 enumNames
[valdesc
.number
] = namestr
154 packet
= inst_dep_record_pb2
.InstDepRecord()
156 # Decode the packet messages until we hit the end of the file
157 while protolib
.decodeMessage(proto_in
, packet
):
160 # Write to file the seq num
161 ascii_out
.write('%s' % (packet
.seq_num
))
162 # Write to file the pc of the instruction, default is 0
163 if packet
.HasField('pc'):
164 ascii_out
.write(',%s' % (packet
.pc
))
166 ascii_out
.write(',0')
167 # Write to file the weight, default is 1
168 if packet
.HasField('weight'):
169 ascii_out
.write(',%s' % (packet
.weight
))
171 ascii_out
.write(',1')
172 # Write to file the type of the record
174 ascii_out
.write(',%s' % enumNames
[packet
.type])
176 print "Seq. num", packet
.seq_num
, "has unsupported type", \
181 # Write to file if it has the optional fields physical addr, size,
183 if packet
.HasField('p_addr'):
184 ascii_out
.write(',%s' % (packet
.p_addr
))
185 if packet
.HasField('size'):
186 ascii_out
.write(',%s' % (packet
.size
))
187 if packet
.HasField('flags'):
188 ascii_out
.write(',%s' % (packet
.flags
))
190 # Write to file the comp delay
191 ascii_out
.write(',%s' % (packet
.comp_delay
))
193 # Write to file the repeated field order dependency
197 for dep
in packet
.rob_dep
:
198 ascii_out
.write(',%s' % dep
)
199 # Write to file the repeated field register dependency
202 num_regdeps
+= 1 # No. of packets with atleast 1 register dependency
203 for dep
in packet
.reg_dep
:
204 ascii_out
.write(',%s' % dep
)
206 ascii_out
.write('\n')
208 print "Parsed packets:", num_packets
209 print "Packets with at least 1 reg dep:", num_regdeps
210 print "Packets with at least 1 rob dep:", num_robdeps
216 if __name__
== "__main__":