2014-03-05 290 views
6

我發現了很多來源來獲取每個進程的CPU使用情況。通常有很多方法可以獲得進程的CPU使用情況。在System.Diagnostics程序 從wmi獲取每個進程的CPU使用情況

  • 從win32_perfformatteddata_perfproc_process

    1. percentprocessortime
    2. PerformanceCounter類由手工計算
    3. Process類(通過process.getcurrentprocess()totalprocessortime) 在here作爲所述。

    FirstWay:

    對於遠程過程監視(我的方案是遠程監控),則percentprocessortime總是顯示值0至100+。這100多個因爲系統中的多個處理器而發生。可以使用percentprocessortime/processorcount來計算。

    問題在firstway:

    我可以讀取WMI瀏覽器中的percentprocessortime,它示出了所有的值是0或100僅比該值不是其他。這個數值是否正確?或者它對監視值有用嗎?

    方式二:

    爲PerformanceCounter類的監測,可以僅爲本地完成。所以我不能使用這個。有可能使用這個遙控器?

    第三條道路:

    (最大的困惑在這裏發生的無論在哪個式使用。)這個計算是通過從一個WMI PerformanceCounter類或Win32_Process類製成。一些說通過使用follwing

    考慮單個CPU和

    (處理器\%處理器時間)= 10%

    (處理器\%用戶時間)= 8%來計算性能計數器

    (處理器\%特權時間)= 2%

    (過程\%處理器時間\應用程序)= 80%

    你的應用程序使用80% (處理器\%用戶時間)是(8 * .8)= 6.4%的CPU。

    更多請參考here

    通過使用下面的公式

    DateTime firstSample, secondSample; 
    firstSample = DateTime.Now; 
    queryObj.Get(); 
    //get cpu usage 
    ulong u_oldCPU = (ulong)queryObj.Properties["UserModeTime"].Value 
           +(ulong)queryObj.Properties["KernelModeTime"].Value; 
    //sleep to create interval 
    System.Threading.Thread.Sleep(1000); 
    //refresh object 
    secondSample = DateTime.Now; 
    queryObj.Get(); 
    //get new usage 
    ulong u_newCPU = (ulong)queryObj.Properties["UserModeTime"].Value 
           + (ulong)queryObj.Properties["KernelModeTime"].Value; 
    decimal msPassed = Convert.ToDecimal(
              (secondSample - firstSample).TotalMilliseconds); 
    
    //formula to get CPU ussage 
    if (u_newCPU > u_oldCPU) 
        PercentProcessorTime = (decimal)((u_newCPU - u_oldCPU)/
               (msPassed * 100 * Environment.ProcessorCount)); 
    
    Console.WriteLine("Process name " + queryObj.Properties["name"].value);      
    Console.WriteLine("processor time " + PercentProcessorTime); 
    

    上面的代碼的結果輸出在85.999有時135.89888計算來自Win32_Process的所述usermodetime和kernelmodetime。我很困惑我可以計算過程的CPU使用率。

    注意: 其重複。我無法從現有資料中得出結論。我很困惑。所以只有我問了一個問題。

  • 回答

    2

    您可以使用WMI來查詢。我想你正在尋找Win32_PerfFormattedData_PerfProc_Process類。

    using System; 
    using System.Management; 
    using System.Windows.Forms; 
    
    namespace WMISample 
    { 
        public class MyWMIQuery 
        { 
        public static void Main() 
        { 
         try 
         { 
          ManagementObjectSearcher searcher = 
           new ManagementObjectSearcher("root\\CIMV2", 
           "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"); 
    
          foreach (ManagementObject queryObj in searcher.Get()) 
          { 
           Console.WriteLine("-----------------------------------"); 
           Console.WriteLine("Name: {0}", queryObj["Name"]); 
           Console.WriteLine("PercentProcessorTime: {0}", queryObj["PercentProcessorTime"]); 
          } 
         } 
         catch (ManagementException e) 
         { 
          MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); 
         } 
        } 
        } 
    } 
    

    輸出: - Output when I run on my machine

    +0

    在我看來,CPU使用率[ 「PercentProcessorTime」]是錯誤的。只有3個進程使用非0 PercentProcessorTime _Total,並且在100處空閒,然後下一個進程的PercentProcessorTime大約爲5。 – aeroson

    相關問題