controls/DebugVariablePanel/RingBuffer.py
author Edouard Tisserant
Thu, 15 Jul 2021 11:48:02 +0200
branchsvghmi
changeset 3281 1fc4274de64e
parent 3258 5ce56021f166
child 3750 f62625418bff
permissions -rw-r--r--
SVGHMI: Fixed halting problem when there is no session opened.
3257
095c73591b7e 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
095c73591b7e 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 -*-
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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
#
095c73591b7e 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.
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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):
3258
5ce56021f166 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: 3257
diff changeset
    15
    def __init__(self, width=None, size=131072, padding=None):
3257
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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,)
095c73591b7e 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 :
095c73591b7e 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,)
095c73591b7e 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)
3258
5ce56021f166 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: 3257
diff changeset
    22
        self.cursor = 0
3257
095c73591b7e 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
095c73591b7e 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):
095c73591b7e 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"""
3258
5ce56021f166 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: 3257
diff changeset
    26
        data = data[-self.size:]
3257
095c73591b7e 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)
3258
5ce56021f166 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: 3257
diff changeset
    28
        if self.size + self.padding - self.cursor < n:
5ce56021f166 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: 3257
diff changeset
    29
            self.compact()
5ce56021f166 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: 3257
diff changeset
    30
        self.buffer[self.cursor:][:n] = data
5ce56021f166 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: 3257
diff changeset
    31
        self.cursor += n
3257
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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):
3258
5ce56021f166 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: 3257
diff changeset
    35
        return min(self.size, self.cursor)
3257
095c73591b7e 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
095c73591b7e 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
095c73591b7e 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):
095c73591b7e 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"""
3258
5ce56021f166 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: 3257
diff changeset
    40
        return self.buffer[max(0, self.cursor - self.size):][:self.count]
3257
095c73591b7e 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
095c73591b7e 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):
095c73591b7e 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
        """
095c73591b7e 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,
095c73591b7e 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
095c73591b7e 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
        """
095c73591b7e 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
    47
        print 'compacting'
3258
5ce56021f166 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: 3257
diff changeset
    48
        self.buffer[:self.count] = self.view
5ce56021f166 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: 3257
diff changeset
    49
        self.cursor -= self.size
3257
095c73591b7e 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
    50