How do you get processor usage of each thread (or CPU core)?

Is there a programmatic way to get the processor usage percent of each thread? If not, then is there a way to retrieve what each CPU core’s processing usage percent is? If not that, then is there a way to retrieve what the current processor usage percent is?

I tried the following (and many other variations similar this):

using UnityEngine;
using System.Diagnostics;

public class Stats : MonoBehaviour
{
    PerformanceCounter cpuCounter;
    
    void Start()
    {
    	cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
    }
    
    void Update()
    {
    	print("% Processor Time: " + cpuCounter.NextValue().ToString());
    }
}

This returns:

% Processor Time: 0

Anyone know what I’m doing wrong or if there is an alternate way to do this?

Performance Monitor → Process → % Processor Time → Process.GetCurrentProcess().ProcessName

And tried…
Performance Monitor → Processor → _Total

I am using Unity Pro 4.0.0b7. I do realize there is a Stats dialog, but I would like users to be able to see stats through the stand-alone player in a custom display.

Also, I tried getting the CPU usage per core using the following:

using UnityEngine;

using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;

public class Stats : MonoBehaviour
{
	List<PerformanceCounter> _cpuCounters = new List<PerformanceCounter>();
	
	void Start()
	{
		for (int i = 0; i < System.Environment.ProcessorCount; i++)
		{
			_cpuCounters.Add(new PerformanceCounter("Processor", "% Processor Time", i.ToString()));
		}
	}
	
	void Update()
	{
		string cpuUsage = "";
		
		for (int i = 0; i < System.Environment.ProcessorCount; i++)
		{
			cpuUsage += _cpuCounters*.NextValue() + ", ";*
  •  }*
    
  •  print("cpuUsage: " + cpuUsage);*
    
  • }*
    }
    This resulted in the following output:
    > cpuUsage: 100, 100, 100, 100, 100, 100, 100, 100,
    What is the correct way to get the CPU usage?

Update

    //You would create an instance per CPU/Core you want
    PerformanceCounter cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Processor Information";
    cpuCounter.CounterName = "% Processor Time";

    //Which CPU/Core 0,0 = CPU 0, Core 0
    cpuCounter.InstanceName = "0,0";

    //Init - Otherwise first value would return 100 in timer
    cpuCounter.NextValue();

Then call cpuCounter.NextValue() at the frequency you desire.

Original

As far as I understand it, when you call cpuCounter.NextValue() it will return 0. It will also return 0 on subsequent calls as you haven’t left enough time between samples.

So it should be like;

  • Call NextValue()
  • Wait 1000ms using Thread.Sleep() (or your sample interval)
  • Call NextValue()

The second call after a wait should return the true cpu time :slight_smile:

May be best to use a coroutine so it doesn’t hang your main thread.

Oh and setup the cpuCounter with;

cpuCounter.CategoryName = “Processor Information”;

cpuCounter.CounterName = “% Processor Time”;

cpuCounter.InstanceName = “_Total”;