Monitor a process's usage of each CPU core
Asked Answered
A

4

18

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

For example,

Name - - - - Core1 - Core2 - Core3 - Core4
firefox.exe - 0.5% - - 0.1% - - 0.2% - - 0.3%

I know a program called "Process Explorer" (by Mark Russinovich) that can do this, but how can I do this in C#? I've had a look at the System.Diagnostics namespace, but I couldn't find anything fitting to my requirements.

Here's a picture of what I'm trying to achieve,

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/XySpQ.png

Articulation answered 22/9, 2013 at 14:45 Comment(3)
@RolandPihlakas I can only see a way to either get usage for individual cores or individual processes not per process per core. If only Windows hadn't made sysinternals closed-source.Delineation
By the way, I did not find such functionality in the Process Explorer. As I see it, Process Explorer also displays only CPU usage per process for sum of cores or per core for overall system, but not per process per core. Still I think it should be possible.Allusive
#261136 this is what you wantOddity
G
3

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.

Glazier answered 7/4, 2014 at 22:33 Comment(2)
Yes, the core tracing thing seems promising. A few citations: "The graph shows logical cores on the y-axis and time on the x-axis. Every thread in the graph has a unique color so that you can track its movement across cores over time." - msdn.microsoft.com/en-us/library/dd627197.aspx "During profile analysis, the Concurrency Visualizer examines all operating system context-switch events for each application thread." - msdn.microsoft.com/en-us/library/dd627193.aspxAllusive
Really, glad to find a way to get this thing done. Thanks !! I am going to give it a try and explore more for my needs.Seve
S
0

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.

Seve answered 7/4, 2014 at 20:13 Comment(1)
Some brain food [Intel specific] on the related topic. software.intel.com/en-us/articles/…Seve
B
-1

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);
    }
Birch answered 31/3, 2014 at 22:37 Comment(1)
Thanks, as I understand it, this code gives core usage for overall system? The question is about obtaining core usage for a single process - how much time a specific process spent on core1, how much on core2 etc.Allusive

© 2022 - 2024 — McMap. All rights reserved.