monitor.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-01-10 15:01:23
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-01-13 11:20:09
  8. */
  9. #include <monitor.h>
  10. namespace MIVA
  11. {
  12. std::shared_ptr<monitor> m_monitor = nullptr;
  13. monitor::monitor(){
  14. }
  15. monitor::~monitor(){
  16. this->Destory();
  17. }
  18. /**
  19. * @description: 释放
  20. * @param {*}
  21. * @return {*}
  22. */
  23. void monitor::Destory(){
  24. if(this->file != nullptr) fclose(this->file);
  25. }
  26. /**
  27. * @description: 创建实例
  28. * @param {*}
  29. * @return {*}
  30. */
  31. std::shared_ptr<monitor> monitor::CreateNew(){
  32. if(m_monitor == nullptr) m_monitor = std::make_shared<monitor>();
  33. return m_monitor;
  34. }
  35. /**
  36. * @description: 获取内存使用情况
  37. * @param {*}
  38. * @return {*}
  39. */
  40. int32_t monitor::getMemoryUsage(){
  41. int32_t MemoryUsage = 0;
  42. this->file = fopen("/proc/meminfo","r");
  43. char keyword[20];
  44. char valuech[20];
  45. fscanf(file,"MemTotal: %s kB\n",keyword);
  46. //long mem = 0;
  47. //mem = atol(keyword)/1000;
  48. fscanf(file,"MemFree: %s kB\n",valuech);
  49. fscanf(file,"MemAvailable: %s kB\n",valuech);
  50. MemoryUsage=atol(valuech)/1000;
  51. if(this->file != nullptr) fclose(this->file);
  52. return MemoryUsage;
  53. }
  54. /**
  55. * @description: 获取Cpu使用情况
  56. * @param {*}
  57. * @return {*}
  58. */
  59. int32_t monitor::getCpuUsage(){
  60. int32_t CpuUsage = 0;
  61. return CpuUsage;
  62. }
  63. /**
  64. * @description: 获取GPU使用情况
  65. * @param {*}
  66. * @return {*}
  67. */
  68. int32_t monitor::getGpuUsage(){
  69. int32_t gpuUsage = 0;
  70. return gpuUsage;
  71. }
  72. /**
  73. * @description: 获取CPU温度
  74. * TODO
  75. * @param {*}
  76. * @return {*}
  77. */
  78. int32_t monitor::getCpuTemp(){
  79. int32_t temp = 0;
  80. string shell = "cat /sys/class/thermal/thermal_zone0/temp";
  81. vector<string> results;
  82. CShell::exeShellCmd(shell, results);
  83. for(auto iter = results.begin(); iter != results.end(); iter++){
  84. temp = std::atoi(iter->c_str());
  85. }
  86. return temp;
  87. }
  88. /**
  89. * @description: 获取GPU温度
  90. * @param {*}
  91. * @return {*}
  92. */
  93. int32_t monitor::getGpuTemp(){
  94. int32_t temp = 0;
  95. string shell = "cat /sys/devices/virtual/thermal/thermal_zone1/temp";
  96. vector<string> results;
  97. CShell::exeShellCmd(shell, results);
  98. for(auto iter = results.begin(); iter != results.end(); iter++){
  99. temp = std::atoi(iter->c_str());
  100. }
  101. return temp;
  102. }
  103. /**
  104. * @description: 获取AUX的温度
  105. * @param {*}
  106. * @return {*}
  107. */
  108. int32_t monitor::getAUXTemp(){
  109. int32_t temp = 0;
  110. string shell = "cat /sys/devices/virtual/thermal/thermal_zone2/temp";
  111. vector<string> results;
  112. CShell::exeShellCmd(shell, results);
  113. for(auto iter = results.begin(); iter != results.end(); iter++){
  114. temp = std::atoi(iter->c_str());
  115. }
  116. return temp;
  117. }
  118. /**
  119. * @description: 获取A0的温度
  120. * @param {*}
  121. * @return {*}
  122. */
  123. int32_t monitor::getA0Temp(){
  124. int32_t temp = 0;
  125. string shell = "cat /sys/devices/virtual/thermal/thermal_zone3/temp";
  126. vector<string> results;
  127. CShell::exeShellCmd(shell, results);
  128. for(auto iter = results.begin(); iter != results.end(); iter++){
  129. temp = std::atoi(iter->c_str());
  130. }
  131. return temp;
  132. }
  133. /**
  134. * @description: 获取Thermal的温度
  135. * @param {*}
  136. * @return {*}
  137. */
  138. int32_t monitor::getThermalTemp(){
  139. int32_t temp = 0;
  140. string shell = "cat /sys/devices/virtual/thermal/thermal_zone5/temp";
  141. vector<string> results;
  142. CShell::exeShellCmd(shell, results);
  143. for(auto iter = results.begin(); iter != results.end(); iter++){
  144. temp = std::atoi(iter->c_str());
  145. }
  146. return temp;
  147. }
  148. /**
  149. * @description: 获取文件句柄使用情况
  150. * @param {*}
  151. * @return {*}
  152. */
  153. int32_t monitor::getFdUsage(){
  154. int32_t fdUsage = 0;
  155. //string shell = "lsof -p $(pidof main) | wc -l";
  156. string shell = "cat ../../data/state/fdState";
  157. vector<string> results;
  158. CShell::exeShellCmd(shell, results);
  159. for(auto iter = results.begin(); iter != results.end(); iter++){
  160. fdUsage = std::atoi(iter->c_str());
  161. }
  162. return fdUsage;
  163. }
  164. } // namespace MIVA