Sfoglia il codice sorgente

Z991239-5492 #comment feat: 调整Windows的cpu计算

陈纪林80310970 1 anno fa
parent
commit
0130b56fdb
1 ha cambiato i file con 11 aggiunte e 4 eliminazioni
  1. 11 4
      Module/mod_ResourceWatcher/ResourceWatcherFSM.cpp

+ 11 - 4
Module/mod_ResourceWatcher/ResourceWatcherFSM.cpp

@@ -3069,6 +3069,10 @@ bool cmp(const pair<int, double>& a, const pair<int, double>& b) {
 
 void ResourceWatcherFSM::GetSystemCPUStatus()
 {
+    oldProcessTime.clear();
+    newProcessTime.clear();
+    processName.clear();
+    processCpu.clear();
 
     FILETIME preIdleTime;
     FILETIME preKernelTime;
@@ -3101,8 +3105,10 @@ void ResourceWatcherFSM::GetSystemCPUStatus()
         bProcessOld = Process32Next(hProcessSnapOld, &pe32Old);
     }
     CloseHandle(hProcessSnapOld);
-
-    Sleep(1000);
+    
+    // 等待一段时间
+    Sleep(100); // 100毫秒
+    ULONGLONG deltaTime = 100 * 1000000; // 100毫秒转换为纳秒
 
     FILETIME idleTime;
     FILETIME kernelTime;
@@ -3163,8 +3169,9 @@ void ResourceWatcherFSM::GetSystemCPUStatus()
             continue;
         }
 
-        double tRatio = 100.0 * (newProcessTime[tPid] - oldProcessTime[tPid]) / (kernel + user);
-        if (tRatio > 0.0 && tRatio < 100);
+        // 转换为百分比
+        double tRatio = 100.0 * (newProcessTime[tPid] - oldProcessTime[tPid]) / deltaTime;
+        if (tRatio > 0.0);
         {
             processCpu[tPid] = tRatio;
         }