Getting physical CPU usage in Java?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I'd like to be able to determine what the total CPU usage is on a
system using a Java program. (for those of you familiar with the Unix
program Top, it's the same idea. I want to be able to poll the system
to see how much CPU time is being used right now.)

Is there an easy way to do this? Is the method platform
indepdendent? ie. Since Java can run on any platform, will the
solution also work on any platform?

Thanks!
 
R

Roedy Green

On Tue, 14 Jul 2009 19:44:01 -0700 (PDT),
Is there an easy way to do this? Is the method platform
indepdendent? ie. Since Java can run on any platform, will the
solution also work on any platform?

For Intel, see http://mindprod.com/products1.html#PENTIUM
to get at the cycle counting registers.

Generally, the closest you will get is System.nanotime

See http://mindprod.com/jgloss/time.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
 
N

nooneinparticular314159

It looks like these solutions tell you how much time it is taking to
run *your* process. What I'm interested in is the amount of time that
the CPU is being utilized overall, by all processes, as would be
reported by top. Is this possible to obtain from within Java?
 
A

Arne Vajhøj

nooneinparticular314159 said:
It looks like these solutions tell you how much time it is taking to
run *your* process. What I'm interested in is the amount of time that
the CPU is being utilized overall, by all processes, as would be
reported by top. Is this possible to obtain from within Java?

On most platforms it will only be possible using JNI.

Java is not a good language for something so extremely OS
specific as looking up processes and checking their
resource usage.

On Linux you could try reading some of the pseudo files
that provides all information about everything.

Arne
 
Joined
Feb 11, 2009
Messages
12
Reaction score
0
Have you tried using profiler applications? Yourkit might be a good one for you.
 
A

alexandre_paterson

Hi Eric,

Perhaps it's time to step back and ask what the result will be used
for.

I don't thinks so :)

"CPU usage" is not a portable notion to begin with, so the need
to query it in a portable program is already suspect.

It depends on how "portable" the program needs to be. If supporting
Windows, OS X, Linux and other Un*x flavors is enough, then it's not
because there are two or three OS specific implementations that the
program is not portable.

I'm working on a codebase that is deployed on a lot of various OSes
and it's 0.002 % of "non-portable" OS-specific script lines (just
did the count with 'wc -l'). The rest is 99.998 % Java code. Is
my program non portable?
... and ask what the result will be used for.

What are the results of 'top' used for?

What are the results of, say:

http://www.lambdaprobe.org/d/screenshots/full/system.png

used for?

Here's a test I often use to decide whether I'm asking a useful
question of the environment: If I made the query and got X as my answer,
what would my program do differently than it would with answer Y, and
why? In the O.P.'s case: If he's able to measure "CPU usage" and finds
that it's forty-two percent, what will he do differently than if he'd
found it to be seven percent or ninety-eight point six percent?

Launch new EC2 instances when the load is too high?

Lower the "load" of your own Java program to let the OS be
more responsive?

The OP has a very good question and it's an indisputable fact
that there are many legitimate cases where this information is
definitely useful.

:)
 
E

Eric Sosman

Hi Eric,



I don't thinks so :)



It depends on how "portable" the program needs to be. If supporting
Windows, OS X, Linux and other Un*x flavors is enough, then it's not
because there are two or three OS specific implementations that the
program is not portable.

On a four-core chip, what is a "CPU" and how much of the
"CPU" is used by one compute-bound infinite loop? One point
of view is that the "CPU" is active all the time, and hence
100% busy. Another says that three of the four cores are idle,
so the "CPU" is only 25% busy.

But wait! Suppose each core supports two "strands" of
execution, cycle-switching between the strands. So seven of
the eight strands are idle, and the "CPU" is 12.5% busy.

But wait! It turns out that the two strands on a core
share the core's lone execution pipeline, so that they can't
both retire an instruction at each cycle; they take turns.
So seven of the eight strands are idle, but one of the idle
strands would slow down the infinite-loop strand if it were
to become active -- so what fraction of the "CPU" is busy?

