blob: 079c4a036f891478c51b6659afc49acd40fa4836 [file] [log] [blame]
mblighf4c35322006-03-13 01:01:10 +00001from autotest_utils import *
apw0865f482006-03-30 18:50:19 +00002import os, sys, kernel, test, pickle, threading
mbligh3a6d6ca2006-04-23 15:50:24 +00003import profiler
mblighf4c35322006-03-13 01:01:10 +00004
apw0865f482006-03-30 18:50:19 +00005# Parallel run interface.
6class AsyncRun(threading.Thread):
7 def __init__(self, cmd):
8 threading.Thread.__init__(self)
9 self.cmd = cmd
10 def run(self):
11 x = self.cmd.pop(0)
12 x(*self.cmd)
13
14# JOB: the actual job against which we do everything.
mblighf4c35322006-03-13 01:01:10 +000015class job:
apwecf41b72006-03-31 14:00:55 +000016 def __init__(self, control, jobtag='default'):
mblighf4c35322006-03-13 01:01:10 +000017 self.autodir = os.environ['AUTODIR']
mbligh82641862006-04-23 06:21:36 +000018 self.testdir = self.autodir + '/tests'
mblighedf430c2006-04-23 06:52:09 +000019 self.profdir = self.autodir + '/profilers'
mblighf4c35322006-03-13 01:01:10 +000020 self.tmpdir = self.autodir + '/tmp'
mbligha66742f2006-04-02 19:54:27 +000021 if os.path.exists(self.tmpdir):
mbligha975fb62006-04-22 19:56:25 +000022 system('rm -rf ' + self.tmpdir)
mbligha66742f2006-04-02 19:54:27 +000023 os.mkdir(self.tmpdir)
mbligh24f7da02006-04-23 06:32:18 +000024 self.resultdir = self.autodir + '/results/' + jobtag
mblighf4c35322006-03-13 01:01:10 +000025 if os.path.exists(self.resultdir):
mbligha975fb62006-04-22 19:56:25 +000026 system('rm -rf ' + self.resultdir)
mblighf4c35322006-03-13 01:01:10 +000027 os.mkdir(self.resultdir)
28 os.mkdir(self.resultdir + "/debug")
29 os.mkdir(self.resultdir + "/analysis")
30
apwecf41b72006-03-31 14:00:55 +000031 self.control = control
mblighf4c35322006-03-13 01:01:10 +000032 self.jobtab = jobtag
33
34 self.stdout = fd_stack(1, sys.stdout)
35 self.stderr = fd_stack(2, sys.stderr)
36
mbligh3a6d6ca2006-04-23 15:50:24 +000037 self.profiler = profiler.profiler(self)
38
mblighf4c35322006-03-13 01:01:10 +000039 def kernel(self, topdir, base_tree):
40 return kernel.kernel(self, topdir, base_tree)
41
apw5a78eef2006-04-03 14:16:59 +000042 def runtest(self, tag, testname, *test_args):
mbligh82641862006-04-23 06:21:36 +000043 sys.path.insert(0, self.testdir + '/' + testname)
apw5a78eef2006-04-03 14:16:59 +000044 exec "import %s" % testname
45 exec "mytest = %s.%s(self, testname + '.' + tag)" % (testname, testname)
mbligh82641862006-04-23 06:21:36 +000046 mytest.bindir = self.testdir + '/' + testname
47 mytest.srcdir = mytest.bindir + '/' + testname
48 if os.path.exists(mytest.srcdir):
49 # Bad idea, as it'll always rebuild, but will do for now
50 system('rm -rf ' + mytest.srcdir)
51 mytest.tmpdir = self.tmpdir + '/' + testname
52 os.mkdir(mytest.tmpdir)
mblighf4c35322006-03-13 01:01:10 +000053 mytest.run(testname, test_args)
apw0865f482006-03-30 18:50:19 +000054
55 def noop(self, text):
56 print "job: noop: " + text
57
58 # Job control primatives.
59 def parallel(self, *l):
60 tasks = []
61 for t in l:
62 task = AsyncRun(t)
63 tasks.append(task)
64 task.start()
65 for t in tasks:
66 t.join()
67
68 # XXX: should have a better name.
69 def quit(self):
70 sys.exit(5)
71
72 def complete(self, status):
73 # We are about to exit 'complete' so clean up the control file.
74 try:
apwecf41b72006-03-31 14:00:55 +000075 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +000076 except:
77 pass
mbligha66742f2006-04-02 19:54:27 +000078 if os.path.exists(self.control):
79 os.rename(self.control, self.control + '.complete')
apw1b021902006-04-03 17:02:56 +000080 sys.exit(status)
apw0865f482006-03-30 18:50:19 +000081
82 # STEPS: the stepping engine -- if the control file defines
83 # step_init we will be using this engine to drive multiple
84 # runs.
85 steps = []
86 def next_step(self, step):
87 self.steps.append(step)
apwecf41b72006-03-31 14:00:55 +000088 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +000089
90 def step_engine(self, init):
91 # If there is a mid-job state file load that in and continue
92 # where it indicates. Otherwise start stepping at the passed
93 # entry.
94 try:
apwecf41b72006-03-31 14:00:55 +000095 self.steps = pickle.load(open(self.control + '.state',
96 'r'))
apw0865f482006-03-30 18:50:19 +000097 except:
98 self.next_step(init)
99
100 # Run the step list.
101 while len(self.steps) > 0:
apwfd922bb2006-04-04 07:47:00 +0000102 step = self.steps.pop(0)
apwecf41b72006-03-31 14:00:55 +0000103 pickle.dump(self.steps, open(self.control + '.state',
104 'w'))
apw0865f482006-03-30 18:50:19 +0000105
106 cmd = step.pop(0)
107 cmd(*step)
108
109 # all done, clean up and exit.
110 self.complete(0)
111