cpu_metric_collector.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
18 #include <ros_monitoring_msgs/MetricData.h>
19 
20 #include <chrono>
21 #include <thread>
22 
23 using namespace ros_monitoring_msgs;
24 
25 
26 #define BASE_METRIC_NAME "cpu_usage_"
27 
28 
30 {
31  // new snapshot
32  new_ = std::make_shared<CPUStats>();
33  new_->ReadStatsCPU();
34 
35  if (old_) {
36  CollectCpuUsage(old_->GetEntries(), new_->GetEntries());
37  }
38 
39  old_ = new_;
40  new_.reset();
41 }
42 
43 void CPUMetricCollector::CollectCpuUsage(const std::vector<CPUData> & entries1,
44  const std::vector<CPUData> & entries2)
45 {
46  const size_t num_of_entries = std::min(entries1.size(), entries2.size());
47 
48  for (size_t i = 0; i < num_of_entries; ++i) {
49  const CPUData & e1 = entries1[i];
50  const CPUData & e2 = entries2[i];
51 
52  const auto active_time = static_cast<float>(e2.GetActiveTime() - e1.GetActiveTime());
53  const auto idle_time = static_cast<float>(e2.GetIdleTime() - e1.GetIdleTime());
54  const float total_time = active_time + idle_time;
55  const float usage = 100.f * active_time / total_time;
56 
57  ros_monitoring_msgs::MetricData md = mgr_.CreateMetric();
58  md.metric_name = BASE_METRIC_NAME + entries1[i].cpu;
59  md.unit = MetricData::UNIT_PERCENTAGE;
60  md.value = usage;
61  mgr_.AddMetric(md);
62  }
63 }
#define BASE_METRIC_NAME
size_t GetActiveTime() const
Definition: cpu_data.cpp:18
void CollectCpuUsage(const std::vector< CPUData > &entries1, const std::vector< CPUData > &entries2)
void Collect() overridefinal
activates metrics collection.
def usage(progname)
size_t GetIdleTime() const
Definition: cpu_data.cpp:24


health_metric_collector
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:32:41