diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-11-26 17:06:27 -0500 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-11-26 17:06:27 -0500 |
| commit | b43b83beead92ff7cf28a5fe5a2710537268aae1 (patch) | |
| tree | d9c29b14cd18a9df520f36d7e85eb460c30fa7a9 /run/executable | |
| parent | cb8db5d30ee769304c2c2b00f2a7d9bcb3c4098f (diff) | |
Read locations of binary files from path instead of config.py.
Diffstat (limited to 'run/executable')
| -rw-r--r-- | run/executable/__init__.py | 0 | ||||
| -rw-r--r-- | run/executable/executable.py | 77 | ||||
| -rw-r--r-- | run/executable/ftcat.py | 33 |
3 files changed, 110 insertions, 0 deletions
diff --git a/run/executable/__init__.py b/run/executable/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/run/executable/__init__.py | |||
diff --git a/run/executable/executable.py b/run/executable/executable.py new file mode 100644 index 0000000..628f711 --- /dev/null +++ b/run/executable/executable.py | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | import sys | ||
| 2 | import subprocess | ||
| 3 | import signal | ||
| 4 | from ..litmus_util import is_executable | ||
| 5 | |||
| 6 | class Executable(object): | ||
| 7 | '''Parent object that represents an executable for use in task-sets.''' | ||
| 8 | |||
| 9 | def __init__(self, exec_file, extra_args=None, stdout_file = None, stderr_file = None): | ||
| 10 | self.exec_file = exec_file | ||
| 11 | self.cwd = None | ||
| 12 | self.stdout_file = stdout_file | ||
| 13 | self.stderr_file = stderr_file | ||
| 14 | self.sp = None | ||
| 15 | |||
| 16 | if extra_args is None: | ||
| 17 | self.extra_args = None | ||
| 18 | else: | ||
| 19 | self.extra_args = list(extra_args) # make a duplicate | ||
| 20 | |||
| 21 | if not is_executable(self.exec_file): | ||
| 22 | raise Exception("Not executable ? : %s" % self.exec_file) | ||
| 23 | |||
| 24 | def __del__(self): | ||
| 25 | # Try and clean up | ||
| 26 | if self.stdout_file is not None: | ||
| 27 | self.stdout_file.close() | ||
| 28 | if self.stderr_file is not None: | ||
| 29 | self.stderr_file.close() | ||
| 30 | |||
| 31 | if self.sp is not None: | ||
| 32 | try: | ||
| 33 | self.sp.terminate() | ||
| 34 | except OSError as e: | ||
| 35 | if e.errno == 3: | ||
| 36 | pass # no such process (already killed), okay | ||
| 37 | else: | ||
| 38 | raise e | ||
| 39 | |||
| 40 | def __get_full_command(self): | ||
| 41 | full_command = [self.exec_file] | ||
| 42 | if self.extra_args is not None: | ||
| 43 | full_command += self.extra_args | ||
| 44 | return full_command | ||
| 45 | |||
| 46 | def __str__(self): | ||
| 47 | return " ".join(self.__get_full_command()) | ||
| 48 | |||
| 49 | def execute(self): | ||
| 50 | '''Execute the binary.''' | ||
| 51 | full_command = self.__get_full_command() | ||
| 52 | self.sp = subprocess.Popen(full_command, stdout=self.stdout_file, | ||
| 53 | stderr=self.stderr_file, cwd=self.cwd) | ||
| 54 | |||
| 55 | def kill(self): | ||
| 56 | self.sp.kill() | ||
| 57 | |||
| 58 | def interrupt(self): | ||
| 59 | self.sp.send_signal(signal.SIGINT) | ||
| 60 | |||
| 61 | def terminate(self): | ||
| 62 | '''Send the terminate signal to the binary.''' | ||
| 63 | self.sp.terminate() | ||
| 64 | |||
| 65 | def wait(self): | ||
| 66 | '''Wait until the executable is finished, checking return code. | ||
| 67 | |||
| 68 | If the exit status is non-zero, raise an exception. | ||
| 69 | |||
| 70 | ''' | ||
| 71 | |||
| 72 | self.sp.wait() | ||
| 73 | if self.sp.returncode != 0: | ||
| 74 | print >>sys.stderr, "Non-zero return: %s %s" % (self.exec_file, " ".join(self.extra_args)) | ||
| 75 | return 0 | ||
| 76 | else: | ||
| 77 | return 1 | ||
diff --git a/run/executable/ftcat.py b/run/executable/ftcat.py new file mode 100644 index 0000000..5da8fa7 --- /dev/null +++ b/run/executable/ftcat.py | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | import os | ||
| 2 | import stat | ||
| 3 | |||
| 4 | from executable import Executable | ||
| 5 | |||
| 6 | class FTcat(Executable): | ||
| 7 | '''Used to wrap the ftcat binary in the Experiment object.''' | ||
| 8 | |||
| 9 | def __init__(self, ft_cat_bin, stdout_file, stderr_file, dev, events, cpu=None): | ||
| 10 | '''Extends the Executable initializer method with ftcat attributes.''' | ||
| 11 | |||
| 12 | # hack to run FTCat at higher priority | ||
| 13 | chrt_bin = '/usr/bin/chrt' | ||
| 14 | |||
| 15 | super(FTcat, self).__init__(chrt_bin) | ||
| 16 | self.stdout_file = stdout_file | ||
| 17 | self.stderr_file = stderr_file | ||
| 18 | |||
| 19 | mode = os.stat(dev)[stat.ST_MODE] | ||
| 20 | if not mode & stat.S_IFCHR: | ||
| 21 | raise Exception("%s is not a character device" % dev) | ||
| 22 | |||
| 23 | if events is None: | ||
| 24 | raise Exception('No events!') | ||
| 25 | |||
| 26 | # hack to run FTCat at higher priority | ||
| 27 | self.extra_args = ['-f', '40'] | ||
| 28 | if cpu is not None: | ||
| 29 | # and bind it to a CPU | ||
| 30 | self.extra_args.extend(['/usr/bin/taskset', '-c', str(cpu)]) | ||
| 31 | events_str_arr = map(str, events) | ||
| 32 | self.extra_args.extend([ft_cat_bin, dev] + events_str_arr) | ||
| 33 | |||
