artdaq_core  v3_06_01
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 <cmath>
7 #include <cstdint>
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  size_t fullSampleCount;
29  double fullSampleRate;
30  double fullValueSum;
33  double fullValueRMS;
34  double fullValueMin;
35  double fullValueMax;
36  double fullValueRate;
38 
41  double recentValueSum;
44  double recentValueRMS;
45  double recentValueMin;
46  double recentValueMax;
47  double recentValueRate;
49 
50  std::vector<size_t> 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 != 0.0 ? 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  size_t getFullSampleCount() const;
290 
291 private:
292  MonitoredQuantity() = delete;
293 
294  // Prevent copying of the MonitoredQuantity
295  MonitoredQuantity(MonitoredQuantity const&) = delete;
296 
297  MonitoredQuantity& operator=(MonitoredQuantity const&) = delete;
298 
300  MonitoredQuantity& operator=(MonitoredQuantity&&) = delete;
301 
302  // Helper functions.
303  void _reset_accumulators();
304 
305  void _reset_results();
306 
307  std::atomic<TIME_POINT_T> _lastCalculationTime;
308  size_t _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.
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.
size_t getSampleCount(DataSetType t=DataSetType::FULL) const
Returns the sample count for the requested interval.
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) ...
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.
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) ...
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.
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.
size_t recentSampleCount
The number of samples in the &quot;recent&quot; time window.
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.
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 &#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.
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.
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.