-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExpDriver.py
321 lines (260 loc) · 11.5 KB
/
ExpDriver.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import sys
sys.path.append('./scripts')
from scripts.ExpStats import runExpWithName
from scripts.Nader import genSourceExp, runOnlyNader
from scripts.Nader import runNader
from scripts.ResultPresenter import genFig5, genFig9, genFig8
import subprocess
import os
import filecmp
import shutil
import argparse
from tqdm.auto import tqdm
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
def getUnsafeLines(directory, isBrotli=False):
line_nums = []
rs_files = subprocess.run(["find", directory, "-name", "*.rs.unsafe", "-o", "-name", "*.rs", "-type", "f"],
capture_output=True, text=True)
filelist = rs_files.stdout.split()
filelist = [fname.replace(directory, '') for fname in filelist]
for fname in filelist:
need_convert = False
write_fname = fname
if fname.endswith(".rs"):
if fname + ".unsafe" in filelist:
continue
else:
need_convert = True
write_fname = fname + ".unsafe"
with open(fname, 'r') as fd:
lines = fd.readlines()
found_unsafe = False
for idx, line in enumerate(lines):
# if "get_unchecked(" in line or "get_unchecked_mut(" in line:
if "get_unchecked" in line:
line_nums.append((write_fname, idx + 1))
found_unsafe = True
if need_convert and found_unsafe:
shutil.move(fname, fname + ".unsafe")
return line_nums
def genHotness(bmark_path, bin_name, arg):
os.chdir(bmark_path)
print("Generating hotness info with Callgrind")
out = subprocess.Popen([ROOT_PATH + '/scripts/genHotness.sh', bin_name, arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ =out.communicate()
out = out.decode("utf-8") # convert to string from bytes
return bmark_path + "baseline/exp-genHotness/cal.out"
def genUncheckedReport(bmark_path):
# get all lines with unsafe
os.chdir(bmark_path)
line_nums = getUnsafeLines(bmark_path)
line_nums_str = [str(a) + " " + str(b) for a, b in line_nums]
with open("unsafe_report.txt", "w") as fd:
fd.writelines("\n".join(line_nums_str))
return line_nums
def convertAndCompareBinaries(bmark_path, line_nums):
os.chdir(bmark_path)
fnames = set([i for i, _ in line_nums])
# Generate unsafe version
genSourceExp(bmark_path, "baseline", "safe", fnames, [])
genSourceExp(bmark_path, "baseline", "unsafe", fnames, line_nums)
# Compare binary
ret = filecmp.cmp(bmark_path + "/baseline/exp-unsafe/exp.exe", bmark_path + "/baseline/exp-safe/exp.exe")
return ret
def getPerfDiff(bmark_path, arg):
os.chdir(bmark_path)
time_exp_unsafe, _, _ = runExpWithName("baseline/exp-unsafe/exp.exe", arg, 5)
print("Unsafe time: ", time_exp_unsafe)
time_exp_safe, _, _ = runExpWithName("baseline/exp-safe/exp.exe", arg, 5)
print("Safe time: ", time_exp_safe)
return (time_exp_safe - time_exp_unsafe) / time_exp_unsafe
def getPerfDiffSilent(bmark_path, arg):
os.chdir(bmark_path)
time_exp_unsafe, _, _ = runExpWithName("baseline/exp-unsafe/exp.exe", arg, 5)
time_exp_safe, _, _ = runExpWithName("baseline/exp-safe/exp.exe", arg, 5)
return (time_exp_safe - time_exp_unsafe) / time_exp_unsafe
def genTable1():
baseline = []
workload = []
compiler = []
print("Generating Table 1")
# Baseline
print("Getting overheads for baseline context...")
bmark_path = ROOT_PATH + "/brotli-expanded/"
arg = ROOT_PATH + "/data/silesia-5.brotli"
line_nums = genUncheckedReport(bmark_path)
convertAndCompareBinaries(bmark_path, line_nums)
for i in tqdm(range(10), leave=True):
diff = getPerfDiffSilent(bmark_path, arg)
baseline.append(diff)
print("\tOverhead == {}".format(sum(baseline)/10))
# Different workload (compression level == 11, instead of 5)
print("Getting overheads for different workload...")
arg = ROOT_PATH + "/data/silesia-11.brotli"
for i in tqdm(range(10), leave=True):
diff = getPerfDiffSilent(bmark_path, arg)
workload.append(diff)
print("\tOverhead == {}".format(sum(workload)/10))
# rustc 1.46.0
print("Getting overheads for different compiler...")
arg = ROOT_PATH + "/data/silesia-5.brotli"
subprocess.run(["/home/.cargo/bin/rustup", "override", "set", "nightly-2020-08-27-x86_64-unknown-linux-gnu"], stdout=open(os.devnull, 'wb'), stderr=open(os.devnull, 'wb'))
convertAndCompareBinaries(bmark_path, line_nums)
for i in tqdm(range(10), leave=True):
diff = getPerfDiffSilent(bmark_path, arg)
compiler.append(diff)
print("\tOverhead == {}".format(sum(compiler)/10))
subprocess.run(["/home/.cargo/bin/rustup", "override", "unset"],
stdout=open(os.devnull, 'wb'), stderr=open(os.devnull, 'wb'))
def genFigure1(root, quick_run=False):
if full_run:
cratedir = os.path.join(root, "data", "crates_full")
else:
cratedir = os.path.join(root, "data", "crates_fast")
os.chdir(os.path.join(root, "scripts"))
subprocess.run(["python3", "tool.py", "--dir", cratedir,
"--compile", "--bench", "10", "--local", "-g"])
os.chdir(os.path.join(root, "data"))
subprocess.run(["mv", "figure1_all.pdf", os.path.join(root, "images/")])
subprocess.run(["mv", "figure1_histogram.pdf", os.path.join(root, "images/")])
subprocess.run(["mv", "figure1_hurt.pdf", os.path.join(root, "images/")])
subprocess.run(["mv", "figure1_improved.pdf", os.path.join(root, "images/")])
subprocess.run(["mv", "figure1_insignificantly_affected.pdf", os.path.join(root, "images/")])
os.chdir(root)
def genFigure7Table3(root, quick_run=False):
if full_run:
appdir = os.path.join(root, "data", "apps_full")
else:
appdir = os.path.join(root, "data", "apps_fast")
os.chdir(os.path.join(root, "scripts"))
subprocess.run(["python3", "uncover_uncheckeds.py", "--root", appdir])
f7_from_path = os.path.join(root, "data", "apps_{}".format(suffix), "figure7.pdf")
f7_to_path = os.path.join(root, "images")
subprocess.run(["mv", f7_from_path, f7_to_path])
t3_from_path = os.path.join(root, "data", "apps_{}".format(suffix), "table3.pdf")
t3_to_path = os.path.join(root, "images")
subprocess.run(["mv", t3_from_path, t3_to_path])
os.chdir(root)
def genFig8WithCOST(quick_run=False):
if not os.path.exists(ROOT_PATH + "/exp-results"):
os.mkdir(ROOT_PATH + "/exp-results")
if not os.path.exists(ROOT_PATH + "/images"):
os.mkdir(ROOT_PATH + "/images")
# prepare by vendoring
print("Preparing COST vendor")
out = subprocess.Popen([ROOT_PATH + '/scripts/prepareCOST.sh'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
out.wait()
endToEnd("COST", ROOT_PATH + "/COST/" , "hilbert " + ROOT_PATH + "/data/soc-LiveJournal1 4847571", 0.02, False)
## Generate brotli figs (Fig5 and Fig9)
def genFig5and9(quick_run=False):
#print("Running Nader on brotli, generating fig 5 and 9")
bmark_path = ROOT_PATH + "/brotli-expanded/"
arg = ROOT_PATH + "/data/silesia-5.brotli"
calout_fname = ROOT_PATH + "/example-results/cal.out.brotli"
runNader(cargo_root_=bmark_path, arg=arg, pickle_name="brotli.pkl", clang_arg=None, test_times=5, calout_fname=calout_fname, quick_run=quick_run)
if not os.path.exists(ROOT_PATH + "/exp-results"):
os.mkdir(ROOT_PATH + "/exp-results")
shutil.move(bmark_path + "brotli.pkl", ROOT_PATH + "/exp-results/brotli.pkl")
shutil.move(bmark_path + "threshold_unsafe_map.pkl", ROOT_PATH + "/exp-results/brotli-map.pkl")
if not os.path.exists(ROOT_PATH + "/images"):
os.mkdir(ROOT_PATH + "/images")
print("Generating plots")
genFig5(ROOT_PATH + "/exp-results", "brotli", ROOT_PATH + "/images/figure5.pdf")
# use test.pkl to generate Fig 5
genFig9(ROOT_PATH + "/exp-results", "brotli", ROOT_PATH + "/images/figure9.pdf")
def genTable4():
subprocess.run(["./scripts/table4.sh"])
def endToEnd(bmark_name, bmark_path, arg=None, threshold=0.03, skip_callgrind=True):
print("Step 1: Generating unchecked report")
line_nums = genUncheckedReport(bmark_path)
if len(line_nums) == 0:
print("No unsafe usage, abort!")
return
else:
print(len(line_nums), " unsafe usages, continue")
print("Step 2: Convert unsafe to safe and compare binaries")
ret = convertAndCompareBinaries(bmark_path, line_nums)
if ret:
print("Binaries are the same, abort!")
return
else:
print("Binaries are different, continue!")
print("Step 3: Identifying performance impact")
perf_diff = getPerfDiff(bmark_path, arg)
if perf_diff < threshold:
print("Performance difference lower than threshold: ", threshold, ", abort!")
return
else:
print("Performance difference higher than threshold: ", threshold, ", continue!")
print("Step 4: Running Nader")
if skip_callgrind:
calout_fname = ROOT_PATH + "/example-results/cal.out." + bmark_name
else:
print("preparing hotness file")
calout_fname = genHotness(bmark_path, "test_bc", arg)
if "brotli" in bmark_path:
isBrotli = True
else:
isBrotli = False
runOnlyNader(cargo_root_=bmark_path, arg=arg, pickle_name=bmark_name+ ".pkl", clang_arg=None, test_times=10, calout_fname=calout_fname, isBrotli=isBrotli)
shutil.move(bmark_path + "/" + bmark_name + ".pkl", ROOT_PATH + "/exp-results/" + bmark_name + ".pkl")
shutil.move(bmark_path + "/threshold_unsafe_map.pkl", ROOT_PATH + "/exp-results/" + bmark_name + "-map.pkl")
genFig8(ROOT_PATH + "/exp-results", bmark_name, ROOT_PATH + "/images/figure8.pdf")
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument("--all", "-a",
action="store_true",
required=False,
help="generate all tables and figures")
parser.add_argument("--figure1",
action="store_true",
required=False,
help="generate figure 1")
parser.add_argument("--table1",
action="store_true",
required=False,
help="generate table 1")
parser.add_argument("--figure59",
action="store_true",
required=False,
help="generate figures 5 and 9")
parser.add_argument("--figure7table3",
action="store_true",
required=False,
help="generate figure 7 and table 3")
parser.add_argument("--table4",
action="store_true",
required=False,
help="generate table 4")
parser.add_argument("--figure8",
action="store_true",
required=False,
help="generate figure 8")
parser.add_argument("--full", "-f",
action="store_true",
required=False,
help="run the full version of plot generation instead of the fast version")
args = parser.parse_args()
return args.all, args.figure1, args.table1, args.figure59, args.figure7table3, args.table4, args.figure8, args.full
if __name__ == "__main__":
gen_all, gen_f1, gen_t1, gen_f59, gen_f7t3, gen_t4, gen_f8, full_run = arg_parse()
root = os.getcwd()
quick_run = False if full_run else True
# Figure 1
if gen_all or gen_f1:
genFigure1(root, quick_run=quick_run)
# Table 1
if gen_all or gen_t1:
genTable1()
# Figure 5 and 9
if gen_all or gen_f59:
genFig5and9(quick_run=quick_run)
# Figure 7 and Table 3
if gen_all or gen_f7t3:
genFigure7Table3(root, quick_run=quick_run)
# Table 4
if gen_all or gen_t4:
genTable4()
# Figure 8
if gen_all or gen_f8:
genFig8WithCOST()