25 #if GPERFTOOLS_CPU_PROFILE 26 #include <gperftools/profiler.h> 42 currentMetrics_(std::make_unique<MetricsDataArray>(this->sourceInterests_)),
43 previousMetrics_(std::make_unique<MetricsDataArray>(this->sourceInterests_)),
90 this->sendValues_ = sendValues;
94 this->sendStats_ = sendStats;
98 if (this->isCollecting_)
101 std::set<size_t> indexes;
102 std::set<size_t> allIndexes;
103 *this->sourceInterests_ = *requestedMetrics.sourceInterests();
106 for (
size_t j = 0; j < requestedMetrics.size(); j++) {
109 allIndexes.insert(j);
115 this->metricsStatsIntermediate_.minIndexes.setIndexes(indexes);
116 this->metricsStats_.min.setIndexes(indexes);
119 this->metricsStatsIntermediate_.maxIndexes.setIndexes(indexes);
120 this->metricsStats_.max.setIndexes(indexes);
123 this->metricsStats_.average.setIndexes(indexes);
126 this->metricsStats_.stdDev.setIndexes(indexes);
129 this->metricsStatsIntermediate_.sum.setIndexes(indexes);
132 this->metricsStatsIntermediate_.sumSquared.setIndexes(indexes);
137 this->requestedMetrics_.clear();
138 for (
size_t i : allIndexes)
139 this->requestedMetrics_.push_back(i);
143 if (this->isCollecting_)
146 this->samplingInterval_ = interval;
148 this->unitsPerSecondFactor_ = 1000.0 / interval.count();
150 this->unitsPerSecondFactor_ = 1.0;
157 if (length != this->processingWindowLength_) {
158 this->processingWindowLength_ = length;
159 this->metricsValues_.reset(length,
MetricsDiffArray(this->sourceInterests_));
162 if (overlap >= length)
163 overlap = length - 1;
164 if (overlap != this->processingWindowOverlap_)
165 this->processingWindowOverlap_ = overlap;
172 this->convertToUnitsPerSecond_ = convertToUnitsPerSecond;
179 #if GPERFTOOLS_CPU_PROFILE 180 ProfilerStart(
"/tmp/aa.prof");
185 size_t processingWindowIndex = 0;
186 auto startTime = std::chrono::steady_clock::now();
190 std::this_thread::sleep_for(this->
samplingInterval_ - (std::chrono::steady_clock::now() - startTime));
191 startTime = std::chrono::steady_clock::now();
200 processingWindowIndex++;
202 if (cachedSendStats) {
211 #if GPERFTOOLS_CPU_PROFILE 222 #if GPERFTOOLS_CPU_PROFILE 246 if (newPoint[val.index] < this->metricsValues_.atAbsoluteIndex(val.value)[val.index])
247 val.value = newPointAbsoluteIndex;
251 if (newPoint[val.index] > this->metricsValues_.atAbsoluteIndex(val.value)[val.index])
252 val.value = newPointAbsoluteIndex;
256 val.value += newPoint[val.index];
260 val.value += newPoint[val.index] * newPoint[val.index];
271 this->
metricsStats_.
min[val.index] = this->metricsValues_.atAbsoluteIndex(val.value)[val.index];
274 this->
metricsStats_.
max[val.index] = this->metricsValues_.atAbsoluteIndex(val.value)[val.index];
277 this->
metricsStats_.
average[val.index] = val.value / static_cast<double>(this->processingWindowLength_);
292 val.value = firstIndex;
294 val.value = firstIndex;
303 val.value = firstIndex;
305 if (this->
metricsValues_[i][val.index] < this->metricsValues_.atAbsoluteIndex(val.value)[val.index])
306 val.value = this->metricsValues_.absoluteIndex(i);
313 val.value = firstIndex;
315 if (this->
metricsValues_[i][val.index] > this->metricsValues_.atAbsoluteIndex(val.value)[val.index])
316 val.value = this->metricsValues_.absoluteIndex(i);
325 val.value += this->metricsValues_[i][val.index];
331 val.value += this->metricsValues_[i][val.index] * this->metricsValues_[i][val.index];
337 val.value -= this->metricsValues_[i][val.index];
342 val.value -= this->metricsValues_[i][val.index] * this->metricsValues_[i][val.index];
362 for (
size_t i = 0; i <
count; i++) {
static constexpr std::chrono::milliseconds defaultSamplingInterval
Default parameter option.
static constexpr bool defaultConvertToUnitsPerSecond
Default parameter option.
void computeFinalStats()
Compute final stats at the end of processing window.
void updateIterativeStats()
Update iterative stats with new MetricsDiffArray.
MetricsStatsArray< DiffValueType > average
Array of averages.
bool convertToUnitsPerSeconds() const noexcept
Returns whether to convert metric differences to units per second.
StatsIndex
Associates a unique index to each statistic.
std::chrono::milliseconds samplingInterval() const noexcept
Returns the metrics sampling interval.
void setRequestedMetrics(const MetricsArray< Statistics::Stats > &requestedMetrics) noexcept
Sets the list of request metrics.
bool sendStats_
Whether to compute stats and give them to the delegate.
static constexpr size_t defaultProcessingWindowLength
Default parameter option.
std::unique_ptr< MetricsDataArray > currentMetrics_
Pointer to latest metric raw values.
size_t processingWindowLength() const noexcept
Returns the length of the processing window used to compute statistics.
virtual void metricsContollerCollectedMetricsValues(const MetricsController &metricsController, const MetricsDiffArray &metricsDiff, const MetricsDataArray &previousMetrics, const MetricsDataArray ¤tMetrics)=0
Function called every time the MetricsController has collected new values.
MetricsControllerDelegate & delegate() const noexcept
Returns the controller's delegate.
bool sendValues() const noexcept
Returns whether the controller alerts the delegate after collecting new values.
static Stats statsRequiredForStat(Stats stat)
Returns the stats which are required to compute this stat.
Sum of squares of values (intermediate statistic)
Generic class to store and manage metrics.
Stats
Bitwise type te select statistics.
void setSamplingInterval(std::chrono::milliseconds interval) noexcept
Sets the metrics sampling interval.
void setSendStats(bool sendStats) noexcept
Sets whether the controller alerts the delegate after computing statistics.
static constexpr bool defaultSendValues
Default parameter option.
void collectMetrics()
Launch metric collection loop.
void setConvertToUnitsPerSeconds(bool convertToUnitsPerSecond) noexcept
Sets whether to convert metric differences to units per second.
const std::vector< size_t > & requestedMetrics() const noexcept
Returns the list of request metrics.
std::vector< size_t > requestedMetrics_
Indexes of metrics requested to be collected.
MetricsStats metricsStats_
Final statistics arrays.
static constexpr size_t count
Number of available statistics.
static constexpr std::array< Stats, count > allStats
Array of all available statistics.
Sum of value (intermediate statistic)
static constexpr bool defaultSendStats
Default parameter option.
static constexpr size_t extraCount
Number of statistics and intermediate values.
bool sendValues_
Whether to give values to the delegate.
size_t processingWindowOverlap() const noexcept
Returns the overlap of the processing window used to compute statistics.
bool isMetricNull(size_t index) const
Checks wether a metric is zero.
const std::vector< IndexedValue > & indexedValues() const noexcept
Returns the requested metric indexes and values.
size_t processingWindowLength_
Length of processing window used to compute statistics.
MetricsController(MetricsControllerDelegate &delegate) noexcept
Construct a new Metrics Controller object.
static constexpr size_t defaultProcessingWindowOverlap
Default parameter option.
MetricsStatsArray< DiffValueType > stdDev
Array of standard deviations.
SourceInterests makeSourceInterests(bool value)
Construct a new shared pointer to a SourceInterests object.
MetricsControllerDelegate & delegate_
Delegate to alert when something happens.
void resetIterativeStats()
Reset iterative stats to begin new processing window.
void forEach(Function func)
Execute a function for each stats array.
void setProcessingWindow(size_t length, size_t overlap) noexcept
Sets the processing window parameters used to compute statistics.
MetricsStatsIntermediate metricsStatsIntermediate_
Intermediate statistics arrays.
static constexpr std::string_view nameStatsAll
static Stats statsFromName(const std::string_view &statName)
Convert a stat name to a stat value.
double unitsPerSecondFactor_
Factor to convert metric differences to units per second.
std::unique_ptr< MetricsDataArray > previousMetrics_
Pointer to earlier metric raw values.
bool sendStats() const noexcept
Returns whether the controller alerts the delegate after computing statistics.
CircularArray::CircularArray< MetricsDiffArray > metricsValues_
CircularArray holding all metric differences, used to compute statistics.
void setSendValues(bool sendValues) noexcept
Sets whether the controller alerts the delegate after collecting new values.
void updateMetrics()
Fetch latest metrics from source and compute their variations.
static constexpr std::array< std::string_view, count > names
Names of available statistics.
std::chrono::milliseconds samplingInterval_
Metrics sampling interval.
Abstract delegate of MetricsController.
virtual bool metricsContollerShouldStopCollectingMetrics(const MetricsController &metricsController)=0
Function called after each iteration to ask the delegate whether the controller should stop collectin...
MetricsStatsArray< DiffValueType > min
Array of minimums.
Class to compute and store metric values variations.
MetricsStatsArray< DiffValueType > max
Array of maximums.
size_t processingWindowOverlap_
Overlap of processing window used to compute statistics.
bool isCollecting_
Whether the receiver is collecting metrics.
static constexpr std::array< StatsDependency, extraCount > StatsDependencies
Array of all statistics dependencies.
bool convertToUnitsPerSecond_
Whether to convert metric differences to units per second.
virtual void metricsContollerCollectedMetricsStats(const MetricsController &metricsController, const MetricsController::MetricsStats &metricsStats)=0
Function called every time the MetricsController has computed statistics.
bool isCollecting() const noexcept
Returns whether the controller is collecting metrics.