Monitor a process's usage of each CPU core

后端 未结 4 1395
春和景丽
春和景丽 2021-02-13 02:01

Is there a way to query or calculate the CPU usage of a single process per each core separately?

For example,

Na

相关标签:
4条回答
  • 2021-02-13 02:18

    This question caught my eye and I started looking into it. Long back I too had a similar requirement but was not able to figure out answer for the same.

    Ultimately I had to use the fall-back and the only option available to monitor process processor usage time in % (without any kind of theoretically calculative work or affinity fixes): Process / % Process Time.

    I saw the graph - "Show one graph per CPU" (snapshot) in question and started investigating "Process Explorer" myself with surprise to figure out how does it manages to show what you or me (in past) were seeking.

    Answer to your question is - "No it is not possible to find out per core processor usage per process" and your assumption of what "Process explorer" is showing is explained in the below quoted statement I found in the "procexp.chm" (available with the zip you download) topic > Process Explorer > System Information > Last Paragraph:

    Note that the mouse tooltips for a processor graph show the name of the process that consumed the most CPU on the entire system at the associated time, not the process that consumed the most CPU on the particular CPU

    I hope I am making sense here.

    0 讨论(0)
  • 2021-02-13 02:20

    There is a way to get that information, you will need to use a Kernel Debugger. My suspect were that if the Visual Studio Concurrency Visualizer can get this information to make beautiful reports and visualizations, then you can also get it from the Windows Kernel for you use.

    You can see some documentation here http://msdn.microsoft.com/en-us/library/windows/hardware/ff552961(v=vs.85).aspx .

    But more interesting things can be found by reversing the Microsoft.ConcurrencyVisualizer.dll that can be found on Visual Studio 2012 installation directory. I think you can also use directly this DLL on your project, although you cannot redeploy it as per http://msdn.microsoft.com/en-US/vstudio/hh857605#files. That DLL is available standalone here http://www.microsoft.com/en-us/download/details.aspx?id=41646.

    The idea is to learn how this DLL uses the Kernel Trace to know what Thread was on what Core and replicate it to get accurate statistics. Also, I think that ProcessExplorer does get some stats by using the Kernel Debugger API because the Visual Studio conflicts with it when I start the Concurrency Visualizer tool.

    This will be fun and hard to do.

    0 讨论(0)
  • 2021-02-13 02:21

    See: http://msdn.microsoft.com/pt-br/library/system.diagnostics.performancecounter.aspx And How to get the CPU Usage in C#?

    0 讨论(0)
  • 2021-02-13 02:28

    Try the below (found it here)

        int coreCount = 0;
        foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
        {
            coreCount += int.Parse(item["NumberOfCores"].ToString());
        }
    
        PerformanceCounter[] pc = new PerformanceCounter[coreCount];
    
        for (int i = 0; i < coreCount; i++)
        {
            pc[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
            Console.WriteLine(pc[i].CounterName);
        }
    
    0 讨论(0)
提交回复
热议问题