From 1ce7f93e95f122d722ba6ea90a754833fa04bb67 Mon Sep 17 00:00:00 2001 From: Edward Wall Date: Tue, 4 Nov 2025 16:19:28 +0100 Subject: [PATCH] adds a simple rate calculation --- src/asynStreamGeneratorDriver.cpp | 45 ++++++++++++++++++++++++++++++- src/asynStreamGeneratorDriver.h | 9 ------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/asynStreamGeneratorDriver.cpp b/src/asynStreamGeneratorDriver.cpp index 4862233..b7c9880 100644 --- a/src/asynStreamGeneratorDriver.cpp +++ b/src/asynStreamGeneratorDriver.cpp @@ -359,7 +359,13 @@ void asynStreamGeneratorDriver::receiveUDP() { // These messages don't have any obious start or end to synchronise // against... const std::size_t bufferSize = 1500; - char buffer[bufferSize + 1]; // so that \0 can fit + char buffer[bufferSize]; + + // We have 10 mcpdids + uint64_t lastBufferNumber* = new uint64_t[10]; + for (size_t i = 0; i < 10; ++i) { + lastBufferNumber[i] = 0; + } while (true) { @@ -454,6 +460,12 @@ void asynStreamGeneratorDriver::processEvents() { // uint32. It does support int64 though.. so we start with that epicsInt32 *counts = new epicsInt32[this->num_channels]; + size_t countDiffsPtr = 0; + epicsInt32 *rates = new epicsInt32[this->num_channels]; + epicsInt32 *countDiff = new epicsInt32[this->num_channels]; + epicsInt32 *countDiffs = new epicsInt32[this->num_channels * 10]; + epicsTimeStamp lastRateUpdate = epicsTime::getCurrent(); + asynStatus status = asynSuccess; NormalisedEvent *ne; uint64_t newestTimestamp = 0; @@ -473,6 +485,9 @@ void asynStreamGeneratorDriver::processEvents() { // resolution with a uint64_t we will have an overflow after around // 4 years newestTimestamp = std::max(newestTimestamp, ne->timestamp); + + ++countDiff[ne->source == 0 ? ne->pixelId + 1 : 0]; + timeQueue.push(ne); } @@ -580,6 +595,34 @@ void asynStreamGeneratorDriver::processEvents() { delete ne; } } + + // Careful changing any of these magic numbers until I clean this up + // as you might end up calculating the wrong rate + epicsTimeStamp currentTime = epicsTime::getCurrent(); + if (epicsTimeDiffInNS(¤tTime, &lastRateUpdate) > 100'000'000ll) { + lastRateUpdate = currentTime; + + for (size_t i = 0; i <= this->num_channels; ++i) { + countDiffs[i * 10 + countDiffsPtr] = countDiff[i]; + + uint64_t cnt = 0; + for (size_t j = 0; j <= 10; ++j) { + cnt += countDiffs[i * 10 + j]; + } + rates[i] = cnt / 10.; + + countDiff[i] = 0; + } + + countDiffsPtr = (countDiffsPtr + 1) % 10; + + lock(); + for (size_t i = 0; i < num_channels; ++i) { + setIntegerParam(P_Rates[i], rates[i]); + } + callParamCallbacks(); + unlock(); + } } } diff --git a/src/asynStreamGeneratorDriver.h b/src/asynStreamGeneratorDriver.h index 72075e2..0cc6579 100644 --- a/src/asynStreamGeneratorDriver.h +++ b/src/asynStreamGeneratorDriver.h @@ -56,15 +56,6 @@ struct __attribute__((__packed__)) MonitorEvent { /******************************************************************************* * Simplified Event Struct Definition */ -struct __attribute__((__packed__)) NormalisedMonitorEvent { - uint64_t TimeStamp; - uint8_t DataID : 4; -}; - -struct __attribute__((__packed__)) NormalisedDetectorEvent { - uint64_t TimeStamp; - uint32_t PixID; -}; struct __attribute__((__packed__)) NormalisedEvent { uint64_t timestamp;