1 #include "artdaq-core/Core/MonitoredQuantity.hh"
6 using namespace artdaq;
8 MonitoredQuantity::MonitoredQuantity(
11 : _expectedCalculationInterval(expectedCalculationInterval)
20 boost::mutex::scoped_lock sl(_accumulationMutex);
21 if (_lastCalculationTime <= 0.0)
25 ++_workingSampleCount;
26 _workingValueSum += value;
27 _workingValueSumOfSquares += (value * value);
28 if (value < _workingValueMin) { _workingValueMin = value; }
29 if (value > _workingValueMax) { _workingValueMax = value; }
30 _workingLastSampleValue = value;
51 if (_lastCalculationTime <= 0.0) {
return false; }
52 if (currentTime - _lastCalculationTime < _expectedCalculationInterval)
59 size_t latestSampleCount;
60 double latestValueSum;
61 double latestValueSumOfSquares;
62 double latestValueMin;
63 double latestValueMax;
65 double latestLastLatchedSampleValue;
67 boost::mutex::scoped_lock sl(_accumulationMutex);
68 latestSampleCount = _workingSampleCount;
69 latestValueSum = _workingValueSum;
70 latestValueSumOfSquares = _workingValueSumOfSquares;
71 latestValueMin = _workingValueMin;
72 latestValueMax = _workingValueMax;
73 latestDuration = currentTime - _lastCalculationTime;
74 latestLastLatchedSampleValue = _workingLastSampleValue;
75 _lastCalculationTime = currentTime;
76 _workingSampleCount = 0;
77 _workingValueSum = 0.0;
78 _workingValueSumOfSquares = 0.0;
79 _workingValueMin = INFINITY;
80 _workingValueMax = -INFINITY;
84 boost::mutex::scoped_lock sl(_resultsMutex);
98 _binValueSumOfSquares[_workingBinId] = latestValueSumOfSquares;
99 _binValueMin[_workingBinId] = latestValueMin;
100 _binValueMax[_workingBinId] = latestValueMax;
103 if (latestDuration > 0.0)
117 for (
unsigned int idx = 0; idx < _binCount; ++idx)
135 if (_workingBinId >= _binCount) { _workingBinId = 0; }
152 double sigSquared = squareAvg - avg * avg;
153 if (sigSquared > 0.0)
184 double sigSquared = squareAvg - avg * avg;
185 if (sigSquared > 0.0)
203 void MonitoredQuantity::_reset_accumulators()
205 _lastCalculationTime = 0;
206 _workingSampleCount = 0;
207 _workingValueSum = 0.0;
208 _workingValueSumOfSquares = 0.0;
209 _workingValueMin = INFINITY;
210 _workingValueMax = -INFINITY;
211 _workingLastSampleValue = 0;
214 void MonitoredQuantity::_reset_results()
217 for (
unsigned int idx = 0; idx < _binCount; ++idx)
221 _binValueSumOfSquares[idx] = 0.0;
222 _binValueMin[idx] = INFINITY;
223 _binValueMax[idx] = -INFINITY;
254 boost::mutex::scoped_lock sl(_accumulationMutex);
255 _reset_accumulators();
258 boost::mutex::scoped_lock sl(_resultsMutex);
284 boost::mutex::scoped_lock sl(_resultsMutex);
285 _intervalForRecentStats = interval;
291 _binCount = std::max(1U, static_cast<unsigned int>(std::lround(_intervalForRecentStats / _expectedCalculationInterval)));
295 _binValueSumOfSquares.reserve(_binCount);
296 _binValueMin.reserve(_binCount);
297 _binValueMax.reserve(_binCount);
303 boost::mutex::scoped_lock sl(_accumulationMutex);
304 _reset_accumulators();
316 boost::mutex::scoped_lock sl(_accumulationMutex);
317 if (_workingSampleCount == 0) {
return true; }
319 auto sleepTime =
static_cast<int64_t
>(timeout * 100000.0);
320 for (
auto idx = 0; idx < 10; ++idx)
324 boost::mutex::scoped_lock sl(_accumulationMutex);
325 if (_workingSampleCount == 0) {
return true; }
333 boost::mutex::scoped_lock results(_resultsMutex);
358 unsigned int sourceBinId = _workingBinId;
359 for (
unsigned int idx = 0; idx < _binCount; ++idx)
361 if (sourceBinId >= _binCount) { sourceBinId = 0; }
378 if (gettimeofday(&now,
nullptr) == 0)
381 result +=
static_cast<TIME_POINT_T>(now.tv_usec) / (1000 * 1000);
388 boost::mutex::scoped_lock results(_resultsMutex);
394 boost::mutex::scoped_lock results(_resultsMutex);
400 boost::mutex::scoped_lock results(_resultsMutex);
406 boost::mutex::scoped_lock results(_resultsMutex);
412 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) ...
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.
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) ...
DURATION_T fullDuration
The full duration of sampling.
bool waitUntilAccumulatorsHaveBeenFlushed(DURATION_T timeout) const
Blocks while the MonitoredQuantity is flushed, up to timeout duration.
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.
size_t recentSampleCount
The number of samples in the "recent" time window.
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.
size_t fullSampleCount
The total number of samples represented.
std::vector< size_t > recentBinnedSampleCounts
Sample counts for each instance of calculateStatistics in _intervalForRecentStats (rolling window) ...
double recentValueRMS
The RMS of the 'recent" samples.
double getRecentValueSum() const
Access the sum of the value samples in the "recent" time span.
size_t getFullSampleCount() const
Access the count of samples for the entire history of the MonitoredQuantity.
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.
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.