Skip to content

Commit ac43df8

Browse files
textanalyticsmantextanalyticsman
textanalyticsman
authored and
textanalyticsman
committed
Completing tasks
1 parent aad1000 commit ac43df8

File tree

5 files changed

+131
-45
lines changed

5 files changed

+131
-45
lines changed

include/format.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string>
55

66
namespace Format {
7-
std::string ElapsedTime(long times); // TODO: See src/format.cpp
7+
std::string ElapsedTime(long times); // See src/format.cpp
88
}; // namespace Format
99

1010
#endif

include/process.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ It contains relevant attributes as shown below
99
class Process {
1010
public:
1111
Process(int pid, std::string puser = "", std::string pcommand = "", float pcpu = 0, std::string pram = "", long puptime = 0);
12-
int Pid(); // TODO: See src/process.cpp
13-
std::string User(); // TODO: See src/process.cpp
14-
std::string Command(); // TODO: See src/process.cpp
15-
float CpuUtilization(); // TODO: See src/process.cpp
16-
std::string Ram(); // TODO: See src/process.cpp
17-
long int UpTime(); // TODO: See src/process.cpp
12+
int Pid();
13+
std::string User();
14+
std::string Command();
15+
float CpuUtilization();
16+
std::string Ram();
17+
long int UpTime();
1818
bool operator<(Process const& a) const; // TODO: See src/process.cpp
1919
// To set values
2020
void SetPid(int pid);

include/system.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99

1010
class System {
1111
public:
12-
Processor& Cpu(); // TODO: See src/system.cpp
13-
std::vector<Process>& Processes(); // TODO: See src/system.cpp
14-
float MemoryUtilization(); // TODO: See src/system.cpp
15-
long UpTime(); // TODO: See src/system.cpp
16-
int TotalProcesses(); // TODO: See src/system.cpp
17-
int RunningProcesses(); // TODO: See src/system.cpp
18-
std::string Kernel(); // TODO: See src/system.cpp
19-
std::string OperatingSystem(); // TODO: See src/system.cpp
12+
Processor& Cpu();
13+
std::vector<Process>& Processes();
14+
float MemoryUtilization();
15+
long UpTime();
16+
int TotalProcesses();
17+
int RunningProcesses();
18+
std::string Kernel();
19+
std::string OperatingSystem();
2020

21-
// TODO: Define any necessary private members
2221
private:
2322
Processor cpu_ = {};
2423
std::vector<Process> processes_ = {};

src/linux_parser.cpp

+103-17
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ float LinuxParser::MemoryUtilization()
123123
return value;
124124
}
125125

126-
// TODO: Read and return the system uptime
126+
// Read and return the system uptime
127127
long LinuxParser::UpTime()
128128
{
129129
long int uptime{0};
@@ -213,20 +213,14 @@ float LinuxParser::CpuUtilization(int pid)
213213
// Positions 14, 15, 16, 17 and 22 are used to calculate CPU consumption per process
214214
// https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat/16736599#16736599
215215
if(index == 14 || index == 15 || index == 16 || index == 17)
216-
{
217216
cpu_tokens.push_back(stof(tmp));
218-
//std::cout << "PID: "<< pid << " Index: " << index << " Token: " << tmp << " ";
219-
}
220217
else if(index == 22)
221218
{
222219
cpu_tokens.push_back(stof(tmp));
223-
//std::cout << "PID: "<< pid << " Index: " << index << " Token: " << tmp << " ";
224220
break;
225221
}
226222
}
227223

228-
//std::cout << "Vector's size: " << cpu_tokens.size() << std::endl;;
229-
230224
float total_time{0.0};
231225
long int hertz{sysconf(_SC_CLK_TCK)};
232226
float seconds{0.0};
@@ -243,11 +237,47 @@ float LinuxParser::CpuUtilization(int pid)
243237
return cpu_consumption;
244238
}
245239

246-
// TODO: Read and return the total number of processes
247-
int LinuxParser::TotalProcesses() { return 0; }
240+
// Read and return the total number of processes
241+
int LinuxParser::TotalProcesses()
242+
{
243+
std::string line;
244+
// Pattern to look for total number of processes
245+
std::string pattern{ R"(processes\s{1}(\d+))" };
246+
247+
std::ifstream filestream(kProcDirectory + kStatFilename);
248+
std::string regular_expression_result;
249+
if (filestream.is_open())
250+
{
251+
while (std::getline(filestream, line))
252+
{
253+
regular_expression_result = RegularExpression(pattern, line, 1);
254+
if(regular_expression_result != "")
255+
break;
256+
}
257+
}
258+
return std::stoi(regular_expression_result);
259+
}
260+
261+
// Read and return the number of running processes
262+
int LinuxParser::RunningProcesses()
263+
{
264+
std::string line;
265+
// Pattern to look for total number of processes
266+
std::string pattern{ R"(procs_running\s{1}(\d+))" };
248267

249-
// TODO: Read and return the number of running processes
250-
int LinuxParser::RunningProcesses() { return 0; }
268+
std::ifstream filestream(kProcDirectory + kStatFilename);
269+
std::string regular_expression_result;
270+
if (filestream.is_open())
271+
{
272+
while (std::getline(filestream, line))
273+
{
274+
regular_expression_result = RegularExpression(pattern, line, 1);
275+
if(regular_expression_result != "")
276+
break;
277+
}
278+
}
279+
return std::stoi(regular_expression_result);
280+
}
251281

