system.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <unistd.h>
  2. #include <cstddef>
  3. #include <set>
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7. #include <typeinfo>
  8. #include <algorithm> // std::reverse
  9. #include "process.h"
  10. #include "processor.h"
  11. #include "system.h"
  12. #include "linux_parser.h"
  13. using std::set;
  14. using std::size_t;
  15. using std::string;
  16. using std::vector;
  17. #include "linux_parser.h"
  18. // TODO: Return the operating system name
  19. std::string System::OperatingSystem() {
  20. string os = LinuxParser::OperatingSystem();
  21. return os;
  22. }
  23. // TODO: Return the system's kernel identifier (string)
  24. std::string System::Kernel() {
  25. string kernel = LinuxParser::Kernel();
  26. return kernel;
  27. }
  28. // TODO: Return the system's CPU
  29. Processor& System::Cpu() {
  30. return cpu_;
  31. }
  32. // TODO: Return the system's memory utilization
  33. float System::MemoryUtilization() {
  34. float utilization;
  35. float totalMemoryUsage = 0;
  36. vector<int> idVec = LinuxParser::Pids() ;
  37. for (int pid : idVec){
  38. Process pro(pid);
  39. int memory_usage = std::stoi(LinuxParser::Ram(pid));
  40. if (memory_usage >0){
  41. totalMemoryUsage += memory_usage ;
  42. }
  43. }
  44. string cmd = "n.a.";
  45. string line, key, value;
  46. string path = LinuxParser::kProcDirectory + "/meminfo";
  47. float memoryMax = 1;
  48. std::ifstream filestream(path);
  49. if (filestream.is_open()) {
  50. while (std::getline(filestream, line)) {
  51. std::replace(line.begin(), line.end(), ':', ' ');
  52. std::istringstream linestream(line);
  53. while (linestream >> key >> value) {
  54. if (key == "MemTotal") {
  55. memoryMax = std::stoi(value)/1024;
  56. }
  57. }
  58. }
  59. }
  60. utilization = totalMemoryUsage / memoryMax;
  61. return utilization; }
  62. // TODO: Return the number of processes actively running on the system
  63. int System::RunningProcesses() {
  64. int runprocess = LinuxParser::RunningProcesses();
  65. return runprocess;
  66. }
  67. // TODO: Return the total number of processes on the system
  68. int System::TotalProcesses() {
  69. int ttlprocess = LinuxParser::TotalProcesses();
  70. return ttlprocess;
  71. }
  72. // TODO: Return the number of seconds since the system started running
  73. long System::UpTime() {
  74. long uptime = LinuxParser::UpTime();
  75. return uptime;
  76. }
  77. // TODO: Return a container composed of the system's processes
  78. vector<Process>& System::Processes() {
  79. vector<int> idVec = LinuxParser::Pids() ;
  80. vector<Process> processes_tmp;
  81. for (int pid : idVec){
  82. Process pro(pid);
  83. int memory_usage = std::stoi(LinuxParser::Ram(pid));
  84. if (memory_usage >0){
  85. processes_tmp.push_back(pro);
  86. }
  87. }
  88. processes_ = processes_tmp;
  89. std::sort(processes_.begin(), processes_.end());
  90. std::reverse(processes_.begin(), processes_.end());
  91. return processes_; }