|
| 1 | +from EventManager.Models.RunnerEvents import RunnerEvents |
| 2 | +from EventManager.EventSubscriptionController import EventSubscriptionController |
| 3 | +from ConfigValidator.Config.Models.RunTableModel import RunTableModel |
| 4 | +from ConfigValidator.Config.Models.FactorModel import FactorModel |
| 5 | +from ConfigValidator.Config.Models.RunnerContext import RunnerContext |
| 6 | +from ConfigValidator.Config.Models.OperationType import OperationType |
| 7 | +from ProgressManager.Output.OutputProcedure import OutputProcedure as output |
| 8 | + |
| 9 | +from typing import Dict, Any, Optional |
| 10 | +from pathlib import Path |
| 11 | +from os.path import dirname, realpath |
| 12 | + |
| 13 | +import subprocess |
| 14 | +import shlex |
| 15 | +from statistics import mean |
| 16 | + |
| 17 | +from Plugins.Profilers.PicoCM3 import PicoCM3, CM3DataTypes, CM3Channels |
| 18 | + |
| 19 | +class RunnerConfig: |
| 20 | + ROOT_DIR = Path(dirname(realpath(__file__))) |
| 21 | + |
| 22 | + # ================================ USER SPECIFIC CONFIG ================================ |
| 23 | + """The name of the experiment.""" |
| 24 | + name: str = "new_runner_experiment" |
| 25 | + |
| 26 | + """The path in which Experiment Runner will create a folder with the name `self.name`, in order to store the |
| 27 | + results from this experiment. (Path does not need to exist - it will be created if necessary.) |
| 28 | + Output path defaults to the config file's path, inside the folder 'experiments'""" |
| 29 | + results_output_path: Path = ROOT_DIR / 'experiments' |
| 30 | + |
| 31 | + """Experiment operation type. Unless you manually want to initiate each run, use `OperationType.AUTO`.""" |
| 32 | + operation_type: OperationType = OperationType.AUTO |
| 33 | + |
| 34 | + """The time Experiment Runner will wait after a run completes. |
| 35 | + This can be essential to accommodate for cooldown periods on some systems.""" |
| 36 | + time_between_runs_in_ms: int = 1000 |
| 37 | + |
| 38 | + # Dynamic configurations can be one-time satisfied here before the program takes the config as-is |
| 39 | + # e.g. Setting some variable based on some criteria |
| 40 | + def __init__(self): |
| 41 | + """Executes immediately after program start, on config load""" |
| 42 | + |
| 43 | + EventSubscriptionController.subscribe_to_multiple_events([ |
| 44 | + (RunnerEvents.BEFORE_EXPERIMENT, self.before_experiment), |
| 45 | + (RunnerEvents.BEFORE_RUN , self.before_run ), |
| 46 | + (RunnerEvents.START_RUN , self.start_run ), |
| 47 | + (RunnerEvents.START_MEASUREMENT, self.start_measurement), |
| 48 | + (RunnerEvents.INTERACT , self.interact ), |
| 49 | + (RunnerEvents.STOP_MEASUREMENT , self.stop_measurement ), |
| 50 | + (RunnerEvents.STOP_RUN , self.stop_run ), |
| 51 | + (RunnerEvents.POPULATE_RUN_DATA, self.populate_run_data), |
| 52 | + (RunnerEvents.AFTER_EXPERIMENT , self.after_experiment ) |
| 53 | + ]) |
| 54 | + |
| 55 | + self.latest_log = None |
| 56 | + self.run_table_model = None # Initialized later |
| 57 | + output.console_log("Custom config loaded") |
| 58 | + |
| 59 | + def create_run_table_model(self) -> RunTableModel: |
| 60 | + """Create and return the run_table model here. A run_table is a List (rows) of tuples (columns), |
| 61 | + representing each run performed""" |
| 62 | + workers_factor = FactorModel("num_workers", [2, 4]) |
| 63 | + write_factor = FactorModel("write_size", [1024, 4096]) |
| 64 | + |
| 65 | + self.run_table_model = RunTableModel( |
| 66 | + factors = [workers_factor, write_factor], |
| 67 | + data_columns=['timestamp', 'channel_1(avg)', 'channel_2(off)', 'channel_3(off)']) # Channel 1 is in Amps |
| 68 | + |
| 69 | + return self.run_table_model |
| 70 | + |
| 71 | + def before_experiment(self) -> None: |
| 72 | + """Perform any activity required before starting the experiment here |
| 73 | + Invoked only once during the lifetime of the program.""" |
| 74 | + |
| 75 | + # Setup the picolog cm3 here (the parameters passed are also the default) |
| 76 | + self.meter = PicoCM3(sample_frequency = 1000, # Sample the CM3 every second |
| 77 | + mains_setting = 0, # Account for 50hz mains frequency |
| 78 | + channel_settings = { # Which channels are enabled in what mode |
| 79 | + CM3Channels.PLCM3_CHANNEL_1.value: CM3DataTypes.PLCM3_1_MILLIVOLT.value, |
| 80 | + CM3Channels.PLCM3_CHANNEL_2.value: CM3DataTypes.PLCM3_OFF.value, |
| 81 | + CM3Channels.PLCM3_CHANNEL_3.value: CM3DataTypes.PLCM3_OFF.value}) |
| 82 | + # Open the device |
| 83 | + self.meter.open_device() |
| 84 | + |
| 85 | + def before_run(self) -> None: |
| 86 | + """Perform any activity required before starting a run. |
| 87 | + No context is available here as the run is not yet active (BEFORE RUN)""" |
| 88 | + pass |
| 89 | + |
| 90 | + def start_run(self, context: RunnerContext) -> None: |
| 91 | + """Perform any activity required for starting the run here. |
| 92 | + For example, starting the target system to measure. |
| 93 | + Activities after starting the run should also be performed here.""" |
| 94 | + |
| 95 | + num_workers = context.run_variation['num_workers'] |
| 96 | + write_size = context.run_variation['write_size'] |
| 97 | + |
| 98 | + # Start stress-ng |
| 99 | + stress_cmd = f"sudo stress-ng \ |
| 100 | + --hdd {num_workers} \ |
| 101 | + --hdd-write-size {write_size} \ |
| 102 | + --hdd-ops 1000000 \ |
| 103 | + --hdd-dev /dev/sda1 \ |
| 104 | + --timeout 60s \ |
| 105 | + --metrics-brief" |
| 106 | + |
| 107 | + stress_log = open(f'{context.run_dir}/stress-ng.log', 'w') |
| 108 | + self.stress_ng = subprocess.Popen(shlex.split(stress_cmd), stdout=stress_log) |
| 109 | + |
| 110 | + def start_measurement(self, context: RunnerContext) -> None: |
| 111 | + """Perform any activity required for starting measurements.""" |
| 112 | + |
| 113 | + # Start the picologs measurements here, create a unique log file for each (or pass the values through a variable) |
| 114 | + self.latest_log = str(context.run_dir.resolve() / 'picocm3.log') |
| 115 | + self.meter.log(finished_fn=lambda: self.stress_ng.poll() == None, logfile=self.latest_log) |
| 116 | + |
| 117 | + def interact(self, context: RunnerContext) -> None: |
| 118 | + """Perform any interaction with the running target system here, or block here until the target finishes.""" |
| 119 | + |
| 120 | + # Wait for stress-ng to finish or time.sleep(60) |
| 121 | + self.stress_ng.wait() |
| 122 | + |
| 123 | + def stop_measurement(self, context: RunnerContext) -> None: |
| 124 | + """Perform any activity here required for stopping measurements.""" |
| 125 | + |
| 126 | + # Wait for stress-ng to finish |
| 127 | + self.stress_ng.wait() |
| 128 | + |
| 129 | + def stop_run(self, context: RunnerContext) -> None: |
| 130 | + """Perform any activity here required for stopping the run. |
| 131 | + Activities after stopping the run should also be performed here.""" |
| 132 | + pass |
| 133 | + |
| 134 | + def populate_run_data(self, context: RunnerContext) -> Optional[Dict[str, Any]]: |
| 135 | + """Parse and process any measurement data here. |
| 136 | + You can also store the raw measurement data under `context.run_dir` |
| 137 | + Returns a dictionary with keys `self.run_table_model.data_columns` and their values populated""" |
| 138 | + |
| 139 | + if self.latest_log == None: |
| 140 | + return {} |
| 141 | + |
| 142 | + # Read data from the relavent CM3 log |
| 143 | + log_data = self.meter.parse_log(self.latest_log) |
| 144 | + |
| 145 | + return {'timestamp': log_data['timestamp'][0] + " - " + log_data['timestamp'][-1], |
| 146 | + 'channel_1(avg)': mean(log_data['channel_1']), |
| 147 | + 'channel_2(off)': mean(log_data['channel_2']), |
| 148 | + 'channel_3(off)': mean(log_data['channel_3'])} |
| 149 | + |
| 150 | + def after_experiment(self) -> None: |
| 151 | + """Perform any activity required after stopping the experiment here |
| 152 | + Invoked only once during the lifetime of the program.""" |
| 153 | + |
| 154 | + # This must always be run |
| 155 | + self.meter.close_device() |
| 156 | + |
| 157 | + # ================================ DO NOT ALTER BELOW THIS LINE ================================ |
| 158 | + experiment_path: Path = None |
0 commit comments