blob: 356a66268f54311b15ae74d26de21b816e9d10a0 [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001"""The main job wrapper
mbligha2508052006-05-28 21:29:53 +00002
mblighc86b0b42006-07-28 17:35:28 +00003This is the core infrastructure.
4"""
5
6__author__ = """Copyright Andy Whitcroft, Martin J. Bligh 2006"""
mbligha2508052006-05-28 21:29:53 +00007
mbligh8f243ec2006-10-10 05:55:49 +00008# standard stuff
apw8fef4ac2006-10-10 22:53:37 +00009import os, sys, re, pickle, shutil
mbligh8f243ec2006-10-10 05:55:49 +000010# autotest stuff
mblighf4c35322006-03-13 01:01:10 +000011from autotest_utils import *
apw8fef4ac2006-10-10 22:53:37 +000012from parallel import *
mbligh8baa2ea2006-12-17 23:01:24 +000013import kernel, xen, test, profilers, barrier, filesystem, fd_stack, boottool
apw059e1b12006-10-12 17:18:26 +000014import harness, config
mblighf4c35322006-03-13 01:01:10 +000015
16class job:
mblighc86b0b42006-07-28 17:35:28 +000017 """The actual job against which we do everything.
18
19 Properties:
mbligh72b88fc2006-12-16 18:41:35 +000020 autodir
mblighc86b0b42006-07-28 17:35:28 +000021 The top level autotest directory (/usr/local/autotest).
22 Comes from os.environ['AUTODIR'].
mbligh72b88fc2006-12-16 18:41:35 +000023 bindir
mblighc86b0b42006-07-28 17:35:28 +000024 <autodir>/bin/
mbligh72b88fc2006-12-16 18:41:35 +000025 testdir
mblighc86b0b42006-07-28 17:35:28 +000026 <autodir>/tests/
27 profdir
28 <autodir>/profilers/
29 tmpdir
30 <autodir>/tmp/
31 resultdir
32 <autodir>/results/<jobtag>
33 stdout
34 fd_stack object for stdout
35 stderr
36 fd_stack object for stderr
37 profilers
38 the profilers object for this job
apw504a7dd2006-10-12 17:18:37 +000039 harness
40 the server harness object for this job
apw059e1b12006-10-12 17:18:26 +000041 config
42 the job configuration for this job
mblighc86b0b42006-07-28 17:35:28 +000043 """
44
mbligh362ab3d2007-08-30 11:24:04 +000045 def __init__(self, control, jobtag, cont, harness_type=None):
mblighc86b0b42006-07-28 17:35:28 +000046 """
47 control
48 The control file (pathname of)
49 jobtag
50 The job tag string (eg "default")
apw96da1a42006-11-02 00:23:18 +000051 cont
52 If this is the continuation of this job
apwe68a7132006-12-01 11:21:37 +000053 harness_type
54 An alternative server harness
mblighc86b0b42006-07-28 17:35:28 +000055 """
mblighf4c35322006-03-13 01:01:10 +000056 self.autodir = os.environ['AUTODIR']
mbligh06743772006-05-18 21:30:19 +000057 self.bindir = self.autodir + '/bin'
mbligh82641862006-04-23 06:21:36 +000058 self.testdir = self.autodir + '/tests'
mbligha2508052006-05-28 21:29:53 +000059 self.profdir = self.autodir + '/profilers'
mblighf4c35322006-03-13 01:01:10 +000060 self.tmpdir = self.autodir + '/tmp'
mbligh24f7da02006-04-23 06:32:18 +000061 self.resultdir = self.autodir + '/results/' + jobtag
mbligha2508052006-05-28 21:29:53 +000062
apw96da1a42006-11-02 00:23:18 +000063 if not cont:
64 if os.path.exists(self.tmpdir):
65 system('rm -rf ' + self.tmpdir)
66 os.mkdir(self.tmpdir)
67
mblighfbfb77d2007-02-15 18:54:03 +000068 if not os.path.exists(self.autodir + '/results'):
69 os.mkdir(self.autodir + '/results')
70
apw96da1a42006-11-02 00:23:18 +000071 if os.path.exists(self.resultdir):
72 system('rm -rf ' + self.resultdir)
73 os.mkdir(self.resultdir)
74
75 os.mkdir(self.resultdir + "/debug")
76 os.mkdir(self.resultdir + "/analysis")
77 os.mkdir(self.resultdir + "/sysinfo")
mblighd9223fc2006-11-26 17:19:54 +000078 shutil.copyfile(control, self.resultdir + "/control")
mbligh4b089662006-06-14 22:34:58 +000079
apwecf41b72006-03-31 14:00:55 +000080 self.control = control
mblighf4c35322006-03-13 01:01:10 +000081 self.jobtab = jobtag
82
mbligh56f1fbb2006-10-01 15:10:56 +000083 self.stdout = fd_stack.fd_stack(1, sys.stdout)
84 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mbligh88ab90f2007-08-29 15:52:49 +000085 self.record_prefix = ''
mblighf4c35322006-03-13 01:01:10 +000086
apw059e1b12006-10-12 17:18:26 +000087 self.config = config.config(self)
88
apwd27e55f2006-12-01 11:22:08 +000089 self.harness = harness.select(harness_type, self)
90
mbligha35553b2006-04-23 15:52:25 +000091 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +000092
mblighcaa605c2006-10-02 00:37:35 +000093 try:
apw90154af2006-12-01 11:23:36 +000094 tool = self.config_get('boottool.executable')
95 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +000096 except:
97 pass
98
mbligh72b88fc2006-12-16 18:41:35 +000099 pwd = os.getcwd()
mbligh06743772006-05-18 21:30:19 +0000100 os.chdir(self.resultdir + "/sysinfo")
101 system(self.bindir + '/sysinfo.py')
mblighe21abaf2007-07-26 16:17:30 +0000102 system('dmesg -c > dmesg', ignorestatus=1)
mbligh06743772006-05-18 21:30:19 +0000103 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +0000104
apw357f50f2006-12-01 11:22:39 +0000105 self.harness.run_start()
106
mbligh362ab3d2007-08-30 11:24:04 +0000107 def control_get(self):
108 return self.control
109
mblighcaa605c2006-10-02 00:37:35 +0000110
apwde1503a2006-10-10 08:34:21 +0000111 def harness_select(self, which):
112 self.harness = harness.select(which, self)
113
114
apw059e1b12006-10-12 17:18:26 +0000115 def config_set(self, name, value):
116 self.config.set(name, value)
117
118
119 def config_get(self, name):
120 return self.config.get(name)
121
mbligh8baa2ea2006-12-17 23:01:24 +0000122 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000123 if not tmp_dir:
124 tmp_dir = self.tmpdir + '/build'
125 if not os.path.exists(tmp_dir):
126 os.mkdir(tmp_dir)
127 if not os.path.isdir(tmp_dir):
128 raise "Temp dir (%s) is not a dir - args backwards?" \
129 % self.tmpdir
130
131 # We label the first build "build" and then subsequent ones
132 # as "build.2", "build.3", etc. Whilst this is a little bit
133 # inconsistent, 99.9% of jobs will only have one build
134 # (that's not done as kernbench, sparse, or buildtest),
135 # so it works out much cleaner. One of life's comprimises.
136 if not results_dir:
137 results_dir = os.path.join(self.resultdir, 'build')
138 i = 2
139 while os.path.exists(results_dir):
140 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000141 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000142 if not os.path.exists(results_dir):
143 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000144
mbligh8baa2ea2006-12-17 23:01:24 +0000145 return (results_dir, tmp_dir)
146
147
148 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
149 kjob = None ):
150 """Summon a xen object"""
151 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
152 build_dir = 'xen'
153 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
154
155
156 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
157 """Summon a kernel object"""
158 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
159 build_dir = 'linux'
160 return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000161
mblighcaa605c2006-10-02 00:37:35 +0000162
mblighfadca202006-09-23 04:40:01 +0000163 def barrier(self, *args):
164 """Create a barrier object"""
165 return barrier.barrier(*args)
166
mblighcaa605c2006-10-02 00:37:35 +0000167
mbligh4b089662006-06-14 22:34:58 +0000168 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000169 """Set up the dependencies for this test.
170
171 deps is a list of libraries required for this test.
172 """
mbligh4b089662006-06-14 22:34:58 +0000173 for dep in deps:
174 try:
175 os.chdir(self.autodir + '/deps/' + dep)
176 system('./' + dep + '.py')
177 except:
178 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000179 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000180
181
mbligh72b88fc2006-12-16 18:41:35 +0000182 def __runtest(self, url, tag, args, dargs):
183 try:
mblighd016ecc2006-11-25 21:41:07 +0000184 test.runtest(self, url, tag, args, dargs)
mbligh72b88fc2006-12-16 18:41:35 +0000185 except AutotestError:
186 raise
187 except:
188 raise UnhandledError('running test ' + \
189 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000190
mblighcaa605c2006-10-02 00:37:35 +0000191
mblighd016ecc2006-11-25 21:41:07 +0000192 def runtest(self, tag, url, *args):
193 raise "Deprecated call to job.runtest. Use run_test instead"
194
195
196 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000197 """Summon a test object and run it.
198
199 tag
200 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000201 url
202 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000203 """
mbligh12a7df72006-10-06 03:54:33 +0000204
mblighd016ecc2006-11-25 21:41:07 +0000205 if not url:
206 raise "Test name is invalid. Switched arguments?"
mbligh12a7df72006-10-06 03:54:33 +0000207 (group, name) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000208 tag = None
209 if dargs.has_key('tag'):
210 tag = dargs['tag']
211 del dargs['tag']
apw5a7335c2007-03-12 20:32:40 +0000212 if tag:
213 name += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000214 try:
215 try:
mblighd016ecc2006-11-25 21:41:07 +0000216 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000217 except Exception, detail:
218 self.record("FAIL " + name + " " + \
219 detail.__str__() + "\n")
220
221 raise
222 else:
223 self.record("GOOD " + name + \
mblighd9223fc2006-11-26 17:19:54 +0000224 " completed successfully\n")
apwf1a81162006-04-25 10:10:29 +0000225 except TestError:
226 return 0
227 except:
228 raise
229 else:
230 return 1
apw0865f482006-03-30 18:50:19 +0000231
mblighd7fb4a62006-10-01 00:57:53 +0000232
mbligh88ab90f2007-08-29 15:52:49 +0000233 def run_group(self, function, *args):
234 """\
235 function:
236 subroutine to run
237 *args:
238 arguments for the function
239 """
240
241 name = function.__name__
242 # if tag:
243 # name += '.' + tag
244 old_record_prefix = self.record_prefix
245 try:
246 try:
247 self.record("START " + name)
248 self.record_prefix += '\t'
249 function(*args)
250 self.record_prefix = old_record_prefix
251 self.record("END %s GOOD" % name)
252 except:
253 self.record_prefix = old_record_prefix
254 self.record("END %s FAIL" % name)
255 # We don't want to raise up an error higher if it's just
256 # a TestError - we want to carry on to other tests. Hence
257 # this outer try/except block.
258 except TestError:
259 pass
260 except:
261 raise TestError(name + ' failed\n' + format_error())
262
263
mblighc2359852007-08-28 18:11:48 +0000264 def filesystem(self, device, mountpoint = None, loop_size = 0):
mblighd7fb4a62006-10-01 00:57:53 +0000265 if not mountpoint:
266 mountpoint = self.tmpdir
mblighc2359852007-08-28 18:11:48 +0000267 return filesystem.filesystem(self, device, mountpoint,loop_size)
mblighd7fb4a62006-10-01 00:57:53 +0000268
mblighcaa605c2006-10-02 00:37:35 +0000269
270 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000271 self.harness.run_reboot()
mblighcaa605c2006-10-02 00:37:35 +0000272 self.bootloader.boot_once(tag)
273 system("reboot")
apw0778a2f2006-10-06 03:11:40 +0000274 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000275
276
apw0865f482006-03-30 18:50:19 +0000277 def noop(self, text):
278 print "job: noop: " + text
279
mblighcaa605c2006-10-02 00:37:35 +0000280
apw0865f482006-03-30 18:50:19 +0000281 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000282
apw8fef4ac2006-10-10 22:53:37 +0000283 def __parallel_execute(self, func, *args):
284 func(*args)
285
286
mblighc86b0b42006-07-28 17:35:28 +0000287 def parallel(self, *tasklist):
288 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000289
290 pids = []
291 for task in tasklist:
292 pids.append(fork_start(self.resultdir,
293 lambda: self.__parallel_execute(*task)))
294 for pid in pids:
295 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000296
mblighcaa605c2006-10-02 00:37:35 +0000297
apw0865f482006-03-30 18:50:19 +0000298 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000299 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000300 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000301 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000302
mblighcaa605c2006-10-02 00:37:35 +0000303
apw0865f482006-03-30 18:50:19 +0000304 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000305 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000306 # We are about to exit 'complete' so clean up the control file.
307 try:
apwecf41b72006-03-31 14:00:55 +0000308 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000309 except:
310 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000311 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000312 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000313
mblighcaa605c2006-10-02 00:37:35 +0000314
apw0865f482006-03-30 18:50:19 +0000315 steps = []
316 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000317 """Define the next step"""
apw83f8d772006-04-27 14:12:56 +0000318 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000319 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000320 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000321
mblighcaa605c2006-10-02 00:37:35 +0000322
apw83f8d772006-04-27 14:12:56 +0000323 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000324 """the stepping engine -- if the control file defines
325 step_init we will be using this engine to drive multiple runs.
326 """
327 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000328 lcl = dict({'job': self})
329
330 str = """
331from error import *
332from autotest_utils import *
333"""
334 exec(str, lcl, lcl)
335 execfile(self.control, lcl, lcl)
336
mblighd9223fc2006-11-26 17:19:54 +0000337 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000338 # If there is a mid-job state file load that in and continue
339 # where it indicates. Otherwise start stepping at the passed
340 # entry.
341 try:
mblighd9223fc2006-11-26 17:19:54 +0000342 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000343 except:
apw83f8d772006-04-27 14:12:56 +0000344 if lcl.has_key('step_init'):
345 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000346
347 # Run the step list.
348 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000349 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000350 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000351
352 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000353 cmd = lcl[cmd]
354 lcl['__cmd'] = cmd
355 lcl['__args'] = step
356 exec("__cmd(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000357
mblighcaa605c2006-10-02 00:37:35 +0000358
apwf1a81162006-04-25 10:10:29 +0000359 def record(self, msg):
mblighc86b0b42006-07-28 17:35:28 +0000360 """Record job-level status"""
apw7db8d0b2006-10-09 08:10:25 +0000361
mblighd9223fc2006-11-26 17:19:54 +0000362 msg = msg.rstrip()
apw7db8d0b2006-10-09 08:10:25 +0000363 # Ensure any continuation lines are marked so we can
364 # detect them in the status file to ensure it is parsable.
mbligh88ab90f2007-08-29 15:52:49 +0000365 msg = re.sub(r"\n", "\n" + self.record_prefix + " ", msg)
366 msg = self.record_prefix + msg
apw7db8d0b2006-10-09 08:10:25 +0000367
apwde1503a2006-10-10 08:34:21 +0000368 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000369 print msg
370 status = self.resultdir + "/status"
mblighd9223fc2006-11-26 17:19:54 +0000371 file(status, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000372
373
mbligh570e93e2006-11-26 05:15:56 +0000374def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000375 """The main interface to this module
376
mbligh72b88fc2006-12-16 18:41:35 +0000377 control
mblighc86b0b42006-07-28 17:35:28 +0000378 The control file to use for this job.
379 cont
380 Whether this is the continuation of a previously started job
381 """
mblighb4eef242007-07-23 18:22:49 +0000382 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000383 state = control + '.state'
384
385 # instantiate the job object ready for the control file.
386 myjob = None
387 try:
388 # Check that the control file is valid
389 if not os.path.exists(control):
390 raise JobError(control + ": control file not found")
391
392 # When continuing, the job is complete when there is no
393 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000394 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000395 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000396 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000397 os.unlink(state)
398
mbligh570e93e2006-11-26 05:15:56 +0000399 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000400
401 # Load in the users control file, may do any one of:
402 # 1) execute in toto
403 # 2) define steps, and select the first via next_step()
404 myjob.step_engine()
405
apwce9abe92006-04-27 14:14:04 +0000406 except JobContinue:
407 sys.exit(5)
408
409 except JobError, instance:
410 print "JOB ERROR: " + instance.args[0]
411 if myjob != None:
apw510398f2007-03-06 19:19:05 +0000412 myjob.record("ABORT " + instance.args[0] + "\n")
apwce9abe92006-04-27 14:14:04 +0000413 myjob.complete(1)
414
415 except:
mblighfbfb77d2007-02-15 18:54:03 +0000416 if myjob:
417 myjob.harness.run_abort()
apwce9abe92006-04-27 14:14:04 +0000418 # Ensure we cannot continue this job, it is in rictus.
419 if os.path.exists(state):
420 os.unlink(state)
421 raise
mbligh892d37f2007-03-01 17:03:25 +0000422
423 # If we get here, then we assume the job is complete and good.
424 myjob.complete(0)
425