Skip to content

Commit c1e1224

Browse files
textanalyticsmantextanalyticsman
textanalyticsman
authored and
textanalyticsman
committed
CPU consumption per process
1 parent 5140976 commit c1e1224

File tree

6 files changed

+98
-17
lines changed

6 files changed

+98
-17
lines changed

include/linux_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const std::string kPasswordPath{"/etc/passwd"};
2121
// Helpers
2222
// Function added to use regular expressions in order to look for patterns on Linux files
2323
std::string RegularExpression(std::string patttern, std::string tex, int group);
24+
float CpuUtilization(int pid);
2425

2526
// System
2627
float MemoryUtilization();
@@ -31,6 +32,7 @@ int RunningProcesses();
3132
std::string OperatingSystem();
3233
std::string Kernel();
3334

35+
3436
// CPU
3537
enum CPUStates {
3638
kUser_ = 0,

include/processor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class Processor {
1111
private:
1212
//This will save the CPU average numbers for two readings
1313
std::vector<std::vector<std::string>> cpu_data;
14-
// This is the delay between readings in seconds used to calculate CPU consumption
15-
unsigned int delay_reading{1000};
1614
};
1715

1816
#endif

src/format.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44

55
using std::string;
66

7-
// TODO: Complete this helper function
87
// INPUT: Long int measuring seconds
98
// OUTPUT: HH:MM:SS
10-
// REMOVE: [[maybe_unused]] once you define the function
11-
string Format::ElapsedTime(long seconds[[maybe_unused]]) { return string(); }
9+
string Format::ElapsedTime(long seconds)
10+
{
11+
long int tdays{ seconds/60/60/24 };
12+
long int thours{ seconds/60/60%24 };
13+
long int tminutes{ seconds/60%60 };
14+
long int tseconds{ seconds%60 };
15+
16+
string result { std::to_string(tdays) + ":" + std::to_string(thours) + ":" + std::to_string(tminutes) + ":" + std::to_string(tseconds)};
17+
18+
return result;
19+
}

src/linux_parser.cpp

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,24 @@ float LinuxParser::MemoryUtilization()
123123
}
124124

125125
// TODO: Read and return the system uptime
126-
long LinuxParser::UpTime() { return 0; }
126+
long LinuxParser::UpTime()
127+
{
128+
long int uptime{0};
129+
std::string line;
130+
131+
std::ifstream filestream(kProcDirectory + kUptimeFilename);
132+
133+
if (filestream.is_open())
134+
std::getline(filestream, line);
135+
136+
std::istringstream linestream{ line };
137+
138+
std::string tmp;
139+
linestream >> tmp;
140+
uptime=std::stod(tmp);
141+
142+
return uptime;
143+
}
127144

128145
// TODO: Read and return the number of jiffies for the system
129146
long LinuxParser::Jiffies() { return 0; }
@@ -168,15 +185,71 @@ vector<string> LinuxParser::CpuUtilization()
168185

169186
}
170187

188+
// To return the CPU utilization per process
189+
float LinuxParser::CpuUtilization(int pid)
190+
{
191+
float cpu_consumption{ 0.0 };
192+
std::string line;
193+
194+
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename);
195+
196+
if (filestream.is_open())
197+
std::getline(filestream, line);
198+
199+
std::istringstream linestream{ line };
200+
vector<float> cpu_tokens;
201+
std::string tmp;
202+
203+
int i{0};
204+
205+
while (linestream >> tmp)
206+
{
207+
++i;
208+
// Positions 14, 15, 16, 17 and 22 are used to calculate CPU consumption per process
209+
// https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat/16736599#16736599
210+
if(i == 14 || i == 15 || i == 16 || i == 17)
211+
cpu_tokens.push_back(stof(tmp));
212+
else if(i == 22)
213+
{
214+
cpu_tokens.push_back(stof(tmp));
215+
break;
216+
}
217+
}
218+
219+
float total_time{0.0};
220+
long int hertz{sysconf(_SC_CLK_TCK)};
221+
float seconds{0.0};
222+
long uptime{UpTime()};
223+
224+
for(int i=0; i<4; ++i)
225+
total_time += cpu_tokens[i];
226+
227+
seconds = uptime - (cpu_tokens[4]/hertz);
228+
229+
cpu_consumption = total_time / hertz / seconds;
230+
231+
return cpu_consumption;
232+
}
233+
171234
// TODO: Read and return the total number of processes
172235
int LinuxParser::TotalProcesses() { return 0; }
173236

174237
// TODO: Read and return the number of running processes
175238
int LinuxParser::RunningProcesses() { return 0; }
176239

177-
// TODO: Read and return the command associated with a process
178-
// REMOVE: [[maybe_unused]] once you define the function
179-
string LinuxParser::Command(int pid[[maybe_unused]]) { return string(); }
240+
// Read and return the command associated with a process
241+
string LinuxParser::Command(int pid)
242+
{
243+
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kCmdlineFilename);
244+
std::string command_line;
245+
246+
if (filestream.is_open())
247+
{
248+
std::getline(filestream, command_line);
249+
}
250+
251+
return command_line;
252+
}
180253

181254
// TODO: Read and return the memory used by a process
182255
// REMOVE: [[maybe_unused]] once you define the function
@@ -205,8 +278,7 @@ string LinuxParser::Uid(int pid)
205278
return user_id;
206279
}
207280

208-
// TODO: Read and return the user associated with a process
209-
// REMOVE: [[maybe_unused]] once you define the function
281+
// Read and return the user associated with a process
210282
string LinuxParser::User(int pid)
211283
{
212284
std::string line;

src/ncurses_display.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ void NCursesDisplay::DisplayProcesses(std::vector<Process>& processes,
5757
int row{0};
5858
int const pid_column{2};
5959
int const user_column{9};
60-
int const cpu_column{16};
61-
int const ram_column{26};
62-
int const time_column{35};
63-
int const command_column{46};
60+
int const cpu_column{19};
61+
int const ram_column{29};
62+
int const time_column{39};
63+
int const command_column{49};
6464
wattron(window, COLOR_PAIR(2));
6565
mvwprintw(window, ++row, pid_column, "PID");
6666
mvwprintw(window, row, user_column, "USER");

src/system.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ vector<Process>& System::Processes()
2424

2525
for(int pid : list_pids)
2626
{
27-
Process process(pid, LinuxParser::User(pid));
27+
Process process(pid, LinuxParser::User(pid), LinuxParser::Command(pid),
28+
LinuxParser::CpuUtilization(pid));
2829
processes_.push_back(process);
2930
}
3031

@@ -47,4 +48,4 @@ int System::RunningProcesses() { return 0; }
4748
int System::TotalProcesses() { return 0; }
4849

4950
// TODO: Return the number of seconds since the system started running
50-
long int System::UpTime() { return 0; }
51+
long int System::UpTime() { return LinuxParser::UpTime(); }

0 commit comments

Comments
 (0)