blob: 4a041dcc2f0ee4ae6de064017aa7fb77568758ce [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
mbligh570e93e2006-11-26 05:15:56 +000045 def __init__(self, control, jobtag, cont, harness_type=''):
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)
mblighf4c35322006-03-13 01:01:10 +000085
apw059e1b12006-10-12 17:18:26 +000086 self.config = config.config(self)
87
apwd27e55f2006-12-01 11:22:08 +000088 self.harness = harness.select(harness_type, self)
89
mbligha35553b2006-04-23 15:52:25 +000090 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +000091
mblighcaa605c2006-10-02 00:37:35 +000092 try:
apw90154af2006-12-01 11:23:36 +000093 tool = self.config_get('boottool.executable')
94 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +000095 except:
96 pass
97
mbligh72b88fc2006-12-16 18:41:35 +000098 pwd = os.getcwd()
mbligh06743772006-05-18 21:30:19 +000099 os.chdir(self.resultdir + "/sysinfo")
100 system(self.bindir + '/sysinfo.py')
mbligh513d4262007-07-18 17:30:14 +0000101 system('dmesg -c > dmesg')
mbligh06743772006-05-18 21:30:19 +0000102 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +0000103
apw357f50f2006-12-01 11:22:39 +0000104 self.harness.run_start()
105
mblighcaa605c2006-10-02 00:37:35 +0000106
apwde1503a2006-10-10 08:34:21 +0000107 def harness_select(self, which):
108 self.harness = harness.select(which, self)
109
110
apw059e1b12006-10-12 17:18:26 +0000111 def config_set(self, name, value):
112 self.config.set(name, value)
113
114
115 def config_get(self, name):
116 return self.config.get(name)
117
mbligh8baa2ea2006-12-17 23:01:24 +0000118 def setup_dirs(self, results_dir, tmp_dir):
mbligh1e8858e2006-11-24 22:18:35 +0000119 if not tmp_dir:
120 tmp_dir = self.tmpdir + '/build'
121 if not os.path.exists(tmp_dir):
122 os.mkdir(tmp_dir)
123 if not os.path.isdir(tmp_dir):
124 raise "Temp dir (%s) is not a dir - args backwards?" \
125 % self.tmpdir
126
127 # We label the first build "build" and then subsequent ones
128 # as "build.2", "build.3", etc. Whilst this is a little bit
129 # inconsistent, 99.9% of jobs will only have one build
130 # (that's not done as kernbench, sparse, or buildtest),
131 # so it works out much cleaner. One of life's comprimises.
132 if not results_dir:
133 results_dir = os.path.join(self.resultdir, 'build')
134 i = 2
135 while os.path.exists(results_dir):
136 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000137 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000138 if not os.path.exists(results_dir):
139 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000140
mbligh8baa2ea2006-12-17 23:01:24 +0000141 return (results_dir, tmp_dir)
142
143
144 def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \
145 kjob = None ):
146 """Summon a xen object"""
147 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
148 build_dir = 'xen'
149 return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob)
150
151
152 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
153 """Summon a kernel object"""
154 (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir)
155 build_dir = 'linux'
156 return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000157
mblighcaa605c2006-10-02 00:37:35 +0000158
mblighfadca202006-09-23 04:40:01 +0000159 def barrier(self, *args):
160 """Create a barrier object"""
161 return barrier.barrier(*args)
162
mblighcaa605c2006-10-02 00:37:35 +0000163
mbligh4b089662006-06-14 22:34:58 +0000164 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000165 """Set up the dependencies for this test.
166
167 deps is a list of libraries required for this test.
168 """
mbligh4b089662006-06-14 22:34:58 +0000169 for dep in deps:
170 try:
171 os.chdir(self.autodir + '/deps/' + dep)
172 system('./' + dep + '.py')
173 except:
174 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000175 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000176
177
mbligh72b88fc2006-12-16 18:41:35 +0000178 def __runtest(self, url, tag, args, dargs):
179 try:
mblighd016ecc2006-11-25 21:41:07 +0000180 test.runtest(self, url, tag, args, dargs)
mbligh72b88fc2006-12-16 18:41:35 +0000181 except AutotestError:
182 raise
183 except:
184 raise UnhandledError('running test ' + \
185 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000186
mblighcaa605c2006-10-02 00:37:35 +0000187
mblighd016ecc2006-11-25 21:41:07 +0000188 def runtest(self, tag, url, *args):
189 raise "Deprecated call to job.runtest. Use run_test instead"
190
191
192 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000193 """Summon a test object and run it.
194
195 tag
196 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000197 url
198 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000199 """
mbligh12a7df72006-10-06 03:54:33 +0000200
mblighd016ecc2006-11-25 21:41:07 +0000201 if not url:
202 raise "Test name is invalid. Switched arguments?"
mbligh12a7df72006-10-06 03:54:33 +0000203 (group, name) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000204 tag = None
205 if dargs.has_key('tag'):
206 tag = dargs['tag']
207 del dargs['tag']
apw5a7335c2007-03-12 20:32:40 +0000208 if tag:
209 name += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000210 try:
211 try:
mblighd016ecc2006-11-25 21:41:07 +0000212 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000213 except Exception, detail:
214 self.record("FAIL " + name + " " + \
215 detail.__str__() + "\n")
216
217 raise
218 else:
219 self.record("GOOD " + name + \
mblighd9223fc2006-11-26 17:19:54 +0000220 " completed successfully\n")
apwf1a81162006-04-25 10:10:29 +0000221 except TestError:
222 return 0
223 except:
224 raise
225 else:
226 return 1
apw0865f482006-03-30 18:50:19 +0000227
mblighd7fb4a62006-10-01 00:57:53 +0000228
229 def filesystem(self, device, mountpoint = None):
230 if not mountpoint:
231 mountpoint = self.tmpdir
mblighb15f9632006-12-04 07:27:57 +0000232 return filesystem.filesystem(self, device, mountpoint)
mblighd7fb4a62006-10-01 00:57:53 +0000233
mblighcaa605c2006-10-02 00:37:35 +0000234
235 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000236 self.harness.run_reboot()
mblighcaa605c2006-10-02 00:37:35 +0000237 self.bootloader.boot_once(tag)
238 system("reboot")
apw0778a2f2006-10-06 03:11:40 +0000239 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000240
241
apw0865f482006-03-30 18:50:19 +0000242 def noop(self, text):
243 print "job: noop: " + text
244
mblighcaa605c2006-10-02 00:37:35 +0000245
apw0865f482006-03-30 18:50:19 +0000246 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000247
apw8fef4ac2006-10-10 22:53:37 +0000248 def __parallel_execute(self, func, *args):
249 func(*args)
250
251
mblighc86b0b42006-07-28 17:35:28 +0000252 def parallel(self, *tasklist):
253 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000254
255 pids = []
256 for task in tasklist:
257 pids.append(fork_start(self.resultdir,
258 lambda: self.__parallel_execute(*task)))
259 for pid in pids:
260 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000261
mblighcaa605c2006-10-02 00:37:35 +0000262
apw0865f482006-03-30 18:50:19 +0000263 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000264 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000265 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000266 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000267
mblighcaa605c2006-10-02 00:37:35 +0000268
apw0865f482006-03-30 18:50:19 +0000269 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000270 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000271 # We are about to exit 'complete' so clean up the control file.
272 try:
apwecf41b72006-03-31 14:00:55 +0000273 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000274 except:
275 pass
mbligh61a6c1a2006-12-25 01:26:38 +0000276 self.harness.run_complete()
apw1b021902006-04-03 17:02:56 +0000277 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000278
mblighcaa605c2006-10-02 00:37:35 +0000279
apw0865f482006-03-30 18:50:19 +0000280 steps = []
281 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000282 """Define the next step"""
apw83f8d772006-04-27 14:12:56 +0000283 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000284 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000285 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000286
mblighcaa605c2006-10-02 00:37:35 +0000287
apw83f8d772006-04-27 14:12:56 +0000288 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000289 """the stepping engine -- if the control file defines
290 step_init we will be using this engine to drive multiple runs.
291 """
292 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000293 lcl = dict({'job': self})
294
295 str = """
296from error import *
297from autotest_utils import *
298"""
299 exec(str, lcl, lcl)
300 execfile(self.control, lcl, lcl)
301
mblighd9223fc2006-11-26 17:19:54 +0000302 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000303 # If there is a mid-job state file load that in and continue
304 # where it indicates. Otherwise start stepping at the passed
305 # entry.
306 try:
mblighd9223fc2006-11-26 17:19:54 +0000307 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000308 except:
apw83f8d772006-04-27 14:12:56 +0000309 if lcl.has_key('step_init'):
310 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000311
312 # Run the step list.
313 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000314 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000315 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000316
317 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000318 cmd = lcl[cmd]
319 lcl['__cmd'] = cmd
320 lcl['__args'] = step
321 exec("__cmd(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000322
mblighcaa605c2006-10-02 00:37:35 +0000323
apwf1a81162006-04-25 10:10:29 +0000324 def record(self, msg):
mblighc86b0b42006-07-28 17:35:28 +0000325 """Record job-level status"""
apw7db8d0b2006-10-09 08:10:25 +0000326
mblighd9223fc2006-11-26 17:19:54 +0000327 msg = msg.rstrip()
apw7db8d0b2006-10-09 08:10:25 +0000328 # Ensure any continuation lines are marked so we can
329 # detect them in the status file to ensure it is parsable.
mblighd9223fc2006-11-26 17:19:54 +0000330 msg = re.sub(r"\n", "\n ", msg)
apw7db8d0b2006-10-09 08:10:25 +0000331
apwde1503a2006-10-10 08:34:21 +0000332 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000333 print msg
334 status = self.resultdir + "/status"
mblighd9223fc2006-11-26 17:19:54 +0000335 file(status, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000336
337
mbligh570e93e2006-11-26 05:15:56 +0000338def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000339 """The main interface to this module
340
mbligh72b88fc2006-12-16 18:41:35 +0000341 control
mblighc86b0b42006-07-28 17:35:28 +0000342 The control file to use for this job.
343 cont
344 Whether this is the continuation of a previously started job
345 """
mblighb4eef242007-07-23 18:22:49 +0000346 control = os.path.abspath(control)
apwce9abe92006-04-27 14:14:04 +0000347 state = control + '.state'
348
349 # instantiate the job object ready for the control file.
350 myjob = None
351 try:
352 # Check that the control file is valid
353 if not os.path.exists(control):
354 raise JobError(control + ": control file not found")
355
356 # When continuing, the job is complete when there is no
357 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000358 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000359 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000360 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000361 os.unlink(state)
362
mbligh570e93e2006-11-26 05:15:56 +0000363 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000364
365 # Load in the users control file, may do any one of:
366 # 1) execute in toto
367 # 2) define steps, and select the first via next_step()
368 myjob.step_engine()
369
apwce9abe92006-04-27 14:14:04 +0000370 except JobContinue:
371 sys.exit(5)
372
373 except JobError, instance:
374 print "JOB ERROR: " + instance.args[0]
375 if myjob != None:
apw510398f2007-03-06 19:19:05 +0000376 myjob.record("ABORT " + instance.args[0] + "\n")
apwce9abe92006-04-27 14:14:04 +0000377 myjob.complete(1)
378
379 except:
mblighfbfb77d2007-02-15 18:54:03 +0000380 if myjob:
381 myjob.harness.run_abort()
apwce9abe92006-04-27 14:14:04 +0000382 # Ensure we cannot continue this job, it is in rictus.
383 if os.path.exists(state):
384 os.unlink(state)
385 raise
mbligh892d37f2007-03-01 17:03:25 +0000386
387 # If we get here, then we assume the job is complete and good.
388 myjob.complete(0)
389