Calculate total CPU usage

前端 未结 7 521
感动是毒
感动是毒 2020-12-18 15:55

What is best algorithm for calculating total CPU usage at a particular time during execution of a process.

I am working windows platform in C++.

相关标签:
7条回答
  • 2020-12-18 16:14

    Don't use WMI Simply Use win32 NAPI

    0 讨论(0)
  • 2020-12-18 16:16

    The simplest method to obtains total CPU load for a period is using GetSystemTimes API. It's a kernel function available starting Windows XP SP1. It returns the amount of CPU time used since system power-up. So, the difference between these values obtained during an interval will yield you total CPU usage for that interval.

    Remember, that kernel time provided by this function includes 'idle' time, so you have to subtract idle time returned by first parameter from it.

    The function returns total time for all cores/processors available, so you may also want to divide the deltas by the number of processors obtained from GetSystemInfo.

    0 讨论(0)
  • 2020-12-18 16:22

    GetProcessTimes for 100-nanosecond resolution. If you want cycle times, use QueryProcessCycleTime in Vista and above.

    0 讨论(0)
  • 2020-12-18 16:25

    Use WMI to get "PercentProcessorTime"; beware that this will probably have its own performance penalty.

    If you are looking to throttle your application, a better way it to have your own internal performance metric specific to your application and throttle based on that (e.g. screen refresh rate, etc.)

    0 讨论(0)
  • 2020-12-18 16:26

    Intel VTune is a great tool for anaylzing cpu resources: http://www.intel.com/cd/software/products/asmo-na/eng/239144.htm

    0 讨论(0)
  • 2020-12-18 16:27

    // Declare
    PerformanceCounter cpuCounter = null;

    // initialize somewhere in the constructor..
    cpuCounter = new PerformanceCounter();
    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    Then to get the CPU usage in % just call
    cpuCounter.NextValue()

    Source:http://zamov.online.fr/EXHTML/CSharp/CSharp_927308.html

    0 讨论(0)
提交回复
热议问题