Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by micsun · Nov 01, 2012 at 09:45 PM · performancecpumonitorusageprocessor

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Althaen · Nov 03, 2012 at 12:23 AM 0
Share

So you want this to show CPU usage inside the game/console while the game is running?

avatar image Althaen · Nov 03, 2012 at 03:12 AM 0
Share

There's probably some software you could download that could tell you.

avatar image 170524_Plato · Sep 12, 2014 at 10:43 AM 1
Share

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 :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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";

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 170524_Plato · Sep 14, 2014 at 05:47 AM -1
Share

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

avatar image Dave29483 · Sep 15, 2014 at 01:46 PM 0
Share

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();
         }
     }
 }
avatar image neural · Nov 05, 2015 at 12:45 PM 0
Share

@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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

CPU usage, fps and colliders 0 Answers

Fire and forget coroutines 1 Answer

Loading time when pressing play in Unity is the same on a new MacBook Pro than it was on 2011 MacBook Air 0 Answers

Unity 3d CPU Usage on a Macbook Pro 3 Answers

iPhone Performance Question relating to CPU-WAITS-GPU 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges