artdaq_core  v3_06_11
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 <atomic>
7 #include <cmath>
8 #include <cstdint>
9 #include <vector>
10 
11 namespace artdaq {
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  size_t fullSampleCount;
30  double fullSampleRate;
31  double fullValueSum;
34  double fullValueRMS;
35  double fullValueMin;
36  double fullValueMax;
37  double fullValueRate;
39 
42  double recentValueSum;
45  double recentValueRMS;
46  double recentValueMin;
47  double recentValueMax;
48  double recentValueRate;
50 
51  std::vector<size_t> 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 != 0.0 ? 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:
166  explicit MonitoredQuantity(
167  DURATION_T expectedCalculationInterval,
168  DURATION_T timeWindowForRecentResults);
169 
174  void addSample(const double value = 1.0);
175 
180  void addSample(const int value = 1);
181 
186  void addSample(const uint32_t value = 1);
187 
192  void addSample(const uint64_t value = 1);
193 
205  bool calculateStatistics(TIME_POINT_T currentTime =
206  getCurrentTime());
207 
211  void reset();
212 
216  void enable();
217 
221  void disable();
222 
227  bool isEnabled() const { return enabled; }
228 
234 
248  {
249  return _intervalForRecentStats;
250  }
251 
257  {
258  return _expectedCalculationInterval;
259  }
260 
267 
272  void getStats(MonitoredQuantityStats& stats) const;
273 
282  static TIME_POINT_T getCurrentTime();
283 
284  // accessors for particular statistics values (more efficient when
285  // only a single value is needed)
287  DURATION_T getFullDuration() const;
288  double getRecentValueSum() const;
289  double getRecentValueAverage() const;
290  size_t getFullSampleCount() const;
291 
292 private:
293  MonitoredQuantity() = delete;
294 
295  // Prevent copying of the MonitoredQuantity
296  MonitoredQuantity(MonitoredQuantity const&) = delete;
297 
298  MonitoredQuantity& operator=(MonitoredQuantity const&) = delete;
299 
301  MonitoredQuantity& operator=(MonitoredQuantity&&) = delete;
302 
303  // Helper functions.
304  void _reset_accumulators();
305 
306  void _reset_results();
307 
308  std::atomic<TIME_POINT_T> _lastCalculationTime;
309  size_t _workingSampleCount;
310  double _workingValueSum;
311  double _workingValueSumOfSquares;
312  double _workingValueMin;
313  double _workingValueMax;
314  double _workingLastSampleValue;
315 
316  mutable boost::mutex _accumulationMutex;
317 
318  unsigned int _binCount;
319  unsigned int _workingBinId;
320  std::vector<double> _binValueSumOfSquares;
321  std::vector<double> _binValueMin;
322  std::vector<double> _binValueMax;
323 
324  mutable boost::mutex _resultsMutex;
325 
326  DURATION_T _intervalForRecentStats; // seconds
327  const DURATION_T _expectedCalculationInterval; // seconds
328 };
329 } // namespace artdaq
330 
331 #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.