artdaq_core  v3_01_04
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
11 {
16  {
17  typedef double DURATION_T;
18  typedef double TIME_POINT_T;
19 
23  enum class DataSetType
24  {
25  FULL = 0,
26  RECENT = 1
27  };
28 
29  long long fullSampleCount;
30  double fullSampleRate;
31  double fullValueSum;
34  double fullValueRMS;
35  double fullValueMin;
36  double fullValueMax;
37  double fullValueRate;
39 
40  long long recentSampleCount;
42  double recentValueSum;
45  double recentValueRMS;
46  double recentValueMin;
47  double recentValueMax;
48  double recentValueRate;
50 
51  std::vector<long long> recentBinnedSampleCounts;
52  std::vector<double> recentBinnedValueSums;
53  std::vector<DURATION_T> recentBinnedDurations;
54  std::vector<TIME_POINT_T> recentBinnedEndTimes;
55 
56  double lastSampleValue;
57  double lastValueRate;
59  bool enabled;
60 
67 
74 
81 
88 
95 
102 
109 
116 
123 
130  {
131  auto v = getSampleRate(t);
132  return v ? 1e6 / v : INFINITY;
133  }
134 
139  double getLastSampleValue() const { return lastSampleValue; }
140 
145  double getLastValueRate() const { return lastValueRate; }
146 
151  bool isEnabled() const { return enabled; }
152  };
153 
159  {
160  public:
161 
167  explicit MonitoredQuantity
168  (
169  DURATION_T expectedCalculationInterval,
170  DURATION_T timeWindowForRecentResults
171  );
172 
177  void addSample(const double value = 1.0);
178 
183  void addSample(const int value = 1);
184 
189  void addSample(const uint32_t value = 1);
190 
195  void addSample(const uint64_t value = 1);
196 
208  bool calculateStatistics(TIME_POINT_T currentTime =
209  getCurrentTime());
210 
214  void reset();
215 
219  void enable();
220 
224  void disable();
225 
230  bool isEnabled() const { return enabled; }
231 
237 
251  {
252  return _intervalForRecentStats;
253  }
254 
260  {
261  return _expectedCalculationInterval;
262  }
263 
270 
275  void getStats(MonitoredQuantityStats& stats) const;
276 
285  static TIME_POINT_T getCurrentTime();
286 
287  // accessors for particular statistics values (more efficient when
288  // only a single value is needed)
290  DURATION_T getFullDuration() const;
291  double getRecentValueSum() const;
292  double getRecentValueAverage() const;
293  long long getFullSampleCount() const;
294 
295  private:
296 
297  // Prevent copying of the MonitoredQuantity
298  MonitoredQuantity(MonitoredQuantity const&) = delete;
299 
300  MonitoredQuantity& operator=(MonitoredQuantity const&) = delete;
301 
302  // Helper functions.
303  void _reset_accumulators();
304 
305  void _reset_results();
306 
307  TIME_POINT_T _lastCalculationTime;
308  long long _workingSampleCount;
309  double _workingValueSum;
310  double _workingValueSumOfSquares;
311  double _workingValueMin;
312  double _workingValueMax;
313  double _workingLastSampleValue;
314 
315  mutable boost::mutex _accumulationMutex;
316 
317  unsigned int _binCount;
318  unsigned int _workingBinId;
319  std::vector<double> _binValueSumOfSquares;
320  std::vector<double> _binValueMin;
321  std::vector<double> _binValueMax;
322 
323  mutable boost::mutex _resultsMutex;
324 
325  DURATION_T _intervalForRecentStats; // seconds
326  const DURATION_T _expectedCalculationInterval; // seconds
327  };
328 } // namespace artdaq
329 
330 #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.