From 81082d00444d3bfd644f2417ef5c1a34d9569a28 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Fri, 16 Oct 2020 13:58:20 +0200 Subject: Code aus Lennarts BA-repo --- lib/lennart/SigrokInterface.py | 165 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 lib/lennart/SigrokInterface.py (limited to 'lib/lennart/SigrokInterface.py') diff --git a/lib/lennart/SigrokInterface.py b/lib/lennart/SigrokInterface.py new file mode 100644 index 0000000..555a25f --- /dev/null +++ b/lib/lennart/SigrokInterface.py @@ -0,0 +1,165 @@ +import json + +from dfatool.lennart.DataInterface import DataInterface + + +# Adding additional parsing functionality +class SigrokResult: + def __init__(self, timestamps, onbeforefirstchange): + """ + Creates SigrokResult object, struct for timestamps and onBeforeFirstChange. + + :param timestamps: list of changing timestamps + :param onbeforefirstchange: if the state before the first change is already on / should always be off, just to be data correct + """ + self.timestamps = timestamps + self.onBeforeFirstChange = onbeforefirstchange + + def __str__(self): + """ + :return: string representation of object + """ + return "" % ( + self.onBeforeFirstChange, + self.timestamps, + ) + + def getDict(self): + """ + :return: dict representation of object + """ + data = { + "onBeforeFirstChange": self.onBeforeFirstChange, + "timestamps": self.timestamps, + } + return data + + @classmethod + def fromFile(cls, path): + """ + Generates SigrokResult from json_file + + :param path: file path + :return: SigrokResult object + """ + with open(path) as json_file: + data = json.load(json_file) + return SigrokResult(data["timestamps"], data["onBeforeFirstChange"]) + pass + + @classmethod + def fromString(cls, string): + """ + Generates SigrokResult from string + + :param string: string + :return: SigrokResult object + """ + data = json.loads(string) + return SigrokResult(data["timestamps"], data["onBeforeFirstChange"]) + pass + + @classmethod + def fromTraces(cls, traces): + """ + Generates SigrokResult from ptalog.json traces + + :param traces: traces from dfatool ptalog.json + :return: SigrokResult object + """ + timestamps = [0] + for tr in traces: + for t in tr["trace"]: + # print(t['online_aggregates']['duration'][0]) + timestamps.append( + timestamps[-1] + (t["online_aggregates"]["duration"][0] * 10 ** -6) + ) + + # print(timestamps) + # prepend FAKE Sync point + t_neu = [0.0, 0.0000001, 1.0, 1.00000001] + for i, x in enumerate(timestamps): + t_neu.append( + round(float(x) + t_neu[3] + 0.20, 6) + ) # list(map(float, t_ist.split(",")[:i+1])) + + # append FAKE Sync point / eine überschneidung + # [30.403632, 30.403639, 31.407265, 31.407271] + # appendData = [29.144855,30.148495,30.148502,30.403632,30.403639,31.407265,31.407271,] + appendData = [0, 1.000001, 1.000002, 1.25, 1.2500001] + + # TODO future work here, why does the sync not work completely + t_neu[-1] = ( + t_neu[-2] + (t_neu[-1] - t_neu[-2]) * 0.9 + ) # Weird offset failure with UART stuff + + offset = t_neu[-1] - appendData[0] + for x in appendData: + t_neu.append(x + offset) + + # print(t_neu) + print(len(t_neu)) + return SigrokResult(t_neu, False) + + +class SigrokInterface(DataInterface): + def __init__( + self, sample_rate, sample_count, driver="fx2lafw", filename="temp/sigrok.log" + ): + """ + + :param sample_rate: Samplerate of the Logic Analyzer + :param sample_count: Count of samples + :param driver: for many Logic Analyzer from Saleae the "fx2lafw" should be working + :param filename: temporary file name + """ + # options + self.sample_count = sample_count + self.sample_rate = sample_rate + self.file = open(filename, "w+") + self.driver = driver + + # internal data + self.changes = [] + self.start = None + self.last_val = None + self.index = 0 + + def runMeasure(self): + """ + Not implemented because implemented in subclasses + :return: None + """ + raise NotImplementedError("The method not implemented") + + def forceStopMeasure(self): + """ + Not implemented because implemented in subclasses + :return: None + """ + raise NotImplementedError("The method not implemented") + + def getData(self): + """ + + :return: + """ + # return sigrok_energy_api_result(self.changes, True if self.start == 0xff else False) + return SigrokResult( + [x / self.sample_rate for x in self.changes], + True if self.start == 0xFF else False, + ) + + def analyzeData(self, byte): + """ + analyze one byte if it differs from the last byte, it will be appended to changes. + + :param byte: one byte to analyze + """ + if self.start is None: + self.start = byte + self.last_val = byte + if byte != self.last_val: + self.changes.append(self.index) + self.last_val = byte + self.index += 1 -- cgit v1.2.3 From 675032e99a02a1d1157305eb4e200b373a196f2a Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 19 Oct 2020 11:47:04 +0200 Subject: Sigrok: Continuous measurement; fix sigrok-cli termination --- lib/lennart/SigrokAPIInterface.py | 9 ++------- lib/lennart/SigrokCLIInterface.py | 36 ++++++++++++++++++++++-------------- lib/lennart/SigrokInterface.py | 6 +----- lib/runner.py | 4 +--- 4 files changed, 26 insertions(+), 29 deletions(-) (limited to 'lib/lennart/SigrokInterface.py') diff --git a/lib/lennart/SigrokAPIInterface.py b/lib/lennart/SigrokAPIInterface.py index a8765ba..a2c087a 100644 --- a/lib/lennart/SigrokAPIInterface.py +++ b/lib/lennart/SigrokAPIInterface.py @@ -51,7 +51,6 @@ class SigrokAPIInterface(SigrokInterface): self, driver="fx2lafw", sample_rate=100_000, - sample_count=1_000_000, debug_output=False, used_datafeed=datafeed_changes, fake=False, @@ -60,12 +59,11 @@ class SigrokAPIInterface(SigrokInterface): :param driver: Driver that should be used :param sample_rate: The sample rate of the Logic analyzer - :param sample_count: The sample count of the Logic analyzer :param debug_output: Should be true if output should be displayed to user :param used_datafeed: one of the datafeeds above, user later as callback. :param fake: """ - super(SigrokAPIInterface, self).__init__(sample_rate, sample_count) + super(SigrokAPIInterface, self).__init__(sample_rate) if fake: raise NotImplementedError("Not implemented!") self.used_datafeed = used_datafeed @@ -98,7 +96,6 @@ class SigrokAPIInterface(SigrokInterface): ) sigrokDevice.open() - sigrokDevice.config_set(ConfigKey.LIMIT_SAMPLES, self.sample_count) sigrokDevice.config_set(ConfigKey.SAMPLERATE, self.sample_rate) enabled_channels = ["D1"] @@ -124,9 +121,7 @@ class SigrokAPIInterface(SigrokInterface): print( "Used time: ", total_time * 1_000_000, - "µs | sample/s: ", - self.sample_count / (total_time), - "Hz ", + "µs", ) self.session.stop() diff --git a/lib/lennart/SigrokCLIInterface.py b/lib/lennart/SigrokCLIInterface.py index 873b226..d7347ca 100644 --- a/lib/lennart/SigrokCLIInterface.py +++ b/lib/lennart/SigrokCLIInterface.py @@ -9,7 +9,6 @@ class SigrokCLIInterface(SigrokInterface): self, bin_temp_file="temp/out.bin", sample_rate=100_000, - sample_count=1_000_000, fake=False, ): """ @@ -17,10 +16,9 @@ class SigrokCLIInterface(SigrokInterface): :param bin_temp_file: temporary file for binary output :param sample_rate: The sample rate of the Logic analyzer - :param sample_count: The sample count of the Logic analyzer :param fake: if it should use existing data """ - super(SigrokCLIInterface, self).__init__(sample_rate, sample_count) + super(SigrokCLIInterface, self).__init__(sample_rate) self.fake = fake self.bin_temp_file = bin_temp_file self.sigrok_cli_thread = None @@ -30,13 +28,15 @@ class SigrokCLIInterface(SigrokInterface): Force stopping measure, sometimes needs pkill for killing definitly :return: None """ - self.sigrok_cli_thread.send_signal(subprocess.signal.SIGKILL) - stdout, stderr = self.sigrok_cli_thread.communicate(timeout=15) - time.sleep(5) - # TODO not nice solution, make better - import os + self.sigrok_cli_thread.terminate() - os.system("pkill -f sigrok") + try: + self.sigrok_cli_thread.wait(timeout=10) + except subprocess.TimeoutExpired: + logger.warning("sigrok-cli has not stopped. Killing it.") + self.sigrok_cli_thread.kill() + + self.sigrok_cli_thread.communicate() self.runOpenAnalyze() def runMeasure(self): @@ -51,11 +51,19 @@ class SigrokCLIInterface(SigrokInterface): """ starts the measurement, not waiting for done """ - shellcommand = ( - 'sigrok-cli --output-file %s --output-format binary --samples %s -d %s --config "samplerate=%s Hz"' - % (self.bin_temp_file, self.sample_count, self.driver, self.sample_rate) - ) - self.sigrok_cli_thread = subprocess.Popen(shellcommand, shell=True) + shellcommand = [ + "sigrok-cli", + "--output-file", + self.bin_temp_file, + "--output-format", + "binary", + "--continuous", + "-d", + self.driver, + "--config", + f"samplerate={self.sample_rate} Hz", + ] + self.sigrok_cli_thread = subprocess.Popen(shellcommand) def waitForAsynchronousMeasure(self): """ diff --git a/lib/lennart/SigrokInterface.py b/lib/lennart/SigrokInterface.py index 555a25f..a5eaffc 100644 --- a/lib/lennart/SigrokInterface.py +++ b/lib/lennart/SigrokInterface.py @@ -103,18 +103,14 @@ class SigrokResult: class SigrokInterface(DataInterface): - def __init__( - self, sample_rate, sample_count, driver="fx2lafw", filename="temp/sigrok.log" - ): + def __init__(self, sample_rate, driver="fx2lafw", filename="temp/sigrok.log"): """ :param sample_rate: Samplerate of the Logic Analyzer - :param sample_count: Count of samples :param driver: for many Logic Analyzer from Saleae the "fx2lafw" should be working :param filename: temporary file name """ # options - self.sample_count = sample_count self.sample_rate = sample_rate self.file = open(filename, "w+") self.driver = driver diff --git a/lib/runner.py b/lib/runner.py index 0f44e97..72d222d 100644 --- a/lib/runner.py +++ b/lib/runner.py @@ -189,14 +189,12 @@ class EnergyTraceLogicAnalyzerMonitor(EnergyTraceMonitor): def __init__(self, port: str, baud: int, callback=None, voltage=3.3): super().__init__(port=port, baud=baud, callback=callback, voltage=voltage) - # TODO Max length - options = {"length": 90, "fake": False, "sample_rate": 1_000_000} + options = {"fake": False, "sample_rate": 1_000_000} self.log_file = "logic_output_log_%s.json" % (time.strftime("%Y%m%d-%H%M%S")) # Initialization of Interfaces self.sig = SigrokCLIInterface( sample_rate=options["sample_rate"], - sample_count=options["length"] * options["sample_rate"], fake=options["fake"], ) -- cgit v1.2.3 From f308a519edecd8ee92f2fe18552620a569f48d3b Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 19 Oct 2020 11:47:53 +0200 Subject: debug log --- lib/lennart/DataProcessor.py | 17 ++++++++++++++--- lib/lennart/EnergyInterface.py | 3 +++ lib/lennart/SigrokAPIInterface.py | 3 +++ lib/lennart/SigrokCLIInterface.py | 3 +++ lib/lennart/SigrokInterface.py | 3 +++ 5 files changed, 26 insertions(+), 3 deletions(-) (limited to 'lib/lennart/SigrokInterface.py') diff --git a/lib/lennart/DataProcessor.py b/lib/lennart/DataProcessor.py index df3f41c..58cc705 100644 --- a/lib/lennart/DataProcessor.py +++ b/lib/lennart/DataProcessor.py @@ -1,3 +1,8 @@ +import logging + +logger = logging.getLogger(__name__) + + class DataProcessor: def __init__(self, sync_data, energy_data): """ @@ -84,7 +89,7 @@ class DataProcessor: (sync_start / 1_000_000, pre_outliers_ts / 1_000_000) ) - # print("SYNC SPOTS: ", datasync_timestamps) + logger.debug(f"Synchronization areas: {datasync_timestamps}") # print(time_stamp_data[2]) start_offset = datasync_timestamps[0][1] - time_stamp_data[2] @@ -92,9 +97,15 @@ class DataProcessor: end_offset = datasync_timestamps[-2][0] - (time_stamp_data[-8] + start_offset) end_timestamp = datasync_timestamps[-2][0] - print(start_timestamp, end_timestamp) + logger.debug( + f"Measurement area: LA timestamp range [{start_timestamp}, {end_timestamp}]" + ) + logger.debug(f"Start/End offsets: {start_offset} / {end_offset}") - # print(start_offset, start_timestamp, end_offset, end_timestamp) + if end_offset > 10: + logger.warning( + f"synchronization end_offset == {end_offset}. It should be no more than a few seconds." + ) with_offset = self.addOffset(time_stamp_data, start_offset) diff --git a/lib/lennart/EnergyInterface.py b/lib/lennart/EnergyInterface.py index 2b23667..19aae84 100644 --- a/lib/lennart/EnergyInterface.py +++ b/lib/lennart/EnergyInterface.py @@ -2,6 +2,9 @@ import re import subprocess from dfatool.lennart.DataInterface import DataInterface +import logging + +logger = logging.getLogger(__name__) class EnergyInterface(DataInterface): diff --git a/lib/lennart/SigrokAPIInterface.py b/lib/lennart/SigrokAPIInterface.py index a2c087a..44da678 100644 --- a/lib/lennart/SigrokAPIInterface.py +++ b/lib/lennart/SigrokAPIInterface.py @@ -6,6 +6,9 @@ import sigrok.core as sr from sigrok.core.classes import * from util.ByteHelper import ByteHelper +import logging + +logger = logging.getLogger(__name__) class SigrokAPIInterface(SigrokInterface): diff --git a/lib/lennart/SigrokCLIInterface.py b/lib/lennart/SigrokCLIInterface.py index d7347ca..b28a8a9 100644 --- a/lib/lennart/SigrokCLIInterface.py +++ b/lib/lennart/SigrokCLIInterface.py @@ -2,6 +2,9 @@ import subprocess import time from dfatool.lennart.SigrokInterface import SigrokInterface +import logging + +logger = logging.getLogger(__name__) class SigrokCLIInterface(SigrokInterface): diff --git a/lib/lennart/SigrokInterface.py b/lib/lennart/SigrokInterface.py index a5eaffc..1733b68 100644 --- a/lib/lennart/SigrokInterface.py +++ b/lib/lennart/SigrokInterface.py @@ -1,6 +1,9 @@ import json from dfatool.lennart.DataInterface import DataInterface +import logging + +logger = logging.getLogger(__name__) # Adding additional parsing functionality -- cgit v1.2.3 From b7e25c6ca7746e86ef201b9be7ecec57bf5b2d2a Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 22 Oct 2020 15:05:09 +0200 Subject: Improve sync=la timing restoration. There's still something fishy though... --- bin/generate-dfa-benchmark.py | 1 + lib/harness.py | 23 ++++++++++++++-------- lib/lennart/DataProcessor.py | 5 +++-- lib/lennart/SigrokInterface.py | 43 +----------------------------------------- lib/loader.py | 24 ++++++++++++++++++++++- 5 files changed, 43 insertions(+), 53 deletions(-) (limited to 'lib/lennart/SigrokInterface.py') diff --git a/bin/generate-dfa-benchmark.py b/bin/generate-dfa-benchmark.py index c8681c5..98c3602 100755 --- a/bin/generate-dfa-benchmark.py +++ b/bin/generate-dfa-benchmark.py @@ -647,6 +647,7 @@ if __name__ == "__main__": log_return_values=need_return_values, repeat=1, energytrace_sync=energytrace_sync, + remove_nop_from_timings=False, # kein einfluss auf ungenauigkeiten ) elif "timing" in opt: harness = OnboardTimerHarness( diff --git a/lib/harness.py b/lib/harness.py index 51013e1..04b14eb 100644 --- a/lib/harness.py +++ b/lib/harness.py @@ -355,10 +355,14 @@ class OnboardTimerHarness(TransitionHarness): the dict `offline_aggregates` with the member `duration`. It contains a list of durations (in us) of the corresponding state/transition for each benchmark iteration. I.e. `.traces[*]['trace'][*]['offline_aggregates']['duration'] = [..., ...]` + :param remove_nop_from_timings: If true, remove the nop duration from reported timings + (i.e., reported timings reflect the estimated transition/state duration with the timer call overhea dremoved). + If false, do not remove nop durations, so the timings more accurately reflect the elapsed wall-clock time during the benchmark. """ - def __init__(self, counter_limits, **kwargs): + def __init__(self, counter_limits, remove_nop_from_timings=True, **kwargs): super().__init__(**kwargs) + self.remove_nop_from_timings = remove_nop_from_timings self.trace_length = 0 ( self.one_cycle_in_us, @@ -422,7 +426,6 @@ class OnboardTimerHarness(TransitionHarness): gpio.led_toggle(1); ptalog.stopTransition(); // ======================= LED SYNC ================================ - arch.sleep_ms(250); }\n\n""" return ret @@ -431,14 +434,17 @@ class OnboardTimerHarness(TransitionHarness): if self.energytrace_sync == "led": ret += "runLASync();\n" ret += "ptalog.passNop();\n" + if self.energytrace_sync == "led": + ret += "arch.sleep_ms(250);\n" ret += super().start_benchmark(benchmark_id) return ret def stop_benchmark(self): ret = "" + ret += super().stop_benchmark() if self.energytrace_sync == "led": ret += "runLASync();\n" - ret += super().stop_benchmark() + ret += "arch.sleep_ms(250);\n" return ret def pass_transition( @@ -498,8 +504,9 @@ class OnboardTimerHarness(TransitionHarness): prev_state_duration_us = ( prev_state_cycles * self.one_cycle_in_us + prev_state_overflow * self.one_overflow_in_us - - self.nop_cycles * self.one_cycle_in_us ) + if self.remove_nop_from_timings: + prev_state_duration_us -= self.nop_cycles * self.one_cycle_in_us final_state = self.traces[self.trace_id]["trace"][-1] if "offline_aggregates" not in final_state: final_state["offline_aggregates"] = {"duration": list()} @@ -561,15 +568,15 @@ class OnboardTimerHarness(TransitionHarness): ) ) duration_us = ( - cycles * self.one_cycle_in_us - + overflow * self.one_overflow_in_us - - self.nop_cycles * self.one_cycle_in_us + cycles * self.one_cycle_in_us + overflow * self.one_overflow_in_us ) prev_state_duration_us = ( prev_state_cycles * self.one_cycle_in_us + prev_state_overflow * self.one_overflow_in_us - - self.nop_cycles * self.one_cycle_in_us ) + if self.remove_nop_from_timings: + duration_us -= self.nop_cycles * self.one_cycle_in_us + prev_state_duration_us -= self.nop_cycles * self.one_cycle_in_us if duration_us < 0: duration_us = 0 # self.traces contains transitions and states, UART output only contains transitions -> use index * 2 diff --git a/lib/lennart/DataProcessor.py b/lib/lennart/DataProcessor.py index 8d762e7..90cc54d 100644 --- a/lib/lennart/DataProcessor.py +++ b/lib/lennart/DataProcessor.py @@ -228,9 +228,10 @@ class DataProcessor: def getDataText(x): # print(x) + dl = len(annotateData) for i, xt in enumerate(self.modified_timestamps): - if xt > x: - return "Value: %s" % annotateData[i - 5] + if xt > x and i >= 4 and i - 5 < dl: + return f"SoT: {annotateData[i - 5]}" def update_annot(x, y, name): annot.xy = (x, y) diff --git a/lib/lennart/SigrokInterface.py b/lib/lennart/SigrokInterface.py index 1733b68..32e8fe2 100644 --- a/lib/lennart/SigrokInterface.py +++ b/lib/lennart/SigrokInterface.py @@ -1,4 +1,5 @@ import json +import numpy as np from dfatool.lennart.DataInterface import DataInterface import logging @@ -62,48 +63,6 @@ class SigrokResult: return SigrokResult(data["timestamps"], data["onBeforeFirstChange"]) pass - @classmethod - def fromTraces(cls, traces): - """ - Generates SigrokResult from ptalog.json traces - - :param traces: traces from dfatool ptalog.json - :return: SigrokResult object - """ - timestamps = [0] - for tr in traces: - for t in tr["trace"]: - # print(t['online_aggregates']['duration'][0]) - timestamps.append( - timestamps[-1] + (t["online_aggregates"]["duration"][0] * 10 ** -6) - ) - - # print(timestamps) - # prepend FAKE Sync point - t_neu = [0.0, 0.0000001, 1.0, 1.00000001] - for i, x in enumerate(timestamps): - t_neu.append( - round(float(x) + t_neu[3] + 0.20, 6) - ) # list(map(float, t_ist.split(",")[:i+1])) - - # append FAKE Sync point / eine überschneidung - # [30.403632, 30.403639, 31.407265, 31.407271] - # appendData = [29.144855,30.148495,30.148502,30.403632,30.403639,31.407265,31.407271,] - appendData = [0, 1.000001, 1.000002, 1.25, 1.2500001] - - # TODO future work here, why does the sync not work completely - t_neu[-1] = ( - t_neu[-2] + (t_neu[-1] - t_neu[-2]) * 0.9 - ) # Weird offset failure with UART stuff - - offset = t_neu[-1] - appendData[0] - for x in appendData: - t_neu.append(x + offset) - - # print(t_neu) - print(len(t_neu)) - return SigrokResult(t_neu, False) - class SigrokInterface(DataInterface): def __init__(self, sample_rate, driver="fx2lafw", filename="temp/sigrok.log"): diff --git a/lib/loader.py b/lib/loader.py index 3f6b5ec..2f8e603 100644 --- a/lib/loader.py +++ b/lib/loader.py @@ -1780,9 +1780,31 @@ class EnergyTraceWithTimer(EnergyTraceWithLogicAnalyzer): pass def analyze_states(self, traces, offline_index: int): + + # Start "Synchronization pulse" + timestamps = [0, 10, 1e6, 1e6 + 10] + + # 250ms zwischen Ende der LASync und Beginn der Messungen + # (wegen sleep(250) in der generierten multipass-runLASync-Funktion) + timestamps.append(timestamps[-1] + 240e3) + for tr in traces: + for t in tr["trace"]: + # print(t['online_aggregates']['duration'][0]) + timestamps.append( + timestamps[-1] + t["online_aggregates"]["duration"][offline_index] + ) + + print(timestamps) + + # Stop "Synchronization pulses". The first one has already started. + timestamps.extend(np.array([10, 1e6, 1e6 + 10]) + timestamps[-1]) + timestamps.extend(np.array([0, 10, 1e6, 1e6 + 10]) + 250e3 + timestamps[-1]) + + timestamps = list(np.array(timestamps) * 1e-6) + from dfatool.lennart.SigrokInterface import SigrokResult - self.sync_data = SigrokResult.fromTraces(traces) + self.sync_data = SigrokResult(timestamps, False) return super().analyze_states(traces, offline_index) -- cgit v1.2.3