Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yu/mac verison #27

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finish memory utilization function.
xiongyu1988 committed Feb 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1923d1df4430675b72c4eeb37fb0d0a12c20634e
63 changes: 54 additions & 9 deletions src/linux_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "linux_parser.h"

#include <dirent.h>
#include <unistd.h>

#include <iostream>
#include <string>
#include <vector>

#include "linux_parser.h"

using std::stof;
using std::string;
using std::to_string;
@@ -67,7 +69,50 @@ vector<int> 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<float> 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; }
2 changes: 1 addition & 1 deletion src/system.cpp
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ vector<Process>& 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(); }