-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit.py
206 lines (190 loc) · 7.57 KB
/
fit.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
#Fault Injection Tool
#Writes to output file 'filename-cfg.cc'
#Assumed: ~perturb all += computations in innermost for loop
# ~type of z is double (for min/max in inject func)
# ~File outputs results in format [\d : \d]
import configparser as cp
import statistics as stats
import os, re, time, sys
#takes the result of readlines() and first line of a function
#returns last line of the function
def boundaries(lines, sline) :
numlines = len(lines)
seen = 0
count = 0
cend = -1
for line in range(sline, numlines) :
current = lines[line]
if (line <= cend) :
continue #we are inside a block comment
if (current.find("/*") != -1) :
cend = block(lines, line)
current = current[:current.find("/*")]
if (current.find("//") != -1) :
current = current[:current.find("//")] #ignore anything after '//'
if (current.count("{") != 0) :
count += current.count("{")
seen = 1 #want to make sure we have seen a { before giving up
if (current.count("}") != 0) :
count -= current.count("}")
if ((count <= 0) & (seen != 0)) :
return line
#returns last line of a block comment (c code, so */)
def block(lines, sline) :
numlines = len(lines)
for line in range(sline, numlines) :
current = lines[line]
if (current.find("*/") != -1) :
return line
#determine first line of innermost for loop
def innermost(lines, sline, eline) :
#if we know there's def no more loops, don't waste time looking
#if (' '.join(lines[sline+1:eline]).count('for') == 0) :
# return sline
cend = -1
for line in range(sline+1, eline) :
current = lines[line]
if (line <= cend) :
continue #we are inside a block comment
if (current.find("/*") != -1) :
cend = block(lines, line)
current = current[:current.find("/*")]
if (current.find("//") != -1) :
current = current[:current.find("//")]
#if ((current.split().count("for") != 0) :
if (current.find("for") != -1) :
e = boundaries(lines, line)
return innermost(lines, line, e)
return sline
#takes lines and boundaries of function, finds a += computation in a
#for loop and injects error into it by calling inject() function
def perturb(lines, erate, start, end) :
s = innermost(lines, start, end)
e = boundaries(lines, s)
#print(s, " : ", e, " Boundaries of for loop")
for l in range(s, e) :
cur = lines[l]
if (cur.count("+=") != 0) :
loc = cur.find("+=")
print("found += at line ", l+1, " and loc ", loc)
loc += 2 #move 'pointer' to account for '+='
front = cur[:loc]
scol = cur.find(";")
back = cur[loc:scol] #strip the ;\n
perturbed = front + ' inject(' + erate + "," + back + ');\n'
lines[l] = perturbed
print("Perturbed line ", l+1)
print(lines[l][:-1])
#create and write output file
def output(lines) :
with open(outname, 'w') as out :
out.writelines(lines)
#Includes the error file
def include(lines, ename) :
incl = '#include "' + ename + '"\n'
lines.insert(0, incl)
return lines
#edits the error file with the user specified rate
def errordef(ename, rate) :
with open(ename, 'r+') as file :
lines = file.readlines()
for line, cur in enumerate(lines) :
if (cur.find("double error_rate") != -1) :
loc = cur.find("error_rate")
loc += 10 #move 'pointer' past the word
cur = cur[:loc] #truncate the line
cur = cur + " = " + str(rate) + ";\n"
lines[line] = cur
with open(ename, 'w') as file :
file.writelines(lines)
#gets rid of file extensions
def stripname(file) :
if (file.find('.') != -1) :
loc = file.find('.')
file = file[:loc]
return file
#assumes code is compiled; executes code and retrieves results
#returns percent correct
def getresults(ex) :
results = os.popen(ex).readlines()
regex = re.compile("(\d+)( : )(\d+)")
results = [m.group(0) for l in results for m in [regex.search(l) ] if m]
result = results[0]
m = regex.search(result)
num = int(m.group(1))
den = int(m.group(3))
return num/den*100
#checks if instance of function is a definition or prototype
#obviously incomplete still....
def checkdef(lines, sline) :
current = lines[sline]
############################################################
#MAIN PROGRAM
cfg = sys.argv[1]
t0 = time.time()
#set values from config file
config = cp.ConfigParser()
config.read(cfg)
filename = config.get('attributes', 'filename')
errorfile = config.get('attributes', 'errorfile')
functions = config.get('attributes', 'functions')
percents = config.get('attributes', 'percents')
rate = config.get('attributes', 'rate')
runs = int(config.get('attributes', 'runs'))
outname = stripname(filename) + '-' + stripname(cfg) + '.cc'
compargs = '' #by default, no extra arguments
cmdargs = ''
if (config.has_option('args', 'compile')) :
compargs = ''.join(config.get('args', 'compile'))
if (config.has_option('args', 'execute')) :
cmdargs = ''.join(config.get('args', 'execute'))
with open(filename, 'r') as file :
lines = file.readlines()
errordef(errorfile, rate)
lines = include(lines, errorfile)
functions = functions.split()
percents = percents.split()
for index, function in enumerate(functions) :
cend = -1
for line, current in enumerate(lines) :
if (line <= cend) :
continue #we are inside a block comment
if (current.find("/*") != -1) :
cend = block(lines, line)
current = current[:current.find("/*")]
if (current.find("//") != -1) :
current = current[:current.find("//")] #ignore anything after '//'
if(current.count(function) != 0) :
print("Found ", function, " at line ", line+1)
if(current.find(";") == -1) :
print("DEFINED at line ", line+1)
start = line
print("\nFinding ",function," boundaries...")
end = boundaries(lines, start) #finds end of function (given start)
print("Boundaries found. Perturbing ",function,"...\n")
erate = percents[index]
print(function, " : ", erate, "%")
perturb(lines, erate, start, end)
output(lines)
print("Runs: ", runs)
print("Rate: ", rate, "%")
#compile and execute baseline
compiler = "g++ " + filename + " " + compargs
command = "./a.out " + cmdargs
os.system(compiler)
baseline = getresults(command)
#run analysis
compiler = "g++ " + outname + " " + compargs
command = "./a.out " + cmdargs
os.system(compiler)
results = []
for i in range(runs) :
results.append(getresults(command))
mean = stats.mean(results)
normalized = mean/baseline*100
with open("results", "w") as datafile :
for i in results :
datafile.write(str(i)+'\n')
print("Normalized mean: ", normalized)
t1 = time.time()
print("Time elapsed: ", t1-t0, " seconds")