forked from chipsalliance/rocket-chip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toaxe.py
executable file
·210 lines (185 loc) · 7.04 KB
/
toaxe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# This file was originally written by Matthew Naylor, University of
# Cambridge.
#
# This software was partly developed by the University of Cambridge
# Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
# ("CTSRD"), as part of the DARPA CRASH research programme.
#
# This software was partly developed by the University of Cambridge
# Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
# ("MRC2"), as part of the DARPA MRC research programme.
#
# This software was partly developed by the University of Cambridge
# Computer Laboratory as part of the Rigorous Engineering of
# Mainstream Systems (REMS) project, funded by EPSRC grant
# EP/K008528/1.
# -------
# Outline
# -------
# This script takes memory-subsystem traces produced by the groundtest
# trace generator (see tracegen.scala) and puts them into a format
# that can be validated by the axe tool (see
# https://github.com/CTSRD-CHERI/axe).
import sys
import re
import sets
def main():
if len(sys.argv) < 2:
sys.stderr.write("Usage: toaxe.py TRACE-FILE [STATS-OUT-FILE]\n")
sys.exit(-1)
if sys.argv[1] == "-":
f = sys.stdin
else:
f = open(sys.argv[1], 'r')
if f == None:
sys.stderr.write("File not found: " + sys.argv[1] + "\n")
sys.exit(-1)
statsFile = None
if len(sys.argv) > 2:
statsFile = open(sys.argv[2], 'a')
lineCount = 0
def error(msg):
sys.stderr.write("Error at line " + str(lineCount) + ": " + msg + "\n")
sys.exit(-1)
# Mapping from address to axe address
addrMap = {}
nextAddr = 0
# Mapping from (thread id, tag id) to axe operation id
tagMap = {}
# Mapping from thread id to operation id
fenceReq = {}
# Mapping from thread id to operation id
loadReserve = {}
# Array of axe operations
ops = []
# Statistics
scCount = 0 # Number of store-conditionals
scSuccessCount = 0 # Number of store-conditionals that succeeded
loadCount = 0 # Number of loads
loadExtCount = 0 # Number of loads of value written by another core
# The previous write to each address by each thread
prevWrite = {}
for line in f:
# Parse thread id and command
m = re.search(' *([0-9]+) *: *([^ ]*) (.*)', line)
if m == None: error("Expected: <thread-id> ':' <command>")
tid, cmd, line = m.group(1), m.group(2), m.group(3)
if cmd == 'fence-req':
# Parse time
m = re.search(' *@ *([0-9]+)', line)
if m == None: error ("expected timestamp")
# Insert placeholder containing request time
ops.append(str(m.group(1)))
fenceReq[tid] = len(ops)-1
elif cmd == 'fence-resp':
# Insert 'sync' operation
if not (tid in fenceReq) or fenceReq[tid] == None:
error("fence-resp without fence-req on thread " + tid)
startTime = ops[fenceReq[tid]]
op = str(tid) + ": sync @ " + startTime
# Add end-time
m = re.search(' *@ *([0-9]+)', line)
if m != None: op = op + ":" + str(m.group(1))
ops[fenceReq[tid]] = (op,)
fenceReq[tid] = None
elif cmd == 'load-req' or cmd == 'load-reserve-req':
# Parse address, tag, and time
m = re.search(' *([0-9a-fx]+) *# *([0-9]+) *@ *([0-9]+)', line)
if m == None: error("expected <address> #<tag> @<timestamp>")
# Update address map
if not (m.group(1) in addrMap):
addrMap[m.group(1)] = nextAddr
nextAddr = nextAddr+1
# Insert place-holder
ops.append((cmd, None, addrMap[m.group(1)], m.group(3), None))
tagMap[(tid, m.group(2))] = len(ops)-1
if cmd == 'load-reserve-req':
loadReserve[tid] = len(ops)-1
elif cmd == 'store-req' or cmd == 'store-cond-req' or cmd == 'swap-req':
# Parse value, address, tag, and time
m = re.search(' *([0-9]+) *([0-9a-fx]+) *# *([0-9]+) *@ *([0-9]+)', line)
if m == None: error("expected <value> <address> #<tag> @<timestamp>")
# Update address map
if not (m.group(2) in addrMap):
addrMap[m.group(2)] = nextAddr
nextAddr = nextAddr+1
# Insert place-holder
lr = loadReserve[tid] if tid in loadReserve else None
ops.append((cmd, m.group(1), addrMap[m.group(2)], m.group(4), lr))
tagMap[(tid, m.group(3))] = len(ops)-1
if cmd == 'store-cond-req': loadReserve[tid] = None
prevWrite[(tid, addrMap[m.group(2)])] = m.group(1)
elif cmd == 'resp':
# Parse value and timestamp
m = re.search(' *([0-9]+) *# *([0-9]+) *@ *([0-9]+)', line)
if m == None: error("expected <value> #<tag> @<timestamp>")
# Find corresponding response
tag = m.group(2)
if not ((tid, tag) in tagMap) or tagMap[(tid, tag)] == None:
error("resp without associated req with tag " + tag +
" on thread " + tid)
opId = tagMap[(tid, tag)]
(c, val, addr, start, lr) = ops[opId]
if c == 'load-req' or c == 'load-reserve-req':
loadCount = loadCount+1
if prevWrite.get((tid, addr), None) != m.group(1):
loadExtCount = loadExtCount+1
if c == 'load-req':
op = tid + ": M[" + str(addr) + '] == ' + m.group(1) + ' @ '
op += start + ':' + m.group(3)
ops[opId] = (op,)
elif c == 'store-req':
op = tid + ": M[" + str(addr) + '] := ' + val + ' @ '
op += start + ':' # + m.group(3)
ops[opId] = (op,)
elif c == 'load-reserve-req':
ops[opId] = (m.group(1), start, m.group(3))
elif c == 'store-cond-req':
if lr == None: error("store conditional without load-reserve")
(loadVal, loadStart, loadFin) = ops[lr]
scCount = scCount + 1
if int(m.group(1)) != 0:
# SC fail
op = tid + ": M[" + str(addr) + "] == " + loadVal
op += " @ " + loadStart + ":" + loadFin
else:
# SC success
scSuccessCount = scSuccessCount + 1
op = tid + ": { M[" + str(addr) + "] == " + loadVal + "; "
op += "M[" + str(addr) + "] := " + val + "} @ "
op += loadStart + ":" + loadFin
ops[lr] = (op,)
ops[opId] = None
elif c == 'swap-req':
op = tid + ": { M[" + str(addr) + '] == ' + m.group(1)
op += '; M[' + str(addr) + '] := ' + val
op += '} @ ' + start + ':' # + m.group(3)
ops[opId] = (op,)
else:
error("Unknown command '" + cmd + "'")
lineCount = lineCount+1
# Print statistics
if (scCount > 0):
scSuccessRate = str(scSuccessCount/float(scCount))[0:6]
print("# LRSC_Success_Rate=" + scSuccessRate)
if statsFile != None:
statsFile.write("LRSC_Success_Rate=" + scSuccessRate + "\n")
if (loadCount > 0):
loadExtRate = str(loadExtCount/float(loadCount))[0:6]
print("# Load_External_Rate=" + loadExtRate)
if statsFile != None:
statsFile.write("Load_External_Rate=" + loadExtRate + "\n")
if statsFile != None:
statsFile.close()
# Print address map in comments
for addr in addrMap:
print ("# &M[" + str(addrMap[addr]) + "] == " + addr)
# Print axe trace
for op in ops:
if op != None and isinstance(op, tuple) and len(op) == 1:
print op[0]
try:
main()
except KeyboardInterrupt:
sys.exit(-1)