blob: 54a2b6dba601c8c990aec2173c6d2a28f905f0ef [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:
20 autodir
21 The top level autotest directory (/usr/local/autotest).
22 Comes from os.environ['AUTODIR'].
23 bindir
24 <autodir>/bin/
25 testdir
26 <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
mblighc86b0b42006-07-28 17:35:28 +000053 """
mblighf4c35322006-03-13 01:01:10 +000054 self.autodir = os.environ['AUTODIR']
mbligh06743772006-05-18 21:30:19 +000055 self.bindir = self.autodir + '/bin'
mbligh82641862006-04-23 06:21:36 +000056 self.testdir = self.autodir + '/tests'
mbligha2508052006-05-28 21:29:53 +000057 self.profdir = self.autodir + '/profilers'
mblighf4c35322006-03-13 01:01:10 +000058 self.tmpdir = self.autodir + '/tmp'
mbligh24f7da02006-04-23 06:32:18 +000059 self.resultdir = self.autodir + '/results/' + jobtag
mbligha2508052006-05-28 21:29:53 +000060
apw96da1a42006-11-02 00:23:18 +000061 if not cont:
62 if os.path.exists(self.tmpdir):
63 system('rm -rf ' + self.tmpdir)
64 os.mkdir(self.tmpdir)
65
66 if os.path.exists(self.resultdir):
67 system('rm -rf ' + self.resultdir)
68 os.mkdir(self.resultdir)
69
70 os.mkdir(self.resultdir + "/debug")
71 os.mkdir(self.resultdir + "/analysis")
72 os.mkdir(self.resultdir + "/sysinfo")
mblighd9223fc2006-11-26 17:19:54 +000073 shutil.copyfile(control, self.resultdir + "/control")
mbligh4b089662006-06-14 22:34:58 +000074
apwecf41b72006-03-31 14:00:55 +000075 self.control = control
mblighf4c35322006-03-13 01:01:10 +000076 self.jobtab = jobtag
77
mbligh56f1fbb2006-10-01 15:10:56 +000078 self.stdout = fd_stack.fd_stack(1, sys.stdout)
79 self.stderr = fd_stack.fd_stack(2, sys.stderr)
mblighf4c35322006-03-13 01:01:10 +000080
mbligh570e93e2006-11-26 05:15:56 +000081 self.harness = harness.select(harness_type, self)
apwde1503a2006-10-10 08:34:21 +000082
apw059e1b12006-10-12 17:18:26 +000083 self.config = config.config(self)
84
mbligha35553b2006-04-23 15:52:25 +000085 self.profilers = profilers.profilers(self)
mbligh72905562006-05-25 01:30:49 +000086
mblighcaa605c2006-10-02 00:37:35 +000087 try:
88 self.bootloader = boottool.boottool()
89 except:
90 pass
91
mbligh06743772006-05-18 21:30:19 +000092 pwd = os.getcwd()
93 os.chdir(self.resultdir + "/sysinfo")
94 system(self.bindir + '/sysinfo.py')
95 os.chdir(pwd)
mbligh3a6d6ca2006-04-23 15:50:24 +000096
mblighcaa605c2006-10-02 00:37:35 +000097
apwde1503a2006-10-10 08:34:21 +000098 def harness_select(self, which):
99 self.harness = harness.select(which, self)
100
101
apw059e1b12006-10-12 17:18:26 +0000102 def config_set(self, name, value):
103 self.config.set(name, value)
104
105
106 def config_get(self, name):
107 return self.config.get(name)
108
109
mbligh1e8858e2006-11-24 22:18:35 +0000110 def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False):
mblighc86b0b42006-07-28 17:35:28 +0000111 """Summon a kernel object"""
mbligh1e8858e2006-11-24 22:18:35 +0000112 if not tmp_dir:
113 tmp_dir = self.tmpdir + '/build'
114 if not os.path.exists(tmp_dir):
115 os.mkdir(tmp_dir)
116 if not os.path.isdir(tmp_dir):
117 raise "Temp dir (%s) is not a dir - args backwards?" \
118 % self.tmpdir
119
120 # We label the first build "build" and then subsequent ones
121 # as "build.2", "build.3", etc. Whilst this is a little bit
122 # inconsistent, 99.9% of jobs will only have one build
123 # (that's not done as kernbench, sparse, or buildtest),
124 # so it works out much cleaner. One of life's comprimises.
125 if not results_dir:
126 results_dir = os.path.join(self.resultdir, 'build')
127 i = 2
128 while os.path.exists(results_dir):
129 results_dir = os.path.join(self.resultdir, 'build.%d' % i)
mblighd9223fc2006-11-26 17:19:54 +0000130 i += 1
mbligh1e8858e2006-11-24 22:18:35 +0000131 if not os.path.exists(results_dir):
132 os.mkdir(results_dir)
133
134 return kernel.kernel(self, base_tree, results_dir, tmp_dir, leave)
mblighf4c35322006-03-13 01:01:10 +0000135
mblighcaa605c2006-10-02 00:37:35 +0000136
mblighfadca202006-09-23 04:40:01 +0000137 def barrier(self, *args):
138 """Create a barrier object"""
139 return barrier.barrier(*args)
140
mblighcaa605c2006-10-02 00:37:35 +0000141
mbligh4b089662006-06-14 22:34:58 +0000142 def setup_dep(self, deps):
mblighc86b0b42006-07-28 17:35:28 +0000143 """Set up the dependencies for this test.
144
145 deps is a list of libraries required for this test.
146 """
mbligh4b089662006-06-14 22:34:58 +0000147 for dep in deps:
148 try:
149 os.chdir(self.autodir + '/deps/' + dep)
150 system('./' + dep + '.py')
151 except:
152 error = "setting up dependency " + dep + "\n"
153 raise UnhandledError(error)
154
155
mblighd016ecc2006-11-25 21:41:07 +0000156 def __runtest(self, url, tag, args, dargs):
apwf1a81162006-04-25 10:10:29 +0000157 try:
mblighd016ecc2006-11-25 21:41:07 +0000158 test.runtest(self, url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000159 except AutotestError:
160 raise
161 except:
162 raise UnhandledError('running test ' + \
163 self.__class__.__name__ + "\n")
164
mblighcaa605c2006-10-02 00:37:35 +0000165
mblighd016ecc2006-11-25 21:41:07 +0000166 def runtest(self, tag, url, *args):
167 raise "Deprecated call to job.runtest. Use run_test instead"
168
169
170 def run_test(self, url, *args, **dargs):
mblighc86b0b42006-07-28 17:35:28 +0000171 """Summon a test object and run it.
172
173 tag
174 tag to add to testname
mbligh12a7df72006-10-06 03:54:33 +0000175 url
176 url of the test to run
mblighc86b0b42006-07-28 17:35:28 +0000177 """
mbligh12a7df72006-10-06 03:54:33 +0000178
mblighd016ecc2006-11-25 21:41:07 +0000179 if not url:
180 raise "Test name is invalid. Switched arguments?"
mbligh12a7df72006-10-06 03:54:33 +0000181 (group, name) = test.testname(url)
mblighd016ecc2006-11-25 21:41:07 +0000182 tag = None
183 if dargs.has_key('tag'):
184 tag = dargs['tag']
185 del dargs['tag']
mbligh7880b1b2006-05-07 16:57:50 +0000186 name += '.' + tag
apwf1a81162006-04-25 10:10:29 +0000187 try:
188 try:
mblighd016ecc2006-11-25 21:41:07 +0000189 self.__runtest(url, tag, args, dargs)
apwf1a81162006-04-25 10:10:29 +0000190 except Exception, detail:
191 self.record("FAIL " + name + " " + \
192 detail.__str__() + "\n")
193
194 raise
195 else:
196 self.record("GOOD " + name + \
mblighd9223fc2006-11-26 17:19:54 +0000197 " completed successfully\n")
apwf1a81162006-04-25 10:10:29 +0000198 except TestError:
199 return 0
200 except:
201 raise
202 else:
203 return 1
apw0865f482006-03-30 18:50:19 +0000204
mblighd7fb4a62006-10-01 00:57:53 +0000205
206 def filesystem(self, device, mountpoint = None):
207 if not mountpoint:
208 mountpoint = self.tmpdir
209 return filesystem.filesystem(device, mountpoint)
210
mblighcaa605c2006-10-02 00:37:35 +0000211
212 def reboot(self, tag='autotest'):
apwde1503a2006-10-10 08:34:21 +0000213 self.harness.run_reboot()
mblighcaa605c2006-10-02 00:37:35 +0000214 self.bootloader.boot_once(tag)
215 system("reboot")
apw0778a2f2006-10-06 03:11:40 +0000216 self.quit()
mblighcaa605c2006-10-02 00:37:35 +0000217
218
apw0865f482006-03-30 18:50:19 +0000219 def noop(self, text):
220 print "job: noop: " + text
221
mblighcaa605c2006-10-02 00:37:35 +0000222
apw0865f482006-03-30 18:50:19 +0000223 # Job control primatives.
mblighc86b0b42006-07-28 17:35:28 +0000224
apw8fef4ac2006-10-10 22:53:37 +0000225 def __parallel_execute(self, func, *args):
226 func(*args)
227
228
mblighc86b0b42006-07-28 17:35:28 +0000229 def parallel(self, *tasklist):
230 """Run tasks in parallel"""
apw8fef4ac2006-10-10 22:53:37 +0000231
232 pids = []
233 for task in tasklist:
234 pids.append(fork_start(self.resultdir,
235 lambda: self.__parallel_execute(*task)))
236 for pid in pids:
237 fork_waitfor(self.resultdir, pid)
apw0865f482006-03-30 18:50:19 +0000238
mblighcaa605c2006-10-02 00:37:35 +0000239
apw0865f482006-03-30 18:50:19 +0000240 def quit(self):
mblighc86b0b42006-07-28 17:35:28 +0000241 # XXX: should have a better name.
apwde1503a2006-10-10 08:34:21 +0000242 self.harness.run_pause()
apwf2c66602006-04-27 14:11:25 +0000243 raise JobContinue("more to come")
apw0865f482006-03-30 18:50:19 +0000244
mblighcaa605c2006-10-02 00:37:35 +0000245
apw0865f482006-03-30 18:50:19 +0000246 def complete(self, status):
mblighc86b0b42006-07-28 17:35:28 +0000247 """Clean up and exit"""
apw0865f482006-03-30 18:50:19 +0000248 # We are about to exit 'complete' so clean up the control file.
249 try:
apwecf41b72006-03-31 14:00:55 +0000250 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +0000251 except:
252 pass
apwde1503a2006-10-10 08:34:21 +0000253 self.harness.run_complete(status)
apw1b021902006-04-03 17:02:56 +0000254 sys.exit(status)
apw0865f482006-03-30 18:50:19 +0000255
mblighcaa605c2006-10-02 00:37:35 +0000256
apw0865f482006-03-30 18:50:19 +0000257 steps = []
258 def next_step(self, step):
mblighc86b0b42006-07-28 17:35:28 +0000259 """Define the next step"""
apw83f8d772006-04-27 14:12:56 +0000260 step[0] = step[0].__name__
apw0865f482006-03-30 18:50:19 +0000261 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +0000262 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +0000263
mblighcaa605c2006-10-02 00:37:35 +0000264
apw83f8d772006-04-27 14:12:56 +0000265 def step_engine(self):
mblighc86b0b42006-07-28 17:35:28 +0000266 """the stepping engine -- if the control file defines
267 step_init we will be using this engine to drive multiple runs.
268 """
269 """Do the next step"""
apw83f8d772006-04-27 14:12:56 +0000270 lcl = dict({'job': self})
271
272 str = """
273from error import *
274from autotest_utils import *
275"""
276 exec(str, lcl, lcl)
277 execfile(self.control, lcl, lcl)
278
mblighd9223fc2006-11-26 17:19:54 +0000279 state = self.control + '.state'
apw0865f482006-03-30 18:50:19 +0000280 # If there is a mid-job state file load that in and continue
281 # where it indicates. Otherwise start stepping at the passed
282 # entry.
283 try:
mblighd9223fc2006-11-26 17:19:54 +0000284 self.steps = pickle.load(open(state, 'r'))
apw0865f482006-03-30 18:50:19 +0000285 except:
apw83f8d772006-04-27 14:12:56 +0000286 if lcl.has_key('step_init'):
287 self.next_step([lcl['step_init']])
apw0865f482006-03-30 18:50:19 +0000288
289 # Run the step list.
290 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000291 step = self.steps.pop(0)
mblighd9223fc2006-11-26 17:19:54 +0000292 pickle.dump(self.steps, open(state, 'w'))
apw0865f482006-03-30 18:50:19 +0000293
294 cmd = step.pop(0)
apw83f8d772006-04-27 14:12:56 +0000295 cmd = lcl[cmd]
296 lcl['__cmd'] = cmd
297 lcl['__args'] = step
298 exec("__cmd(*__args)", lcl, lcl)
apw0865f482006-03-30 18:50:19 +0000299
300 # all done, clean up and exit.
301 self.complete(0)
302
mblighcaa605c2006-10-02 00:37:35 +0000303
apwf1a81162006-04-25 10:10:29 +0000304 def record(self, msg):
mblighc86b0b42006-07-28 17:35:28 +0000305 """Record job-level status"""
apw7db8d0b2006-10-09 08:10:25 +0000306
mblighd9223fc2006-11-26 17:19:54 +0000307 msg = msg.rstrip()
apw7db8d0b2006-10-09 08:10:25 +0000308 # Ensure any continuation lines are marked so we can
309 # detect them in the status file to ensure it is parsable.
mblighd9223fc2006-11-26 17:19:54 +0000310 msg = re.sub(r"\n", "\n ", msg)
apw7db8d0b2006-10-09 08:10:25 +0000311
apwde1503a2006-10-10 08:34:21 +0000312 self.harness.test_status(msg)
apwf1a81162006-04-25 10:10:29 +0000313 print msg
314 status = self.resultdir + "/status"
mblighd9223fc2006-11-26 17:19:54 +0000315 file(status, "a").write(msg + "\n")
apwce9abe92006-04-27 14:14:04 +0000316
317
mbligh570e93e2006-11-26 05:15:56 +0000318def runjob(control, cont = False, tag = "default", harness_type = ''):
mblighc86b0b42006-07-28 17:35:28 +0000319 """The main interface to this module
320
321 control
322 The control file to use for this job.
323 cont
324 Whether this is the continuation of a previously started job
325 """
apwce9abe92006-04-27 14:14:04 +0000326 state = control + '.state'
327
328 # instantiate the job object ready for the control file.
329 myjob = None
330 try:
331 # Check that the control file is valid
332 if not os.path.exists(control):
333 raise JobError(control + ": control file not found")
334
335 # When continuing, the job is complete when there is no
336 # state file, ensure we don't try and continue.
mblighf3fef462006-09-13 16:05:05 +0000337 if cont and not os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000338 sys.exit(1)
mblighf3fef462006-09-13 16:05:05 +0000339 if cont == False and os.path.exists(state):
apwce9abe92006-04-27 14:14:04 +0000340 os.unlink(state)
341
mbligh570e93e2006-11-26 05:15:56 +0000342 myjob = job(control, tag, cont, harness_type)
apwce9abe92006-04-27 14:14:04 +0000343
344 # Load in the users control file, may do any one of:
345 # 1) execute in toto
346 # 2) define steps, and select the first via next_step()
347 myjob.step_engine()
348
349 # If we get here, then we assume the job is complete and good.
350 myjob.complete(0)
351
352 except JobContinue:
353 sys.exit(5)
354
355 except JobError, instance:
356 print "JOB ERROR: " + instance.args[0]
357 if myjob != None:
358 myjob.complete(1)
359
360 except:
361 # Ensure we cannot continue this job, it is in rictus.
362 if os.path.exists(state):
363 os.unlink(state)
364 raise