blob: 56843543cd22e54a8270c0001a79a168adb3e632 [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 *
mbligh8f243ec2006-10-10 05:55:49 +000013import kernel, 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
68 if os.path.exists(self.resultdir):
69 system('rm -rf ' + self.resultdir)
70 os.mkdir(self.resultdir)
71
72 os.mkdir(self.resultdir + "/debug")
73 os.mkdir(self.resultdir + "/analysis")
74 os.mkdir(self.resultdir + "/sysinfo")
mblighd9223fc2006-11-26 17:19:54 +000075 shutil.copyfile(control, self.resultdir + "/control")
mbligh4b089662006-06-14 22:34:58 +000076
apwecf41b72006-03-31 14:00:55 +000077 self.control = control
mblighf4c35322006-03-13 01:01:10 +000078 self.jobtab = jobtag
79
mbligh56f1fbb2006-10-01 15:10:56 +000080 self.stdout = fd_stack.fd_stack(1, sys.stdout)
81 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mblighf4c35322006-03-13 01:01:10 +000082
apw059e1b12006-10-12 17:18:26 +000083 self.config = config.config(self)
84
apwd27e55f2006-12-01 11:22:08 +000085 self.harness = harness.select(harness_type, self)
86
mbligha35553b2006-04-23 15:52:25 +000087 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +000088
mblighcaa605c2006-10-02 00:37:35 +000089 try:
apw90154af2006-12-01 11:23:36 +000090 tool = self.config_get('boottool.executable')
91 self.bootloader = boottool.boottool(tool)
mblighcaa605c2006-10-02 00:37:35 +000092 except:
93 pass
94
mbligh72b88fc2006-12-16 18:41:35 +000095 pwd = os.getcwd()
mbligh06743772006-05-18 21:30:19 +000096 os.chdir(self.resultdir + "/sysinfo")
97 system(self.bindir + '/sysinfo.py')
98 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +000099
apw357f50f2006-12-01 11:22:39 +0000100 self.harness.run_start()
101
mblighcaa605c2006-10-02 00:37:35 +0000102
apwde1503a2006-10-10 08:34:21 +0000103 def harness_select(self, which):
104 self.harness = harness.select(which, self)
105
106
apw059e1b12006-10-12 17:18:26 +0000107 def config_set(self, name, value):
108 self.config.set(name, value)
109
110
111 def config_get(self, name):
112 return self.config.get(name)
113
114
mbligh1e8858e2006-11-24 22:18:35 +0000115 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
mblighc86b0b42006-07-28 17:35:28 +0000116 """Summon a kernel object"""
mbligh1e8858e2006-11-24 22:18:35 +0000117 if not tmp_dir:
118 tmp_dir = self.tmpdir + '/build'
119 if not os.path.exists(tmp_dir):
120 os.mkdir(tmp_dir)
121 if not os.path.isdir(tmp_dir):
122 raise "Temp dir (%s) is not a dir - args backwards?" \
123 % self.tmpdir
124
125 # We label the first build "build" and then subsequent ones
126 # as "build.2", "build.3", etc. Whilst this is a little bit
127 # inconsistent, 99.9% of jobs will only have one build
128 # (that's not done as kernbench, sparse, or buildtest),
129 # so it works out much cleaner. One of life's comprimises.
130 if not results_dir:
131 results_dir = os.path.join(self.resultdir, 'build')
132 i = 2
133 while os.path.exists(results_dir):
134 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000135 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000136 if not os.path.exists(results_dir):
137 os.mkdir(results_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000138
mbligh1e8858e2006-11-24 22:18:35 +0000139 return kernel.kernel(self, base_tree, results_dir, tmp_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000140
mblighcaa605c2006-10-02 00:37:35 +0000141
mblighfadca202006-09-23 04:40:01 +0000142 def barrier(self, *args):
143 """Create a barrier object"""
144 return barrier.barrier(*args)
145
mblighcaa605c2006-10-02 00:37:35 +0000146
mbligh4b089662006-06-14 22:34:58 +0000147 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000148 """Set up the dependencies for this test.
149
150 deps is a list of libraries required for this test.
151 """
mbligh4b089662006-06-14 22:34:58 +0000152 for dep in deps:
153 try:
154 os.chdir(self.autodir + '/deps/' + dep)
155 system('./' + dep + '.py')
156 except:
157 error = "setting up dependency " + dep + "\n"
mbligh72b88fc2006-12-16 18:41:35 +0000158 raise UnhandledError(error)
mbligh4b089662006-06-14 22:34:58 +0000159
160
mbligh72b88fc2006-12-16 18:41:35 +0000161 def __runtest(self, url, tag, args, dargs):
162 try:
mblighd016ecc2006-11-25 21:41:07 +0000163 test.runtest(self, url, tag, args, dargs)
mbligh72b88fc2006-12-16 18:41:35 +0000164 except AutotestError:
165 raise
166 except:
167 raise UnhandledError('running test ' + \
168 self.__class__.__name__ + "\n")
apwf1a81162006-04-25 10:10:29 +0000169
mblighcaa605c2006-10-02 00:37:35 +0000170
mblighd016ecc2006-11-25 21:41:07 +0000171 def runtest(self, tag, url, *args):
172 raise "Deprecated call to job.runtest. Use run_test instead"
173
174
175 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000176 """Summon a test object and run it.
177
178 tag
179 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000180 url
181 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000182 """
mbligh12a7df72006-10-06 03:54:33 +0000183
mblighd016ecc2006-11-25 21:41:07 +0000184 if not url:
185 raise "Test name is invalid. Switched arguments?"
mbligh12a7df72006-10-06 03:54:33 +0000186 (group, name) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000187 tag = None
188 if dargs.has_key('tag'):
189 tag = dargs['tag']
190 del dargs['tag']
mbligh7880b1b2006-05-07 16:57:50 +0000191 name += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000192 try:
193 try:
mblighd016ecc2006-11-25 21:41:07 +0000194 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000195 except Exception, detail:
196 self.record("FAIL " + name + " " + \
197 detail.__str__() + "\n")
198
199 raise
200 else:
201 self.record("GOOD " + name + \
mblighd9223fc2006-11-26 17:19:54 +0000202 " completed successfully\n")
apwf1a81162006-04-25 10:10:29 +0000203 except TestError:
204 return 0
205 except:
206 raise
207 else:
208 return 1
apw0865f482006-03-30 18:50:19 +0000209
mblighd7fb4a62006-10-01 00:57:53 +0000210
211 def filesystem(self, device, mountpoint = None):
212 if not mountpoint:
213 mountpoint = self.tmpdir
mblighb15f9632006-12-04 07:27:57 +0000214 return filesystem.filesystem(self, device, mountpoint)
mblighd7fb4a62006-10-01 00:57:53 +0000215
mblighcaa605c2006-10-02 00:37:35 +0000216
217 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000218 self.harness.run_reboot()
mblighcaa605c2006-10-02 00:37:35 +0000219 self.bootloader.boot_once(tag)
220 system("reboot")
apw0778a2f2006-10-06 03:11:40 +0000221 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000222
223
apw0865f482006-03-30 18:50:19 +0000224 def noop(self, text):
225 print "job: noop: " + text
226
mblighcaa605c2006-10-02 00:37:35 +0000227
apw0865f482006-03-30 18:50:19 +0000228 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000229
apw8fef4ac2006-10-10 22:53:37 +0000230 def __parallel_execute(self, func, *args):
231 func(*args)
232
233
mblighc86b0b42006-07-28 17:35:28 +0000234 def parallel(self, *tasklist):
235 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000236
237 pids = []
238 for task in tasklist:
239 pids.append(fork_start(self.resultdir,
240 lambda: self.__parallel_execute(*task)))
241 for pid in pids:
242 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000243
mblighcaa605c2006-10-02 00:37:35 +0000244
apw0865f482006-03-30 18:50:19 +0000245 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000246 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000247 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000248 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000249
mblighcaa605c2006-10-02 00:37:35 +0000250
apw0865f482006-03-30 18:50:19 +0000251 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000252 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000253 # We are about to exit 'complete' so clean up the control file.
254 try:
apwecf41b72006-03-31 14:00:55 +0000255 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000256 except:
257 pass
apwde1503a2006-10-10 08:34:21 +0000258 self.harness.run_complete(status)
apw1b021902006-04-03 17:02:56 +0000259 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000260
mblighcaa605c2006-10-02 00:37:35 +0000261
apw0865f482006-03-30 18:50:19 +0000262 steps = []
263 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000264 """Define the next step"""
apw83f8d772006-04-27 14:12:56 +0000265 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000266 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000267 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000268
mblighcaa605c2006-10-02 00:37:35 +0000269
apw83f8d772006-04-27 14:12:56 +0000270 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000271 """the stepping engine -- if the control file defines
272 step_init we will be using this engine to drive multiple runs.
273 """
274 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000275 lcl = dict({'job': self})
276
277 str = """
278from error import *
279from autotest_utils import *
280"""
281 exec(str, lcl, lcl)
282 execfile(self.control, lcl, lcl)
283
mblighd9223fc2006-11-26 17:19:54 +0000284 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000285 # If there is a mid-job state file load that in and continue
286 # where it indicates. Otherwise start stepping at the passed
287 # entry.
288 try:
mblighd9223fc2006-11-26 17:19:54 +0000289 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000290 except:
apw83f8d772006-04-27 14:12:56 +0000291 if lcl.has_key('step_init'):
292 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000293
294 # Run the step list.
295 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000296 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000297 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000298
299 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000300 cmd = lcl[cmd]
301 lcl['__cmd'] = cmd
302 lcl['__args'] = step
303 exec("__cmd(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000304
305 # all done, clean up and exit.
306 self.complete(0)
307
mblighcaa605c2006-10-02 00:37:35 +0000308
apwf1a81162006-04-25 10:10:29 +0000309 def record(self, msg):
mblighc86b0b42006-07-28 17:35:28 +0000310 """Record job-level status"""
apw7db8d0b2006-10-09 08:10:25 +0000311
mblighd9223fc2006-11-26 17:19:54 +0000312 msg = msg.rstrip()
apw7db8d0b2006-10-09 08:10:25 +0000313 # Ensure any continuation lines are marked so we can
314 # detect them in the status file to ensure it is parsable.
mblighd9223fc2006-11-26 17:19:54 +0000315 msg = re.sub(r"\n", "\n ", msg)
apw7db8d0b2006-10-09 08:10:25 +0000316
apwde1503a2006-10-10 08:34:21 +0000317 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000318 print msg
319 status = self.resultdir + "/status"
mblighd9223fc2006-11-26 17:19:54 +0000320 file(status, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000321
322
mbligh570e93e2006-11-26 05:15:56 +0000323def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000324 """The main interface to this module
325
mbligh72b88fc2006-12-16 18:41:35 +0000326 control
mblighc86b0b42006-07-28 17:35:28 +0000327 The control file to use for this job.
328 cont
329 Whether this is the continuation of a previously started job
330 """
apwce9abe92006-04-27 14:14:04 +0000331 state = control + '.state'
332
333 # instantiate the job object ready for the control file.
334 myjob = None
335 try:
336 # Check that the control file is valid
337 if not os.path.exists(control):
338 raise JobError(control + ": control file not found")
339
340 # When continuing, the job is complete when there is no
341 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000342 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000343 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000344 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000345 os.unlink(state)
346
mbligh570e93e2006-11-26 05:15:56 +0000347 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000348
349 # Load in the users control file, may do any one of:
350 # 1) execute in toto
351 # 2) define steps, and select the first via next_step()
352 myjob.step_engine()
353
354 # If we get here, then we assume the job is complete and good.
355 myjob.complete(0)
356
357 except JobContinue:
358 sys.exit(5)
359
360 except JobError, instance:
361 print "JOB ERROR: " + instance.args[0]
362 if myjob != None:
363 myjob.complete(1)
364
365 except:
366 # Ensure we cannot continue this job, it is in rictus.
367 if os.path.exists(state):
368 os.unlink(state)
369 raise