CPU Usage inaccurate

272 views
Skip to first unread message

bosie

unread,
Jul 10, 2010, 8:35:27 AM7/10/10
to JavaSysMon
Hi,

i just downloaded javasysmon and tried to use it in my multi-threaded
java app. code here: https://gist.github.com/58a009cd489e6b6a30b0

and this is the output:
OS name: Mac OS X 10.6.3 Current PID: 89449
Number of CPUs: 8 CPU frequency: 2260 MHz
RAM total: 6144Mb free: 1760Mb SWAP total: 5120Mb free: 664Mb
Sampling CPU usage...
CPU Usage: 0.47382122
OS name: Mac OS X 10.6.3 Current PID: 89449
Number of CPUs: 8 CPU frequency: 2260 MHz
RAM total: 6144Mb free: 1272Mb SWAP total: 5120Mb free: 664Mb
Sampling CPU usage...
CPU Usage: 0.4680109
OS name: Mac OS X 10.6.3 Current PID: 89449
Number of CPUs: 8 CPU frequency: 2260 MHz
RAM total: 6144Mb free: 894Mb SWAP total: 5120Mb free: 664Mb
Sampling CPU usage...
CPU Usage: 0.46426386

interesting is the cpu usage because according to top I use between
500% and 600% on my 8-core machine (Mac Pro 8-core, MacOSX 10.6, Java
1.6.0_20). It is quite the discrepancy. I have a hyperthreaded CPU, so
it shows up as 16 cores but 0.46426386 from 16 are still not
500-600% :/

Any ideas?

Thanks in advance.

Best,
Thomas

Jez Humble

unread,
Jul 10, 2010, 10:38:31 AM7/10/10
to javas...@googlegroups.com
Hey Thomas

What are the user, sys and idle % numbers you're seeing? What JavaSysMon gives you in the printed output is 1 - (idle/total). If you use the Java API directly you can get the total user, sys and idle ticks.

I used Darwin's code for top as a reference to create the Mac OSX native code for JavaSysMon, so the two should agree. I'm a bit confused about what 500% CPU usage actually means - on my 2-core Mac, the user, sys and idle % numbers in top always add up to 100%.

Jez.

bosie

unread,
Jul 10, 2010, 2:35:59 PM7/10/10
to JavaSysMon
Hi Jez,

i see this:
Load Avg: 1.58, 1.08, 0.83 CPU usage: 35.98% user, 8.18% sys, 55.82%
idle
My 500% were referencing the process itself. see here:
PID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT
RSHRD RSIZE VPRVT VSIZE PGRP PPID STATE UID FAULTS COW
14419 java 744.4 00:25.37 34/1 2 255 709 1216M+
3328K- 1225M+ 2174M+ 52G 280 280 running 501 315337+ 303

This is my app. But I agree, the load avg/cpu usage line doesn't seem
to correspond to the process list below

Thomas

Jez Humble

unread,
Jul 10, 2010, 4:33:55 PM7/10/10
to javas...@googlegroups.com
I'm not sure how top calculates CPU usage for individual processes. However JavaSysMon can tell you how much of the CPU time your process is using. Use JavaSysMon().getProcessTable(), find your process in it (JavaSysMon().getPid() will get your pid) and the relevant ProcessInfo object has getUserMillis() and getSystemMillis() to tell you the total number of milliseconds the process has been running. Sample this over time and you should be able to calculate the CPU usage.

Thanks,

Jez.  

bosie

unread,
Jul 10, 2010, 5:34:40 PM7/10/10
to JavaSysMon
Uhm. monitor already returns only the current process when used in an
app. And I just started another app that consumes the remaining CPU,
monitor didn't seem to care because the reported CPU usage remained at
a constant. So that actually works.
It seems to be working now anyway, only thing is the HT CPUs. Is there
a way to get the number of cores without the HT ones? I see that you
get it through JNI, I haven't found a way to tell Java not to count HT
cores as 'regular' cores.

Thanks,
Thomas


On Jul 10, 10:33 pm, Jez Humble <j...@india.com> wrote:
> I'm not sure how top calculates CPU usage for individual processes. However
> JavaSysMon can tell you how much of the CPU time your process is using. Use
> JavaSysMon().getProcessTable(), find your process in it
> (JavaSysMon().getPid() will get your pid) and the relevant ProcessInfo
> object has getUserMillis() and getSystemMillis() to tell you the total
> number of milliseconds the process has been running. Sample this over time
> and you should be able to calculate the CPU usage.
>
> Thanks,
>
> Jez.
>

bosie

unread,
Jul 10, 2010, 5:38:27 PM7/10/10
to JavaSysMon
Sorry, what i meant before, since I am only building an average over a
certain timespan (in my case 2 seconds), the discrepancy is probably
explained by that. It is close enough to be called a minor
discrepancy ;)

Thomas

On Jul 10, 10:33 pm, Jez Humble <j...@india.com> wrote:
> I'm not sure how top calculates CPU usage for individual processes. However
> JavaSysMon can tell you how much of the CPU time your process is using. Use
> JavaSysMon().getProcessTable(), find your process in it
> (JavaSysMon().getPid() will get your pid) and the relevant ProcessInfo
> object has getUserMillis() and getSystemMillis() to tell you the total
> number of milliseconds the process has been running. Sample this over time
> and you should be able to calculate the CPU usage.
>
> Thanks,
>
> Jez.
>

Jez Humble

unread,
Jul 11, 2010, 2:26:53 AM7/11/10
to javas...@googlegroups.com
Yeah, on OSX post-Tiger there's no way to get performance info on other processes without elevated privileges. 

For getting the # cores, I use sysconf - the shell equivalent would be "getconf NPROCESSORS_CONF". You could try sysctl - some of the machdep keys might have this info (I don't have CPUs with hyperthreading so I can't tell) - you could always run sysctl as an external process if so.

Jez.

bosie

unread,
Jul 11, 2010, 5:25:47 PM7/11/10
to JavaSysMon
thank you, machdep indeed has this info:
machdep.cpu.logical_per_package: 16
machdep.cpu.cores_per_package: 8

I guess this is my solution ;)

Thomas

On Jul 11, 8:26 am, Jez Humble <j...@india.com> wrote:
> Yeah, on OSX post-Tiger there's no way to get performance info on other
> processes without elevated privileges.
>
> For getting the # cores, I use sysconf - the shell equivalent would be
> "getconf NPROCESSORS_CONF". You could try sysctl - some of the machdep keys
> might have this info (I don't have CPUs with hyperthreading so I can't tell)
> - you could always run sysctl as an external process if so.
>
> Jez.
>
Reply all
Reply to author
Forward
0 new messages