Skip to content

Commit

Permalink
updated project
Browse files Browse the repository at this point in the history
  • Loading branch information
mabustos6 committed Jul 31, 2023
1 parent 7456dca commit cf72922
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 69 deletions.
3 changes: 3 additions & 0 deletions include/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ It contains relevant attributes as shown below
*/
class Process {
public:
Process(int pid);
int Pid(); // TODO: See src/process.cpp
std::string User(); // TODO: See src/process.cpp
std::string Command(); // TODO: See src/process.cpp
Expand All @@ -18,6 +19,8 @@ class Process {

// TODO: Declare any necessary private members
private:
int pid_;
float cpu_utilization_;
};

#endif
34 changes: 33 additions & 1 deletion src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,36 @@ using std::string;
// INPUT: Long int measuring seconds
// OUTPUT: HH:MM:SS
// REMOVE: [[maybe_unused]] once you define the function
string Format::ElapsedTime(long seconds[[maybe_unused]]) { return string(); }
#include <string>

#include "format.h"

using std::string;
using std::to_string;
// TODO: Complete this helper function
// INPUT: Long int measuring seconds
// OUTPUT: HH:MM:SS
// REMOVE: [[maybe_unused]] once you define the function
string Format::ElapsedTime(long seconds) {
int h = 00;
int m = 00;
int s = 00;
string hour, min, sec;

h = seconds / (60 * 60);
m = seconds%(60 * 60) / 60;
s = seconds%(60 * 60) % 60;

if (h < 10) {hour = "0" + to_string(h); }
else {hour = to_string(h); }

if (m < 10) {min = "0" + to_string(m); }
else {min = to_string(m); }

if (s < 10) {sec = "0" + to_string(s); }
else {sec = to_string(s); }

string time = hour + ": " + min + ": " + sec;

return time;
}
246 changes: 209 additions & 37 deletions src/linux_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "linux_parser.h"

using std::stoi;
using std::stof;
using std::string;
using std::to_string;
Expand Down Expand Up @@ -35,15 +36,15 @@ string LinuxParser::OperatingSystem() {

// DONE: An example of how to read data from the filesystem
string LinuxParser::Kernel() {
string os, kernel;
string os, kernel, version;
string line;
std::ifstream stream(kProcDirectory + kVersionFilename);
if (stream.is_open()) {
std::getline(stream, line);
std::istringstream linestream(line);
linestream >> os >> kernel;
linestream >> os >> kernel >> version;
}
return kernel;
return version;
}

// BONUS: Update this to use std::filesystem
Expand All @@ -58,58 +59,229 @@ vector<int> LinuxParser::Pids() {
string filename(file->d_name);
if (std::all_of(filename.begin(), filename.end(), isdigit)) {
int pid = stoi(filename);
pids.push_back(pid);
pids.emplace_back(pid);
}
}
}
closedir(directory);
return pids;
}

// TODO: Read and return the system memory utilization
float LinuxParser::MemoryUtilization() { return 0.0; }

// ? Calculation : (Total memory - free memory) / Total Memory
float LinuxParser::MemoryUtilization() {
string line, label, value;
float total, free;

std::ifstream filestream(kProcDirectory + kMeminfoFilename);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::istringstream linestream(line);
while (linestream >> label >> value) {
if (label == "MemTotal:") {
total = stof(value);
}
if (label == "MemFree:") {
free = stof(value);
}
}
}
}
return (total - free) / total;
}
//use kernel example
// TODO: Read and return the system uptime
long LinuxParser::UpTime() { return 0; }
long LinuxParser::UpTime() {
string uptime, line;
std::ifstream stream(kProcDirectory + kUptimeFilename);
if (stream.is_open()) {
std::getline(stream, line);
std::istringstream linestream(line);
linestream >> uptime;
}
//stol--> converts string to long int
return std::stol(uptime);
}

// TODO: Read and return the number of jiffies for the system
long LinuxParser::Jiffies() { return 0; }
long LinuxParser::Jiffies() {
return LinuxParser::ActiveJiffies() + LinuxParser::IdleJiffies();
}

// Read and return the number of active jiffies for a PID
long LinuxParser::ActiveJiffies(int pid) {
string line, value;
vector<string> values {};
long total;

std::ifstream filestream(kProcDirectory + to_string(pid) + kStatFilename);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::istringstream linestream(line);
while (linestream >> value) {
values.emplace_back(value);
}
}
}
total = stoi(values[13]) + stoi(values[14]) + stoi(values[15]) + stoi(values[16]); // total=utime+stime+cutime+cstime per stack overflow
return total / sysconf(_SC_CLK_TCK);
}

long LinuxParser::ActiveJiffies() {
// Read and return the number of active jiffies for the system
auto jiffies = CpuUtilization();

return stol(jiffies[CPUStates::kUser_]) + stol(jiffies[CPUStates::kNice_]) +
stol(jiffies[CPUStates::kSystem_]) + stol(jiffies[CPUStates::kIRQ_]) +
stol(jiffies[CPUStates::kSoftIRQ_]) +
stol(jiffies[CPUStates::kSteal_]);
}

