process-list.cpp
Go to the documentation of this file.
1 /* Copyright LAAS, CNRS
2  * Author: O. Stasse, 2019
3  * See LICENSE file in the root directory of this repository.
4  */
6 #include <fstream>
7 #include <sstream>
8 #include <string>
9 
10 using namespace dynamicgraph::CPU;
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  percent_(0.0) {}
23 
24 void CPUData::ProcessLine(std::istringstream &aCPULine) {
25  unsigned long long int luser_mode_time = 0, lnice_time = 0, lsystem_time = 0,
26  lidle_time = 0, liowait_time = 0, lirq_time = 0,
27  lsoftirq_time = 0, lsteal_time = 0, lguest_time = 0,
28  lguest_nice_time;
29 
30  aCPULine >> luser_mode_time;
31  aCPULine >> lnice_time;
32  aCPULine >> lsystem_time;
33  aCPULine >> lidle_time;
34  aCPULine >> liowait_time;
35  aCPULine >> lirq_time;
36  aCPULine >> lsoftirq_time;
37  aCPULine >> lsteal_time;
38  aCPULine >> lguest_time;
39  aCPULine >> lguest_nice_time;
40 
41  // Remove guest time already in user_time:
42  luser_mode_time -= lguest_time;
43  lnice_time -= lguest_nice_time;
44 
45  // Compute cumulative time
46  unsigned long long int lidle_all_time = 0, lsystem_all_time = 0,
47  lguest_all_time = 0, ltotal_time = 0;
48 
49  lidle_all_time = lidle_time + liowait_time;
50  lsystem_all_time = lsystem_time + lirq_time + lsoftirq_time;
51  lguest_all_time = lguest_time + lguest_nice_time;
52  ltotal_time = luser_mode_time + lnice_time + lsystem_all_time +
53  lidle_all_time + lsteal_time + lguest_all_time;
54 
55  // Update periodic computation.
57  nice_period_ = computePeriod(lnice_time, nice_time_);
58  system_period_ = computePeriod(lsystem_time, system_time_);
60  idle_period_ = computePeriod(lidle_time, idle_time_);
62  iowait_period_ = computePeriod(liowait_time, idle_time_);
63  irq_period_ = computePeriod(lirq_time, irq_time_);
65  steal_period_ = computePeriod(lsteal_time, steal_time_);
66  guest_period_ = computePeriod(lguest_all_time, guest_time_);
67  total_period_ = computePeriod(ltotal_time, total_time_);
68 
70  user_mode_time_ = luser_mode_time;
71  nice_time_ = lnice_time;
72  system_time_ = lsystem_time;
73  system_all_time_ = lsystem_all_time;
74  idle_time_ = lidle_time;
75  idle_all_time_ = lidle_all_time;
76  iowait_time_ = liowait_time;
77  irq_time_ = lirq_time;
78  softirq_time_ = lsoftirq_time;
79  steal_time_ = lsteal_time;
80  guest_time_ = lguest_all_time;
81  total_time_ = ltotal_time;
82 
83  if (total_period_ != 0) {
84  percent_ = (double)(user_mode_period_) / (double)(total_period_) * 100.0;
85  percent_ += (double)(nice_period_) / (double)(total_period_) * 100.0;
86  percent_ += (double)(system_period_) / (double)(total_period_) * 100.0;
87  percent_ += (double)(irq_period_) / (double)(total_period_) * 100.0;
88  percent_ += (double)(softirq_period_) / (double)(total_period_) * 100.0;
89  percent_ += (double)(steal_period_) / (double)(total_period_) * 100.0;
90  percent_ += (double)(iowait_period_) / (double)(total_period_) * 100.0;
91  }
92 }
93 
95  vCPUData_.clear();
96  init();
97 }
98 
99 void System::init() {
100  init_ = false;
101  readProcStat();
102  init_ = true;
103 }
104 
105 void System::ProcessCPULine(unsigned int cpunb, std::istringstream &aCPULine) {
106  vCPUData_[cpunb].ProcessLine(aCPULine);
107 }
108 
110  std::ifstream aif;
111  cpuNb_ = 1;
112 
113  aif.open("/proc/stat", std::ifstream::in);
114  std::string aline;
115  aline.clear();
116  while (std::getline(aif, aline)) {
117  // Read on line of the file
118  std::istringstream anISSLine(aline);
119  std::string line_hdr;
120  anISSLine >> line_hdr;
121 
122  // Check if the line start with cpu
123  std::size_t pos = line_hdr.find("cpu");
124  std::string str_cpunbr = line_hdr.substr(pos + 3);
125 
126  // Check if this is the first line
127  if (pos == 0 and str_cpunbr.empty()) {
128  gCPUData_.ProcessLine(anISSLine);
129  gCPUData_.cpu_id_ = -1;
130  } else {
131  // If not then check if there is a CPU number
132  if (pos == 0) {
133  std::istringstream iss(str_cpunbr);
134  unsigned int lcpunb;
135  iss >> lcpunb;
136  // If we did not initialize
137  if (!init_) {
138  // Count the number of CPU.
139  if (lcpunb > cpuNb_) cpuNb_ = lcpunb;
140  } else
141  // Otherwise process the line.
142  ProcessCPULine(lcpunb, anISSLine);
143  }
144  }
145  }
146 
147  if (!init_) {
149  vCPUData_.resize(cpuNb_ + 1);
150  for (unsigned long i = 0; i < (unsigned long)cpuNb_; i++)
151  vCPUData_[i].cpu_id_ = (int)i;
152  }
153  aif.close();
154 }
dynamicgraph::CPU::System::ProcessCPULine
void ProcessCPULine(unsigned int cpunb, std::istringstream &aCPULine)
Definition: process-list.cpp:105
dynamicgraph::CPU::CPUData::idle_all_time_
unsigned long long int idle_all_time_
Time spend in doing nothing.
Definition: process-list.hh:52
dynamicgraph::CPU::CPUData::computePeriod
unsigned long long int computePeriod(unsigned long long int &a, unsigned long long int &b)
Definition: process-list.hh:32
dynamicgraph::CPU::System::gCPUData_
CPUData gCPUData_
Global CPU information.
Definition: process-list.hh:153
dynamicgraph::CPU::System::System
System()
Definition: process-list.cpp:94
dynamicgraph::CPU::CPUData::guest_time_
unsigned long long int guest_time_
Definition: process-list.hh:64
dynamicgraph::CPU::CPUData::idle_time_
unsigned long long int idle_time_
Time spend in doing nothing.
Definition: process-list.hh:50
dynamicgraph::CPU::CPUData::CPUData
CPUData()
Definition: process-list.cpp:11
dynamicgraph::CPU::CPUData::system_all_time_
unsigned long long int system_all_time_
Time spend in system mode.
Definition: process-list.hh:48
dynamicgraph::CPU::CPUData::iowait_period_
unsigned long long int iowait_period_
Time spend in waiting an input/output to complete.
Definition: process-list.hh:88
dynamicgraph::CPU::CPUData::steal_time_
unsigned long long int steal_time_
Definition: process-list.hh:61
dynamicgraph::CPU::CPUData::system_period_
unsigned long long int system_period_
Time spend in system mode.
Definition: process-list.hh:80
dynamicgraph::CPU::CPUData::user_mode_period_
unsigned long long int user_mode_period_
Time spend in user mode.
Definition: process-list.hh:76
dynamicgraph::CPU::CPUData::irq_time_
unsigned long long int irq_time_
Time spend in servicing hardware interrupts.
Definition: process-list.hh:56
dynamicgraph::CPU::CPUData::system_all_period_
unsigned long long int system_all_period_
Time spend in all system mode.
Definition: process-list.hh:82
dynamicgraph::CPU::CPUData::guest_period_
unsigned long long int guest_period_
Definition: process-list.hh:98
dynamicgraph::CPU::System::readProcStat
void readProcStat()
Update CPU data information from /proc/stat.
Definition: process-list.cpp:109
dynamicgraph::CPU::CPUData::system_time_
unsigned long long int system_time_
Time spend in system mode.
Definition: process-list.hh:46
dynamicgraph::CPU::System::vCPUData_
std::vector< CPUData > vCPUData_
Vector of CPU informations.
Definition: process-list.hh:150
dynamicgraph::CPU::System::init
void init()
Read /proc/state file to extract CPU count.
Definition: process-list.cpp:99
dynamicgraph::CPU::CPUData::idle_all_period_
unsigned long long int idle_all_period_
Time spend in doing nothing.
Definition: process-list.hh:86
dynamicgraph::CPU::CPUData::steal_period_
unsigned long long int steal_period_
Definition: process-list.hh:95
dynamicgraph::CPU::CPUData::idle_period_
unsigned long long int idle_period_
Time spend in doing nothing.
Definition: process-list.hh:84
dynamicgraph::CPU::CPUData::irq_period_
unsigned long long int irq_period_
Time spend in servicing hardware interrupts.
Definition: process-list.hh:90
dynamicgraph::CPU::CPUData::cpu_id_
int cpu_id_
Definition: process-list.hh:30
process-list.hh
dynamicgraph::CPU::System::init_
bool init_
Definition: process-list.hh:130
dynamicgraph::CPU::CPUData::nice_period_
unsigned long long int nice_period_
Time spend in user mode with low priority (nice mode)
Definition: process-list.hh:78
dynamicgraph::CPU::CPUData::total_period_
unsigned long long int total_period_
Various classes of time spend by the CPU by periodTotal time.
Definition: process-list.hh:74
dynamicgraph::CPU::System::cpuNb_
unsigned int cpuNb_
Number of CPU.
Definition: process-list.hh:145
dynamicgraph::CPU::CPUData::total_time_
unsigned long long int total_time_
Various classes of time spend by the CPUTotal time.
Definition: process-list.hh:40
dynamicgraph::CPU::CPUData::softirq_period_
unsigned long long int softirq_period_
Time spend in servicing software interrupts.
Definition: process-list.hh:92
dynamicgraph::CPU::CPUData::softirq_time_
unsigned long long int softirq_time_
Time spend in servicing software interrupts.
Definition: process-list.hh:58
dynamicgraph::CPU
Definition: process-list.hh:17
dynamicgraph::CPU::CPUData::percent_
double percent_
Definition: process-list.hh:101
dynamicgraph::CPU::CPUData::user_mode_time_
unsigned long long int user_mode_time_
Time spend in user mode.
Definition: process-list.hh:42
dynamicgraph::CPU::CPUData::ProcessLine
void ProcessLine(std::istringstream &aCPULine)
Definition: process-list.cpp:24
dynamicgraph::CPU::CPUData::iowait_time_
unsigned long long int iowait_time_
Time spend in waiting an input/output to complete.
Definition: process-list.hh:54
dynamicgraph::CPU::CPUData::nice_time_
unsigned long long int nice_time_
Time spend in user mode with low priority (nice mode)
Definition: process-list.hh:44


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38