I happen to work for a company (soon to be but a memory)
that makes processor chips in various of these combinations.
Eight cores with thirty-two strands, eight cores with sixty-
four strands, four cores with eight strands -- and that's just
the company's own CPU designs; we also build systems that use
multi-core multi-strand processors from AMD and Intel. Having
used several of these myself, I can tell you: "CPU utilization"
is a non-portable notion even at the hardware level, never mind
what obfuscations the O/S contributes. (Hypervisors, anyone?)
Launch new EC2 instances when the load is too high?

Lower the "load" of your own Java program to let the OS be
more responsive?

The CPU utilization on the machine I'm using to write this
message can never, ever exceed fifty percent. Why? Because the
O/S believes that the system has two CPU's, but it actually has
just one CPU supporting two "hyperthreaded" execution strands,
only one of which can do anything at a given time. When my "CPU
utilization" is fifty percent, the system is saturated, busy,
incapable of performing more work. A program that says "Only
fifty percent? Hah! Plenty of headroom here!" is unlikely to
make good decisions ...

Application programs, by the way, have no business trying
to make such decisions. The fundamental problem is that the
program does not know its own importance in relation to other
activities on the system; "importance" is in the mind of the
system owner/operator/user, where the program can't detect it.
Does the program refrain from taking on more work because it
thinks the CPU's are overburdened? Bad choice: The user was
just about to migrate a couple more CPU's into the domain to
help the program get more stuff done. Does the program see
that the CPU utilization is low and decide to spawn twelve more
instances? Bad choice: The user was just about to steal a few
CPU's away and give them to a more important activity. The
program is "down in the trenches," and can't see enough of the
battlefield to make good strategic decisions about resources.

Not so many years ago it was easy to measure the utilization
of a disk: If it executed ten 10-ms I/O operations in the course
of one second, it was busy for 100 ms and its utilization was
ten percent. But then disks started to be able to handle multiple
operations simultaneously, and SAN's and NAS'es and such could
handle dozens at a time. Those ten 10-ms operations might overlap
and all be completed within a single 20-ms interval, leaving the
disk completely idle for 980 ms for a utilization of two percent.
"Disk utilization," once a crucial figure for capacity planning,
system tuning, and system management, became nearly worthless --
and what it meant varied widely from one disk subsystem to the
next. Something very similar has now happened to CPU's.
 
A

Arne Vajhøj

I don't thinks so :)

That is usually a very relevant question.
It depends on how "portable" the program needs to be. If supporting
Windows, OS X, Linux and other Un*x flavors is enough, then it's not
because there are two or three OS specific implementations that the
program is not portable.

I'm working on a codebase that is deployed on a lot of various OSes
and it's 0.002 % of "non-portable" OS-specific script lines (just
did the count with 'wc -l'). The rest is 99.998 % Java code. Is
my program non portable?

Obviously not.
What are the results of 'top' used for?

It is used on Linux to check load.

It does not exist on Windows, OpenVMS, z/OS etc..
Launch new EC2 instances when the load is too high?

Lower the "load" of your own Java program to let the OS be
more responsive?

The OP has a very good question and it's an indisputable fact
that there are many legitimate cases where this information is
definitely useful.

Could be.

But until we do get more information, then we can not
say whether the current direction is optimal or a change
of direction would be better.

Arne
 
J

jlp

Arne Vajhøj a écrit :
That is usually a very relevant question.


Obviously not.


It is used on Linux to check load.

It does not exist on Windows, OpenVMS, z/OS etc..


Could be.

But until we do get more information, then we can not
say whether the current direction is optimal or a change
of direction would be better.

Arne
Look at pure java clase ( >= JDK 1.5):
Interface java.lang.management.ThreadMXBean
I use it with AspectJ/LTW , and i can get Time System CPU and Time User
CPU of a method.
Profilers gives it also.
 
A

Arne Vajhøj

jlp said:
Arne Vajhøj a écrit :
Look at pure java clase ( >= JDK 1.5):
Interface java.lang.management.ThreadMXBean
I use it with AspectJ/LTW , and i can get Time System CPU and Time User
CPU of a method.
Profilers gives it also.

The original poster wanted the CPU usage of all processes
on the system - can ThreadMXBean give that ??

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top