mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 1 | __author__ = """Copyright Andy Whitcroft, Martin J. Bligh - 2006, 2007""" |
| 2 | |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 3 | import sys, os, subprocess, traceback, time, utils |
mbligh | 3177be4 | 2008-01-15 20:36:54 +0000 | [diff] [blame] | 4 | from common.error import * |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 5 | |
| 6 | |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 7 | def parallel(tasklist, timeout=None): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 8 | """Run an set of predefined subcommands in parallel""" |
| 9 | pids = [] |
| 10 | error = False |
| 11 | for task in tasklist: |
| 12 | task.fork_start() |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 13 | |
| 14 | remaining_timeout = None |
| 15 | if timeout: |
| 16 | endtime = time.time() + timeout |
| 17 | |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 18 | for task in tasklist: |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 19 | if timeout: |
| 20 | remaining_timeout = max(endtime - time.time(), 1) |
mbligh | 158ba7b | 2008-03-07 18:29:12 +0000 | [diff] [blame^] | 21 | try: |
| 22 | status = task.fork_waitfor(remaining_timeout) |
| 23 | except AutoservSubcommandError: |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 24 | error = True |
mbligh | 158ba7b | 2008-03-07 18:29:12 +0000 | [diff] [blame^] | 25 | else: |
| 26 | if status != 0: |
| 27 | error = True |
| 28 | |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 29 | if error: |
mbligh | 4d6feff | 2008-01-14 16:48:56 +0000 | [diff] [blame] | 30 | raise AutoservError('One or more subcommands failed') |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 31 | |
| 32 | |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 33 | def parallel_simple(function, arglist, log=True, timeout=None): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 34 | """Each element in the arglist used to create a subcommand object, |
| 35 | where that arg is used both as a subdir name, and a single argument |
| 36 | to pass to "function". |
| 37 | We create a subcommand object for each element in the list, |
| 38 | then execute those subcommand objects in parallel.""" |
mbligh | dd3235b | 2008-01-14 16:44:19 +0000 | [diff] [blame] | 39 | |
| 40 | # Bypass the multithreading if only one machine. |
| 41 | if len (arglist) == 1: |
| 42 | function(arglist[0]) |
| 43 | return |
| 44 | |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 45 | subcommands = [] |
| 46 | for arg in arglist: |
mbligh | 0c9e782 | 2007-07-25 22:47:51 +0000 | [diff] [blame] | 47 | args = [arg] |
mbligh | 84c0ab1 | 2007-10-24 21:28:58 +0000 | [diff] [blame] | 48 | if log: |
| 49 | subdir = str(arg) |
| 50 | else: |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 51 | subdir = None |
mbligh | 0c9e782 | 2007-07-25 22:47:51 +0000 | [diff] [blame] | 52 | subcommands.append(subcommand(function, args, subdir)) |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 53 | parallel(subcommands, timeout) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 54 | |
| 55 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 56 | def _where_art_thy_filehandles(): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 57 | os.system("ls -l /proc/%d/fd >> /dev/tty" % os.getpid()) |
| 58 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 59 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 60 | def _print_to_tty(string): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 61 | open('/dev/tty', 'w').write(string + '\n') |
| 62 | |
| 63 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 64 | def _redirect_stream(fd, output): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 65 | newfd = os.open(output, os.O_WRONLY | os.O_CREAT) |
| 66 | os.dup2(newfd, fd) |
| 67 | os.close(newfd) |
| 68 | if fd == 1: |
| 69 | sys.stdout = os.fdopen(fd, 'w') |
| 70 | if fd == 2: |
| 71 | sys.stderr = os.fdopen(fd, 'w') |
| 72 | |
| 73 | |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 74 | def _redirect_stream_tee(fd, output, tag): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 75 | """Use the low-level fork & pipe operations here to get a fd, |
| 76 | not a filehandle. This ensures that we get both the |
| 77 | filehandle and fd for stdout/stderr redirected correctly.""" |
| 78 | r, w = os.pipe() |
| 79 | pid = os.fork() |
| 80 | if pid: # Parent |
| 81 | os.dup2(w, fd) |
| 82 | os.close(r) |
| 83 | os.close(w) |
| 84 | if fd == 1: |
| 85 | sys.stdout = os.fdopen(fd, 'w', 1) |
| 86 | if fd == 2: |
| 87 | sys.stderr = os.fdopen(fd, 'w', 1) |
| 88 | return |
| 89 | else: # Child |
| 90 | os.close(w) |
| 91 | log = open(output, 'w') |
| 92 | f = os.fdopen(r, 'r') |
| 93 | for line in iter(f.readline, ''): |
| 94 | # Tee straight to file |
| 95 | log.write(line) |
| 96 | log.flush() |
| 97 | # Prepend stdout with the tag |
| 98 | print tag + ' : ' + line, |
| 99 | sys.stdout.flush() |
| 100 | log.close() |
| 101 | os._exit(0) |
| 102 | |
| 103 | |
| 104 | class subcommand: |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 105 | def __init__(self, func, args, subdir = None, stdprint = True): |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 106 | # func(args) - the subcommand to run |
| 107 | # subdir - the subdirectory to log results in |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 108 | # stdprint - whether to print results to stdout/stderr |
| 109 | if subdir: |
| 110 | self.subdir = os.path.abspath(subdir) |
mbligh | a1956d3 | 2008-02-08 16:49:56 +0000 | [diff] [blame] | 111 | if not os.path.exists(self.subdir): |
| 112 | os.mkdir(self.subdir) |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 113 | self.debug = os.path.join(self.subdir, 'debug') |
mbligh | 61878d9 | 2008-02-08 16:50:17 +0000 | [diff] [blame] | 114 | if not os.path.exists(self.debug): |
| 115 | os.mkdir(self.debug) |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 116 | self.stdout = os.path.join(self.debug, 'stdout') |
| 117 | self.stderr = os.path.join(self.debug, 'stderr') |
| 118 | else: |
| 119 | self.subdir = None |
| 120 | self.debug = '/dev/null' |
| 121 | self.stdout = '/dev/null' |
| 122 | self.stderr = '/dev/null' |
| 123 | |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 124 | self.func = func |
| 125 | self.args = args |
| 126 | self.lambda_function = lambda: func(*args) |
| 127 | self.pid = None |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 128 | self.stdprint = stdprint |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def redirect_output(self): |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 132 | if self.stdprint: |
| 133 | if self.subdir: |
| 134 | tag = os.path.basename(self.subdir) |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 135 | _redirect_stream_tee(1, self.stdout, tag) |
| 136 | _redirect_stream_tee(2, self.stderr, tag) |
mbligh | b491d02 | 2007-08-09 23:04:56 +0000 | [diff] [blame] | 137 | else: |
mbligh | cee25b1 | 2007-08-31 08:53:05 +0000 | [diff] [blame] | 138 | _redirect_stream(1, self.stdout) |
| 139 | _redirect_stream(2, self.stderr) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def fork_start(self): |
| 143 | sys.stdout.flush() |
| 144 | sys.stderr.flush() |
| 145 | self.pid = os.fork() |
| 146 | |
| 147 | if self.pid: # I am the parent |
| 148 | return |
| 149 | |
| 150 | # We are the child from this point on. Never return. |
mbligh | d7685d3 | 2007-08-10 22:08:42 +0000 | [diff] [blame] | 151 | if self.subdir: |
| 152 | os.chdir(self.subdir) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 153 | self.redirect_output() |
| 154 | |
| 155 | try: |
| 156 | self.lambda_function() |
| 157 | |
| 158 | except: |
mbligh | 42ff92f | 2007-11-24 19:13:15 +0000 | [diff] [blame] | 159 | traceback.print_exc() |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 160 | sys.stdout.flush() |
| 161 | sys.stderr.flush() |
| 162 | os._exit(1) |
| 163 | |
| 164 | sys.stdout.flush() |
| 165 | sys.stderr.flush() |
| 166 | os._exit(0) |
| 167 | |
| 168 | |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 169 | def fork_waitfor(self, timeout=None): |
| 170 | if not timeout: |
| 171 | (pid, status) = os.waitpid(self.pid, 0) |
| 172 | else: |
| 173 | pid = None |
| 174 | start_time = time.time() |
| 175 | while time.time() <= start_time + timeout: |
| 176 | (pid, status) = os.waitpid(self.pid, os.WNOHANG) |
| 177 | if pid: |
| 178 | break |
| 179 | time.sleep(1) |
| 180 | |
| 181 | if not pid: |
| 182 | utils.nuke_pid(self.pid) |
| 183 | print "subcommand failed pid %d" % self.pid |
mbligh | eec4d7a | 2008-02-08 16:50:43 +0000 | [diff] [blame] | 184 | print "%s" % (self.func,) |
mbligh | c3aee0f | 2008-01-17 16:26:39 +0000 | [diff] [blame] | 185 | print "timeout after %ds" % timeout |
| 186 | print |
| 187 | return None |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 188 | |
| 189 | if status != 0: |
| 190 | print "subcommand failed pid %d" % pid |
mbligh | eec4d7a | 2008-02-08 16:50:43 +0000 | [diff] [blame] | 191 | print "%s" % (self.func,) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 192 | print "rc=%d" % status |
| 193 | print |
| 194 | if os.path.exists(self.stderr): |
| 195 | for line in open(self.stderr).readlines(): |
| 196 | print line, |
| 197 | print "\n--------------------------------------------\n" |
mbligh | 6e2ffec | 2008-03-05 16:08:34 +0000 | [diff] [blame] | 198 | raise AutoservSubcommandError(self.func, status) |
mbligh | b0fab82 | 2007-07-25 16:40:19 +0000 | [diff] [blame] | 199 | return status |