0

I am reading a kernel file /proc/stat using ifstream :

std::ifstream proc_stat_file("/proc/stat", std::ifstream::in);

This file contains the CPU usage times for different processes and gets updated by the kernel frequently. I am writing an app which needs to log the total CPU times parsing this file every second. I have opened the file once using ifstream in the constructor of the class. I am trying to read the file contents in a member function of the class:

 void read_cpu_times()
  {

    std::string line;
    const std::string cpu_string("cpu");
    const std::size_t cpu_string_len = cpu_string.size();

    while (std::getline(proc_stat_file, line)) {
      // cpu stats line found
      if (!line.compare(0, cpu_string_len, cpu_string)) {
        std::istringstream ss(line);

        // store entry
        m_entries.emplace_back(cpu_info_obj());
        cpu_info_obj & entry = m_entries.back();

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

        // count the number of cpu cores
        if (entry.cpu_label.size() > cpu_string_len) {
          ++m_cpu_cores;
        }

        // read times
        for (uint8_t i = 0U; i < static_cast<uint8_t>(CpuTimeState::CPU_TIME_STATES_NUM); ++i) {
          ss >> entry.cpu_time_array[i];
        }
      }
    }
    // compute cpu total time
    // Guest and Guest_nice are not included in the total time calculation since, they are
    // already accounted in user and nice.

    m_cpu_total_time = (m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_USER)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_NICE)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_SYSTEM)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_IDLE)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_IOWAIT)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_IRQ)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_SOFTIRQ)] +
      m_entries[0].cpu_time_array[static_cast<uint8_t>(CpuTimeState::CS_STEAL)]);

    //Reset the eof file flag and move file pointer to beginning for next read
    //proc_stat_file.
    proc_stat_file.clear();
    proc_stat_file.seekg(0, std::ifstream::beg);

This read_cpu_times() function gets called every second. But I am not getting updated values of m_cpu_total_time in between calls. I am not sure why. Any ideas?

iz_
  • 15,923
  • 3
  • 25
  • 40
  • Perhaps you need to [flush](https://en.cppreference.com/w/cpp/io/manip/flush) it? –  Oct 17 '19 at 05:10
  • See also: https://stackoverflow.com/a/14105680/10957435 –  Oct 17 '19 at 05:12

1 Answers1

1

I was able to resolve my issue. The file contents were getting updated but I was not clearing my m_entries vector after every file read . So it always kept on reading from the first element of vector m_entries which would be the data that was read the first time since it was never cleared out.