From 63a23f0bb9b5087e267c63f930b0159d25debba0 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sun, 4 Feb 2024 01:12:31 -0800 Subject: [PATCH 01/19] enable OperatingSystem information. --- src/system.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/system.cpp b/src/system.cpp index b15d0116..db518512 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -7,6 +7,7 @@ #include "process.h" #include "processor.h" #include "system.h" +#include "linux_parser.h" using std::set; using std::size_t; @@ -26,7 +27,8 @@ std::string System::Kernel() { return string(); } float System::MemoryUtilization() { return 0.0; } // TODO: Return the operating system name -std::string System::OperatingSystem() { return string(); } +//std::string System::OperatingSystem() { return string(); } +std::string System::OperatingSystem() { return LinuxParser::OperatingSystem(); } // TODO: Return the number of processes actively running on the system int System::RunningProcesses() { return 0; } From 1923d1df4430675b72c4eeb37fb0d0a12c20634e Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sun, 4 Feb 2024 13:22:54 -0800 Subject: [PATCH 02/19] Finish memory utilization function. --- src/linux_parser.cpp | 63 +++++++++++++++++++++++++++++++++++++------- src/system.cpp | 2 +- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 75cfa5d7..d62b658c 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -1,10 +1,12 @@ +#include "linux_parser.h" + #include #include + +#include #include #include -#include "linux_parser.h" - using std::stof; using std::string; using std::to_string; @@ -67,7 +69,50 @@ vector LinuxParser::Pids() { } // TODO: Read and return the system memory utilization -float LinuxParser::MemoryUtilization() { return 0.0; } +// Memory Utilization = (Total Memory - Available Memory) / Total Memory +// Available Memory = Free Memory + Buffers + Cached +float LinuxParser::MemoryUtilization() { + std::ifstream filestream(kProcDirectory + kMeminfoFilename); + std::string line; + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " << kProcDirectory + kMeminfoFilename + << std::endl; + } + std::string currentKey; + std::string currentValue; + float MemUtilization; + float MemAvailable; + std::vector memInfo; + while (getline(filestream, line)) { + std::istringstream iss(line); + // read info + iss >> currentKey >> currentValue; + if (currentKey == "MemTotal:") { + // convert from kb to mb + float val = stof(currentValue) / 1024 / 1024; + memInfo.push_back(val); + } + if (currentKey == "MemFree:") { + // convert from kb to mb + float val = stof(currentValue) / 1024 / 1024; + memInfo.push_back(val); + } + if (currentKey == "Buffers:") { + // convert from kb to mb + float val = stof(currentValue) / 1024 / 1024; + memInfo.push_back(val); + } + if (currentKey == "Cached:") { + // convert from kb to mb + float val = stof(currentValue) / 1024 / 1024; + memInfo.push_back(val); + } + } + MemAvailable = memInfo[1] + memInfo[2] + memInfo[3]; + MemUtilization = (((memInfo[0]) - (MemAvailable)) / (memInfo[0]) ) * 100; + + return MemUtilization; +} // TODO: Read and return the system uptime long LinuxParser::UpTime() { return 0; } @@ -77,7 +122,7 @@ long LinuxParser::Jiffies() { return 0; } // 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; } +long LinuxParser::ActiveJiffies(int pid [[maybe_unused]]) { return 0; } // TODO: Read and return the number of active jiffies for the system long LinuxParser::ActiveJiffies() { return 0; } @@ -96,20 +141,20 @@ int LinuxParser::RunningProcesses() { return 0; } // 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(); } +string LinuxParser::Command(int pid [[maybe_unused]]) { return string(); } // 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(); } +string LinuxParser::Ram(int pid [[maybe_unused]]) { return string(); } // 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(); } +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(); } +string LinuxParser::User(int pid [[maybe_unused]]) { return string(); } // 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 [[maybe_unused]]) { return 0; } diff --git a/src/system.cpp b/src/system.cpp index db518512..d563d555 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -24,7 +24,7 @@ vector& System::Processes() { return processes_; } std::string System::Kernel() { return string(); } // TODO: Return the system's memory utilization -float System::MemoryUtilization() { return 0.0; } +float System::MemoryUtilization() { return LinuxParser::MemoryUtilization(); } // TODO: Return the operating system name //std::string System::OperatingSystem() { return string(); } From f36c618ba403787ee9f9db4b18dd9f38a7a84d4f Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sun, 4 Feb 2024 21:40:33 -0800 Subject: [PATCH 03/19] enable TotalProcesses function. --- src/linux_parser.cpp | 22 +++++++++++++++++++++- src/system.cpp | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index d62b658c..b4f924a9 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -134,7 +134,27 @@ long LinuxParser::IdleJiffies() { return 0; } vector LinuxParser::CpuUtilization() { return {}; } // TODO: Read and return the total number of processes -int LinuxParser::TotalProcesses() { return 0; } +int LinuxParser::TotalProcesses() { + std::ifstream filestream(kProcDirectory + kStatFilename); + std::string line; + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename + << std::endl; + } + std::string currentKey; + std::string currentValue; + int totalProcesses; +while (getline(filestream, line)) { + std::istringstream iss(line); + // read info + iss >> currentKey >> currentValue; + if (currentKey == "processes") { + totalProcesses = std::stoi(currentValue); + } + break; + } + + return totalProcesses; } // TODO: Read and return the number of running processes int LinuxParser::RunningProcesses() { return 0; } diff --git a/src/system.cpp b/src/system.cpp index d563d555..1731a8dd 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -34,7 +34,7 @@ std::string System::OperatingSystem() { return LinuxParser::OperatingSystem(); } int System::RunningProcesses() { return 0; } // TODO: Return the total number of processes on the system -int System::TotalProcesses() { return 0; } +int System::TotalProcesses() { return LinuxParser::TotalProcesses(); } // TODO: Return the number of seconds since the system started running long int System::UpTime() { return 0; } \ No newline at end of file From 1db99f546b47b7681219d807172d3cd2e8200705 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Mon, 5 Feb 2024 09:41:48 -0800 Subject: [PATCH 04/19] update some code. --- CMakeLists.txt | 2 +- include/process.h | 7 +++ src/linux_parser.cpp | 104 +++++++++++++++++++++++++++++++++++++++---- src/process.cpp | 2 +- src/system.cpp | 49 +++++++++++++------- 5 files changed, 138 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2702e965..5a193420 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8.12) project(monitor) set(CURSES_NEED_NCURSES TRUE) diff --git a/include/process.h b/include/process.h index 6b7d1c22..8c86e32c 100644 --- a/include/process.h +++ b/include/process.h @@ -8,6 +8,7 @@ It contains relevant attributes as shown below */ class Process { public: + Process(int pid): pid_(pid) {}; int Pid(); // TODO: See src/process.cpp std::string User(); // TODO: See src/process.cpp std::string Command(); // TODO: See src/process.cpp @@ -18,6 +19,12 @@ class Process { // TODO: Declare any necessary private members private: + int pid_; + std::string user_; + std::string command_; + float cpuUtilization_; + std::string ram_; + long int upTime_; }; #endif \ No newline at end of file diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index b4f924a9..0a905b70 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -68,7 +68,7 @@ vector LinuxParser::Pids() { return pids; } -// TODO: Read and return the system memory utilization +// DONE: Read and return the system memory utilization // Memory Utilization = (Total Memory - Available Memory) / Total Memory // Available Memory = Free Memory + Buffers + Cached float LinuxParser::MemoryUtilization() { @@ -114,8 +114,24 @@ float LinuxParser::MemoryUtilization() { return MemUtilization; } -// TODO: Read and return the system uptime -long LinuxParser::UpTime() { return 0; } +// DONE: Read and return the system uptime +long LinuxParser::UpTime() { + std::ifstream filestream(kProcDirectory + kUptimeFilename); + std::string line; + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " << kProcDirectory + kUptimeFilename + << std::endl; + } + long uptime; + while (getline(filestream, line)) { + std::istringstream iss(line); + // read info + std::string currentValue; + iss >> currentValue; + uptime = std::stoi(currentValue); + break; + } + return uptime; } // TODO: Read and return the number of jiffies for the system long LinuxParser::Jiffies() { return 0; } @@ -133,7 +149,7 @@ long LinuxParser::IdleJiffies() { return 0; } // TODO: Read and return CPU utilization vector LinuxParser::CpuUtilization() { return {}; } -// TODO: Read and return the total number of processes +// DONE: Read and return the total number of processes int LinuxParser::TotalProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; @@ -156,8 +172,28 @@ while (getline(filestream, line)) { return totalProcesses; } -// TODO: Read and return the number of running processes -int LinuxParser::RunningProcesses() { return 0; } +// DONE: Read and return the number of running processes +int LinuxParser::RunningProcesses() { + std::ifstream filestream(kProcDirectory + kStatFilename); + std::string line; + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename + << std::endl; + } + std::string currentKey; + std::string currentValue; + int runningProcesses; +while (getline(filestream, line)) { + std::istringstream iss(line); + // read info + iss >> currentKey >> currentValue; + if (currentKey == "procs_running") { + runningProcesses = std::stoi(currentValue); + } + break; + } + + return runningProcesses; } // TODO: Read and return the command associated with a process // REMOVE: [[maybe_unused]] once you define the function @@ -169,12 +205,62 @@ string LinuxParser::Ram(int pid [[maybe_unused]]) { return string(); } // 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(); } +string LinuxParser::Uid(int pid) { + string line, key, uid; + std::ifstream stream(kProcDirectory + std::to_string(pid) + kStatusFilename); + if (stream.is_open()) { + while (std::getline(stream, line)) { + std::istringstream linestream(line); + linestream >> key; + if (key == "Uid:") { + linestream >> uid; + break; + } + } + } + return uid; + } // 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(); } +string LinuxParser::User(int pid) { + string uid = Uid(pid); + string id, x, temp, line; + string name = "DEFAULT"; + std::ifstream stream(kPasswordPath); + if (stream.is_open()) { + while (std::getline(stream, line)) { + std::replace(line.begin(), line.end(), ':', ' '); + std::istringstream linestream(line); + + linestream >> temp >> x >> id; + if (id == uid) { + name = temp; + break; + } + } + } + return name; + } // 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) { + std::string line, value; + vector values; + long starttime = 0; + std::ifstream stream(kProcDirectory + std::to_string(pid) + kStatFilename); + if (stream.is_open()) { + std::getline(stream, line); + std::istringstream linestream(line); + while (linestream >> value) { + values.push_back(value); + } + } + try { + starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); + } catch (...) { + starttime = 0; + } + return starttime; + } diff --git a/src/process.cpp b/src/process.cpp index 82119905..1e767f9a 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -11,7 +11,7 @@ using std::to_string; using std::vector; // TODO: Return this process's ID -int Process::Pid() { return 0; } +int Process::Pid() { return pid_; } // TODO: Return this process's CPU utilization float Process::CpuUtilization() { return 0; } diff --git a/src/system.cpp b/src/system.cpp index 1731a8dd..6a829516 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,13 +1,15 @@ +#include "system.h" + #include + #include #include #include #include +#include "linux_parser.h" #include "process.h" #include "processor.h" -#include "system.h" -#include "linux_parser.h" using std::set; using std::size_t; @@ -18,23 +20,40 @@ using std::vector; Processor& System::Cpu() { return cpu_; } // TODO: Return a container composed of the system's processes -vector& System::Processes() { return processes_; } - -// TODO: Return the system's kernel identifier (string) -std::string System::Kernel() { return string(); } - -// TODO: Return the system's memory utilization +vector& System::Processes() { + vector pids = LinuxParser::Pids(); + for (std::vector::iterator it = pids.begin(); it != pids.end(); it++) { + int pidIndex = *it; + std::string userVal = LinuxParser::User(pidIndex); + // LinuxParser::Command(pidIndex); + // LinuxParser::Ram(pidIndex); + long upTimeVal = LinuxParser::UpTime(pidIndex); + std::string uidVal = LinuxParser::Uid(pidIndex); + // LinuxParser::CpuUtilization(pidIndex); + Process process(pidIndex); // create a process object + processes_.push_back(process); // add the process object to the vector + // processes_.push_back(process); // add the process object to the vector + + }; + + return processes_; +} + +// DONE: Return the system's kernel identifier (string) +std::string System::Kernel() { return LinuxParser::Kernel(); } + +// DONE: Return the system's memory utilization float System::MemoryUtilization() { return LinuxParser::MemoryUtilization(); } -// TODO: Return the operating system name -//std::string System::OperatingSystem() { return string(); } +// DONE: Return the operating system name +// std::string System::OperatingSystem() { return string(); } std::string System::OperatingSystem() { return LinuxParser::OperatingSystem(); } -// TODO: Return the number of processes actively running on the system -int System::RunningProcesses() { return 0; } +// DONE: Return the number of processes actively running on the system +int System::RunningProcesses() { return LinuxParser::TotalProcesses(); } -// TODO: Return the total number of processes on the system +// DONE: Return the total number of processes on the system int System::TotalProcesses() { return LinuxParser::TotalProcesses(); } -// TODO: Return the number of seconds since the system started running -long int System::UpTime() { return 0; } \ No newline at end of file +// DONE: Return the number of seconds since the system started running +long int System::UpTime() { return LinuxParser::UpTime(); } \ No newline at end of file From 16686f0a53358906e58c8e04d70320ed5a761ab7 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Mon, 5 Feb 2024 18:34:53 -0800 Subject: [PATCH 05/19] edit linux_parser.cpp --- src/linux_parser.cpp | 186 ++++++++++++++++++++++++++++--------------- 1 file changed, 121 insertions(+), 65 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 0a905b70..50a04207 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -109,14 +109,14 @@ float LinuxParser::MemoryUtilization() { } } MemAvailable = memInfo[1] + memInfo[2] + memInfo[3]; - MemUtilization = (((memInfo[0]) - (MemAvailable)) / (memInfo[0]) ) * 100; + MemUtilization = (((memInfo[0]) - (MemAvailable)) / (memInfo[0])) * 100; return MemUtilization; } // DONE: Read and return the system uptime -long LinuxParser::UpTime() { - std::ifstream filestream(kProcDirectory + kUptimeFilename); +long LinuxParser::UpTime() { + std::ifstream filestream(kProcDirectory + kUptimeFilename); std::string line; if (!filestream.is_open()) { std::cerr << "Failed to open file: " << kProcDirectory + kUptimeFilename @@ -130,8 +130,9 @@ long LinuxParser::UpTime() { iss >> currentValue; uptime = std::stoi(currentValue); break; - } - return uptime; } + } + return uptime; +} // TODO: Read and return the number of jiffies for the system long LinuxParser::Jiffies() { return 0; } @@ -150,7 +151,7 @@ long LinuxParser::IdleJiffies() { return 0; } vector LinuxParser::CpuUtilization() { return {}; } // DONE: Read and return the total number of processes -int LinuxParser::TotalProcesses() { +int LinuxParser::TotalProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { @@ -160,7 +161,7 @@ int LinuxParser::TotalProcesses() { std::string currentKey; std::string currentValue; int totalProcesses; -while (getline(filestream, line)) { + while (getline(filestream, line)) { std::istringstream iss(line); // read info iss >> currentKey >> currentValue; @@ -170,10 +171,11 @@ while (getline(filestream, line)) { break; } - return totalProcesses; } + return totalProcesses; +} // DONE: Read and return the number of running processes -int LinuxParser::RunningProcesses() { +int LinuxParser::RunningProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { @@ -183,7 +185,7 @@ int LinuxParser::RunningProcesses() { std::string currentKey; std::string currentValue; int runningProcesses; -while (getline(filestream, line)) { + while (getline(filestream, line)) { std::istringstream iss(line); // read info iss >> currentKey >> currentValue; @@ -193,74 +195,128 @@ while (getline(filestream, line)) { break; } - return runningProcesses; } - -// 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(); } + return runningProcesses; +} -// 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 command associated with a process +string LinuxParser::Command(int pid) { + std::ifstream filestream(kProcDirectory + std::to_string(pid) + + kCmdlineFilename); + std::string line; + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " + << kProcDirectory + std::to_string(pid) + kCmdlineFilename + << std::endl; + } + std::string command; + while (getline(filestream, line)) { + std::istringstream iss(line); + // read info + iss >> command; + break; + } + return command; +} -// TODO: Read and return the user ID associated with a process -// REMOVE: [[maybe_unused]] once you define the function -string LinuxParser::Uid(int pid) { - string line, key, uid; - std::ifstream stream(kProcDirectory + std::to_string(pid) + kStatusFilename); - if (stream.is_open()) { - while (std::getline(stream, line)) { - std::istringstream linestream(line); - linestream >> key; - if (key == "Uid:") { - linestream >> uid; - break; +// DONE: Read and return the memory used by a process +string LinuxParser::Ram(int pid) { + std::string line; + std::ifstream filestream(kProcDirectory + std::to_string(pid) + + kStatusFilename); + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " + << kProcDirectory + std::to_string(pid) + kStatusFilename + << std::endl; + } + std::string currentKey; + std::string currentValue; + std::string ram; + while (std::getline(filestream, line)) { + std::istringstream iss(line); + while (iss >> currentKey >> currentValue) { + if (currentKey == "VmSize:") { + ram = currentValue; } + break; + } + } + + return ram; +} + +// DONE: Read and return the user ID associated with a process +string LinuxParser::Uid(int pid) { + std::string line; + std::ifstream filestream(kProcDirectory + std::to_string(pid) + + kStatusFilename); + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " + << kProcDirectory + std::to_string(pid) + kStatusFilename + << std::endl; + } + std::string currentKey; + std::string currentValue; + std::string uid; + while (std::getline(filestream, line)) { + std::istringstream iss(line); + iss >> currentKey; + if (currentKey == "Uid:") { + iss >> currentValue; + uid = currentValue; + break; } } + return uid; - } +} -// TODO: Read and return the user associated with a process -// REMOVE: [[maybe_unused]] once you define the function -string LinuxParser::User(int pid) { - string uid = Uid(pid); - string id, x, temp, line; - string name = "DEFAULT"; - std::ifstream stream(kPasswordPath); - if (stream.is_open()) { - while (std::getline(stream, line)) { - std::replace(line.begin(), line.end(), ':', ' '); - std::istringstream linestream(line); +// DONE: Read and return the user associated with a process +string LinuxParser::User(int pid) { + std::string uid = Uid(pid); + std::string name; + std::ifstream filestream(kPasswordPath); + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " << kPasswordPath << std::endl; + } + std::string temp; + std::string user; + std::string x; + std::string line; + std::string id; - linestream >> temp >> x >> id; - if (id == uid) { - name = temp; - break; - } + while (std::getline(filestream, line)) { + std::replace(line.begin(), line.end(), ':', ' '); + std::istringstream iss(line); + iss >> user >> x >> id; + if (id == uid) { + name = temp; + break; } } + return name; - } +} -// TODO: Read and return the uptime of a process -// REMOVE: [[maybe_unused]] once you define the function -long LinuxParser::UpTime(int pid) { - std::string line, value; - vector values; +// DONE: Read and return the uptime of a process +long LinuxParser::UpTime(int pid) { long starttime = 0; - std::ifstream stream(kProcDirectory + std::to_string(pid) + kStatFilename); - if (stream.is_open()) { - std::getline(stream, line); - std::istringstream linestream(line); - while (linestream >> value) { - values.push_back(value); - } + std::ifstream filestream(kProcDirectory + std::to_string(pid) + + kStatFilename); + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " + << kProcDirectory + std::to_string(pid) + kStatFilename + << std::endl; } - try { - starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); - } catch (...) { - starttime = 0; + std::string line; + std::string cuurentValue; + vector values; + std::getline(filestream, line); + std::istringstream iss(line); + while (iss >> cuurentValue) { + values.push_back(cuurentValue); } + + starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); + return starttime; - } +} From e8dbf9b78f68cd0009a9b055d5251b6c1526ccfb Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Mon, 5 Feb 2024 18:57:02 -0800 Subject: [PATCH 06/19] update CpuUtilization(). --- src/linux_parser.cpp | 33 ++++++++++++++++++++++++++++----- src/system.cpp | 8 +++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 50a04207..dfd4114e 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -147,8 +147,28 @@ long LinuxParser::ActiveJiffies() { return 0; } // TODO: Read and return the number of idle jiffies for the system long LinuxParser::IdleJiffies() { return 0; } -// TODO: Read and return CPU utilization -vector LinuxParser::CpuUtilization() { return {}; } +// DONE: Read and return CPU utilization +vector LinuxParser::CpuUtilization() { + std::ifstream filestream(kProcDirectory + kStatFilename); + std::string line; + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename + << std::endl; + } + std::string currentKey; + std::string currentValue; + vector cpuUtilization; + while (getline(filestream, line)) { + std::istringstream iss(line); + iss >> currentKey; + if (currentKey == "cpu") { + while (iss >> currentValue) { + cpuUtilization.push_back(currentValue); + } + } + } + return cpuUtilization; + } // DONE: Read and return the total number of processes int LinuxParser::TotalProcesses() { @@ -315,8 +335,11 @@ long LinuxParser::UpTime(int pid) { while (iss >> cuurentValue) { values.push_back(cuurentValue); } - - starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); - + + try { + starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); + } catch (...) { + starttime = 0; + } return starttime; } diff --git a/src/system.cpp b/src/system.cpp index 6a829516..064d2efa 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -25,15 +25,13 @@ vector& System::Processes() { for (std::vector::iterator it = pids.begin(); it != pids.end(); it++) { int pidIndex = *it; std::string userVal = LinuxParser::User(pidIndex); - // LinuxParser::Command(pidIndex); - // LinuxParser::Ram(pidIndex); + std::string commandVal = LinuxParser::Command(pidIndex); + std::string ramVal = LinuxParser::Ram(pidIndex); long upTimeVal = LinuxParser::UpTime(pidIndex); std::string uidVal = LinuxParser::Uid(pidIndex); - // LinuxParser::CpuUtilization(pidIndex); + vector cpuUtilVale = LinuxParser::CpuUtilization(); Process process(pidIndex); // create a process object processes_.push_back(process); // add the process object to the vector - // processes_.push_back(process); // add the process object to the vector - }; return processes_; From 78190b424d2a1ceeedb25d471fd648532d92f6dd Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Tue, 6 Feb 2024 01:28:58 -0800 Subject: [PATCH 07/19] update format.cpp, linux_parser.cpp and system.cpp --- src/format.cpp | 13 ++++++++----- src/linux_parser.cpp | 2 +- src/system.cpp | 5 ++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/format.cpp b/src/format.cpp index 8f8f854b..f14c94f1 100644 --- a/src/format.cpp +++ b/src/format.cpp @@ -1,11 +1,14 @@ -#include - #include "format.h" +#include + using std::string; -// TODO: Complete this helper function +// DONE: 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[[maybe_unused]]) { return string(); } \ No newline at end of file +string Format::ElapsedTime(long seconds) { + return std::to_string(seconds / 3600) + ":" + + std::to_string((seconds % 3600) / 60) + ":" + + std::to_string(seconds % 60); +} \ No newline at end of file diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index dfd4114e..b7a17eaf 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -309,7 +309,7 @@ string LinuxParser::User(int pid) { std::istringstream iss(line); iss >> user >> x >> id; if (id == uid) { - name = temp; + name = user; break; } } diff --git a/src/system.cpp b/src/system.cpp index 064d2efa..4610e3f1 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -30,8 +30,8 @@ vector& System::Processes() { long upTimeVal = LinuxParser::UpTime(pidIndex); std::string uidVal = LinuxParser::Uid(pidIndex); vector cpuUtilVale = LinuxParser::CpuUtilization(); - Process process(pidIndex); // create a process object - processes_.push_back(process); // add the process object to the vector + Process process(pidIndex); + processes_.push_back(process); }; return processes_; @@ -44,7 +44,6 @@ std::string System::Kernel() { return LinuxParser::Kernel(); } float System::MemoryUtilization() { return LinuxParser::MemoryUtilization(); } // DONE: Return the operating system name -// std::string System::OperatingSystem() { return string(); } std::string System::OperatingSystem() { return LinuxParser::OperatingSystem(); } // DONE: Return the number of processes actively running on the system From 6e95064e5470ec1a01e3443e06c3f911c07650cd Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Tue, 6 Feb 2024 11:42:25 -0800 Subject: [PATCH 08/19] edit system::Process(). --- src/linux_parser.cpp | 28 ++++++++-------------------- src/process.cpp | 9 +++++---- src/system.cpp | 6 +++++- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index b7a17eaf..384afd42 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -85,25 +85,20 @@ float LinuxParser::MemoryUtilization() { std::vector memInfo; while (getline(filestream, line)) { std::istringstream iss(line); - // read info iss >> currentKey >> currentValue; if (currentKey == "MemTotal:") { - // convert from kb to mb float val = stof(currentValue) / 1024 / 1024; memInfo.push_back(val); } if (currentKey == "MemFree:") { - // convert from kb to mb float val = stof(currentValue) / 1024 / 1024; memInfo.push_back(val); } if (currentKey == "Buffers:") { - // convert from kb to mb float val = stof(currentValue) / 1024 / 1024; memInfo.push_back(val); } if (currentKey == "Cached:") { - // convert from kb to mb float val = stof(currentValue) / 1024 / 1024; memInfo.push_back(val); } @@ -148,7 +143,7 @@ long LinuxParser::ActiveJiffies() { return 0; } long LinuxParser::IdleJiffies() { return 0; } // DONE: Read and return CPU utilization -vector LinuxParser::CpuUtilization() { +vector LinuxParser::CpuUtilization() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { @@ -168,7 +163,7 @@ vector LinuxParser::CpuUtilization() { } } return cpuUtilization; - } +} // DONE: Read and return the total number of processes int LinuxParser::TotalProcesses() { @@ -220,21 +215,14 @@ int LinuxParser::RunningProcesses() { // DONE: Read and return the command associated with a process string LinuxParser::Command(int pid) { + std::string command; std::ifstream filestream(kProcDirectory + std::to_string(pid) + kCmdlineFilename); - std::string line; - if (!filestream.is_open()) { - std::cerr << "Failed to open file: " - << kProcDirectory + std::to_string(pid) + kCmdlineFilename - << std::endl; - } - std::string command; - while (getline(filestream, line)) { - std::istringstream iss(line); - // read info - iss >> command; - break; + if (filestream.is_open()) { + std::getline(filestream, command); + filestream.close(); } + return command; } @@ -335,7 +323,7 @@ long LinuxParser::UpTime(int pid) { while (iss >> cuurentValue) { values.push_back(cuurentValue); } - + try { starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); } catch (...) { diff --git a/src/process.cpp b/src/process.cpp index 1e767f9a..be5868c3 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -5,6 +5,7 @@ #include #include "process.h" +#include "linux_parser.h" using std::string; using std::to_string; @@ -14,16 +15,16 @@ using std::vector; int Process::Pid() { return pid_; } // TODO: Return this process's CPU utilization -float Process::CpuUtilization() { return 0; } +float Process::CpuUtilization() { return cpuUtilization_; } // TODO: Return the command that generated this process -string Process::Command() { return string(); } +string Process::Command() { return command_ = LinuxParser::Command(pid_); } // TODO: Return this process's memory utilization -string Process::Ram() { return string(); } +string Process::Ram() { return ram_ = LinuxParser::Ram(pid_); } // TODO: Return the user (name) that generated this process -string Process::User() { return string(); } +string Process::User() { return user_ = LinuxParser::User(pid_); } // TODO: Return the age of this process (in seconds) long int Process::UpTime() { return 0; } diff --git a/src/system.cpp b/src/system.cpp index 4610e3f1..9677b0b6 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -30,7 +30,11 @@ vector& System::Processes() { long upTimeVal = LinuxParser::UpTime(pidIndex); std::string uidVal = LinuxParser::Uid(pidIndex); vector cpuUtilVale = LinuxParser::CpuUtilization(); - Process process(pidIndex); + Process process(pidIndex); + std::string ramVal0 = process.Ram(); + std::string userVal0 = process.User(); + std::string commandVal0 = process.Command(); + float cpuUVal0 = process.CpuUtilization(); processes_.push_back(process); }; From 7de49e6e4716322577705b8792c650191d669b8a Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Tue, 6 Feb 2024 13:33:43 -0800 Subject: [PATCH 09/19] fix runningProcesses issue. --- include/processor.h | 2 +- src/linux_parser.cpp | 30 +++++++++++++++++++++++++++--- src/system.cpp | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/include/processor.h b/include/processor.h index 6951a659..2d3f439d 100644 --- a/include/processor.h +++ b/include/processor.h @@ -9,4 +9,4 @@ class Processor { private: }; -#endif \ No newline at end of file +#endif diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 384afd42..26b350b8 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -153,12 +153,37 @@ vector LinuxParser::CpuUtilization() { std::string currentKey; std::string currentValue; vector cpuUtilization; + double cpuUtilization0; while (getline(filestream, line)) { + // PrevIdle = previdle + previowait + // Idle = idle + iowait + + // PrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + + // prevsteal NonIdle = user + nice + system + irq + softirq + steal + + // PrevTotal = PrevIdle + PrevNonIdle + // Total = Idle + NonIdle + + // # differentiate: actual value minus the previous one + // totald = Total - PrevTotal + // idled = Idle - PrevIdle + + // CPU_Percentage = (totald - idled)/totald + std::istringstream iss(line); iss >> currentKey; if (currentKey == "cpu") { - while (iss >> currentValue) { - cpuUtilization.push_back(currentValue); + int user, nice, system, idle, iowait, irq, softirq, steal, guest, + guest_nice; + + while (iss >> user >> nice >> system >> idle >> iowait >> irq >> + softirq >> steal >> guest >> guest_nice) { + int totalIdle = idle + iowait; + int totalNonIdle = user + nice + system + irq + softirq + steal; + int total = totalIdle + totalNonIdle; + + cpuUtilization0 = + (total - totalIdle) / static_cast(total) * 100.0; } } } @@ -207,7 +232,6 @@ int LinuxParser::RunningProcesses() { if (currentKey == "procs_running") { runningProcesses = std::stoi(currentValue); } - break; } return runningProcesses; diff --git a/src/system.cpp b/src/system.cpp index 9677b0b6..8a3c2fd4 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -51,7 +51,7 @@ float System::MemoryUtilization() { return LinuxParser::MemoryUtilization(); } std::string System::OperatingSystem() { return LinuxParser::OperatingSystem(); } // DONE: Return the number of processes actively running on the system -int System::RunningProcesses() { return LinuxParser::TotalProcesses(); } +int System::RunningProcesses() { return LinuxParser::RunningProcesses(); } // DONE: Return the total number of processes on the system int System::TotalProcesses() { return LinuxParser::TotalProcesses(); } From ccecee4a8fb41d443111e4fa4b5c6abd93482325 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Tue, 6 Feb 2024 15:03:07 -0800 Subject: [PATCH 10/19] fix ram unit issue. --- src/linux_parser.cpp | 3 ++- src/system.cpp | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 26b350b8..f8f1daff 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -267,7 +268,7 @@ string LinuxParser::Ram(int pid) { std::istringstream iss(line); while (iss >> currentKey >> currentValue) { if (currentKey == "VmSize:") { - ram = currentValue; + ram = to_string( (int) std::floor(stof(currentValue) / 1024)); } break; } diff --git a/src/system.cpp b/src/system.cpp index 8a3c2fd4..9662bead 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -21,6 +21,7 @@ Processor& System::Cpu() { return cpu_; } // TODO: Return a container composed of the system's processes vector& System::Processes() { + processes_.clear(); vector pids = LinuxParser::Pids(); for (std::vector::iterator it = pids.begin(); it != pids.end(); it++) { int pidIndex = *it; @@ -30,12 +31,12 @@ vector& System::Processes() { long upTimeVal = LinuxParser::UpTime(pidIndex); std::string uidVal = LinuxParser::Uid(pidIndex); vector cpuUtilVale = LinuxParser::CpuUtilization(); - Process process(pidIndex); - std::string ramVal0 = process.Ram(); - std::string userVal0 = process.User(); + Process process(pidIndex); + std::string ramVal0 = process.Ram(); + std::string userVal0 = process.User(); std::string commandVal0 = process.Command(); float cpuUVal0 = process.CpuUtilization(); - processes_.push_back(process); + processes_.push_back(process); }; return processes_; From 219d3bddd586d8d1f6119c82049f4b991227b1a3 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Tue, 6 Feb 2024 22:42:15 -0800 Subject: [PATCH 11/19] fix uptime and memory%. --- src/linux_parser.cpp | 107 ++++++++++++++++++++++--------------------- src/process.cpp | 2 +- src/system.cpp | 12 ++--- 3 files changed, 62 insertions(+), 59 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index f8f1daff..240aff23 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -105,7 +105,7 @@ float LinuxParser::MemoryUtilization() { } } MemAvailable = memInfo[1] + memInfo[2] + memInfo[3]; - MemUtilization = (((memInfo[0]) - (MemAvailable)) / (memInfo[0])) * 100; + MemUtilization = (((memInfo[0]) - (MemAvailable)) / (memInfo[0])); return MemUtilization; } @@ -154,40 +154,23 @@ vector LinuxParser::CpuUtilization() { std::string currentKey; std::string currentValue; vector cpuUtilization; - double cpuUtilization0; - while (getline(filestream, line)) { - // PrevIdle = previdle + previowait - // Idle = idle + iowait - - // PrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + - // prevsteal NonIdle = user + nice + system + irq + softirq + steal +if (filestream.is_open()) { + std::string line; + std::getline(filestream, line); - // PrevTotal = PrevIdle + PrevNonIdle - // Total = Idle + NonIdle - - // # differentiate: actual value minus the previous one - // totald = Total - PrevTotal - // idled = Idle - PrevIdle + std::istringstream linestream(line); + std::string token; - // CPU_Percentage = (totald - idled)/totald + // Skip the first token "cpu" + linestream >> token; - std::istringstream iss(line); - iss >> currentKey; - if (currentKey == "cpu") { - int user, nice, system, idle, iowait, irq, softirq, steal, guest, - guest_nice; - - while (iss >> user >> nice >> system >> idle >> iowait >> irq >> - softirq >> steal >> guest >> guest_nice) { - int totalIdle = idle + iowait; - int totalNonIdle = user + nice + system + irq + softirq + steal; - int total = totalIdle + totalNonIdle; - - cpuUtilization0 = - (total - totalIdle) / static_cast(total) * 100.0; - } + while (linestream >> token) { + cpuUtilization.push_back(token); } } + + filestream.close(); + return cpuUtilization; } @@ -331,28 +314,48 @@ string LinuxParser::User(int pid) { } // DONE: Read and return the uptime of a process +// long LinuxParser::UpTime(int pid) { +// long starttime = 0; +// std::ifstream filestream(kProcDirectory + std::to_string(pid) + +// kStatFilename); +// if (!filestream.is_open()) { +// std::cerr << "Failed to open file: " +// << kProcDirectory + std::to_string(pid) + kStatFilename +// << std::endl; +// } +// std::string line; +// std::string cuurentValue; +// vector values; +// std::getline(filestream, line); +// std::istringstream iss(line); +// while (iss >> cuurentValue) { +// values.push_back(cuurentValue); +// } + +// try { +// starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); +// } catch (...) { +// starttime = 0; +// } +// return starttime; +// } long LinuxParser::UpTime(int pid) { - long starttime = 0; - std::ifstream filestream(kProcDirectory + std::to_string(pid) + - kStatFilename); - if (!filestream.is_open()) { - std::cerr << "Failed to open file: " - << kProcDirectory + std::to_string(pid) + kStatFilename - << std::endl; - } - std::string line; - std::string cuurentValue; - vector values; - std::getline(filestream, line); - std::istringstream iss(line); - while (iss >> cuurentValue) { - values.push_back(cuurentValue); - } - - try { - starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); - } catch (...) { - starttime = 0; + string word; + string line; + std::ifstream stream(kProcDirectory + to_string(pid) + kStatFilename); + if (stream.is_open()) { + std::getline(stream, line); + std::istringstream linestream(line); + int count{1}; + while (getline(linestream, word, ' ')) { + if (count == 22) { + long value = stol(word) / sysconf(_SC_CLK_TCK); + return value; + } + count++; + } } - return starttime; } + + + diff --git a/src/process.cpp b/src/process.cpp index be5868c3..59575807 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -27,7 +27,7 @@ string Process::Ram() { return ram_ = LinuxParser::Ram(pid_); } string Process::User() { return user_ = LinuxParser::User(pid_); } // TODO: Return the age of this process (in seconds) -long int Process::UpTime() { return 0; } +long int Process::UpTime() { return upTime_ = LinuxParser::UpTime(pid_); } // TODO: Overload the "less than" comparison operator for Process objects // REMOVE: [[maybe_unused]] once you define the function diff --git a/src/system.cpp b/src/system.cpp index 9662bead..03f21ef1 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -25,12 +25,12 @@ vector& System::Processes() { vector pids = LinuxParser::Pids(); for (std::vector::iterator it = pids.begin(); it != pids.end(); it++) { int pidIndex = *it; - std::string userVal = LinuxParser::User(pidIndex); - std::string commandVal = LinuxParser::Command(pidIndex); - std::string ramVal = LinuxParser::Ram(pidIndex); - long upTimeVal = LinuxParser::UpTime(pidIndex); - std::string uidVal = LinuxParser::Uid(pidIndex); - vector cpuUtilVale = LinuxParser::CpuUtilization(); + // std::string userVal = LinuxParser::User(pidIndex); + // std::string commandVal = LinuxParser::Command(pidIndex); + // std::string ramVal = LinuxParser::Ram(pidIndex); + // long upTimeVal = LinuxParser::UpTime(pidIndex); + // std::string uidVal = LinuxParser::Uid(pidIndex); + // vector cpuUtilVale = LinuxParser::CpuUtilization(); Process process(pidIndex); std::string ramVal0 = process.Ram(); std::string userVal0 = process.User(); From c0e1a293d3ea5f6c07a8e6c86da55ee6cd88f0ff Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Tue, 6 Feb 2024 23:40:13 -0800 Subject: [PATCH 12/19] update linuxParser::CpuUtilization function. --- src/linux_parser.cpp | 86 +++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 57 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 240aff23..0d97b53d 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -145,35 +145,24 @@ long LinuxParser::IdleJiffies() { return 0; } // DONE: Read and return CPU utilization vector LinuxParser::CpuUtilization() { + vector jiffies; std::ifstream filestream(kProcDirectory + kStatFilename); - std::string line; if (!filestream.is_open()) { std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename << std::endl; } - std::string currentKey; - std::string currentValue; - vector cpuUtilization; -if (filestream.is_open()) { - std::string line; + std::string line, cpu, value; + if (filestream.is_open()) { std::getline(filestream, line); - std::istringstream linestream(line); - std::string token; - - // Skip the first token "cpu" - linestream >> token; - - while (linestream >> token) { - cpuUtilization.push_back(token); + linestream >> cpu; + while (linestream >> value) { + jiffies.push_back(value); } } - - filestream.close(); - - return cpuUtilization; } + // DONE: Read and return the total number of processes int LinuxParser::TotalProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); @@ -314,47 +303,30 @@ string LinuxParser::User(int pid) { } // DONE: Read and return the uptime of a process -// long LinuxParser::UpTime(int pid) { -// long starttime = 0; -// std::ifstream filestream(kProcDirectory + std::to_string(pid) + -// kStatFilename); -// if (!filestream.is_open()) { -// std::cerr << "Failed to open file: " -// << kProcDirectory + std::to_string(pid) + kStatFilename -// << std::endl; -// } -// std::string line; -// std::string cuurentValue; -// vector values; -// std::getline(filestream, line); -// std::istringstream iss(line); -// while (iss >> cuurentValue) { -// values.push_back(cuurentValue); -// } - -// try { -// starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); -// } catch (...) { -// starttime = 0; -// } -// return starttime; -// } long LinuxParser::UpTime(int pid) { - string word; - string line; - std::ifstream stream(kProcDirectory + to_string(pid) + kStatFilename); - if (stream.is_open()) { - std::getline(stream, line); - std::istringstream linestream(line); - int count{1}; - while (getline(linestream, word, ' ')) { - if (count == 22) { - long value = stol(word) / sysconf(_SC_CLK_TCK); - return value; - } - count++; - } + long starttime = 0; + std::ifstream filestream(kProcDirectory + std::to_string(pid) + + kStatFilename); + if (!filestream.is_open()) { + std::cerr << "Failed to open file: " + << kProcDirectory + std::to_string(pid) + kStatFilename + << std::endl; + } + std::string line; + std::string cuurentValue; + vector values; + std::getline(filestream, line); + std::istringstream iss(line); + while (iss >> cuurentValue) { + values.push_back(cuurentValue); + } + + try { + starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); + } catch (...) { + starttime = 0; } + return starttime; } From ac6c6c7cfa85e395ab0c203dd81404f326b77ac2 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Wed, 7 Feb 2024 00:32:18 -0800 Subject: [PATCH 13/19] fix aggregate CPU information. --- include/processor.h | 3 +++ src/linux_parser.cpp | 1 + src/processor.cpp | 31 +++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/processor.h b/include/processor.h index 2d3f439d..4ff9bd06 100644 --- a/include/processor.h +++ b/include/processor.h @@ -3,10 +3,13 @@ class Processor { public: + Processor() : prevTotal_(0), prevIdle_(0) {}; float Utilization(); // TODO: See src/processor.cpp // TODO: Declare any necessary private members private: + float prevTotal_; + float prevIdle_; }; #endif diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 0d97b53d..a96acdfa 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -160,6 +160,7 @@ vector LinuxParser::CpuUtilization() { jiffies.push_back(value); } } + return jiffies; } diff --git a/src/processor.cpp b/src/processor.cpp index 91662895..83cecda4 100644 --- a/src/processor.cpp +++ b/src/processor.cpp @@ -1,4 +1,31 @@ #include "processor.h" +#include "linux_parser.h" -// TODO: Return the aggregate CPU utilization -float Processor::Utilization() { return 0.0; } \ No newline at end of file +float Processor::Utilization() { + std::vector currentUtilization = LinuxParser::CpuUtilization(); + if (currentUtilization.size() < 10) { + return 0.0; + } + + float user = std::stoi(currentUtilization[0]); + float nice = std::stoi(currentUtilization[1]); + float system = std::stoi(currentUtilization[2]); + float idle = std::stoi(currentUtilization[3]); + float iowait = std::stoi(currentUtilization[4]); + float irq = std::stoi(currentUtilization[5]); + float softirq = std::stoi(currentUtilization[6]); + float steal = std::stoi(currentUtilization[7]); + + float totalTime = user + nice + system + idle + iowait + irq + softirq + steal; + float idleTime = idle + iowait; + + float deltaTimeTotal = totalTime - prevTotal_; + float deltaTimeIdle = idleTime - prevIdle_; + + float cpuUtilization = (deltaTimeTotal != 0) ? (float)(deltaTimeTotal - deltaTimeIdle) / deltaTimeTotal : 0.0; + + prevTotal_ = totalTime; + prevIdle_ = idleTime; + + return cpuUtilization; +} \ No newline at end of file From eed2aba9999da72ba53f1940c0d9c71efb767a69 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Wed, 7 Feb 2024 01:38:54 -0800 Subject: [PATCH 14/19] fix some errors. --- src/linux_parser.cpp | 136 ++++++++++++++++++++++++++++++++++++++----- src/processor.cpp | 4 +- src/system.cpp | 2 +- 3 files changed, 123 insertions(+), 19 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index a96acdfa..2640aadd 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -131,36 +131,140 @@ long LinuxParser::UpTime() { } // TODO: Read and return the number of jiffies for the system -long LinuxParser::Jiffies() { return 0; } +long LinuxParser::Jiffies() { + std::ifstream filestream(kProcDirectory + kStatFilename); + std::string line; + std::string key; + long total_jiffies = 0; + + if (filestream.is_open()) { + while (std::getline(filestream, line)) { + std::istringstream linestream(line); + linestream >> key; + if (key == "cpu") { + long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; + linestream >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice; + total_jiffies = user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice; + break; + } + } + } + + return total_jiffies; +} // 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; } +long LinuxParser::ActiveJiffies(int pid) { + long activeJiffies = 0; + + std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename); + if (filestream.is_open()) { + string line; + if (std::getline(filestream, line)) { + std::istringstream linestream(line); + std::istream_iterator streamIterator(linestream); + std::istream_iterator endIterator; + + // Skip the fields until the utime field + std::advance(streamIterator, 12); + + // Read the utime field + long utime = std::stol(*streamIterator++); + + // Read the stime field + long stime = std::stol(*streamIterator++); + + activeJiffies = utime + stime; + } + } + + filestream.close(); + return activeJiffies; +} // TODO: Read and return the number of active jiffies for the system -long LinuxParser::ActiveJiffies() { return 0; } +long LinuxParser::ActiveJiffies() { + long activeJiffies = 0; + + std::ifstream filestream(kProcDirectory + kStatFilename); + if (filestream.is_open()) { + string line; + if (std::getline(filestream, line)) { + std::istringstream linestream(line); + string cpuLabel; + linestream >> cpuLabel; // Skip the CPU label + long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; + linestream >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice; + activeJiffies = user + nice + system + irq + softirq + steal + guest + guest_nice; + } + } + + filestream.close(); + return activeJiffies; + } // TODO: Read and return the number of idle jiffies for the system -long LinuxParser::IdleJiffies() { return 0; } +long LinuxParser::IdleJiffies() { + std::ifstream filestream(kProcDirectory + kStatFilename); + std::string line; + std::string key; + long idle_jiffies = 0; + + if (filestream.is_open()) { + while (std::getline(filestream, line)) { + std::istringstream linestream(line); + linestream >> key; + if (key == "cpu") { + long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; + linestream >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice; + idle_jiffies = idle; + break; + } + } + } + + return idle_jiffies; +} + +// float LinuxParser::ProcessCpuUtilization(int pid) { +// long total_jiffies_start = LinuxParser::Jiffies(); +// long process_jiffies_start = LinuxParser::ActiveJiffies(pid); + +// // Sleep for a short duration to get the time difference +// std::this_thread::sleep_for(std::chrono::milliseconds(100)); + +// long total_jiffies_end = LinuxParser::Jiffies(); +// long process_jiffies_end = LinuxParser::ActiveJiffies(pid); + +// // Calculate the difference in jiffies +// long total_jiffies_diff = total_jiffies_end - total_jiffies_start; +// long process_jiffies_diff = process_jiffies_end - process_jiffies_start; + +// // Calculate the CPU utilization +// float cpu_utilization = static_cast(process_jiffies_diff) / total_jiffies_diff; + +// return cpu_utilization; +// } // DONE: Read and return CPU utilization vector LinuxParser::CpuUtilization() { - vector jiffies; + vector cpuUtilization; + std::ifstream filestream(kProcDirectory + kStatFilename); - if (!filestream.is_open()) { - std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename - << std::endl; - } - std::string line, cpu, value; if (filestream.is_open()) { - std::getline(filestream, line); - std::istringstream linestream(line); - linestream >> cpu; - while (linestream >> value) { - jiffies.push_back(value); + string line; + if (std::getline(filestream, line)) { + std::istringstream linestream(line); + string value; + while (linestream >> value) { + cpuUtilization.push_back(value); + } } } - return jiffies; + + filestream.close(); + return cpuUtilization; } diff --git a/src/processor.cpp b/src/processor.cpp index 83cecda4..bf68871e 100644 --- a/src/processor.cpp +++ b/src/processor.cpp @@ -7,7 +7,7 @@ float Processor::Utilization() { return 0.0; } - float user = std::stoi(currentUtilization[0]); + std::string user = currentUtilization[0]; float nice = std::stoi(currentUtilization[1]); float system = std::stoi(currentUtilization[2]); float idle = std::stoi(currentUtilization[3]); @@ -16,7 +16,7 @@ float Processor::Utilization() { float softirq = std::stoi(currentUtilization[6]); float steal = std::stoi(currentUtilization[7]); - float totalTime = user + nice + system + idle + iowait + irq + softirq + steal; + float totalTime = nice + system + idle + iowait + irq + softirq + steal; float idleTime = idle + iowait; float deltaTimeTotal = totalTime - prevTotal_; diff --git a/src/system.cpp b/src/system.cpp index 03f21ef1..7ac49b92 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -35,7 +35,7 @@ vector& System::Processes() { std::string ramVal0 = process.Ram(); std::string userVal0 = process.User(); std::string commandVal0 = process.Command(); - float cpuUVal0 = process.CpuUtilization(); + //float cpuUVal0 = process.CpuUtilization(); processes_.push_back(process); }; From abf4284d8334260566592eea4432200952851370 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Fri, 9 Feb 2024 12:51:40 -0800 Subject: [PATCH 15/19] first complete version. --- include/linux_parser.h | 1 + src/linux_parser.cpp | 107 ++++++++++++++++++----------------------- src/process.cpp | 2 +- src/system.cpp | 18 ++----- 4 files changed, 52 insertions(+), 76 deletions(-) diff --git a/include/linux_parser.h b/include/linux_parser.h index 988ac069..58569c0e 100644 --- a/include/linux_parser.h +++ b/include/linux_parser.h @@ -41,6 +41,7 @@ enum CPUStates { kGuestNice_ }; std::vector CpuUtilization(); +float cpuProcessUtilization(int pid); long Jiffies(); long ActiveJiffies(); long ActiveJiffies(int pid); diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 2640aadd..1ab85ca9 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -2,8 +2,8 @@ #include #include -#include +#include #include #include #include @@ -76,7 +76,7 @@ float LinuxParser::MemoryUtilization() { std::ifstream filestream(kProcDirectory + kMeminfoFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file: " << kProcDirectory + kMeminfoFilename + std::cerr << "Failed to open file MemoryUtilization: " << kProcDirectory + kMeminfoFilename << std::endl; } std::string currentKey; @@ -115,7 +115,7 @@ long LinuxParser::UpTime() { std::ifstream filestream(kProcDirectory + kUptimeFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file: " << kProcDirectory + kUptimeFilename + std::cerr << "Failed to open file UpTime: " << kProcDirectory + kUptimeFilename << std::endl; } long uptime; @@ -136,73 +136,83 @@ long LinuxParser::Jiffies() { std::string line; std::string key; long total_jiffies = 0; - if (filestream.is_open()) { while (std::getline(filestream, line)) { std::istringstream linestream(line); linestream >> key; if (key == "cpu") { - long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; - linestream >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice; - total_jiffies = user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice; + long user, nice, system, idle, iowait, irq, softirq, steal, guest, + guest_nice; + linestream >> user >> nice >> system >> idle >> iowait >> irq >> + softirq >> steal >> guest >> guest_nice; + total_jiffies = user + nice + system + idle + iowait + irq + softirq + + steal + guest + guest_nice; break; } } } - return total_jiffies; } -// TODO: Read and return the number of active jiffies for a PID -// REMOVE: [[maybe_unused]] once you define the function +// DONE: Read and return the number of active jiffies for a PID long LinuxParser::ActiveJiffies(int pid) { long activeJiffies = 0; - - std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename); + std::ifstream filestream(kProcDirectory + std::to_string(pid) + + kStatFilename); if (filestream.is_open()) { string line; if (std::getline(filestream, line)) { std::istringstream linestream(line); std::istream_iterator streamIterator(linestream); std::istream_iterator endIterator; - // Skip the fields until the utime field std::advance(streamIterator, 12); - // Read the utime field long utime = std::stol(*streamIterator++); - // Read the stime field long stime = std::stol(*streamIterator++); - activeJiffies = utime + stime; } } - filestream.close(); return activeJiffies; } -// TODO: Read and return the number of active jiffies for the system -long LinuxParser::ActiveJiffies() { - long activeJiffies = 0; +float LinuxParser::cpuProcessUtilization(int pid) { + long total_time; + long seconds; + float cpuUsage; + long uptime = LinuxParser::UpTime(); + long starttimePerHz = LinuxParser::UpTime(pid); + // Next we get the total elapsed time in seconds since the process started: + total_time = LinuxParser::ActiveJiffies(pid); + seconds = uptime - starttimePerHz; + // Next we get the total elapsed time in seconds since the process started: + cpuUsage = (float) 100 * (total_time / sysconf(_SC_CLK_TCK)) / seconds; + return cpuUsage; +} +// TODO: Read and return the number of active jiffies for the system +long LinuxParser::ActiveJiffies() { + long activeJiffies = 0; std::ifstream filestream(kProcDirectory + kStatFilename); if (filestream.is_open()) { string line; if (std::getline(filestream, line)) { std::istringstream linestream(line); string cpuLabel; - linestream >> cpuLabel; // Skip the CPU label - long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; - linestream >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice; - activeJiffies = user + nice + system + irq + softirq + steal + guest + guest_nice; + linestream >> cpuLabel; // Skip the CPU label + long user, nice, system, idle, iowait, irq, softirq, steal, guest, + guest_nice; + linestream >> user >> nice >> system >> idle >> iowait >> irq >> + softirq >> steal >> guest >> guest_nice; + activeJiffies = + user + nice + system + irq + softirq + steal + guest + guest_nice; } } - filestream.close(); return activeJiffies; - } +} // TODO: Read and return the number of idle jiffies for the system long LinuxParser::IdleJiffies() { @@ -216,8 +226,10 @@ long LinuxParser::IdleJiffies() { std::istringstream linestream(line); linestream >> key; if (key == "cpu") { - long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice; - linestream >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal >> guest >> guest_nice; + long user, nice, system, idle, iowait, irq, softirq, steal, guest, + guest_nice; + linestream >> user >> nice >> system >> idle >> iowait >> irq >> + softirq >> steal >> guest >> guest_nice; idle_jiffies = idle; break; } @@ -227,26 +239,6 @@ long LinuxParser::IdleJiffies() { return idle_jiffies; } -// float LinuxParser::ProcessCpuUtilization(int pid) { -// long total_jiffies_start = LinuxParser::Jiffies(); -// long process_jiffies_start = LinuxParser::ActiveJiffies(pid); - -// // Sleep for a short duration to get the time difference -// std::this_thread::sleep_for(std::chrono::milliseconds(100)); - -// long total_jiffies_end = LinuxParser::Jiffies(); -// long process_jiffies_end = LinuxParser::ActiveJiffies(pid); - -// // Calculate the difference in jiffies -// long total_jiffies_diff = total_jiffies_end - total_jiffies_start; -// long process_jiffies_diff = process_jiffies_end - process_jiffies_start; - -// // Calculate the CPU utilization -// float cpu_utilization = static_cast(process_jiffies_diff) / total_jiffies_diff; - -// return cpu_utilization; -// } - // DONE: Read and return CPU utilization vector LinuxParser::CpuUtilization() { vector cpuUtilization; @@ -267,13 +259,12 @@ vector LinuxParser::CpuUtilization() { return cpuUtilization; } - // DONE: Read and return the total number of processes int LinuxParser::TotalProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename + std::cerr << "Failed to open file TotalProcesses: " << kProcDirectory + kStatFilename << std::endl; } std::string currentKey; @@ -297,7 +288,7 @@ int LinuxParser::RunningProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file: " << kProcDirectory + kStatFilename + std::cerr << "Failed to open file RunningProcesses: " << kProcDirectory + kStatFilename << std::endl; } std::string currentKey; @@ -334,7 +325,7 @@ string LinuxParser::Ram(int pid) { std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatusFilename); if (!filestream.is_open()) { - std::cerr << "Failed to open file: " + std::cerr << "Failed to open file Ram: " << kProcDirectory + std::to_string(pid) + kStatusFilename << std::endl; } @@ -345,7 +336,7 @@ string LinuxParser::Ram(int pid) { std::istringstream iss(line); while (iss >> currentKey >> currentValue) { if (currentKey == "VmSize:") { - ram = to_string( (int) std::floor(stof(currentValue) / 1024)); + ram = to_string((int)std::floor(stof(currentValue) / 1024)); } break; } @@ -360,7 +351,7 @@ string LinuxParser::Uid(int pid) { std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatusFilename); if (!filestream.is_open()) { - std::cerr << "Failed to open file: " + std::cerr << "Failed to open file Uid: " << kProcDirectory + std::to_string(pid) + kStatusFilename << std::endl; } @@ -386,7 +377,7 @@ string LinuxParser::User(int pid) { std::string name; std::ifstream filestream(kPasswordPath); if (!filestream.is_open()) { - std::cerr << "Failed to open file: " << kPasswordPath << std::endl; + std::cerr << "Failed to open file User: " << kPasswordPath << std::endl; } std::string temp; std::string user; @@ -413,7 +404,7 @@ long LinuxParser::UpTime(int pid) { std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename); if (!filestream.is_open()) { - std::cerr << "Failed to open file: " + std::cerr << "Failed to open file UpTime: " << kProcDirectory + std::to_string(pid) + kStatFilename << std::endl; } @@ -425,7 +416,6 @@ long LinuxParser::UpTime(int pid) { while (iss >> cuurentValue) { values.push_back(cuurentValue); } - try { starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); } catch (...) { @@ -433,6 +423,3 @@ long LinuxParser::UpTime(int pid) { } return starttime; } - - - diff --git a/src/process.cpp b/src/process.cpp index 59575807..c3e6af1f 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -15,7 +15,7 @@ using std::vector; int Process::Pid() { return pid_; } // TODO: Return this process's CPU utilization -float Process::CpuUtilization() { return cpuUtilization_; } +float Process::CpuUtilization() { return cpuUtilization_ = LinuxParser::cpuProcessUtilization(pid_); } // TODO: Return the command that generated this process string Process::Command() { return command_ = LinuxParser::Command(pid_); } diff --git a/src/system.cpp b/src/system.cpp index 7ac49b92..5e40c524 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -16,29 +16,17 @@ using std::size_t; using std::string; using std::vector; -// TODO: Return the system's CPU +// DONE: Return the system's CPU Processor& System::Cpu() { return cpu_; } -// TODO: Return a container composed of the system's processes +// DONE: Return a container composed of the system's processes vector& System::Processes() { processes_.clear(); vector pids = LinuxParser::Pids(); for (std::vector::iterator it = pids.begin(); it != pids.end(); it++) { int pidIndex = *it; - // std::string userVal = LinuxParser::User(pidIndex); - // std::string commandVal = LinuxParser::Command(pidIndex); - // std::string ramVal = LinuxParser::Ram(pidIndex); - // long upTimeVal = LinuxParser::UpTime(pidIndex); - // std::string uidVal = LinuxParser::Uid(pidIndex); - // vector cpuUtilVale = LinuxParser::CpuUtilization(); - Process process(pidIndex); - std::string ramVal0 = process.Ram(); - std::string userVal0 = process.User(); - std::string commandVal0 = process.Command(); - //float cpuUVal0 = process.CpuUtilization(); - processes_.push_back(process); + processes_.push_back(Process(pidIndex)); }; - return processes_; } From 0264a197a2f185b2894234645e8c4ea699257c57 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sat, 10 Feb 2024 02:18:56 -0800 Subject: [PATCH 16/19] clean the code. --- src/linux_parser.cpp | 78 +++----------------------------------------- src/processor.cpp | 12 +++++-- src/system.cpp | 2 -- 3 files changed, 14 insertions(+), 78 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index 1ab85ca9..e3df1673 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -131,28 +131,7 @@ long LinuxParser::UpTime() { } // TODO: Read and return the number of jiffies for the system -long LinuxParser::Jiffies() { - std::ifstream filestream(kProcDirectory + kStatFilename); - std::string line; - std::string key; - long total_jiffies = 0; - if (filestream.is_open()) { - while (std::getline(filestream, line)) { - std::istringstream linestream(line); - linestream >> key; - if (key == "cpu") { - long user, nice, system, idle, iowait, irq, softirq, steal, guest, - guest_nice; - linestream >> user >> nice >> system >> idle >> iowait >> irq >> - softirq >> steal >> guest >> guest_nice; - total_jiffies = user + nice + system + idle + iowait + irq + softirq + - steal + guest + guest_nice; - break; - } - } - } - return total_jiffies; -} +long LinuxParser::Jiffies() { return 0.0;} // DONE: Read and return the number of active jiffies for a PID long LinuxParser::ActiveJiffies(int pid) { @@ -193,51 +172,10 @@ float LinuxParser::cpuProcessUtilization(int pid) { } // TODO: Read and return the number of active jiffies for the system -long LinuxParser::ActiveJiffies() { - long activeJiffies = 0; - std::ifstream filestream(kProcDirectory + kStatFilename); - if (filestream.is_open()) { - string line; - if (std::getline(filestream, line)) { - std::istringstream linestream(line); - string cpuLabel; - linestream >> cpuLabel; // Skip the CPU label - long user, nice, system, idle, iowait, irq, softirq, steal, guest, - guest_nice; - linestream >> user >> nice >> system >> idle >> iowait >> irq >> - softirq >> steal >> guest >> guest_nice; - activeJiffies = - user + nice + system + irq + softirq + steal + guest + guest_nice; - } - } - filestream.close(); - return activeJiffies; -} +long LinuxParser::ActiveJiffies() { return 0.0;} // TODO: Read and return the number of idle jiffies for the system -long LinuxParser::IdleJiffies() { - std::ifstream filestream(kProcDirectory + kStatFilename); - std::string line; - std::string key; - long idle_jiffies = 0; - - if (filestream.is_open()) { - while (std::getline(filestream, line)) { - std::istringstream linestream(line); - linestream >> key; - if (key == "cpu") { - long user, nice, system, idle, iowait, irq, softirq, steal, guest, - guest_nice; - linestream >> user >> nice >> system >> idle >> iowait >> irq >> - softirq >> steal >> guest >> guest_nice; - idle_jiffies = idle; - break; - } - } - } - - return idle_jiffies; -} +long LinuxParser::IdleJiffies() { return 0.0;} // DONE: Read and return CPU utilization vector LinuxParser::CpuUtilization() { @@ -413,13 +351,7 @@ long LinuxParser::UpTime(int pid) { vector values; std::getline(filestream, line); std::istringstream iss(line); - while (iss >> cuurentValue) { - values.push_back(cuurentValue); - } - try { - starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); - } catch (...) { - starttime = 0; - } + while (iss >> cuurentValue) { values.push_back(cuurentValue);} + starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); return starttime; } diff --git a/src/processor.cpp b/src/processor.cpp index bf68871e..4a17b0f8 100644 --- a/src/processor.cpp +++ b/src/processor.cpp @@ -6,7 +6,6 @@ float Processor::Utilization() { if (currentUtilization.size() < 10) { return 0.0; } - std::string user = currentUtilization[0]; float nice = std::stoi(currentUtilization[1]); float system = std::stoi(currentUtilization[2]); @@ -22,8 +21,15 @@ float Processor::Utilization() { float deltaTimeTotal = totalTime - prevTotal_; float deltaTimeIdle = idleTime - prevIdle_; - float cpuUtilization = (deltaTimeTotal != 0) ? (float)(deltaTimeTotal - deltaTimeIdle) / deltaTimeTotal : 0.0; - + float cpuUtilization; + if (deltaTimeTotal != 0) + { + cpuUtilization = (float)(deltaTimeTotal - deltaTimeIdle) / deltaTimeTotal; + } + else + { + cpuUtilization = 0.0; + } prevTotal_ = totalTime; prevIdle_ = idleTime; diff --git a/src/system.cpp b/src/system.cpp index 5e40c524..3e5605e2 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,7 +1,5 @@ #include "system.h" - #include - #include #include #include From afed2ec6b941dd658306d53522dd7c88811fe435 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sat, 10 Feb 2024 23:27:28 -0800 Subject: [PATCH 17/19] check issue from std::stoi --- src/linux_parser.cpp | 66 ++++++++++++++++++++++++++++++-------------- src/processor.cpp | 18 +++++++----- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index e3df1673..f112e0f3 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -60,8 +60,14 @@ vector LinuxParser::Pids() { // Is every character of the name a digit? string filename(file->d_name); if (std::all_of(filename.begin(), filename.end(), isdigit)) { - int pid = stoi(filename); - pids.push_back(pid); + //int pid = stoi(filename); + try { + long long int pid = stoi(filename); + } catch (std::out_of_range& e) { + std::cerr << "Number is out of range: " << e.what() << '\n'; + } + long long int pid0 = stoi(filename); + pids.push_back(pid0); } } } @@ -76,8 +82,8 @@ float LinuxParser::MemoryUtilization() { std::ifstream filestream(kProcDirectory + kMeminfoFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file MemoryUtilization: " << kProcDirectory + kMeminfoFilename - << std::endl; + std::cerr << "Failed to open file MemoryUtilization: " + << kProcDirectory + kMeminfoFilename << std::endl; } std::string currentKey; std::string currentValue; @@ -115,8 +121,8 @@ long LinuxParser::UpTime() { std::ifstream filestream(kProcDirectory + kUptimeFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file UpTime: " << kProcDirectory + kUptimeFilename - << std::endl; + std::cerr << "Failed to open file UpTime: " + << kProcDirectory + kUptimeFilename << std::endl; } long uptime; while (getline(filestream, line)) { @@ -124,14 +130,19 @@ long LinuxParser::UpTime() { // read info std::string currentValue; iss >> currentValue; - uptime = std::stoi(currentValue); + // uptime = std::stoi(currentValue); + try { + uptime = std::stoi(currentValue); + } catch (std::out_of_range& e) { + std::cerr << "Number is out of range: " << e.what() << '\n'; + } break; } return uptime; } // TODO: Read and return the number of jiffies for the system -long LinuxParser::Jiffies() { return 0.0;} +long LinuxParser::Jiffies() { return 0.0; } // DONE: Read and return the number of active jiffies for a PID long LinuxParser::ActiveJiffies(int pid) { @@ -167,15 +178,15 @@ float LinuxParser::cpuProcessUtilization(int pid) { total_time = LinuxParser::ActiveJiffies(pid); seconds = uptime - starttimePerHz; // Next we get the total elapsed time in seconds since the process started: - cpuUsage = (float) 100 * (total_time / sysconf(_SC_CLK_TCK)) / seconds; + cpuUsage = (float)100 * (total_time / sysconf(_SC_CLK_TCK)) / seconds; return cpuUsage; } // TODO: Read and return the number of active jiffies for the system -long LinuxParser::ActiveJiffies() { return 0.0;} +long LinuxParser::ActiveJiffies() { return 0.0; } // TODO: Read and return the number of idle jiffies for the system -long LinuxParser::IdleJiffies() { return 0.0;} +long LinuxParser::IdleJiffies() { return 0.0; } // DONE: Read and return CPU utilization vector LinuxParser::CpuUtilization() { @@ -202,20 +213,26 @@ int LinuxParser::TotalProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file TotalProcesses: " << kProcDirectory + kStatFilename - << std::endl; + std::cerr << "Failed to open file TotalProcesses: " + << kProcDirectory + kStatFilename << std::endl; } std::string currentKey; std::string currentValue; - int totalProcesses; + long long int totalProcesses; while (getline(filestream, line)) { std::istringstream iss(line); // read info iss >> currentKey >> currentValue; if (currentKey == "processes") { - totalProcesses = std::stoi(currentValue); + // totalProcesses = std::stoi(currentValue); + try { + totalProcesses = std::stoi(currentValue); + } catch (std::out_of_range& e) { + std::cerr << "Number is out of range: " << e.what() << '\n'; + } + break; } - break; + } return totalProcesses; @@ -226,18 +243,23 @@ int LinuxParser::RunningProcesses() { std::ifstream filestream(kProcDirectory + kStatFilename); std::string line; if (!filestream.is_open()) { - std::cerr << "Failed to open file RunningProcesses: " << kProcDirectory + kStatFilename - << std::endl; + std::cerr << "Failed to open file RunningProcesses: " + << kProcDirectory + kStatFilename << std::endl; } std::string currentKey; std::string currentValue; - int runningProcesses; + long long int runningProcesses; while (getline(filestream, line)) { std::istringstream iss(line); // read info iss >> currentKey >> currentValue; if (currentKey == "procs_running") { - runningProcesses = std::stoi(currentValue); + // runningProcesses = std::stoi(currentValue); + try { + runningProcesses = std::stoi(currentValue); + } catch (std::out_of_range& e) { + std::cerr << "Number is out of range: " << e.what() << '\n'; + } } } @@ -351,7 +373,9 @@ long LinuxParser::UpTime(int pid) { vector values; std::getline(filestream, line); std::istringstream iss(line); - while (iss >> cuurentValue) { values.push_back(cuurentValue);} + while (iss >> cuurentValue) { + values.push_back(cuurentValue); + } starttime = stol(values[21]) / sysconf(_SC_CLK_TCK); return starttime; } diff --git a/src/processor.cpp b/src/processor.cpp index 4a17b0f8..93c9d258 100644 --- a/src/processor.cpp +++ b/src/processor.cpp @@ -1,4 +1,5 @@ #include "processor.h" + #include "linux_parser.h" float Processor::Utilization() { @@ -15,6 +16,12 @@ float Processor::Utilization() { float softirq = std::stoi(currentUtilization[6]); float steal = std::stoi(currentUtilization[7]); + // try { + // int num = std::stoi(str); + // } catch (std::out_of_range& e) { + // std::cerr << "Number is out of range: " << e.what() << '\n'; + // } + float totalTime = nice + system + idle + iowait + irq + softirq + steal; float idleTime = idle + iowait; @@ -22,13 +29,10 @@ float Processor::Utilization() { float deltaTimeIdle = idleTime - prevIdle_; float cpuUtilization; - if (deltaTimeTotal != 0) - { - cpuUtilization = (float)(deltaTimeTotal - deltaTimeIdle) / deltaTimeTotal; - } - else - { - cpuUtilization = 0.0; + if (deltaTimeTotal != 0) { + cpuUtilization = (float)(deltaTimeTotal - deltaTimeIdle) / deltaTimeTotal; + } else { + cpuUtilization = 0.0; } prevTotal_ = totalTime; prevIdle_ = idleTime; From bb987212f39670eb28a77ff1e466943698a59e41 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sun, 11 Feb 2024 03:54:07 -0800 Subject: [PATCH 18/19] fix the issue. --- src/format.cpp | 10 +++++++--- src/processor.cpp | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/format.cpp b/src/format.cpp index f14c94f1..eb826fff 100644 --- a/src/format.cpp +++ b/src/format.cpp @@ -8,7 +8,11 @@ using std::string; // INPUT: Long int measuring seconds // OUTPUT: HH:MM:SS string Format::ElapsedTime(long seconds) { - return std::to_string(seconds / 3600) + ":" + - std::to_string((seconds % 3600) / 60) + ":" + - std::to_string(seconds % 60); + string timeFormat; + int hours = seconds / 3600; + int minutes = (seconds % 3600) / 60; + int sec = seconds % 60; + timeFormat = std::to_string(hours) + ":" + std::to_string(minutes) + ":" + std::to_string(sec); + + return timeFormat; } \ No newline at end of file diff --git a/src/processor.cpp b/src/processor.cpp index 93c9d258..982a142e 100644 --- a/src/processor.cpp +++ b/src/processor.cpp @@ -11,13 +11,13 @@ float Processor::Utilization() { float nice = std::stoi(currentUtilization[1]); float system = std::stoi(currentUtilization[2]); float idle = std::stoi(currentUtilization[3]); - float iowait = std::stoi(currentUtilization[4]); + float iowait = 0.0; //std::stoi(currentUtilization[4]); float irq = std::stoi(currentUtilization[5]); float softirq = std::stoi(currentUtilization[6]); float steal = std::stoi(currentUtilization[7]); // try { - // int num = std::stoi(str); + // int num = std::stoi(iowait); // } catch (std::out_of_range& e) { // std::cerr << "Number is out of range: " << e.what() << '\n'; // } From d307fdd0418773f0c4e242fca797bd79988f9b05 Mon Sep 17 00:00:00 2001 From: Yu Xiong Date: Sun, 11 Feb 2024 16:29:29 -0800 Subject: [PATCH 19/19] updates from Mac --- .DS_Store | Bin 0 -> 8196 bytes CMakeLists.txt | 11 ++++++++--- Makefile | 9 ++++++++- include/process.h | 14 +++++++------- include/processor.h | 2 +- src/format.cpp | 5 +++-- src/linux_parser.cpp | 16 ++++++++-------- src/ncurses_display.cpp | 4 +++- src/process.cpp | 12 +++++++++--- src/processor.cpp | 2 +- src/system.cpp | 2 ++ 11 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..34811c65546039acb6e500a89498c6f62b280faa GIT binary patch literal 8196 zcmeHMy^9k;9DS3EF?W8Q-8I5qEky4xaJ}0t4&nzF13U=2W-RpaUj zHgk#|*ELrPu4kR77>`bCqt@tk`#Gj`hy&t)I3Ny)1LDBn;sD>-x|A2Z_wA^(;($2t zUpkUhHse z(S}30-T4-Y!Cw+elFq!9u;CP!_(EqT{O6xBQ&v}QT>w)-<1qI z(2X-^^Yi)2pY~tg{AhPJGmpQ!i&=qs>~S%wuVR}!m2!t_NJI6778$m-5_n=brc{-icr0`MQTjr~~?J&0&S^RhP+f@xE&}zP^~aHXWVq zbbd5+=^-a&k@vs#L0WO(s17XIfHj@}r;ES;A9Y&civ!}oKjeTaCbeXff?aN%ldf~^ vh;@%u7tI@6G$CwEI!-j{IC1*Jkk=8ZIwp2Fw#Xh9`4C`ikVYK%qYnHAT^l`k literal 0 HcmV?d00001 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a193420..ba05830e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,14 @@ cmake_minimum_required(VERSION 2.8.12) project(monitor) -set(CURSES_NEED_NCURSES TRUE) -find_package(Curses REQUIRED) -include_directories(${CURSES_INCLUDE_DIRS}) +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wextra -Wall -Werror -ansi -pedantic -pthread --std=c++11") +include_directories(includes libs/c++ /opt/homebrew/opt/ncurses/include) +link_directories(/opt/homebrew/opt/ncurses/lib) +link_libraries(ncurses) + +# set(CURSES_NEED_NCURSES TRUE) +# find_package(ncurses REQUIRED) +# include_directories(${CURSES_INCLUDE_DIRS}) include_directories(include) file(GLOB SOURCES "src/*.cpp") diff --git a/Makefile b/Makefile index 340d4230..ffa2d86b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,11 @@ +CXXFLAGS += -W -Wextra -Wall -Werror -ansi -pedantic +CXXFLAGS += -pthread --std=c++11 -Iincludes -Ilibs/c++ -I/usr/local/opt/ncurses/include +LDFLAGS += -lncurses -L/usr/local/opt/ncurses/lib + +# existing Makefile targets go here + .PHONY: all -all: format test build +all: format build .PHONY: format format: @@ -22,3 +28,4 @@ debug: .PHONY: clean clean: rm -rf build + diff --git a/include/process.h b/include/process.h index 8c86e32c..14ceeffb 100644 --- a/include/process.h +++ b/include/process.h @@ -8,7 +8,7 @@ It contains relevant attributes as shown below */ class Process { public: - Process(int pid): pid_(pid) {}; + Process(int pid) : pid_(pid){}; int Pid(); // TODO: See src/process.cpp std::string User(); // TODO: See src/process.cpp std::string Command(); // TODO: See src/process.cpp @@ -19,12 +19,12 @@ class Process { // TODO: Declare any necessary private members private: - int pid_; - std::string user_; - std::string command_; - float cpuUtilization_; - std::string ram_; - long int upTime_; + int pid_; + std::string user_; + std::string command_; + float cpuUtilization_; + std::string ram_; + long int upTime_; }; #endif \ No newline at end of file diff --git a/include/processor.h b/include/processor.h index 4ff9bd06..80956d40 100644 --- a/include/processor.h +++ b/include/processor.h @@ -3,7 +3,7 @@ class Processor { public: - Processor() : prevTotal_(0), prevIdle_(0) {}; + Processor() : prevTotal_(0), prevIdle_(0){}; float Utilization(); // TODO: See src/processor.cpp // TODO: Declare any necessary private members diff --git a/src/format.cpp b/src/format.cpp index eb826fff..34c37d69 100644 --- a/src/format.cpp +++ b/src/format.cpp @@ -12,7 +12,8 @@ string Format::ElapsedTime(long seconds) { int hours = seconds / 3600; int minutes = (seconds % 3600) / 60; int sec = seconds % 60; - timeFormat = std::to_string(hours) + ":" + std::to_string(minutes) + ":" + std::to_string(sec); - + timeFormat = std::to_string(hours) + ":" + std::to_string(minutes) + ":" + + std::to_string(sec); + return timeFormat; } \ No newline at end of file diff --git a/src/linux_parser.cpp b/src/linux_parser.cpp index f112e0f3..35e9b861 100644 --- a/src/linux_parser.cpp +++ b/src/linux_parser.cpp @@ -5,9 +5,9 @@ #include #include +#include #include #include - using std::stof; using std::string; using std::to_string; @@ -60,13 +60,12 @@ vector LinuxParser::Pids() { // Is every character of the name a digit? string filename(file->d_name); if (std::all_of(filename.begin(), filename.end(), isdigit)) { - //int pid = stoi(filename); + long long int pid0; try { - long long int pid = stoi(filename); + pid0 = stoi(filename); } catch (std::out_of_range& e) { std::cerr << "Number is out of range: " << e.what() << '\n'; } - long long int pid0 = stoi(filename); pids.push_back(pid0); } } @@ -120,11 +119,12 @@ float LinuxParser::MemoryUtilization() { long LinuxParser::UpTime() { std::ifstream filestream(kProcDirectory + kUptimeFilename); std::string line; + long uptime = 0; if (!filestream.is_open()) { std::cerr << "Failed to open file UpTime: " << kProcDirectory + kUptimeFilename << std::endl; - } - long uptime; + } else { + while (getline(filestream, line)) { std::istringstream iss(line); // read info @@ -138,6 +138,7 @@ long LinuxParser::UpTime() { } break; } + filestream.close();} return uptime; } @@ -218,7 +219,7 @@ int LinuxParser::TotalProcesses() { } std::string currentKey; std::string currentValue; - long long int totalProcesses; + long long int totalProcesses = 0; while (getline(filestream, line)) { std::istringstream iss(line); // read info @@ -232,7 +233,6 @@ int LinuxParser::TotalProcesses() { } break; } - } return totalProcesses; diff --git a/src/ncurses_display.cpp b/src/ncurses_display.cpp index fdff8893..6ffc7535 100644 --- a/src/ncurses_display.cpp +++ b/src/ncurses_display.cpp @@ -1,11 +1,13 @@ +#include "ncurses_display.h" + #include +#include #include #include #include #include #include "format.h" -#include "ncurses_display.h" #include "system.h" using std::string; diff --git a/src/process.cpp b/src/process.cpp index c3e6af1f..5d23e90c 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -1,10 +1,12 @@ +#include "process.h" + #include + #include #include #include #include -#include "process.h" #include "linux_parser.h" using std::string; @@ -15,7 +17,9 @@ using std::vector; int Process::Pid() { return pid_; } // TODO: Return this process's CPU utilization -float Process::CpuUtilization() { return cpuUtilization_ = LinuxParser::cpuProcessUtilization(pid_); } +float Process::CpuUtilization() { + return cpuUtilization_ = LinuxParser::cpuProcessUtilization(pid_); +} // TODO: Return the command that generated this process string Process::Command() { return command_ = LinuxParser::Command(pid_); } @@ -31,4 +35,6 @@ long int Process::UpTime() { return upTime_ = LinuxParser::UpTime(pid_); } // TODO: Overload the "less than" comparison operator for Process objects // REMOVE: [[maybe_unused]] once you define the function -bool Process::operator<(Process const& a[[maybe_unused]]) const { return true; } \ No newline at end of file +bool Process::operator<(Process const& a [[maybe_unused]]) const { + return true; +} \ No newline at end of file diff --git a/src/processor.cpp b/src/processor.cpp index 982a142e..d7fecd55 100644 --- a/src/processor.cpp +++ b/src/processor.cpp @@ -11,7 +11,7 @@ float Processor::Utilization() { float nice = std::stoi(currentUtilization[1]); float system = std::stoi(currentUtilization[2]); float idle = std::stoi(currentUtilization[3]); - float iowait = 0.0; //std::stoi(currentUtilization[4]); + float iowait = 0.0; // std::stoi(currentUtilization[4]); float irq = std::stoi(currentUtilization[5]); float softirq = std::stoi(currentUtilization[6]); float steal = std::stoi(currentUtilization[7]); diff --git a/src/system.cpp b/src/system.cpp index 3e5605e2..5e40c524 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,5 +1,7 @@ #include "system.h" + #include + #include #include #include