diff options
| author | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-09-16 20:46:19 -0400 |
|---|---|---|
| committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2012-09-16 20:46:19 -0400 |
| commit | bdb33621ac67b2cd9fadf3f3b006419ebb16a713 (patch) | |
| tree | 8918b5dbc6db8a73c275e445153c7ea42857210b /experiment | |
| parent | fd92ecb5a642eeae6c54d3cca1508fc4c4cb6a87 (diff) | |
Created run_exps.py script.
Currently poorly documented.
Diffstat (limited to 'experiment')
| -rw-r--r-- | experiment/__init__.py | 0 | ||||
| -rw-r--r-- | experiment/executable/__init__.py | 0 | ||||
| -rw-r--r-- | experiment/executable/executable.py | 71 | ||||
| -rw-r--r-- | experiment/executable/ftcat.py | 33 | ||||
| -rw-r--r-- | experiment/experiment.py | 153 | ||||
| -rw-r--r-- | experiment/litmus_util.py | 63 | ||||
| -rw-r--r-- | experiment/proc_entry.py | 12 | ||||
| -rw-r--r-- | experiment/tracer.py | 118 |
8 files changed, 450 insertions, 0 deletions
diff --git a/experiment/__init__.py b/experiment/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/experiment/__init__.py | |||
diff --git a/experiment/executable/__init__.py b/experiment/executable/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/experiment/executable/__init__.py | |||
diff --git a/experiment/executable/executable.py b/experiment/executable/executable.py new file mode 100644 index 0000000..6697a8d --- /dev/null +++ b/experiment/executable/executable.py | |||
| @@ -0,0 +1,71 @@ | |||
| 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 execute(self): | ||
| 47 | """Execute the binary.""" | ||
| 48 | full_command = self.__get_full_command() | ||
| 49 | self.sp = subprocess.Popen(full_command, stdout=self.stdout_file, | ||
| 50 | stderr=self.stderr_file, cwd=self.cwd) | ||
| 51 | |||
| 52 | def kill(self): | ||
| 53 | self.sp.kill() | ||
| 54 | |||
| 55 | def interrupt(self): | ||
| 56 | self.sp.send_signal(signal.SIGINT) | ||
| 57 | |||
| 58 | def terminate(self): | ||
| 59 | """Send the terminate signal to the binary.""" | ||
| 60 | self.sp.terminate() | ||
| 61 | |||
| 62 | def wait(self): | ||
| 63 | """Wait until the executable is finished, checking return code. | ||
| 64 | |||
| 65 | If the exit status is non-zero, raise an exception. | ||
| 66 | |||
| 67 | """ | ||
| 68 | |||
| 69 | self.sp.wait() | ||
| 70 | if self.sp.returncode != 0: | ||
| 71 | print >>sys.stderr, "Non-zero return: %s %s" % (self.exec_file, self.extra_args) | ||
diff --git a/experiment/executable/ftcat.py b/experiment/executable/ftcat.py new file mode 100644 index 0000000..9966312 --- /dev/null +++ b/experiment/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 | |||
diff --git a/experiment/experiment.py b/experiment/experiment.py new file mode 100644 index 0000000..29e6bd7 --- /dev/null +++ b/experiment/experiment.py | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | import os | ||
| 2 | import time | ||
| 3 | import litmus_util | ||
| 4 | from operator import methodcaller | ||
| 5 | from tracer import SchedTracer, LogTracer, PerfTracer, LinuxTracer, OverheadTracer | ||
| 6 | |||
| 7 | class ExperimentException(Exception): | ||
| 8 | """Used to indicate when there are problems with an experiment.""" | ||
| 9 | def __init__(self, name): | ||
| 10 | self.name = name | ||
| 11 | |||
| 12 | |||
| 13 | class ExperimentDone(ExperimentException): | ||
| 14 | """Raised when an experiment looks like it's been run already.""" | ||
| 15 | def __str__(self): | ||
| 16 | return "Experiment finished already: %d" % self.name | ||
| 17 | |||
| 18 | |||
| 19 | class ExperimentInterrupted(ExperimentException): | ||
| 20 | """Raised when an experiment appears to be interrupted (partial results).""" | ||
| 21 | def __str__(self): | ||
| 22 | return "Experiment was interrupted in progress: %d" % self.name | ||
| 23 | |||
| 24 | |||
| 25 | class ExperimentFailed(ExperimentException): | ||
| 26 | def __str__(self): | ||
| 27 | return "Experiment failed during execution: %d" % self.name | ||
| 28 | |||
| 29 | |||
| 30 | class Experiment(object): | ||
| 31 | """Execute one task-set and save the results. Experiments have unique IDs.""" | ||
| 32 | |||
| 33 | def __init__(self, name, scheduler, working_dir, finished_dir, proc_entries, executables): | ||
| 34 | """Run an experiment, optionally wrapped in tracing.""" | ||
| 35 | |||
| 36 | self.name = name | ||
| 37 | self.scheduler = scheduler | ||
| 38 | self.working_dir = working_dir | ||
| 39 | self.finished_dir = finished_dir | ||
| 40 | self.proc_entries = proc_entries | ||
| 41 | self.executables = executables | ||
| 42 | |||
| 43 | self.__make_dirs() | ||
| 44 | self.__assign_executable_cwds() | ||
| 45 | |||
| 46 | self.tracers = [] | ||
| 47 | if SchedTracer.enabled(): | ||
| 48 | self.tracers.append( SchedTracer(working_dir) ) | ||
| 49 | if LinuxTracer.enabled(): | ||
| 50 | self.tracers.append( LinuxTracer(working_dir) ) | ||
| 51 | if LogTracer.enabled(): | ||
| 52 | self.tracers.append( LogTracer(working_dir) ) | ||
| 53 | if PerfTracer.enabled(): | ||
| 54 | self.tracers.append( PerfTracer(working_dir) ) | ||
| 55 | |||
| 56 | # Overhead trace must be handled seperately, see __run_tasks | ||
| 57 | if OverheadTracer.enabled(): | ||
| 58 | self.overhead_trace = OverheadTracer(working_dir) | ||
| 59 | else: | ||
| 60 | self.overhead_trace = None | ||
| 61 | |||
| 62 | def __make_dirs(self): | ||
| 63 | if os.path.exists(self.finished_dir): | ||
| 64 | raise ExperimentDone(self.name) | ||
| 65 | if os.path.exists(self.working_dir): | ||
| 66 | raise ExperimentInterrupted(self.name) | ||
| 67 | |||
| 68 | os.mkdir(self.working_dir) | ||
| 69 | |||
| 70 | def __assign_executable_cwds(self): | ||
| 71 | def assign_cwd(executable): | ||
| 72 | executable.cwd = self.working_dir | ||
| 73 | map(assign_cwd, self.executables) | ||
| 74 | |||
| 75 | def __run_tasks(self): | ||
| 76 | exec_pause = 0.3 | ||
| 77 | self.log("Starting the program in ({0} seconds)".format( | ||
| 78 | len(self.executables) * exec_pause)) | ||
| 79 | for e in self.executables: | ||
| 80 | e.execute() | ||
| 81 | time.sleep(exec_pause) | ||
| 82 | |||
| 83 | sleep_time = 2 | ||
| 84 | self.log("Sleeping for %d seconds before release" % sleep_time) | ||
| 85 | time.sleep(sleep_time) | ||
| 86 | |||
| 87 | # Overhead tracer must be started right after release or overhead | ||
| 88 | # measurements will be full of irrelevant records | ||
| 89 | if self.overhead_trace: | ||
| 90 | self.log("Starting overhead trace") | ||
| 91 | self.overhead_trace.start_tracing() | ||
| 92 | |||
| 93 | released = litmus_util.release_tasks() | ||
| 94 | |||
| 95 | ret = True | ||
| 96 | if released != len(self.executables): | ||
| 97 | self.log("Failed to release %d tasks! Re-releasing and killing".format( | ||
| 98 | len(self.experiments) - released)) | ||
| 99 | |||
| 100 | time.sleep(10) | ||
| 101 | litmus_util.release_tasks() | ||
| 102 | |||
| 103 | time.sleep(20) | ||
| 104 | map(methodcaller('kill'), self.executables) | ||
| 105 | |||
| 106 | ret = False | ||
| 107 | |||
| 108 | self.log("Waiting for program to finish...") | ||
| 109 | map(methodcaller('wait'), self.executables) | ||
| 110 | |||
| 111 | # And it must be stopped here for the same reason | ||
| 112 | if self.overhead_trace: | ||
| 113 | self.log("Stopping overhead trace") | ||
| 114 | self.overhead_trace.stop_tracing() | ||
| 115 | |||
| 116 | if not ret: | ||
| 117 | raise ExperimentFailed(self.name) | ||
| 118 | |||
| 119 | def __save_results(self): | ||
| 120 | os.rename(self.working_dir, self.finished_dir) | ||
| 121 | |||
| 122 | def log(self, msg): | ||
| 123 | print "[Exp %s]: %s" % (self.name, msg) | ||
| 124 | |||
| 125 | def run_exp(self): | ||
| 126 | self.setup() | ||
| 127 | self.__run_tasks() | ||
| 128 | self.teardown() | ||
| 129 | |||
| 130 | def setup(self): | ||
| 131 | self.log("Switching to %s" % self.scheduler) | ||
| 132 | litmus_util.switch_scheduler(self.scheduler) | ||
| 133 | |||
| 134 | self.log("Writing %d proc entries" % len(self.proc_entries)) | ||
| 135 | map(methodcaller('write_proc'), self.proc_entries) | ||
| 136 | |||
| 137 | self.log("Starting %d tracers" % len(self.tracers)) | ||
| 138 | map(methodcaller('start_tracing'), self.tracers) | ||
| 139 | |||
| 140 | def teardown(self): | ||
| 141 | sleep_time = 5 | ||
| 142 | self.log("Sleeping %d seconds to allow buffer flushing" % sleep_time) | ||
| 143 | time.sleep(sleep_time) | ||
| 144 | |||
| 145 | self.log("Stopping tracers") | ||
| 146 | map(methodcaller('stop_tracing'), self.tracers) | ||
| 147 | |||
| 148 | self.log("Switching to Linux scheduler") | ||
| 149 | litmus_util.switch_scheduler("Linux") | ||
| 150 | |||
| 151 | self.log("Saving results in %s" % self.finished_dir) | ||
| 152 | self.__save_results() | ||
| 153 | self.log("Experiment done!") | ||
diff --git a/experiment/litmus_util.py b/experiment/litmus_util.py new file mode 100644 index 0000000..114f4c9 --- /dev/null +++ b/experiment/litmus_util.py | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | import re | ||
| 2 | import time | ||
| 3 | import subprocess | ||
| 4 | import os | ||
| 5 | import stat | ||
| 6 | import config.config as conf | ||
| 7 | |||
| 8 | def num_cpus(): | ||
| 9 | """Return the number of CPUs in the system.""" | ||
| 10 | |||
| 11 | lnx_re = re.compile(r'^(processor|online)') | ||
| 12 | cpus = 0 | ||
| 13 | |||
| 14 | with open('/proc/cpuinfo', 'r') as f: | ||
| 15 | for line in f: | ||
| 16 | if lnx_re.match(line): | ||
| 17 | cpus += 1 | ||
| 18 | return cpus | ||
| 19 | |||
| 20 | def switch_scheduler(switch_to_in): | ||
| 21 | """Switch the scheduler to whatever is passed in. | ||
| 22 | |||
| 23 | This methods sleeps for two seconds to give Linux the chance to execute | ||
| 24 | schedule switching code. Raises an exception if the switch does not work. | ||
| 25 | |||
| 26 | """ | ||
| 27 | |||
| 28 | switch_to = str(switch_to_in).strip() | ||
| 29 | |||
| 30 | with open('/proc/litmus/active_plugin', 'w') as active_plugin: | ||
| 31 | subprocess.Popen(["echo", switch_to], stdout=active_plugin) | ||
| 32 | |||
| 33 | # it takes a bit to do the switch, sleep an arbitrary amount of time | ||
| 34 | time.sleep(2) | ||
| 35 | |||
| 36 | with open('/proc/litmus/active_plugin', 'r') as active_plugin: | ||
| 37 | cur_plugin = active_plugin.read().strip() | ||
| 38 | |||
| 39 | if switch_to != cur_plugin: | ||
| 40 | raise Exception("Could not switch to plugin: %s" % switch_to) | ||
| 41 | |||
| 42 | |||
| 43 | def is_executable(fname): | ||
| 44 | """Return whether the file passed in is executable""" | ||
| 45 | mode = os.stat(fname)[stat.ST_MODE] | ||
| 46 | return mode & stat.S_IXUSR and mode & stat.S_IRUSR | ||
| 47 | |||
| 48 | def is_device(dev): | ||
| 49 | if not os.path.exists(dev): | ||
| 50 | return False | ||
| 51 | mode = os.stat(dev)[stat.ST_MODE] | ||
| 52 | return not (not mode & stat.S_IFCHR) | ||
| 53 | |||
| 54 | def release_tasks(): | ||
| 55 | |||
| 56 | try: | ||
| 57 | data = subprocess.check_output([conf.BINS['release']]) | ||
| 58 | except subprocess.CalledProcessError: | ||
| 59 | raise Exception('Something went wrong in release_ts') | ||
| 60 | |||
| 61 | released = re.findall(r"([0-9]+) real-time", data)[0] | ||
| 62 | |||
| 63 | return int(released) | ||
diff --git a/experiment/proc_entry.py b/experiment/proc_entry.py new file mode 100644 index 0000000..0b7f9ce --- /dev/null +++ b/experiment/proc_entry.py | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | import os | ||
| 2 | |||
| 3 | class ProcEntry(object): | ||
| 4 | def __init__(self, proc, data): | ||
| 5 | self.proc = proc | ||
| 6 | self.data = data | ||
| 7 | |||
| 8 | def write_proc(self): | ||
| 9 | if not os.path.exists(self.proc): | ||
| 10 | raise Exception("Invalid proc entry %s" % self.proc) | ||
| 11 | with open(self.proc, 'w') as entry: | ||
| 12 | entry.write(self.data) | ||
diff --git a/experiment/tracer.py b/experiment/tracer.py new file mode 100644 index 0000000..d7743ad --- /dev/null +++ b/experiment/tracer.py | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | import litmus_util | ||
| 2 | import os | ||
| 3 | from operator import methodcaller | ||
| 4 | from executable.ftcat import FTcat,Executable | ||
| 5 | from config.config import FILES,BINS | ||
| 6 | |||
| 7 | class Tracer(object): | ||
| 8 | def __init__(self, name, output_dir): | ||
| 9 | self.name = name | ||
| 10 | self.output_dir = output_dir | ||
| 11 | self.bins = [] | ||
| 12 | |||
| 13 | def start_tracing(self): | ||
| 14 | map(methodcaller("execute"), self.bins) | ||
| 15 | |||
| 16 | def stop_tracing(self): | ||
| 17 | map(methodcaller('terminate'), self.bins) | ||
| 18 | map(methodcaller('wait'), self.bins) | ||
| 19 | |||
| 20 | |||
| 21 | class LinuxTracer(Tracer): | ||
| 22 | EVENT_ROOT = "/sys/kernel/debug/tracing" | ||
| 23 | LITMUS_EVENTS = "%s/events/litmus" % EVENT_ROOT | ||
| 24 | |||
| 25 | def __init__(self, output_dir): | ||
| 26 | super(LinuxTracer, self).__init__("trace-cmd", output_dir) | ||
| 27 | |||
| 28 | extra_args = ["record", "-e", "sched:sched_switch", | ||
| 29 | "-e", "litmus:*", | ||
| 30 | "-o", "%s/%s" % (output_dir, FILES['linux_data'])] | ||
| 31 | stdout = open('%s/trace-cmd-stdout.txt' % self.output_dir, 'w') | ||
| 32 | stderr = open('%s/trace-cmd-stderr.txt' % self.output_dir, 'w') | ||
| 33 | |||
| 34 | execute = Executable(BINS['trace-cmd'], extra_args, stdout, stderr) | ||
| 35 | self.bins.append(execute) | ||
| 36 | |||
| 37 | @staticmethod | ||
| 38 | def enabled(): | ||
| 39 | return os.path.exists(LinuxTracer.LITMUS_EVENTS) | ||
| 40 | |||
| 41 | def stop_tracing(self): | ||
| 42 | map(methodcaller('interrupt'), self.bins) | ||
| 43 | map(methodcaller('wait'), self.bins) | ||
| 44 | |||
| 45 | |||
| 46 | class LogTracer(Tracer): | ||
| 47 | DEVICE_STR = '/dev/litmus/log' | ||
| 48 | |||
| 49 | def __init__(self, output_dir): | ||
| 50 | super(LogTracer, self).__init__("Logger", output_dir) | ||
| 51 | |||
| 52 | out_file = open("%s/%s" % (self.output_dir, FILES['log_data']), 'w') | ||
| 53 | |||
| 54 | cat = (Executable("/bin/cat", [LogTracer.DEVICE_STR])) | ||
| 55 | cat.stdout_file = out_file | ||
| 56 | |||
| 57 | self.bins.append(cat) | ||
| 58 | |||
| 59 | @staticmethod | ||
| 60 | def enabled(): | ||
| 61 | return litmus_util.is_device(LogTracer.DEVICE_STR) | ||
| 62 | |||
| 63 | |||
| 64 | class SchedTracer(Tracer): | ||
| 65 | EVENTS = range(501, 510) # not including 511 | ||
| 66 | DEVICE_STR = '/dev/litmus/sched_trace' | ||
| 67 | |||
| 68 | def __init__(self, output_dir): | ||
| 69 | super(SchedTracer, self).__init__("Sched Trace", output_dir) | ||
| 70 | |||
| 71 | if SchedTracer.enabled(): | ||
| 72 | for cpu in range(litmus_util.num_cpus()): | ||
| 73 | # Executable will close the stdout/stderr files | ||
| 74 | stdout_f = open('%s/st-%d.bin' % (self.output_dir, cpu), 'w') | ||
| 75 | stderr_f = open('%s/st-%d-stderr.txt' % (self.output_dir, cpu), 'w') | ||
| 76 | dev = '{0}{1}'.format(SchedTracer.DEVICE_STR, cpu) | ||
| 77 | ftc = FTcat(BINS['ftcat'], stdout_f, stderr_f, dev, SchedTracer.EVENTS, cpu=cpu) | ||
| 78 | |||
| 79 | self.bins.append(ftc) | ||
| 80 | |||
| 81 | @staticmethod | ||
| 82 | def enabled(): | ||
| 83 | return litmus_util.is_device("%s%d" % (SchedTracer.DEVICE_STR, 0)) | ||
| 84 | |||
| 85 | |||
| 86 | class OverheadTracer(Tracer): | ||
| 87 | DEVICE_STR = '/dev/litmus/ft_trace0' | ||
| 88 | EVENTS = [# 'SCHED_START', 'SCHED_END', 'SCHED2_START', 'SCHED2_END', | ||
| 89 | 'RELEASE_START', 'RELEASE_END', | ||
| 90 | 'LVLA_RELEASE_START', 'LVLA_RELEASE_END', | ||
| 91 | 'LVLA_SCHED_START', 'LVLA_SCHED_END', | ||
| 92 | 'LVLB_RELEASE_START', 'LVLB_RELEASE_END', | ||
| 93 | 'LVLB_SCHED_START', 'LVLB_SCHED_END', | ||
| 94 | 'LVLC_RELEASE_START', 'LVLC_RELEASE_END', | ||
| 95 | 'LVLC_SCHED_START', 'LVLC_SCHED_END'] | ||
| 96 | |||
| 97 | def __init__(self, output_dir): | ||
| 98 | super(OverheadTracer, self).__init__("Overhead Trace", output_dir) | ||
| 99 | |||
| 100 | stdout_f = open('{0}/{1}'.format(self.output_dir, FILES['ft_data']), 'w') | ||
| 101 | stderr_f = open('{0}/{1}.stderr.txt'.format(self.output_dir, FILES['ft_data']), 'w') | ||
| 102 | ftc = FTcat(BINS['ftcat'], stdout_f, stderr_f, | ||
| 103 | OverheadTracer.DEVICE_STR, OverheadTracer.EVENTS) | ||
| 104 | |||
| 105 | self.bins.append(ftc) | ||
| 106 | |||
| 107 | @staticmethod | ||
| 108 | def enabled(): | ||
| 109 | return litmus_util.is_device(OverheadTracer.DEVICE_STR) | ||
| 110 | |||
| 111 | |||
| 112 | class PerfTracer(Tracer): | ||
| 113 | def __init__(self, output_dir): | ||
| 114 | super(PerfTracer, self).__init__("CPU perf counters", output_dir) | ||
| 115 | |||
| 116 | @staticmethod | ||
| 117 | def enabled(): | ||
| 118 | return False | ||
