1 #include "artdaq-core/Core/MonitoredQuantity.hh"
6 using namespace artdaq;
13 _expectedCalculationInterval(expectedCalculationInterval)
15 setNewTimeWindowForRecentResults(timeWindowForRecentResults);
22 boost::mutex::scoped_lock sl(_accumulationMutex);
23 if (_lastCalculationTime <= 0.0)
27 ++_workingSampleCount;
28 _workingValueSum += value;
29 _workingValueSumOfSquares += (value * value);
30 if (value < _workingValueMin) { _workingValueMin = value; }
31 if (value > _workingValueMax) { _workingValueMax = value; }
32 _workingLastSampleValue = value;
53 if (_lastCalculationTime <= 0.0) {
return false; }
54 if (currentTime - _lastCalculationTime < _expectedCalculationInterval)
61 long long latestSampleCount;
62 double latestValueSum;
63 double latestValueSumOfSquares;
64 double latestValueMin;
65 double latestValueMax;
67 double latestLastLatchedSampleValue;
69 boost::mutex::scoped_lock sl(_accumulationMutex);
70 latestSampleCount = _workingSampleCount;
71 latestValueSum = _workingValueSum;
72 latestValueSumOfSquares = _workingValueSumOfSquares;
73 latestValueMin = _workingValueMin;
74 latestValueMax = _workingValueMax;
75 latestDuration = currentTime - _lastCalculationTime;
76 latestLastLatchedSampleValue = _workingLastSampleValue;
77 _lastCalculationTime = currentTime;
78 _workingSampleCount = 0;
79 _workingValueSum = 0.0;
80 _workingValueSumOfSquares = 0.0;
81 _workingValueMin = INFINITY;
82 _workingValueMax = -INFINITY;
86 boost::mutex::scoped_lock sl(_resultsMutex);
100 _binValueSumOfSquares[_workingBinId] = latestValueSumOfSquares;
101 _binValueMin[_workingBinId] = latestValueMin;
102 _binValueMax[_workingBinId] = latestValueMax;
105 if (latestDuration > 0.0)
119 for (
unsigned int idx = 0; idx < _binCount; ++idx)
137 if (_workingBinId >= _binCount) { _workingBinId = 0; }
154 double sigSquared = squareAvg - avg * avg;
155 if (sigSquared > 0.0)
186 double sigSquared = squareAvg - avg * avg;
187 if (sigSquared > 0.0)
205 void MonitoredQuantity::_reset_accumulators()
207 _lastCalculationTime = 0;
208 _workingSampleCount = 0;
209 _workingValueSum = 0.0;
210 _workingValueSumOfSquares = 0.0;
211 _workingValueMin = INFINITY;
212 _workingValueMax = -INFINITY;
213 _workingLastSampleValue = 0;
216 void MonitoredQuantity::_reset_results()
219 for (
unsigned int idx = 0; idx < _binCount; ++idx)
223 _binValueSumOfSquares[idx] = 0.0;
224 _binValueMin[idx] = INFINITY;
225 _binValueMax[idx] = -INFINITY;
256 boost::mutex::scoped_lock sl(_accumulationMutex);
257 _reset_accumulators();
260 boost::mutex::scoped_lock sl(_resultsMutex);
286 boost::mutex::scoped_lock sl(_resultsMutex);
287 _intervalForRecentStats = interval;
293 _binCount = std::max(1U,
294 static_cast<unsigned int>(
295 (_intervalForRecentStats / _expectedCalculationInterval) + 0.5
301 _binValueSumOfSquares.reserve(_binCount);
302 _binValueMin.reserve(_binCount);
303 _binValueMax.reserve(_binCount);
309 boost::mutex::scoped_lock sl(_accumulationMutex);
310 _reset_accumulators();
322 boost::mutex::scoped_lock sl(_accumulationMutex);
323 if (_workingSampleCount == 0) {
return true; }
325 auto sleepTime =
static_cast<long>(timeout * 100000.0);
326 for (
auto idx = 0; idx < 10; ++idx)
330 boost::mutex::scoped_lock sl(_accumulationMutex);
331 if (_workingSampleCount == 0) {
return true; }
340 boost::mutex::scoped_lock results(_resultsMutex);
365 unsigned int sourceBinId = _workingBinId;
366 for (
unsigned int idx = 0; idx < _binCount; ++idx)
368 if (sourceBinId >= _binCount) { sourceBinId = 0; }
385 if (gettimeofday(&now, 0) == 0)
388 result +=
static_cast<TIME_POINT_T>(now.tv_usec) / (1000 * 1000);
395 boost::mutex::scoped_lock results(_resultsMutex);
401 boost::mutex::scoped_lock results(_resultsMutex);
407 boost::mutex::scoped_lock results(_resultsMutex);
413 boost::mutex::scoped_lock results(_resultsMutex);
419 boost::mutex::scoped_lock results(_resultsMutex);
bool enabled
Whether the MonitoredQuantity is collecting data.
bool calculateStatistics(TIME_POINT_T currentTime=getCurrentTime())
Forces a calculation of the statistics for the monitored quantity.
std::vector< double > recentBinnedValueSums
Sums for each instance of calculateStatistics in _intervalForRecentStats (rolling window) ...
long long getFullSampleCount() const
Access the count of samples for the entire history of the MonitoredQuantity.
double recentValueAverage
The average of the "recent" samples.
struct containing MonitoredQuantity data
double fullValueMax
The largest value of all sampels.
double recentValueMax
The largest value of the "recent" samples.
double lastSampleValue
Value of the most recent sample.
static TIME_POINT_T getCurrentTime()
Returns the current point in time.
double recentSampleRate
The number of samples in the "recent" time window, divided by the length of that window.
double fullValueAverage
The average of all samples.
long long recentSampleCount
The number of samples in the "recent" time window.
double fullValueSumOfSquares
The sum of the squares of all samples.
std::vector< TIME_POINT_T > recentBinnedEndTimes
Last sample time in each instance of calculateStatistics in _intervalForRecentStats (rolling window) ...
MonitoredQuantity(DURATION_T expectedCalculationInterval, DURATION_T timeWindowForRecentResults)
Instantiates a MonitoredQuantity object.
DURATION_T fullDuration
The full duration of sampling.
bool waitUntilAccumulatorsHaveBeenFlushed(DURATION_T timeout) const
Blocks while the MonitoredQuantity is flushed, up to timeout duration.
long long fullSampleCount
The total number of samples represented.
double DURATION_T
A Duration.
DURATION_T recentDuration
The length of the "recent" time window.
void addSample(const double value=1.0)
Adds the specified doubled valued sample value to the monitor instance.
double TIME_POINT_T
A point in time.
TIME_POINT_T getLastCalculationTime() const
Access the last calculation time.
std::vector< DURATION_T > recentBinnedDurations
Duration between each instance of calcualteStatistics in _intervalForRecentStats (rolling window) ...
double fullValueRate
The sum of all samples over the full duration of sampling.
double lastValueRate
Latest rate point (sum of values over calculateStatistics interval)
TIME_POINT_T lastCalculationTime
Last time calculateStatistics was called.
double recentValueSumOfSquares
The sum of the squares of the "recent" samples.
double recentValueSum
The sum of the "recent" samples.
double recentValueRate
The sum of the "recent" samples, divided by the length of the "recent" time window.
double recentValueMin
The smallest value of the "recent" samples.
double getRecentValueAverage() const
Access the average of the value samples in the "recent" time span.
double recentValueRMS
The RMS of the 'recent" samples.
double getRecentValueSum() const
Access the sum of the value samples in the "recent" time span.
double fullValueMin
The smallest value of all samples.
double fullSampleRate
The total number of samples over the full duration of sampling.
double fullValueRMS
The RMS of all samples.
void getStats(MonitoredQuantityStats &stats) const
Write all our collected statistics into the given Stats struct.
std::vector< long long > recentBinnedSampleCounts
Sample counts for each instance of calculateStatistics in _intervalForRecentStats (rolling window) ...
double fullValueSum
The sum of all samples.
void setNewTimeWindowForRecentResults(DURATION_T interval)
Specifies a new time interval to be used when calculating "recent" statistics.
DURATION_T getFullDuration() const
Access the full duration of the statistics.