- Home /
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[i].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?
So you want this to show CPU usage inside the game/console while the game is running?
There's probably some software you could download that could tell you.
Bump did you solve the issue? Even i want to get the usage of each core but output is co$$anonymous$$g as 100 for all cores :(
Answer by Dave29483 · Sep 12, 2014 at 12:07 PM
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 :)
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";
Still not working i did this
System.Diagnostics.PerformanceCounter counter = new System.Diagnostics.PerformanceCounter();
counter.CategoryName = "Processor";
counter.CounterName = "% Processor Time";
counter.InstanceName = "_Total";
and inside coroutine
while( true ){
yield return new WaitForSeconds( 1 );
print( "Cpu Usage : "+counter.NextValue() );
}
still it's giving 100 as output
Just did a quick test which seems to work fine.
Also you are using category "Processor" and not "Processor Information"...
using System;
using System.Collections.Generic;
using System.Component$$anonymous$$odel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace CPU
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
PerformanceCounter cpuCounter = new PerformanceCounter();
private void button1_Click(object sender, EventArgs e)
{
//You would create an instance per CPU/Core you want
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();
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
Text = cpuCounter.NextValue().ToString();
}
}
}
@Dave, the question is about doing this on unity. Your code as those of others does not work on unity even though it may work on simple standalones. If anybody finds a true solution tested on unity please note that a it is still searched for. Thank you