252282
// Read and return the command associated with a process
253283
string LinuxParser::Command(int pid)
@@ -263,9 +293,35 @@ string LinuxParser::Command(int pid)
263293
return command_line;
264294
}
265295

266-
// TODO: Read and return the memory used by a process
267-
// REMOVE: [[maybe_unused]] once you define the function
268-
string LinuxParser::Ram(int pid[[maybe_unused]]) { return string(); }
296+
// Read and return the memory used by a process
297+
string LinuxParser::Ram(int pid)
298+
{
299+
std::string line;
300+
std::string pattern{ R"(VmSize:\s{1}(\d+)\s{1}kB)" };
301+
302+
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatusFilename);
303+
std::string ram;
304+
305+
if (filestream.is_open())
306+
{
307+
while (std::getline(filestream, line))
308+
{
309+
ram = RegularExpression(pattern, line, 1);
310+
311+
if(ram != "")
312+
break;
313+
}
314+
}
315+
316+
long int result{0};
317+
if(ram!="") // to control cases when the process died in the middle of the process
318+
{
319+
result = std::stol(ram);
320+
result = result / 1024;
321+
}
322+
323+
return std::to_string(result);
324+
}
269325

270326
// Read and return the user ID associated with a process
271327
string LinuxParser::Uid(int pid)
@@ -315,6 +371,36 @@ string LinuxParser::User(int pid)
315371
return user_name;
316372
}
317373

318-
// TODO: Read and return the uptime of a process
319-
// REMOVE: [[maybe_unused]] once you define the function
320-
long LinuxParser::UpTime(int pid[[maybe_unused]]) { return 0; }
374+
// Read and return the uptime of a process
375+
long LinuxParser::UpTime(int pid)
376+
{
377+
long int uptime{ 0 };
378+
std::string line;
379+
380+
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename);
381+
382+
if (filestream.is_open())
383+
std::getline(filestream, line);
384+
385+
// Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
386+
// Thus, if no data is detected, the analysis is by passed
387+
if(line != "")
388+
{
389+
std::istringstream linestream{ line };
390+
std::string tmp;
391+
int index{0};
392+
393+
while (linestream >> tmp)
394+
{
395+
++index;
396+
if(index == 22)
397+
break;
398+
}
399+
400+
long int hertz{ sysconf(_SC_CLK_TCK) };
401+
402+
uptime = std::stol(tmp) / hertz;
403+
}
404+
405+
return uptime;
406+
}

src/system.cpp

+13-12
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,35 @@ using std::vector;
1717
// TODO: Return the system's CPU
1818
Processor& System::Cpu() { return cpu_; }
1919

20-
// TODO: Return a container composed of the system's processes
20+
// Return a container composed of the system's processes
2121
vector<Process>& System::Processes()
2222
{
2323
vector<int> list_pids = LinuxParser::Pids();
2424

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

3132
return processes_;
3233
}
3334

34-
// TODO: Return the system's kernel identifier (string)
35-
std::string System::Kernel() { return string(); }
35+
// Return the system's kernel identifier (string)
36+
std::string System::Kernel() { return LinuxParser::Kernel(); }
3637

37-
// TODO: Return the system's memory utilization
38+
// Return the system's memory utilization
3839
float System::MemoryUtilization() { return LinuxParser::MemoryUtilization(); }
3940

40-
// TODO: Return the operating system name
41-
std::string System::OperatingSystem() { return string(); }
41+
// Return the operating system name
42+
std::string System::OperatingSystem() { return LinuxParser::OperatingSystem(); }
4243

43-
// TODO: Return the number of processes actively running on the system
44-
int System::RunningProcesses() { return 0; }
44+
// Return the number of processes actively running on the system
45+
int System::RunningProcesses() { return LinuxParser::RunningProcesses(); }
4546

46-
// TODO: Return the total number of processes on the system
47-
int System::TotalProcesses() { return 0; }
47+
// Return the total number of processes on the system
48+
int System::TotalProcesses() { return LinuxParser::TotalProcesses(); }
4849

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

0 commit comments

Comments
 (0)