MetroCollect  2.3.4
MetricsController.h
Go to the documentation of this file.
1 //
2 // MetricsController.cc
3 //
4 // Created on July 24th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <chrono>
24 #include <memory>
25 #include <vector>
26 
28 
29 #include "MetricsDataArray.h"
30 #include "MetricsDiffArray.h"
31 #include "MetricsStatsArray.h"
32 
33 using namespace std::literals;
34 
35 
36 namespace MetroCollect {
37  class MetricsControllerDelegate;
38 
43  public:
44  static constexpr bool defaultSendValues = false;
45  static constexpr bool defaultSendStats = false;
46 #if PROFILING
47  static constexpr std::chrono::milliseconds defaultSamplingInterval = 0ms;
48 #else
49  static constexpr std::chrono::milliseconds defaultSamplingInterval = 100ms;
50 #endif
51  static constexpr size_t defaultProcessingWindowLength = 10;
52  static constexpr size_t defaultProcessingWindowOverlap = 0;
53  static constexpr bool defaultConvertToUnitsPerSecond = true;
54 
63 
70  template<typename Function>
71  void forEach(Function func) {
72  func(minIndexes, Statistics::StatsIndexMin);
73  func(maxIndexes, Statistics::StatsIndexMax);
74  func(sum, Statistics::StatsIndexIntermediateSum);
75  func(sumSquared, Statistics::StatsIndexIntermediateSumSquared);
76  }
77 
84  template<typename Function>
85  void forEach(Function func) const {
86  func(minIndexes, Statistics::StatsIndexMin);
87  func(maxIndexes, Statistics::StatsIndexMax);
88  func(sum, Statistics::StatsIndexIntermediateSum);
89  func(sumSquared, Statistics::StatsIndexIntermediateSumSquared);
90  }
91  };
92 
96  struct MetricsStats {
101 
108  template<typename Function>
109  void forEach(Function func) {
110  func(min, Statistics::StatsMin);
111  func(max, Statistics::StatsMax);
112  func(average, Statistics::StatsAverage);
113  func(stdDev, Statistics::StatsStdDev);
114  }
115 
122  template<typename Function>
123  void forEach(Function func) const {
124  func(min, Statistics::StatsMin);
125  func(max, Statistics::StatsMax);
126  func(average, Statistics::StatsAverage);
127  func(stdDev, Statistics::StatsStdDev);
128  }
129  };
130 
131  protected:
134  std::vector<size_t> requestedMetrics_;
135  bool sendValues_;
136  bool sendStats_;
137 
139  std::chrono::milliseconds samplingInterval_;
142 
145 
146  std::unique_ptr<MetricsDataArray> currentMetrics_;
147  std::unique_ptr<MetricsDataArray> previousMetrics_;
148 
152 
153  void updateMetrics();
154  void updateIterativeStats();
155  void computeFinalStats();
156  void resetIterativeStats();
157 
158  public:
164  MetricsController(MetricsControllerDelegate& delegate) noexcept;
165 
166 
170  MetricsControllerDelegate& delegate() const noexcept;
171 
172 
176  bool isCollecting() const noexcept;
177 
181  bool sendValues() const noexcept;
182 
186  bool sendStats() const noexcept;
187 
191  const std::vector<size_t>& requestedMetrics() const noexcept;
192 
196  std::chrono::milliseconds samplingInterval() const noexcept;
197 
201  size_t processingWindowLength() const noexcept;
202 
206  size_t processingWindowOverlap() const noexcept;
207 
211  bool convertToUnitsPerSeconds() const noexcept;
212 
213 
217  void setSendValues(bool sendValues) noexcept;
218 
222  void setSendStats(bool sendStats) noexcept;
223 
227  void setRequestedMetrics(const MetricsArray<Statistics::Stats>& requestedMetrics) noexcept;
228 
232  void setSamplingInterval(std::chrono::milliseconds interval) noexcept;
233 
240  void setProcessingWindow(size_t length, size_t overlap) noexcept;
241 
245  void setConvertToUnitsPerSeconds(bool convertToUnitsPerSecond) noexcept;
246 
247 
251  void collectMetrics();
252 
253 
261  bool isMetricNull(size_t index) const;
262  };
263 
264 
271  public:
280  virtual void metricsContollerCollectedMetricsValues(const MetricsController& metricsController, const MetricsDiffArray& metricsDiff, const MetricsDataArray& previousMetrics, const MetricsDataArray& currentMetrics) = 0;
281 
288  virtual void metricsContollerCollectedMetricsStats(const MetricsController& metricsController, const MetricsController::MetricsStats& metricsStats) = 0;
289 
297  virtual bool metricsContollerShouldStopCollectingMetrics(const MetricsController& metricsController) = 0;
298  };
299 }
MetricsStatsArray< DiffValueType > average
Array of averages.
Struct to store all final metric stats.
std::shared_ptr< std::vector< MetricsSource::Interests > > SourceInterests
Type used to store and share interests of multiple sources.
bool sendStats_
Whether to compute stats and give them to the delegate.
MetricsStatsArray< DiffValueType > sumSquared
Current sum of all values squared.
STL namespace.
std::unique_ptr< MetricsDataArray > currentMetrics_
Pointer to latest metric raw values.
void forEach(Function func) const
Execute a const function for each intermediate stats array.
A circular array container.
Definition: CircularArray.h:42
Generic class to store and manage metrics.
Definition: MetricsArray.h:40
Class holding various statistics-related types and variables.
Definition: Statistics.h:35
std::vector< size_t > requestedMetrics_
Indexes of metrics requested to be collected.
MetricsStats metricsStats_
Final statistics arrays.
bool sendValues_
Whether to give values to the delegate.
MetricsStatsArray< size_t > maxIndexes
Current indexes of maximums.
size_t processingWindowLength_
Length of processing window used to compute statistics.
Struct to store all intermediate metric stats.
MetricsStatsArray< DiffValueType > stdDev
Array of standard deviations.
MetricsControllerDelegate & delegate_
Delegate to alert when something happens.
void forEach(Function func)
Execute a function for each stats array.
MetricsStatsIntermediate metricsStatsIntermediate_
Intermediate statistics arrays.
Class to fetch metrics from the kernel and store them.
double unitsPerSecondFactor_
Factor to convert metric differences to units per second.
std::unique_ptr< MetricsDataArray > previousMetrics_
Pointer to earlier metric raw values.
MetricsStatsArray< DiffValueType > sum
Current sum of all values.
CircularArray::CircularArray< MetricsDiffArray > metricsValues_
CircularArray holding all metric differences, used to compute statistics.
std::chrono::milliseconds samplingInterval_
Metrics sampling interval.
Abstract delegate of MetricsController.
void forEach(Function func)
Execute a function for each intermediate stats array.
MetricsStatsArray< DiffValueType > min
Array of minimums.
void forEach(Function func) const
Execute a const function for each stats array.
Class to control the collection of metrics.
Class to compute and store metric values variations.
MetricsStatsArray< DiffValueType > max
Array of maximums.
MetricsSource::SourceInterests sourceInterests_
Boolean arrays to store source interests.
size_t processingWindowOverlap_
Overlap of processing window used to compute statistics.
bool isCollecting_
Whether the receiver is collecting metrics.
bool convertToUnitsPerSecond_
Whether to convert metric differences to units per second.
MetricsStatsArray< size_t > minIndexes
Current indexes of minimums.