diff options
Diffstat (limited to 'lib/loader.py')
-rw-r--r-- | lib/loader.py | 312 |
1 files changed, 301 insertions, 11 deletions
diff --git a/lib/loader.py b/lib/loader.py index c35eb4c..531559f 100644 --- a/lib/loader.py +++ b/lib/loader.py @@ -107,7 +107,14 @@ def _preprocess_mimosa(measurement): def _preprocess_etlog(measurement): setup = measurement["setup"] - etlog = EnergyTraceLog( + + energytrace_class = EnergyTraceWithBarcode + if measurement["sync_mode"] == "la": + energytrace_class = EnergyTraceWithLogicAnalyzer + elif measurement["sync_mode"] == "timer": + energytrace_class = EnergyTraceWithTimer + + etlog = energytrace_class( float(setup["voltage"]), int(setup["state_duration"]), measurement["transition_names"], @@ -242,7 +249,7 @@ class RawData: file system, making subsequent loads near-instant. """ - def __init__(self, filenames, with_traces=False): + def __init__(self, filenames, with_traces=False, skip_cache=False): """ Create a new RawData object. @@ -321,7 +328,7 @@ class RawData: self.pta = self.ptalog["pta"] self.set_cache_file() - if not with_traces: + if not with_traces and not skip_cache: self.load_cache() def set_cache_file(self): @@ -406,7 +413,7 @@ class RawData: processed_data["error"] = "; ".join(processed_data["datasource_errors"]) return False - # Note that the low-level parser (EnergyTraceLog) already checks + # Note that the low-level parser (EnergyTraceWithBarcode) already checks # whether the transition count is correct return True @@ -909,6 +916,10 @@ class RawData: new_filenames = list() with tarfile.open(filename) as tf: ptalog = self.ptalog + if "sync" in ptalog["opt"]["energytrace"]: + sync_mode = ptalog["opt"]["energytrace"]["sync"] + else: + sync_mode = "bar" # Benchmark code may be too large to be executed in a single # run, so benchmarks (a benchmark is basically a list of DFA runs) @@ -963,13 +974,16 @@ class RawData: "state_duration": ptalog["opt"]["sleep"], } ) - for repeat_id, etlog_file in enumerate(ptalog["files"][j]): - member = tf.getmember(etlog_file) + for repeat_id, etlog_files in enumerate(ptalog["files"][j]): + members = list(map(tf.getmember, etlog_files)) offline_data.append( { - "content": tf.extractfile(member).read(), + "content": list( + map(lambda f: tf.extractfile(f).read(), members) + ), + "sync_mode": sync_mode, "fileno": j, - "info": member, + "info": members[0], "setup": self.setup_by_fileno[j], "repeat_id": repeat_id, "expected_trace": ptalog["traces"][j], @@ -1161,7 +1175,7 @@ def pta_trace_to_aggregate(traces, ignore_trace_indexes=[]): return by_name, parameter_names, arg_count -class EnergyTraceLog: +class EnergyTraceWithBarcode: """ EnergyTrace log loader for DFA traces. @@ -1184,7 +1198,7 @@ class EnergyTraceLog: with_traces=False, ): """ - Create a new EnergyTraceLog object. + Create a new EnergyTraceWithBarcode object. :param voltage: supply voltage [V], usually 3.3 V :param state_duration: state duration [ms] @@ -1226,7 +1240,7 @@ class EnergyTraceLog: ) return list() - lines = log_data.decode("ascii").split("\n") + lines = log_data[0].decode("ascii").split("\n") data_count = sum(map(lambda x: len(x) > 0 and x[0] != "#", lines)) data_lines = filter(lambda x: len(x) > 0 and x[0] != "#", lines) @@ -1599,6 +1613,282 @@ class EnergyTraceLog: return None, None, None, None +class EnergyTraceWithLogicAnalyzer: + def __init__( + self, + voltage: float, + state_duration: int, + transition_names: list, + with_traces=False, + ): + + """ + Create a new EnergyTraceWithLogicAnalyzer object. + + :param voltage: supply voltage [V], usually 3.3 V + :param state_duration: state duration [ms] + :param transition_names: list of transition names in PTA transition order. + Needed to map barcode synchronization numbers to transitions. + """ + self.voltage = voltage + self.state_duration = state_duration * 1e-3 + self.transition_names = transition_names + self.with_traces = with_traces + self.errors = list() + + def load_data(self, log_data): + from data.timing.SigrokInterface import SigrokResult + from data.energy.EnergyInterface import EnergyInterface + + # Daten laden + self.sync_data = SigrokResult.fromString(log_data[0]) + self.energy_data = EnergyInterface.getDataFromString(str(log_data[1])) + + pass + + def analyze_states(self, traces, offline_index: int): + u""" + Split log data into states and transitions and return duration, energy, and mean power for each element. + + :param traces: expected traces, needed to synchronize with the measurement. + traces is a list of runs, traces[*]['trace'] is a single run + (i.e. a list of states and transitions, starting with a transition + and ending with a state). + :param offline_index: This function uses traces[*]['trace'][*]['online_aggregates']['duration'][offline_index] to find sync codes + + :param charges: raw charges (each element describes the charge in pJ transferred during 10 µs) + :param trigidx: "charges" indexes corresponding to a trigger edge, see `trigger_edges` + :param ua_func: charge(pJ) -> current(µA) function as returned by `calibration_function` + + :returns: returns list of states and transitions, starting with a transition and ending with astate + Each element is a dict containing: + * `isa`: 'state' or 'transition' + * `W_mean`: Mittelwert der Leistungsaufnahme + * `W_std`: Standardabweichung der Leistungsaufnahme + * `s`: Dauer + if isa == 'transition, it also contains: + * `W_mean_delta_prev`: Differenz zwischen W_mean und W_mean des vorherigen Zustands + * `W_mean_delta_next`: Differenz zwischen W_mean und W_mean des Folgezustands + """ + + # Tatsächlich Daten auswerten + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + # remove Dirty Data from previously running program (happens if logic Analyzer Measurement starts earlier than + # the HW Reset from energytrace) + use_data_after_index = 0 + for x in range(1, len(self.sync_data.timestamps)): + if self.sync_data.timestamps[x] - self.sync_data.timestamps[x - 1] > 1.3: + use_data_after_index = x + break + + time_stamp_data = self.sync_data.timestamps[use_data_after_index:] + + start_offset = None + start_timestamp = None + end_offset = None + end_timestamp = None + + last_values = [] # maybe needed to flatten the data + plot_data_x = [] + plot_data_y = [] + last_data = [0, 0, 0, 0] + + power_sync_watt = 0.0162 + + # MAIN ENERGY DATA ITERATION + for energytrace_dataset in self.energy_data: + usedtime = energytrace_dataset[0] - last_data[0] + usedenergy = energytrace_dataset[3] - last_data[3] + power = usedenergy / usedtime * 10 ** -3 # in watts + + if power > 0: + if power > power_sync_watt and start_offset is None: + # automatic START offset calculation + start_offset = energytrace_dataset[0] / 1_000_000 - time_stamp_data[0] + start_timestamp = energytrace_dataset[0] / 1_000_000 + if power > power_sync_watt and start_offset is not None: # TODO BETTER + # automatic END offset calculation (Drift) + end_offset = energytrace_dataset[0] / 1_000_000 - time_stamp_data[-1] - start_offset + end_timestamp = energytrace_dataset[0] / 1_000_000 + if len(last_values) > 10: + last_values.pop(0) + last_values.append(power) + # print("%s \tUsed Energy: %i \tUsed Time: %i \tPower: %f \t l_10: %f" % (now_data[0], usedenergy, usedtime, power, sum(last_values) / len(last_values))) + plot_data_x.append(energytrace_dataset[0] / 1_000_000) + plot_data_y.append(power) + + last_data = energytrace_dataset + + # -------------------- + # add start offset to the data + modified_timestamps_with_offset = [] + for x in time_stamp_data: + if x + start_offset >= 0: + modified_timestamps_with_offset.append(x + start_offset) + + # -------------------- + # Add drift to datapoints + modified_timestamps_with_drift = [] + endFactor = (end_timestamp + end_offset - start_timestamp) / (end_timestamp - start_timestamp) + for x in modified_timestamps_with_offset: + modified_timestamps_with_drift.append(((x - start_timestamp) * endFactor) + start_timestamp) + + self.start_offset = 0 + def getPowerBetween(start, end, base_power=0.001469): + first_index = 0 + all_power = 0 + all_power_count = 0 + for ind in range(self.start_offset, len(plot_data_x)): + first_index = ind + if plot_data_x[ind] > start: + break + + for ind in range(first_index, len(plot_data_x)): + all_power += plot_data_y[ind] + all_power_count += 1 + if plot_data_x[ind] > end: + self.start_offset = ind - 1 + break + return (all_power / all_power_count) - base_power + + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + energy_trace = list() + + expected_transitions = list() + for trace_number, trace in enumerate(traces): + for state_or_transition_number, state_or_transition in enumerate( + trace["trace"] + ): + if state_or_transition["isa"] == "transition": + try: + expected_transitions.append( + ( + state_or_transition["name"], + state_or_transition["online_aggregates"]["duration"][ + offline_index + ] + * 1e-6, + ) + ) + except IndexError: + self.errors.append( + 'Entry #{} ("{}") in trace #{} has no duration entry for offline_index/repeat_id {}'.format( + state_or_transition_number, + state_or_transition["name"], + trace_number, + offline_index, + ) + ) + return energy_trace + + + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + end_transition_ts = None + timestamps_sync_start = 0 + energy_trace_new = list() + + for ts_index in range(0 + timestamps_sync_start, int(len(modified_timestamps_with_drift) / 2)): + start_transition_ts = modified_timestamps_with_drift[ts_index * 2] + if end_transition_ts is not None: + power = getPowerBetween(end_transition_ts, start_transition_ts) + #print("STATE", end_transition_ts * 10 ** 6, start_transition_ts * 10 ** 6, (start_transition_ts - end_transition_ts) * 10 ** 6, power) + if (start_transition_ts - end_transition_ts) * 10 ** 6 > 900_000 and power > power_sync_watt * 0.9 and ts_index > 10: + #remove last transition and stop (upcoming data only sync) + del energy_trace_new[-1] + break + pass + + state = { + "isa": "state", + "W_mean": power, + "W_std": 0.0001, + "s": (start_transition_ts - end_transition_ts), #* 10 ** 6, + } + energy_trace_new.append(state) + + energy_trace_new[-2]["W_mean_delta_next"] = ( + energy_trace_new[-2]["W_mean"] - energy_trace_new[-1]["W_mean"] + ) + + # get energy end_transition_ts + end_transition_ts = modified_timestamps_with_drift[ts_index * 2 + 1] + power = getPowerBetween(start_transition_ts, end_transition_ts) + #print("TRANS", start_transition_ts * 10 ** 6, end_transition_ts * 10 ** 6, (end_transition_ts - start_transition_ts) * 10 ** 6, power) + transition = { + "isa": "transition", + "W_mean": power, + "W_std": 0.0001, + "s": (end_transition_ts - start_transition_ts), #* 10 ** 6, + } + + if (end_transition_ts - start_transition_ts) * 10 ** 6 > 2_000_000: + # TODO Last data set corrupted? HOT FIX!!!!!!!!!!!! REMOVE LATER + #for x in range(4): + # del energy_trace_new[-1] + #break + pass + + energy_trace_new.append(transition) + # print(start_transition_ts, "-", end_transition_ts, "-", end_transition_ts - start_transition_ts) + + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + energy_trace_new = energy_trace_new[4:] + + + + for number, item in enumerate(expected_transitions): + name, duration = item + transition = { + "isa": "transition", + "W_mean": max(np.random.normal(0.023, 0.002), 0), + "W_std": 0.0001, + "s": duration, + "name": name, + } + + energy_trace.append(transition) + + if len(energy_trace) > 1: + energy_trace[-1]["W_mean_delta_prev"] = ( + energy_trace[-1]["W_mean"] - energy_trace[-2]["W_mean"] + ) + + state = { + "isa": "state", + "W_mean": max(np.random.normal(0.023, 0.002), 0), + "W_std": 0.0001, + "s": self.state_duration, + "name": "state", + } + + energy_trace.append(state) + + energy_trace[-2]["W_mean_delta_next"] = ( + energy_trace[-2]["W_mean"] - energy_trace[-1]["W_mean"] + ) + + st = "" + for i, x in enumerate(energy_trace_new[-10:]): + st += "(%s|%s|%s)" % (energy_trace[i-10]["name"],x['W_mean'],x['s']) + + #print(st) + print(len(self.sync_data.timestamps), " - ", len(energy_trace_new), " - ", len(energy_trace), " - ", ",".join([str(x["s"]) for x in energy_trace_new[-6:]]), " - ", ",".join([str(x["s"]) for x in energy_trace[-6:]])) + if len(energy_trace_new) < len(energy_trace): + return None + return energy_trace_new + + +class EnergyTraceWithTimer(EnergyTraceWithLogicAnalyzer): + pass + + class MIMOSA: """ MIMOSA log loader for DFA traces with auto-calibration. |