How to get the number of CPU cycles used by a process

后端 未结 3 844
离开以前
离开以前 2021-02-02 18:19

I have a need to get the number of CPU cycles used by a specific process using C# (or VB.Net). This information is available in the Process properties popup within Sysinternal\

相关标签:
3条回答
  • 2021-02-02 18:43

    Process Explorer calls QueryProcessCycleTime to get that information. You will probably have to use P/Invoke to call it. I would expect its P/Invoke signature to look like:

    [DllImport("kernel32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool QueryProcessCycleTime(IntPtr ProcessHandle, out ulong CycleTime);
    
    0 讨论(0)
  • 2021-02-02 18:52

    There is a CodeProject article here: http://www.codeproject.com/KB/system/processescpuusage.aspx

    I think this article will help you do exactly what you are trying to do. Basically they show you 2 ways to do it, one using the managed System.Diagnostics tools and the second using a Win API method call.

    0 讨论(0)
  • 2021-02-02 18:53

    You will need to query the performance counters inside the CPUs. This is low level and very hardware specific, so you'll have to thunk to native code to get it. PAPI is the closest thing to a portable library for this task.

    Be aware that context switches can change many of these internal CPU registers, so you'll need to do this query from inside your process. Querying the CPU counters from a different process will give you spurious results.

    Remember also that CPU cycle count is not the same as (and on the x86, isn't even similar to) "number of instructions performed."

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