소스 검색

Z991239-3009 #comment feat: cpu和内存监控细化功能

陈纪林80310970 3 년 전
부모
커밋
6bbab23fe6
3개의 변경된 파일138개의 추가작업 그리고 2개의 파일을 삭제
  1. 1 2
      CMakeSettings.json
  2. 134 0
      Module/mod_ResourceWatcher/ResourceWatcherFSM.cpp
  3. 3 0
      Module/mod_ResourceWatcher/ResourceWatcherFSM.h

+ 1 - 2
CMakeSettings.json

@@ -37,8 +37,7 @@
       "cmakeCommandArgs": "-D BUILD_TESTING=OFF",
       "buildCommandArgs": "",
       "ctestCommandArgs": "-C Release",
-      "inheritEnvironments": [ "msvc_x86" ],
-      "variables": []
+      "inheritEnvironments": [ "msvc_x86" ]
     },
     {
       "name": "ARM64-GCC-Debug",

+ 134 - 0
Module/mod_ResourceWatcher/ResourceWatcherFSM.cpp

@@ -3907,6 +3907,136 @@ void ResourceWatcherFSM::UploadSysVersionInfo(UINT & ver)
     LogWarn(Severity_Middle, Error_Hardware, LOG_RESOURCEWATCHER_SYSTEMINFO, ret.second.c_str());
 }
 
+void ResourceWatcherFSM::CPUProcessStatus()
+{
+    
+    map<string, string> nameProcess;
+
+    CSystemRunInfo runInfo = { 0 };
+    GetEntityBase()->GetFunction()->GetSystemRunInfo(runInfo);
+
+    for (int i = 0; i < runInfo.strRunningEntityNames.GetCount(); i++)
+    {
+        string entityName = runInfo.strRunningEntityNames[i].GetData();
+        CEntityRunInfo entityInfo = { 0 };
+        GetEntityBase()->GetFunction()->GetEntityRunInfo(entityName.c_str(), entityInfo);
+        //Dbg("No. %d Current run entity : %s, procesID = %d !", i + 1, entityName, entityInfo.dwProcessID);
+        nameProcess[to_string(entityInfo.dwProcessID)] = entityName;
+    }
+
+    //cout << "---------CPU----------" << endl;
+    int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
+    //cout << "cpu number = " << cpu_num << endl;
+
+    FILE* fp;
+    char buffer[2048];
+    fp = popen("ps c -eo pid,%cpu,command | sort -k2 -rn | head -10", "r");
+    int i = 0;
+
+    string s;
+    string str[3];
+
+    //cpu
+    Dbg("CPU TOP 10 PROCESS!");
+    while (i < 10)
+    {
+        fgets(buffer, sizeof(buffer), fp);
+
+        s = buffer;
+        istringstream is(s);
+        is >> str[0] >> str[1] >> str[2];
+
+
+        double temp;
+        stringstream ss1;
+        ss1 << str[1];
+        ss1 >> temp;
+        temp = temp / cpu_num;
+
+        stringstream ss2;
+        ss2 << temp;
+        ss2 >> str[1];
+        //printf("%s", s.c_str());
+        if (nameProcess.find(str[0]) != nameProcess.end())
+        {
+            str[2] = nameProcess[str[0]];
+        }
+
+        //cout << str[0] << ", " << str[1] << "%, " << str[2] << endl;
+        //cout << s;
+
+        Dbg("Process id = %s, CPU usage = %s, Process Name = %s. ", str[0].c_str(), (str[1] + "%").c_str() , str[2].c_str());
+
+        ++i;
+    }
+    pclose(fp);
+}
+
+void ResourceWatcherFSM::MemoryProcessStatus()
+{
+
+    map<string, string> nameProcess;
+
+    CSystemRunInfo runInfo = { 0 };
+    GetEntityBase()->GetFunction()->GetSystemRunInfo(runInfo);
+
+    for (int i = 0; i < runInfo.strRunningEntityNames.GetCount(); i++)
+    {
+        string entityName = runInfo.strRunningEntityNames[i].GetData();
+        CEntityRunInfo entityInfo = { 0 };
+        GetEntityBase()->GetFunction()->GetEntityRunInfo(entityName.c_str(), entityInfo);
+        //Dbg("No. %d Current run entity : %s, procesID = %d !", i + 1, entityName, entityInfo.dwProcessID);
+        nameProcess[to_string(entityInfo.dwProcessID)] = entityName;
+    }
+
+    FILE* fp;
+    char buffer[2048];
+    fp = popen("ps c -eo pid,%mem,rss,command | sort -k3 -rn | head -10", "r");
+    int i = 0;
+
+    string s;
+    string str[4];
+
+    //cout << "---------MEMORY----------" << endl;    
+
+    //mem
+    Dbg("MEM TOP 10 PROCESS!");
+    while (i < 10)
+    {
+        fgets(buffer, sizeof(buffer), fp);
+
+        s = buffer;
+        istringstream is(s);
+        is >> str[0] >> str[1] >> str[2] >> str[3];
+
+
+        double temp;
+        stringstream ss1;
+        ss1 << str[2];
+        ss1 >> temp;
+        temp = temp / (1024 * 1024);
+
+        stringstream ss2;
+        ss2 << temp;
+        ss2 >> str[2];
+        //printf("%s", s.c_str());
+
+        if (nameProcess.find(str[0]) != nameProcess.end())
+        {
+            str[3] = nameProcess[str[0]];
+        }
+
+        //cout << str[0] << ", " << str[1] << ", " << str[2] << " GB," << str[3] << endl;
+        Dbg("Process id = %s, Memory usage = %s, Memory size = %s, Process Name = %s. ", 
+                str[0].c_str(), (str[1] + "%").c_str(), (str[2] + " GB").c_str(), str[3].c_str());
+        //cout << s;
+
+        ++i;
+    }
+    pclose(fp);
+
+}
+
 void ResourceWatcherFSM::HardwareInfoTimer(void* pData)
 {
     GetSystemCPUStatus();
@@ -3967,6 +4097,8 @@ void ResourceWatcherFSM::GetSystemCPUStatus()
         );
     }
 
+    CPUProcessStatus();
+
 }
 
 void ResourceWatcherFSM::GetSystemMemoryStatus()
@@ -4009,6 +4141,8 @@ void ResourceWatcherFSM::GetSystemMemoryStatus()
                                        szResult, szResult2, szResult3, mUsedRate * 100)
         );
     }
+
+    MemoryProcessStatus();
 }
 
 void ResourceWatcherFSM::GetDiskStatusFrom(LPCTSTR path)

+ 3 - 0
Module/mod_ResourceWatcher/ResourceWatcherFSM.h

@@ -476,6 +476,9 @@ public:
 	UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
 	void UploadSysVersionInfo(UINT& ver);
 
+	void CPUProcessStatus();
+	void MemoryProcessStatus();
+
 	void HardwareInfoTimer(void* pData);
 
 	BOOL ReadCPUInfo(CPUInfo* cpu);