GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/mt/process-list.cpp Lines: 97 97 100.0 %
Date: 2023-03-15 12:04:10 Branches: 45 70 64.3 %

Line Branch Exec Source
1
/* Copyright LAAS, CNRS
2
 * Author: O. Stasse, 2019
3
 * See LICENSE file in the root directory of this repository.
4
 */
5
#include <dynamic-graph/process-list.hh>
6
#include <fstream>
7
#include <sstream>
8
#include <string>
9
10
using namespace dynamicgraph::CPU;
11
17
CPUData::CPUData()
12
    : user_mode_time_(0),
13
      nice_time_(0),
14
      system_time_(0),
15
      idle_time_(0),
16
      iowait_time_(0),
17
      irq_time_(0),
18
      softirq_time_(0),
19
      steal_time_(0),
20
      guest_time_(0),
21
      guest_nice_time_(0),
22
17
      percent_(0.0) {}
23
24
171
void CPUData::ProcessLine(std::istringstream &aCPULine) {
25
171
  unsigned long long int luser_mode_time = 0, lnice_time = 0, lsystem_time = 0,
26
171
                         lidle_time = 0, liowait_time = 0, lirq_time = 0,
27
171
                         lsoftirq_time = 0, lsteal_time = 0, lguest_time = 0,
28
                         lguest_nice_time;
29
30
171
  aCPULine >> luser_mode_time;
31
171
  aCPULine >> lnice_time;
32
171
  aCPULine >> lsystem_time;
33
171
  aCPULine >> lidle_time;
34
171
  aCPULine >> liowait_time;
35
171
  aCPULine >> lirq_time;
36
171
  aCPULine >> lsoftirq_time;
37
171
  aCPULine >> lsteal_time;
38
171
  aCPULine >> lguest_time;
39
171
  aCPULine >> lguest_nice_time;
40
41
  // Remove guest time already in user_time:
42
171
  luser_mode_time -= lguest_time;
43
171
  lnice_time -= lguest_nice_time;
44
45
  // Compute cumulative time
46
171
  unsigned long long int lidle_all_time = 0, lsystem_all_time = 0,
47
171
                         lguest_all_time = 0, ltotal_time = 0;
48
49
171
  lidle_all_time = lidle_time + liowait_time;
50
171
  lsystem_all_time = lsystem_time + lirq_time + lsoftirq_time;
51
171
  lguest_all_time = lguest_time + lguest_nice_time;
52
171
  ltotal_time = luser_mode_time + lnice_time + lsystem_all_time +
53
171
                lidle_all_time + lsteal_time + lguest_all_time;
54
55
  // Update periodic computation.
56
171
  user_mode_period_ = computePeriod(luser_mode_time, user_mode_time_);
57
171
  nice_period_ = computePeriod(lnice_time, nice_time_);
58
171
  system_period_ = computePeriod(lsystem_time, system_time_);
59
171
  system_all_period_ = computePeriod(lsystem_all_time, system_all_time_);
60
171
  idle_period_ = computePeriod(lidle_time, idle_time_);
61
171
  idle_all_period_ = computePeriod(lidle_all_time, idle_all_time_);
62
171
  iowait_period_ = computePeriod(liowait_time, idle_time_);
63
171
  irq_period_ = computePeriod(lirq_time, irq_time_);
64
171
  softirq_period_ = computePeriod(lsoftirq_time, softirq_time_);
65
171
  steal_period_ = computePeriod(lsteal_time, steal_time_);
66
171
  guest_period_ = computePeriod(lguest_all_time, guest_time_);
67
171
  total_period_ = computePeriod(ltotal_time, total_time_);
68
69
  /// Update time.
70
171
  user_mode_time_ = luser_mode_time;
71
171
  nice_time_ = lnice_time;
72
171
  system_time_ = lsystem_time;
73
171
  system_all_time_ = lsystem_all_time;
74
171
  idle_time_ = lidle_time;
75
171
  idle_all_time_ = lidle_all_time;
76
171
  iowait_time_ = liowait_time;
77
171
  irq_time_ = lirq_time;
78
171
  softirq_time_ = lsoftirq_time;
79
171
  steal_time_ = lsteal_time;
80
171
  guest_time_ = lguest_all_time;
81
171
  total_time_ = ltotal_time;
82
83
171
  if (total_period_ != 0) {
84
154
    percent_ = (double)(user_mode_period_) / (double)(total_period_)*100.0;
85
154
    percent_ += (double)(nice_period_) / (double)(total_period_)*100.0;
86
154
    percent_ += (double)(system_period_) / (double)(total_period_)*100.0;
87
154
    percent_ += (double)(irq_period_) / (double)(total_period_)*100.0;
88
154
    percent_ += (double)(softirq_period_) / (double)(total_period_)*100.0;
89
154
    percent_ += (double)(steal_period_) / (double)(total_period_)*100.0;
90
154
    percent_ += (double)(iowait_period_) / (double)(total_period_)*100.0;
91
  }
92
171
}
93
94
1
System::System() {
95
1
  vCPUData_.clear();
96
1
  init();
97
1
}
98
99
1
void System::init() {
100
1
  init_ = false;
101
1
  readProcStat();
102
1
  init_ = true;
103
1
}
104
105
160
void System::ProcessCPULine(unsigned int cpunb, std::istringstream &aCPULine) {
106
160
  vCPUData_[cpunb].ProcessLine(aCPULine);
107
160
}
108
109
11
void System::readProcStat() {
110
22
  std::ifstream aif;
111
11
  cpuNb_ = 1;
112
113
11
  aif.open("/proc/stat", std::ifstream::in);
114
22
  std::string aline;
115
11
  aline.clear();
116

275
  while (std::getline(aif, aline)) {
117
    // Read on line of the file
118
528
    std::istringstream anISSLine(aline);
119
528
    std::string line_hdr;
120
264
    anISSLine >> line_hdr;
121
122
    // Check if the line start with cpu
123
264
    std::size_t pos = line_hdr.find("cpu");
124
528
    std::string str_cpunbr = line_hdr.substr(pos + 3);
125
126
    // Check if this is the  first line
127

264
    if (pos == 0 and str_cpunbr.empty()) {
128
11
      gCPUData_.ProcessLine(anISSLine);
129
11
      gCPUData_.cpu_id_ = -1;
130
    } else {
131
      // If not then check if there is a CPU number
132
253
      if (pos == 0) {
133
352
        std::istringstream iss(str_cpunbr);
134
        unsigned int lcpunb;
135
176
        iss >> lcpunb;
136
        // If we did not initialize
137
176
        if (!init_) {
138
          // Count the number of CPU.
139
16
          if (lcpunb > cpuNb_) cpuNb_ = lcpunb;
140
        } else
141
          // Otherwise process the line.
142
160
          ProcessCPULine(lcpunb, anISSLine);
143
      }
144
    }
145
  }
146
147
11
  if (!init_) {
148
    /// The number of CPU has been detected by going through /proc/stat.
149
1
    vCPUData_.resize(cpuNb_ + 1);
150
16
    for (unsigned long i = 0; i < (unsigned long)cpuNb_; i++)
151
15
      vCPUData_[i].cpu_id_ = (int)i;
152
  }
153
11
  aif.close();
154
11
}