2

I'm working on a project that requires monitoring of the % of cpu in use. It has to be on both Mac and Windows, so I'm using Mono and Xamarin.

The code I'm using to get this is (this is a test, but the counter is the same):

var cpuCounter = new PerformanceCounter
{
    CategoryName = "Processor",
    CounterName = "% Processor Time",
    InstanceName = "_Total"
};

cpuCounter.NextValue ();
Thread.Sleep (1000);

for (var i = 0; i < 50; i++) {
    Console.WriteLine (cpuCounter.NextValue());
    Thread.Sleep (1000);
}

On a pc, this returns what the Task Manager shows. On a mac, this returns 100 on every tick, even when the activity monitor shows differently.

I've tried googling and nothing seems to work.

Ideas?

mccow002
  • 6,754
  • 3
  • 26
  • 36
  • We had a similar issue. We had to run the code in objective C to get it done.You can check the details here -> http://stackoverflow.com/questions/11460184/in-a-c-sharp-program-i-am-trying-to-get-the-cpu-usage-percentage-of-the-applica – Prashant Mar 19 '15 at 17:22
  • The article is very helpful in understanding how cpus work, but do you have any pointers for how you implemented it in objective c? I tried to implement an objective c counter myself and got no where. – mccow002 Mar 19 '15 at 19:13
  • Did my answer work for you ?. I have added the code in my answer. – Prashant Mar 23 '15 at 15:51

1 Answers1

3

We had a similar issue. The way we solved it was by writing a script. I will share the code in the script. And the way we called it from Mono.

Script(lets call it getCPU.sh and save it at say /Library/Application Support/UniqueApp)

#!/bin/bash
ps x -o pcpu,comm | grep UniqueApp

Mono code -

  var psi = new ProcessStartInfo("/bin/sh", "\"/Library/Application Support/UniqueApp/getCPU.sh\"")
                              {
                                  RedirectStandardOutput = true,
                                  UseShellExecute = false
                              };
                Process p = Process.Start(psi);
                string outString = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                outString = outString.Substring(0, 5).Trim()

outString will have the CPU percentage used by UniqueApp

If you want the CPU percentage for the entire machine change the getCPU.sh to have the following code -

#!/bin/bash
ps x -o pcpu | tail -n+2| awk '{s+=$1} END {print s}'
Prashant
  • 2,190
  • 4
  • 33
  • 64
  • Sorry, it took me a few days to get back to this problem. I tried this solution - I'm attempting to get the overall cpu usage for the whole machine. Sometimes the values were over 100%. Any idea why this is happening, and did you do anything in your app to handle this? – mccow002 Mar 24 '15 at 16:35
  • @mccow002 - We were monitoring only the CPU used by our App. So we did not run into the issue you are facing which is the value going beyond 100%. Is your mac multi core ? As per this thread(http://forums.macrumors.com/showthread.php?t=1186691), it is possible for processes to go as high as 760%-800% on i7 macs. – Prashant Mar 26 '15 at 13:12
  • So then you would just get the number from the shell script and divide it by the number of cores, right? – mccow002 Mar 26 '15 at 15:33
  • Awesome. Accepted Answer – mccow002 Mar 26 '15 at 18:26