-
Notifications
You must be signed in to change notification settings - Fork 0
/
grade.py
211 lines (176 loc) · 6.34 KB
/
grade.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
#!/usr/bin/env python3
import math
import signal
import subprocess
import sys
STUDENT_DEFINED = ['mm_calloc', 'mm_checkheap', 'mm_free', 'mm_init',
'mm_malloc', 'mm_realloc']
MINUTIL = 60
TIMEOUT = 30
TRACEFILES = [
"traces/amptjp-bal.rep",
"traces/amptjp.rep",
"traces/bash.rep",
"traces/binary.rep",
"traces/binary-bal.rep",
"traces/binary2.rep",
"traces/binary2-bal.rep",
"traces/cccp.rep",
"traces/cccp-bal.rep",
"traces/chrome.rep",
"traces/coalescing.rep",
"traces/coalescing-bal.rep",
"traces/coalesce-big.rep",
"traces/corners.rep",
"traces/cp-decl.rep",
"traces/cp-decl-bal.rep",
"traces/expr.rep",
"traces/expr-bal.rep",
"traces/firefox.rep",
"traces/fs.rep",
"traces/hostname.rep",
"traces/login.rep",
"traces/lrucd.rep",
"traces/ls.rep",
"traces/ls.1.rep",
"traces/malloc.rep",
"traces/malloc-free.rep",
"traces/nlydf.rep",
"traces/perl.rep",
"traces/perl.1.rep",
"traces/perl.2.rep",
"traces/perl.3.rep",
"traces/pulseaudio.rep",
"traces/qyqyc.rep",
"traces/random.rep",
"traces/random-bal.rep",
"traces/random2.rep",
"traces/random2-bal.rep",
"traces/realloc2.rep",
"traces/realloc2-bal.rep",
"traces/rm.rep",
"traces/rm.1.rep",
"traces/rulsr.rep",
"traces/short1.rep",
"traces/short1-bal.rep",
"traces/short2.rep",
"traces/short2-bal.rep",
"traces/stty.rep",
"traces/tty.rep",
"traces/xterm.rep"]
TRACEFILES_EXTRA = [
"traces-private/alaska.rep",
"traces-private/firefox-reddit2.rep",
"traces-private/firefox-reddit.rep",
"traces-private/freeciv.rep",
"traces-private/merry-go-round.rep",
"traces-private/realloc-bal.rep",
"traces-private/realloc.rep",
"traces-private/seglist.rep"]
def runtrace(trace):
mdriver = subprocess.run([
"valgrind",
"--tool=callgrind",
"--callgrind-out-file=callgrind.out",
"--toggle-collect=mm_malloc",
"--toggle-collect=mm_free",
"--toggle-collect=mm_realloc",
"--toggle-collect=mm_calloc",
"--", "./mdriver", "-f", trace],
capture_output=True, timeout=TIMEOUT)
output = mdriver.stdout.decode()
print(output)
if any(line.startswith('ERROR') for line in output.splitlines()):
raise SystemExit("Your solution is incorrect - check messages above!")
if mdriver.returncode < 0:
signame = signal.Signals(-mdriver.returncode).name
raise SystemExit(f"Your solution has been terminated by {signame}!")
if mdriver.returncode > 0:
raise SystemExit(f"Your solution has exited abnormally "
f"with status {mdriver.returncode}!")
# Process statistics from mdriver
stats = mdriver.stdout.decode().splitlines()[3][4:].split()
try:
util = float(stats[1][:-1])
used = int(stats[2])
total = int(stats[3])
except ValueError:
print(stats)
raise SystemExit("Reading statistics from 'mdriver' has failed :( "
"This is a bug - please report it!")
annotate = subprocess.run([
"callgrind_annotate", "--tree=calling", "callgrind.out"],
capture_output=True)
# Process output from callgrind_annotate
insn = 0
show = 10000
for i, line in enumerate(annotate.stdout.decode().splitlines()):
if i >= show and line:
print(line)
if 'PROGRAM TOTALS' in line:
insn = int(line.strip().split()[0].replace(',', ''))
if 'file:function' in line:
show = i + 3
sys.stdout.flush()
return util, insn, used, total
def check_symbols():
nm = subprocess.run(['nm', '-g', '--defined-only', 'mm.o'],
stdout=subprocess.PIPE)
for line in nm.stdout.decode().splitlines():
symbol = line.split()[-1]
if symbol not in STUDENT_DEFINED:
print(f'Symbol "{symbol}" in "mm.o" cannot be visible externally!')
raise SystemExit("Your solution was disqualified! :(")
def check_sections():
objdump = subprocess.run(['objdump', '-h', 'mm.o'],
stdout=subprocess.PIPE)
data_size = 0
for line in objdump.stdout.decode().splitlines()[5::2]:
fs = line.split()
if fs[1] in ['.data', '.bss']:
data_size += int(fs[2], 16)
threshold = 128
if data_size >= threshold:
print(f"Your solution stores too much data ({data_size} bytes) "
f"in '.data' and '.bss' sections.")
print(f"Please move top level data definitions into heap header!")
raise SystemExit("Your solution was disqualified! :(")
if __name__ == '__main__':
check_symbols()
check_sections()
all_ops = []
all_insn = []
all_util = []
all_used = []
all_total = []
for trace in TRACEFILES:
print("\nRunning mdriver for '%s'..." % trace)
with open(trace, "r") as f:
_ = int(f.readline())
_ = int(f.readline())
ops = int(f.readline())
util = 0.0 # default utilization penalty for timeout
insn = 50000.0 * ops # default throughput penalty for timeout
heapsz = math.inf
try:
util, insn, used, total = runtrace(trace)
except subprocess.TimeoutExpired:
print("Penalty accrued for timeout of %ds." % TIMEOUT)
all_insn.append(insn)
all_util.append(util)
all_ops.append(ops)
all_used.append(used)
all_total.append(total)
# Grade solution
weighted_util = 0
for util, ops in zip(all_util, all_ops):
weighted_util += util * (ops / sum(all_ops))
total_util = sum(all_used) / sum(all_total)
print("\nWeighted memory utilization: %.1f%%" % weighted_util)
print("Total memory utilization: %.2f%%" % (100.0 * total_util))
print("Instructions per operation: %d" % (sum(all_insn) / sum(all_ops)))
if weighted_util < MINUTIL:
print("Minimum threshold for memory utilization "
"of %d%% has not been met!" % MINUTIL)
print("Your solution was disqualified! :(")
sys.exit(1)