artdaq_core  v3_05_10
MonitoredQuantity.hh
1 #ifndef artdaq_core_Core_MonitoredQuantity_hh
2 #define artdaq_core_Core_MonitoredQuantity_hh
3 
4 #include <boost/thread/mutex.hpp>
5 
6 #include <math.h>
7 #include <stdint.h>
8 #include <vector>
9 
10 namespace artdaq {
15 {
16  typedef double DURATION_T;
17  typedef double TIME_POINT_T;
18 
22  enum class DataSetType
23  {
24  FULL = 0,
25  RECENT = 1
26  };
27 
28  long long fullSampleCount;
29  double fullSampleRate;
30  double fullValueSum;
33  double fullValueRMS;
34  double fullValueMin;
35  double fullValueMax;
36  double fullValueRate;
38 
39  long long recentSampleCount;
41  double recentValueSum;
44  double recentValueRMS;
45  double recentValueMin;
46  double recentValueMax;
47  double recentValueRate;
49 
50  std::vector<long long> recentBinnedSampleCounts;
51  std::vector<double> recentBinnedValueSums;
52  std::vector<DURATION_T> recentBinnedDurations;
53  std::vector<TIME_POINT_T> recentBinnedEndTimes;
54 
55  double lastSampleValue;
56  double lastValueRate;
58  bool enabled;
59 
66 
73 
80 
87 
94 
101 
108 
115 
122 
129  {
130  auto v = getSampleRate(t);
131  return v ? 1e6 / v : INFINITY;
132  }
133 
138  double getLastSampleValue() const { return lastSampleValue; }
139 
144  double getLastValueRate() const { return lastValueRate; }
145 
150  bool isEnabled() const { return enabled; }
151 };
152 
158 {
159 public:
165  explicit MonitoredQuantity(
166  DURATION_T expectedCalculationInterval,
167  DURATION_T timeWindowForRecentResults);
168 
173  void addSample(const double value = 1.0);
174 
179  void addSample(const int value = 1);
180 
185  void addSample(const uint32_t value = 1);
186 
191  void addSample(const uint64_t value = 1);
192 
204  bool calculateStatistics(TIME_POINT_T currentTime =
205  getCurrentTime());
206 
210  void reset();
211 
215  void enable();
216 
220  void disable();
221 
226  bool isEnabled() const { return enabled; }
227 
233 
247  {
248  return _intervalForRecentStats;
249  }
250 
256  {
257  return _expectedCalculationInterval;
258  }
259 
266 
271  void getStats(MonitoredQuantityStats& stats) const;
272 
281  static TIME_POINT_T getCurrentTime();
282 
283  // accessors for particular statistics values (more efficient when
284  // only a single value is needed)
286  DURATION_T getFullDuration() const;
287  double getRecentValueSum() const;
288  double getRecentValueAverage() const;
289  long long getFullSampleCount() const;
290 
291 private:
292  // Prevent copying of the MonitoredQuantity
293  MonitoredQuantity(MonitoredQuantity const&) = delete;
294 
295  MonitoredQuantity& operator=(MonitoredQuantity const&) = delete;
296 
297  // Helper functions.
298  void _reset_accumulators();
299 
300  void _reset_results();
301 
302  std::atomic<TIME_POINT_T> _lastCalculationTime;
303  long long _workingSampleCount;
304  double _workingValueSum;
305  double _workingValueSumOfSquares;
306  double _workingValueMin;
307  double _workingValueMax;
308  double _workingLastSampleValue;
309 
310  mutable boost::mutex _accumulationMutex;
311 
312  unsigned int _binCount;
313  unsigned int _workingBinId;
314  std::vector<double> _binValueSumOfSquares;
315  std::vector<double> _binValueMin;
316  std::vector<double> _binValueMax;
317 
318  mutable boost::mutex _resultsMutex;
319 
320  DURATION_T _intervalForRecentStats; // seconds
321  const DURATION_T _expectedCalculationInterval; // seconds
322 };
323 } // namespace artdaq
324 
325 #endif /* artdaq_core_Core_MonitoredQuantity_hh */
double getSampleRate(DataSetType t=DataSetType::FULL) const
Returns the sample rate in the requested interval.
long long getSampleCount(DataSetType t=DataSetType::FULL) const
Returns the sample count for the requested interval.
DataSetType
Which data points to return (all or only recent)
double getLastSampleValue() const
Accessor for the last sample value recorded.
bool enabled
Whether the MonitoredQuantity is collecting data.
double getValueRMS(DataSetType t=DataSetType::FULL) const
Returns the RMS of the values in the requested interval.
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 &quot;recent&quot; samples.
struct containing MonitoredQuantity data
double fullValueMax
The largest value of all sampels.
double getLastValueRate() const
Accessor for the lastValueRate (Sum of last samples over interval between calculateStatisics calls) ...
double recentValueMax
The largest value of the &quot;recent&quot; samples.
double lastSampleValue
Value of the most recent sample.
double getValueRate(DataSetType t=DataSetType::FULL) const
Returns the sum of the values in the requested interval, divided by the duration of the requested int...
double getValueAverage(DataSetType t=DataSetType::FULL) const
Returns the average of the values in the requested interval.
This class keeps track of statistics for a set of sample values and provides timing information on th...
bool isEnabled() const
Tests whether the monitor is currently enabled.
double getValueMin(DataSetType t=DataSetType::FULL) const
Returns the smallest of the values in the requested interval.
static TIME_POINT_T getCurrentTime()
Returns the current point in time.
double recentSampleRate
The number of samples in the &quot;recent&quot; time window, divided by the length of that window.
double fullValueAverage
The average of all samples.
double getValueMax(DataSetType t=DataSetType::FULL) const
Returns the largest of the values in the requested interval.
long long recentSampleCount
The number of samples in the &quot;recent&quot; time window.
DURATION_T getDuration(DataSetType t=DataSetType::FULL) const
Returns the duration of the requested interval.
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.
DURATION_T ExpectedCalculationInterval() const
Returns the expected interval between calculateStatistics calls.
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.
DURATION_T recentDuration
The length of the &quot;recent&quot; 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 getSampleLatency(DataSetType t=DataSetType::FULL) const
double fullValueRate
The sum of all samples over the full duration of sampling.
double lastValueRate
Latest rate point (sum of values over calculateStatistics interval)
double getValueSum(DataSetType t=DataSetType::FULL) const
Returns the sum of values in the requested interval.
TIME_POINT_T lastCalculationTime
Last time calculateStatistics was called.
double recentValueSumOfSquares
The sum of the squares of the &quot;recent&quot; samples.
double recentValueSum
The sum of the &quot;recent&quot; samples.
double recentValueRate
The sum of the &quot;recent&quot; samples, divided by the length of the &quot;recent&quot; time window.
double recentValueMin
The smallest value of the &quot;recent&quot; samples.
double getRecentValueAverage() const
Access the average of the value samples in the &quot;recent&quot; time span.
double recentValueRMS
The RMS of the &#39;recent" samples.
DURATION_T getTimeWindowForRecentResults() const
Returns the length of the time window that has been specified for recent results. ...
double getRecentValueSum() const
Access the sum of the value samples in the &quot;recent&quot; 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) ...
bool isEnabled() const
Access the enable flag.
double fullValueSum
The sum of all samples.
void setNewTimeWindowForRecentResults(DURATION_T interval)
Specifies a new time interval to be used when calculating &quot;recent&quot; statistics.
DURATION_T getFullDuration() const
Access the full duration of the statistics.