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
|
import os
import time
import run.litmus_util as lu
import shutil as sh
from operator import methodcaller
class ExperimentException(Exception):
'''Used to indicate when there are problems with an experiment.'''
def __init__(self, name):
self.name = name
class ExperimentDone(ExperimentException):
'''Raised when an experiment looks like it's been run already.'''
def __str__(self):
return "Experiment finished already: %d" % self.name
class SystemCorrupted(Exception):
pass
class Experiment(object):
'''Execute one task-set and save the results. Experiments have unique IDs.'''
INTERRUPTED_DIR = ".interrupted"
def __init__(self, name, scheduler, working_dir, finished_dir,
proc_entries, executables, tracer_types):
'''Run an experiment, optionally wrapped in tracing.'''
self.name = name
self.scheduler = scheduler
self.working_dir = working_dir
self.finished_dir = finished_dir
self.proc_entries = proc_entries
self.executables = executables
self.exec_out = None
self.exec_err = None
self.tracer_types = tracer_types
self.regular_tracers = []
self.exact_tracers = []
def __setup_tracers(self):
tracers = [ t(self.working_dir) for t in self.tracer_types ]
self.regular_tracers = [t for t in tracers if not t.is_exact()]
self.exact_tracers = [t for t in tracers if t.is_exact()]
for t in tracers:
self.log("Enabling %s" % t.get_name())
def __make_dirs(self):
interrupted = None
if os.path.exists(self.finished_dir):
raise ExperimentDone(self.name)
if os.path.exists(self.working_dir):
self.log("Found interrupted experiment, saving in %s" %
Experiment.INTERRUPTED_DIR)
interrupted = "%s/%s" % (os.path.split(self.working_dir)[0],
Experiment.INTERRUPTED_DIR)
old_int = "%s/%s" % (self.working_dir, Experiment.INTERRUPTED_DIR)
if os.path.exists(interrupted):
sh.rmtree(interrupted)
if os.path.exists(old_int):
sh.rmtree(old_int)
os.rename(self.working_dir, interrupted)
os.mkdir(self.working_dir)
if interrupted:
os.rename(interrupted, "%s/%s" % (self.working_dir,
os.path.split(interrupted)[1]))
def __assign_executable_cwds(self):
def assign_cwd(executable):
executable.cwd = self.working_dir
map(assign_cwd, self.executables)
def __try_kill_all(self):
try:
if lu.waiting_tasks():
released = lu.release_tasks()
self.log("Re-released %d tasks" % released)
time.sleep(1)
self.log("Killing all tasks")
for e in self.executables:
try:
e.kill()
except:
pass
time.sleep(1)
except:
self.log("Failed to kill all tasks.")
def __strip_path(self, path):
'''Shorten path to something more readable.'''
file_dir = os.path.split(self.working_dir)[0]
if path.index(file_dir) == 0:
path = path[len(file_dir):]
return path.strip("/")
def __check_tasks_status(self):
'''Raises an exception if any tasks have failed.'''
msgs = []
for e in self.executables:
status = e.poll()
if status != None and status:
err_msg = "Task %s failed with status: %s" % (e.wait(), status)
msgs += [err_msg]
if msgs:
# Show at most 3 messages so that every task failing doesn't blow
# up the terminal
if len(msgs) > 3:
num_errs = len(msgs) - 3
msgs = msgs[0:4] + ["...%d more task errors..." % num_errs]
out_name = self.__strip_path(self.exec_out.name)
err_name = self.__strip_path(self.exec_err.name)
help = "Check dmesg, %s, and %s" % (out_name, err_name)
raise Exception("\n".join(msgs + [help]))
def __wait_for_ready(self):
self.log("Sleeping until tasks are ready for release...")
wait_start = time.time()
num_ready = lu.waiting_tasks()
while num_ready < len(self.executables):
# Quit if too much time passes without a task becoming ready
if time.time() - wait_start > 180.0:
s = "waiting: %d, submitted: %d" %\
(lu.waiting_tasks(), len(self.executables))
raise Exception("Too much time spent waiting for tasks! %s" % s)
time.sleep(1)
# Quit if any tasks fail
self.__check_tasks_status()
# Reset the waiting time whenever more tasks become ready
now_ready = lu.waiting_tasks()
if now_ready != num_ready:
wait_start = time.time()
num_ready = now_ready
def __run_tasks(self):
self.log("Starting %d tasks" % len(self.executables))
for i,e in enumerate(self.executables):
try:
e.execute()
except:
raise Exception("Executable failed to start: %s" % e)
self.__wait_for_ready()
# Exact tracers (like overheads) must be started right after release or
# measurements will be full of irrelevant records
self.log("Starting %d released tracers" % len(self.exact_tracers))
map(methodcaller('start_tracing'), self.exact_tracers)
time.sleep(1)
try:
self.log("Releasing %d tasks" % len(self.executables))
released = lu.release_tasks()
if released != len(self.executables):
# Some tasks failed to release, kill all tasks and fail
# Need to release non-released tasks before they can be killed
raise Exception("Released %s tasks, expected %s tasks" %
(released, len(self.executables)))
self.log("Waiting for program to finish...")
for e in self.executables:
if not e.wait():
raise Exception("Executable %s failed to complete!" % e)
finally:
# And these must be stopped here for the same reason
self.log("Stopping exact tracers")
map(methodcaller('stop_tracing'), self.exact_tracers)
def __save_results(self):
os.rename(self.working_dir, self.finished_dir)
def __to_linux(self):
msgs = []
sched = lu.scheduler()
if sched != "Linux":
self.log("Switching back to Linux scheduler")
try:
lu.switch_scheduler("Linux")
except:
msgs += ["Scheduler is %s, cannot switch to Linux!" % sched]
running = lu.all_tasks()
if running:
msgs += ["%d real-time tasks still running!" % running]
if msgs:
raise SystemCorrupted("\n".join(msgs))
def __setup(self):
self.__make_dirs()
self.__assign_executable_cwds()
self.__setup_tracers()
self.log("Writing %d proc entries" % len(self.proc_entries))
map(methodcaller('write_proc'), self.proc_entries)
self.log("Starting %d regular tracers" % len(self.regular_tracers))
map(methodcaller('start_tracing'), self.regular_tracers)
time.sleep(1)
self.log("Switching to %s" % self.scheduler)
lu.switch_scheduler(self.scheduler)
time.sleep(1)
self.exec_out = open('%s/exec-out.txt' % self.working_dir, 'w')
self.exec_err = open('%s/exec-err.txt' % self.working_dir, 'w')
def set_out(executable):
executable.stdout_file = self.exec_out
executable.stderr_file = self.exec_err
map(set_out, self.executables)
def __teardown(self):
self.exec_out and self.exec_out.close()
self.exec_err and self.exec_err.close()
self.log("Stopping regular tracers")
map(methodcaller('stop_tracing'), self.regular_tracers)
os.system('sync')
def log(self, msg):
print("[Exp %s]: %s" % (self.name, msg))
def run_exp(self):
self.__to_linux()
succ = False
exception = None
try:
self.__setup()
try:
self.__run_tasks()
self.log("Saving results in %s" % self.finished_dir)
succ = True
except Exception as e:
exception = e
# Give time for whatever failed to finish failing
time.sleep(2)
self.__try_kill_all()
finally:
try:
self.__teardown()
self.__to_linux()
except Exception as e:
exception = exception or e
finally:
if exception: raise exception
if succ:
self.__save_results()
self.log("Experiment done!")
|