MetroCollect  2.3.4
MetroCollectStats.cc
Go to the documentation of this file.
1 //
2 // MetroCollectStats.cc
3 //
4 // Created on July 26th 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 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 
26 
27 
34 void printMetrics(const std::vector<std::pair<std::string, std::string>>& names, const MetroCollect::MetricsController::MetricsStats& s) {
35  std::cout << std::setw(54) << " ";
36  for (const auto& st : MetroCollect::Statistics::names)
37  std::cout << std::setw(16) << std::left << st << "\t";
38  std::cout << "Unit" << std::endl;
39  for (size_t i = 0; i < s.min.size(); i++) {
40  std::cout << std::setw(54) << std::left << names[i].first;
41  s.forEach([&](const auto& st, MetroCollect::Statistics::Stats ) {
42  std::cout << std::setw(16) << st[i] << "\t";
43  });
44  std::cout << names[i].second << std::endl;
45  }
46  std::cout << std::endl << std::endl;
47 }
48 
49 
58  // (*interests)[0] = true;
59  // auto [begin, end] = request.metricsSliceForSource(0);
60  // for (auto itr = begin; itr < end; itr++)
61  for (auto itr = request.begin(); itr < request.end(); itr++)
63  return request;
64 }
65 
66 
74 template<typename T>
76  std::vector<MetroCollect::MetricsSource::FieldInfo> fields(metricsArray.fieldCount());
77  for (size_t i = 0; i < fields.size(); i++)
78  fields[i] = metricsArray.fieldInfoAtIndex(i);
79 
80  std::vector<std::pair<std::string, std::string>> names;
81  names.reserve(fields.size());
82  std::stringstream ss;
83  for (const auto& field : fields) {
84  ss.str(std::string());
85  for (const auto& part : field.name)
86  ss << "/" << part;
87  names.push_back(std::pair{ss.str(), field.unit});
88  }
89  return names;
90 }
91 
92 
97  size_t iterationCount;
98  std::vector<std::pair<std::string, std::string>> names;
99 
101 
102  }
103 
104 #if PRINT_METRICS
106  printMetrics(names, metricsStats);
107 #else
109 #endif
110  }
111 
113  iterationCount--;
114  return iterationCount == 0;
115  }
116 };
117 
118 
119 
128 int main(int argc, char* argv[]) {
133 
134  if (argc > 1)
135  samplingInterval = std::chrono::milliseconds(std::atol(argv[1]));
136  if (argc > 2)
137  windowLength = std::atol(argv[2]);
138  if (argc > 3)
139  windowOverlap = std::atol(argv[3]);
140  if (argc > 4)
141  d.iterationCount = std::atol(argv[4]);
142  else
143  d.iterationCount = 0;
144 
145  auto m = generateMetrics();
146  d.names = getAllFieldNames(m);
147  MetroCollect::MetricsController controller(d);
148  controller.setSendStats(true);
149  controller.setSamplingInterval(samplingInterval);
150  controller.setProcessingWindow(windowLength, windowOverlap);
151  controller.setRequestedMetrics(m);
152 
153  controller.collectMetrics();
154 
155  return 0;
156 }
bool metricsContollerShouldStopCollectingMetrics(const MetroCollect::MetricsController &) override
Function called after each iteration to ask the delegate whether the controller should stop collectin...
static constexpr std::chrono::milliseconds defaultSamplingInterval
Default parameter option.
Struct to store all final metric stats.
size_t iterationCount
Number of iterations remaining to be done.
void setRequestedMetrics(const MetricsArray< Statistics::Stats > &requestedMetrics) noexcept
Sets the list of request metrics.
static constexpr size_t defaultProcessingWindowLength
Default parameter option.
Stats
Bitwise type te select statistics.
Definition: Statistics.h:64
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.
void collectMetrics()
Launch metric collection loop.
All statistics selected.
Definition: Statistics.h:71
void printMetrics(const std::vector< std::pair< std::string, std::string >> &names, const MetroCollect::MetricsController::MetricsStats &s)
Print metric stats on screen.
static constexpr size_t defaultProcessingWindowOverlap
Default parameter option.
Small struct to implement MetricsController delegate.
SourceInterests makeSourceInterests(bool value)
Construct a new shared pointer to a SourceInterests object.
void forEach(Function func)
Execute a function for each stats array.
std::vector< std::pair< std::string, std::string > > names
Name of collected metrics.
void setProcessingWindow(size_t length, size_t overlap) noexcept
Sets the processing window parameters used to compute statistics.
int main(int argc, char *argv[])
MetroCollectStats main function.
const MetricsSource::FieldInfo fieldInfoAtIndex(size_t index) const noexcept
Get details about a specific metric field.
Class to fetch metrics from the kernel and store them.
auto generateMetrics()
Generate metric request to give to the MetricsController.
void metricsContollerCollectedMetricsValues(const MetroCollect::MetricsController &, const MetroCollect::MetricsDiffArray &, const MetroCollect::MetricsDataArray &, const MetroCollect::MetricsDataArray &) override
Function called every time the MetricsController has collected new values.
static constexpr std::array< std::string_view, count > names
Names of available statistics.
Definition: Statistics.h:58
void metricsContollerCollectedMetricsStats(const MetroCollect::MetricsController &, const MetroCollect::MetricsController::MetricsStats &) override
Function called every time the MetricsController has computed statistics.
Abstract delegate of MetricsController.
size_t fieldCount() const noexcept
Returns the total number of available metrics.
Definition: MetricsArray.cc:74
MetricsStatsArray< DiffValueType > min
Array of minimums.
Class to control the collection of metrics.
Class to compute and store metric values variations.
size_t size() const noexcept
Returns the size of the underlying array.
auto getAllFieldNames(const MetroCollect::MetricsArray< T > &metricsArray)
Get fields name of all metrics.