cpu_stats.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 
17 
18 #include <fstream>
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 
24 {
25  std::ifstream file_stat("/proc/stat");
26 
27  std::string line;
28 
29  const std::string str_tot("tot");
30  const std::string str_cpu("cpu");
31  const std::size_t len_str_cpu = str_cpu.size();
32 
33  while (std::getline(file_stat, line)) {
34  // cpu stats line found
35  if (!line.compare(0, len_str_cpu, str_cpu)) {
36  std::istringstream ss(line);
37 
38  // store entry
39  entries_.emplace_back(CPUData());
40  CPUData & entry = entries_.back();
41 
42  // read cpu label
43  ss >> entry.cpu;
44 
45  // remove "cpu" from the label when it's a processor number
46  if (entry.cpu.size() > len_str_cpu) {
47  entry.cpu.erase(0, len_str_cpu);
48  entry.cpu = "core_" + entry.cpu;
49  }
50  // replace "cpu" with "tot" when it's total values
51  else {
52  entry.cpu = str_tot;
53  }
54  // read times
55  for (int i = 0; i < CPUData::kNumCpuStates; ++i) {
56  ss >> entry.times[i];
57  }
58  }
59  }
60 }
std::string cpu
Definition: cpu_data.h:39
size_t times[kNumCpuStates]
Definition: cpu_data.h:40
void ReadStatsCPU()
parse /proc/stats.
Definition: cpu_stats.cpp:23
std::vector< CPUData > entries_
Definition: cpu_stats.h:40


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