diff --git a/src/asynStreamGeneratorDriver.cpp b/src/asynStreamGeneratorDriver.cpp index f76055e..5ba860b 100644 --- a/src/asynStreamGeneratorDriver.cpp +++ b/src/asynStreamGeneratorDriver.cpp @@ -477,10 +477,13 @@ void asynStreamGeneratorDriver::processEvents() { // uint32. It does support int64 though.. so we start with that epicsInt32 *counts = new epicsInt32[this->num_channels]; + const uint64_t minRateSamplePeriod = 100'000'000ll; + const size_t rateAverageWindow = 20; 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]; + epicsInt32 *countDiffs = new epicsInt32[this->num_channels * rateAverageWindow]; + uint64_t *timeSpans = new uint64_t[this->num_channels]; epicsTimeStamp lastRateUpdate = epicsTime::getCurrent(); asynStatus status = asynSuccess; @@ -613,23 +616,30 @@ void asynStreamGeneratorDriver::processEvents() { // 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) { + if (epicsTimeDiffInNS(¤tTime, &lastRateUpdate) > minRateSamplePeriod) { + timeSpans[countDiffsPtr] = epicsTimeDiffInNS(¤tTime, &lastRateUpdate); + + uint64_t totalTime = 0; + for (size_t i = 0; i <= rateAverageWindow; ++i) { + totalTime += timeSpans[i]; + } + lastRateUpdate = currentTime; for (size_t i = 0; i <= this->num_channels; ++i) { - countDiffs[i * 10 + countDiffsPtr] = countDiff[i]; + countDiffs[i * rateAverageWindow + countDiffsPtr] = countDiff[i]; uint64_t cnt = 0; - for (size_t j = 0; j <= 10; ++j) { - cnt += countDiffs[i * 10 + j]; + for (size_t j = 0; j <= rateAverageWindow; ++j) { + cnt += countDiffs[i * rateAverageWindow + j]; } rates[i] = - cnt; // would / 10 to average than * 10 as want per second + cnt / (totalTime * 1e-9); countDiff[i] = 0; } - countDiffsPtr = (countDiffsPtr + 1) % 10; + countDiffsPtr = (countDiffsPtr + 1) % rateAverageWindow; if (countDiffsPtr % 5 == 0) { lock();