long LinuxParser::IdleJiffies() {
// Read and return the number of idle jiffies for the system
auto jiffies = CpuUtilization();
return stol(jiffies[CPUStates::kIdle_]) + stol(jiffies[CPUStates::kIOwait_]);
}


vector<string> LinuxParser::CpuUtilization() {
// Read and return CPU utilization
string line, cpu, value;
vector<string> jiffies;
std::ifstream stream(kProcDirectory + kStatFilename);
if (stream.is_open()) {
std::getline(stream, line);
std::istringstream linestream(line);

linestream >> cpu;

while (linestream >> value) {
jiffies.emplace_back(value);
}
}
return jiffies;
}

int LinuxParser::TotalProcesses() {
// Read and return the total number of processes
int processes;
string key, line;
std::ifstream stream(kProcDirectory + kStatFilename);
if (stream.is_open()) {
while (std::getline(stream, line)) {
std::istringstream linestream(line);
linestream >> key;
if (key == "processes") {
linestream >> processes;
break;
}
}
}
return processes;
}

// TODO: Read and return the number of active jiffies for a PID
// REMOVE: [[maybe_unused]] once you define the function
long LinuxParser::ActiveJiffies(int pid[[maybe_unused]]) { return 0; }
int LinuxParser::RunningProcesses() {
// Read and return the number of running processes
int processes;
string key, line;
std::ifstream stream(kProcDirectory + kStatFilename);
if (stream.is_open()) {
while (std::getline(stream, line)) {
std::istringstream linestream(line);
linestream >> key;
if (key == "procs_running") {
linestream >> processes;
break;
}
}
}
return processes;
}

// TODO: Read and return the number of active jiffies for the system
long LinuxParser::ActiveJiffies() { return 0; }

// TODO: Read and return the number of idle jiffies for the system
long LinuxParser::IdleJiffies() { return 0; }
// Read and return the command associated with a process
string LinuxParser::Command(int pid) {
string command;
std::ifstream stream(kProcDirectory + std::to_string(pid) + kCmdlineFilename);
if (stream.is_open()) {
std::getline(stream, command);
}
return command;
}

// TODO: Read and return CPU utilization
vector<string> LinuxParser::CpuUtilization() { return {}; }
// DONE: Read and return the memory used by a process
string LinuxParser::Ram(int pid) {
string line, label, value;
long ram;

// TODO: Read and return the total number of processes
int LinuxParser::TotalProcesses() { return 0; }
std::ifstream filestream(kProcDirectory + to_string(pid) + kStatusFilename);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::istringstream linestream(line);
while (linestream >> label >> value) {
if (label == "VmRSS:") {
ram = stol(value) / 1000;// convert kilobyte value to megabytes
}
}
}
}
return to_string(ram); // covert intram into a string
;
}

// TODO: Read and return the number of running processes
int LinuxParser::RunningProcesses() { return 0; }
// DONE: Read and return the user ID associated with a process
string LinuxParser::Uid(int pid) {
string line, label, value, user;

// TODO: Read and return the command associated with a process
// REMOVE: [[maybe_unused]] once you define the function
string LinuxParser::Command(int pid[[maybe_unused]]) { return string(); }
std::ifstream filestream(kProcDirectory + to_string(pid) + kStatusFilename);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::istringstream linestream(line);
while (linestream >> label >> value) {
if (label == "Uid:") {
user = value;
}
}
}
}
return user;
}

// TODO: Read and return the memory used by a process
// REMOVE: [[maybe_unused]] once you define the function
string LinuxParser::Ram(int pid[[maybe_unused]]) { return string(); }
// DONE: Read and return the user associated with a process
string LinuxParser::User(int pid) {
string line, label, value, id_value, user;
string uid = Uid(pid);

std::ifstream filestream(kPasswordPath);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::replace(line.begin(), line.end(), ':', ' ');
std::istringstream linestream(line);
while (linestream >> label >> value >> id_value) {
if (id_value == uid) {//this matches the balue to the uid
user = label;//gives you the name of the user
}
}
}
}
return user;
}

// TODO: Read and return the user ID associated with a process
// REMOVE: [[maybe_unused]] once you define the function
string LinuxParser::Uid(int pid[[maybe_unused]]) { return string(); }

// TODO: Read and return the user associated with a process
// REMOVE: [[maybe_unused]] once you define the function
string LinuxParser::User(int pid[[maybe_unused]]) { return string(); }
// DONE: Read and return the uptime of a process

// TODO: Read and return the uptime of a process
// REMOVE: [[maybe_unused]] once you define the function
long LinuxParser::UpTime(int pid[[maybe_unused]]) { return 0; }
long LinuxParser::UpTime(int pid) {
string line, value;
vector<string> ticks {};
int upTimePid;
std::ifstream filestream(kProcDirectory + to_string(pid) + kStatFilename);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::istringstream linestream(line);
while (linestream >> value) {
ticks.emplace_back(value);
}
}
}
upTimePid = UpTime() - stol(ticks[21]) / sysconf(_SC_CLK_TCK); // return the starttime (in seconds) per stack overflow
return upTimePid;
}
Loading

0 comments on commit cf72922

Please sign in to comment.