controls/DebugVariablePanel/RingBuffer.py
author Edouard Tisserant <edouard@beremiz.fr>
Mon, 22 Jul 2024 12:12:33 +0200
changeset 3994 c399fe412dbd
parent 3916 6ca1adad3f0e
permissions -rw-r--r--
MQTT: WIP, prevent crash after disconnect. Disable auto-reconnect on connection lost callback.
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     3
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     4
# This file is part of Beremiz
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     5
# Copyright (C) 2021: Edouard TISSERANT
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     6
#
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     7
# See COPYING file for copyrights details.
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     8
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
     9
# Based on Eelco Hoogendoorn stackoverflow answer about RingBuffer with numpy
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    10
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    11
import numpy as np
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    12
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    13
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    14
class RingBuffer(object):
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    15
    def __init__(self, width=None, size=131072, padding=None):
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    16
        self.size = size
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    17
        self.padding = size if padding is None else padding
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    18
        shape = (self.size+self.padding,)
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    19
        if width :
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    20
            shape += (width,)
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    21
        self.buffer = np.zeros(shape)
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    22
        self.cursor = 0
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    23
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    24
    def append(self, data):
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    25
        """this is an O(n) operation"""
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    26
        data = data[-self.size:]
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    27
        n = len(data)
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    28
        if self.size + self.padding - self.cursor < n:
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    29
            self.compact()
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    30
        self.buffer[self.cursor:][:n] = data
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    31
        self.cursor += n
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    32
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    33
    @property
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    34
    def count(self):
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    35
        return min(self.size, self.cursor)
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    36
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    37
    @property
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    38
    def view(self):
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    39
        """this is always an O(1) operation"""
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    40
        return self.buffer[max(0, self.cursor - self.size):][:self.count]
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    41
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    42
    def compact(self):
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    43
        """
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    44
        note: only when this function is called, is an O(size) performance hit incurred,
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    45
        and this cost is amortized over the whole padding space
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    46
        """
2742
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    47
        self.buffer[:self.count] = self.view
5f7445b582d4 IDE: Fixed variable traces graphs RingBuffers. Removed an apparently useless wxCallAfter in trend graph that was leading to pydeadobject exception on wxGTK when double-clicking.
Edouard Tisserant
parents: 2741
diff changeset
    48
        self.cursor -= self.size
2741
3cc5663af196 IDE: Cleaned up some useless tests in variable trace data handling code, changed from bare numpy arrays to RingBuffers inorder to avoid RAM outage and crash after long tracing session.
Edouard Tisserant
parents:
diff changeset
    49