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?