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