-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunExperiments.py
executable file
·312 lines (253 loc) · 9.93 KB
/
runExperiments.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
# Copyright (c) 2008-2011, Jan Gasthaus
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#!/usr/bin/env python
import sys, os
import subprocess
from code import interact
import readline
import Queue
from threading import Thread
from time import sleep
import pexpect
import logging
"""This script can be used to run a command with several different command
line options on several computers. The results that are printed on the
command line are written to a file.
Usage:
runExperiments.py [command file] [command] [additional (fixed) parameters]
Typical usage:
runExperiments.py params.txt "java -jar experimenter.jar" "--batch"
This will run the commands "java -jar experimenter.jar --batch" combined with
each line of command line options contained in params.txt.
The script runs these commands in separate threads (called workers), which
can either be local or remote via a SSH connection. The script starts out
with no workers, so in order to start the commands, workers have to be
added using either
add_local(# local workers) or
add_workers(["hostname1", "hostname2", ...])
The command prompt shown by the script is a full-blown Python shell, so
commands like
add_workers(["worker" + str(i) + ".foobar.com" for i in range(1,100)])
are also possible. The command
status()
shows the commands currently processed by the workers and also the number
of remaining jobs.
"""
__version__ = "$Id: runExperiments.py 153 2007-09-09 18:09:33Z sm $"
PARAM_FILE = "parameters.txt"
CMD = "./run_experiment"
CWD = "experiments"
ADDITIONAL_PARAMS = ""
NUM_LOCAL_WORKERS = 2
SSH_WORKERS = {}
LOCAL_WORKERS = {}
JOB_QUEUE = Queue.Queue(0)
RESULT_QUEUE = Queue.Queue(0)
FAILED = []
KILLCMD = "killall python; sleep 2; ps xu | grep python"
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='runExperiments.errors',
filemode='w')
## hack taken from libsvm's grid.py
## to turn queue into a stack
def _put(self,item):
if sys.hexversion >= 0x020400A1:
self.queue.appendleft(item)
else:
self.queue.insert(0,item)
import new
JOB_QUEUE._put = new.instancemethod(_put,JOB_QUEUE,JOB_QUEUE.__class__)
class ResultWatcher(Thread):
def __init__(self,result_queue):
Thread.__init__(self)
self.result_queue = result_queue
self.outfile = open("results.txt","a")
def run(self):
while True:
result = self.result_queue.get()
self.handle_result(result)
self.result_queue.task_done()
def handle_result(self,result):
self.outfile.write(result[2])
self.outfile.flush()
#print result[0] + ": " + result[2]
class Worker(Thread):
def __init__(self,name,job_queue,result_queue):
Thread.__init__(self)
self.name = name
self.job_queue = job_queue
self.result_queue = result_queue
self.running = True
self.current_job = "idle"
self.failures = 0
self.status = "status not set"
def run(self):
while self.running:
command = self.job_queue.get()
try:
self.current_job = command
result = self.run_command(command)
if result is None:
raise Exception("get no result")
except Exception, inst:
# we failed, put in FAILED list
FAILED.append(command)
print "command " + command + "FAILED!"
self.failures += 1
sleep(3) # wait for 3 seconds before we try again
if self.failures > 3:
sleep(60) # wait a little longer
if self.failures > 5:
print "worker %s had more than 5 failures; stopping" % self.name
self.current_job = "failed"
break
else:
self.result_queue.put((self.name,command,result))
self.failures = 0
finally:
self.job_queue.task_done()
if self.current_job != "failed":
self.current_job = "idle"
self.current_job = "stopped"
def stop(self):
self.running = False
def handle_command(self,cmdline):
p = pexpect.spawn(cmdline)
while True:
i = p.expect(["Done",r"Status: [^\r]+\r\n",pexpect.EOF])
if i == 1:
self.status=p.after
elif i==0:
self.status = "Done"
return "Done"
else:
logging.error(cmdline)
logging.error(p.before)
return
class LocalWorker(Worker):
def run_command(self,command):
cmdline = "/usr/bin/nice -n 19 %s" % command
return self.handle_command(cmdline)
class SSHWorker(Worker):
def __init__(self,host,job_queue,result_queue):
Worker.__init__(self,host,job_queue,result_queue)
self.host = host
self.cwd = CWD
def run_command(self,command):
cmdline = ('ssh -o StrictHostKeyChecking=no '
+ '-o ConnectTimeout=5 '
+ '-x %s "cd %s; /usr/bin/nice -n 19 %s"' %
(self.host,self.cwd,command))
return self.handle_command(cmdline)
def checkResultLine(line):
"""Checks whether the output line is a valid result.
Result lines consist of at least 16 fields (15 parameter values and one result)
"""
return len(line.split('\t')) > 15 or line.strip() == "Done"
def usage():
print """Usage:
runExperiments.py [command file] [command] [additional (fixed) parameters]
Typical usage:
runExperiments.py params.txt "java -jar experimenter.jar" "--batch"
"""
def add_jobs(jobfile):
params = [p.strip() for p in open(jobfile,"r").readlines()]
cmds = [(CMD + " " + p + " " + ADDITIONAL_PARAMS).strip() for p in params]
for cmd in cmds:
JOB_QUEUE.put(cmd)
def add_ssh(machine,num):
for i in range(num):
name = machine + str(i)
SSH_WORKERS[name] = SSHWorker(machine,JOB_QUEUE,RESULT_QUEUE)
SSH_WORKERS[name].setDaemon(True)
SSH_WORKERS[name].start()
def add_workers(workerList):
for worker in workerList:
SSH_WORKERS[worker] = SSHWorker(worker,JOB_QUEUE,RESULT_QUEUE)
SSH_WORKERS[worker].setDaemon(True)
SSH_WORKERS[worker].start()
def add_local(num):
for i in range(num):
LOCAL_WORKERS["local" + str(i)] = LocalWorker('local' + str(i),JOB_QUEUE,RESULT_QUEUE)
LOCAL_WORKERS["local" + str(i)].setDaemon(True)
LOCAL_WORKERS["local" + str(i)].start()
def stop_workers(workerlist):
for worker in workerlist:
if worker in SSH_WORKERS:
SSH_WORKERS[worker].stop()
if worker in LOCAL_WORKERS:
LOCAL_WORKERS[worker].stop()
def stop():
for worker in SSH_WORKERS:
SSH_WORKERS[worker].stop()
for worker in LOCAL_WORKERS:
LOCAL_WORKERS[worker].stop()
def kill(workerlist):
for worker in workerlist:
if worker in SSH_WORKERS:
cmdline = 'ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 -x %s "%s"' % \
(worker,KILLCMD)
(input,result) = os.popen2(cmdline,'r')
for line in result.readlines():
print line
def killall():
kill(SSH_WORKERS.keys())
def status():
print_workers()
print "Remaining jobs: " + str(JOB_QUEUE.qsize())
def save_rest(filename):
pass
def print_workers():
for worker in LOCAL_WORKERS:
print (worker + "\t" + str(LOCAL_WORKERS[worker].failures) + "\t"
+ LOCAL_WORKERS[worker].current_job + "\t"
+ LOCAL_WORKERS[worker].status)
for worker in SSH_WORKERS:
print (worker + "\t" + str(SSH_WORKERS[worker].failures) + "\t"
+ SSH_WORKERS[worker].current_job + "\t"
+ SSH_WORKERS[worker].status)
## shortcuts
def main():
global PARAM_FILE, CMD, ADDITIONAL_PARAMS
if len(sys.argv) == 1:
usage()
exit()
if len(sys.argv) > 1:
PARAM_FILE = sys.argv[1]
if len(sys.argv) > 2:
CMD = sys.argv[2]
if len(sys.argv) > 3:
ADDITIONAL_PARAMS = ' '.join(sys.argv[3:])
print "FILE: " + PARAM_FILE + "; CMD: " + CMD + "; ADDITIONAL_PARAMS: " + ADDITIONAL_PARAMS
add_jobs(PARAM_FILE)
res = ResultWatcher(RESULT_QUEUE)
res.setDaemon(True)
res.start()
interact("runExperiments Interactive Shell",raw_input,globals())
JOB_QUEUE.join()
print "All jobs completed."
RESULT_QUEUE.join()
if __name__ == "__main__":
main()