| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #include <unistd.h>
- #include <cstddef>
- #include <set>
- #include <string>
- #include <vector>
- #include <iostream>
- #include <typeinfo>
- #include <algorithm> // std::reverse
- #include "process.h"
- #include "processor.h"
- #include "system.h"
- #include "linux_parser.h"
- using std::set;
- using std::size_t;
- using std::string;
- using std::vector;
- #include "linux_parser.h"
- // TODO: Return the operating system name
- std::string System::OperatingSystem() {
- string os = LinuxParser::OperatingSystem();
- return os;
- }
- // TODO: Return the system's kernel identifier (string)
- std::string System::Kernel() {
- string kernel = LinuxParser::Kernel();
- return kernel;
- }
- // TODO: Return the system's CPU
- Processor& System::Cpu() {
- return cpu_;
- }
- // TODO: Return the system's memory utilization
- float System::MemoryUtilization() {
- float utilization;
- float totalMemoryUsage = 0;
- vector<int> idVec = LinuxParser::Pids() ;
- for (int pid : idVec){
- Process pro(pid);
- int memory_usage = std::stoi(LinuxParser::Ram(pid));
- if (memory_usage >0){
- totalMemoryUsage += memory_usage ;
- }
- }
- string cmd = "n.a.";
- string line, key, value;
- string path = LinuxParser::kProcDirectory + "/meminfo";
- float memoryMax = 1;
- std::ifstream filestream(path);
- if (filestream.is_open()) {
- while (std::getline(filestream, line)) {
- std::replace(line.begin(), line.end(), ':', ' ');
- std::istringstream linestream(line);
- while (linestream >> key >> value) {
- if (key == "MemTotal") {
- memoryMax = std::stoi(value)/1024;
- }
- }
- }
- }
- utilization = totalMemoryUsage / memoryMax;
- return utilization; }
- // TODO: Return the number of processes actively running on the system
- int System::RunningProcesses() {
-
- int runprocess = LinuxParser::RunningProcesses();
- return runprocess;
- }
- // TODO: Return the total number of processes on the system
- int System::TotalProcesses() {
-
- int ttlprocess = LinuxParser::TotalProcesses();
- return ttlprocess;
- }
- // TODO: Return the number of seconds since the system started running
- long System::UpTime() {
- long uptime = LinuxParser::UpTime();
- return uptime;
- }
- // TODO: Return a container composed of the system's processes
- vector<Process>& System::Processes() {
-
- vector<int> idVec = LinuxParser::Pids() ;
- vector<Process> processes_tmp;
- for (int pid : idVec){
- Process pro(pid);
- int memory_usage = std::stoi(LinuxParser::Ram(pid));
- if (memory_usage >0){
- processes_tmp.push_back(pro);
- }
- }
- processes_ = processes_tmp;
- std::sort(processes_.begin(), processes_.end());
- std::reverse(processes_.begin(), processes_.end());
-
- return processes_; }
|