void CPUStats::ReadStatsCPU()

in health_metric_collector/src/cpu_stats.cpp [23:60]


void CPUStats::ReadStatsCPU()
{
  std::ifstream file_stat("/proc/stat");

  std::string line;

  const std::string str_tot("tot");
  const std::string str_cpu("cpu");
  const std::size_t len_str_cpu = str_cpu.size();

  while (std::getline(file_stat, line)) {
    // cpu stats line found
    if (!line.compare(0, len_str_cpu, str_cpu)) {
      std::istringstream ss(line);

      // store entry
      entries_.emplace_back(CPUData());
      CPUData & entry = entries_.back();

      // read cpu label
      ss >> entry.cpu;

      // remove "cpu" from the label when it's a processor number
      if (entry.cpu.size() > len_str_cpu) {
        entry.cpu.erase(0, len_str_cpu);
        entry.cpu = "core_" + entry.cpu;
      }
      // replace "cpu" with "tot" when it's total values
      else {
        entry.cpu = str_tot;
      }
      // read times
      for (int i = 0; i < CPUData::kNumCpuStates; ++i) {
        ss >> entry.times[i];
      }
    }
  }
}