This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes CPU usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "CPU usage" Watch "CPU usage" New topic
Author

CPU usage

Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
Hi all,

I knew that Java doesn't have API for measure CPU Usage. I have already found .dll written in C , then using JNI to communicate with it. But the problem is that I just can use that .dll in Windows. Do you know any library that I can use in Linux? Thanks very much
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A different approach: In Java5 or later, look at JConsole in the JDK. The APIs it uses to get all that information are available for you to use, too. See if it has the metrics you need.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

If you are willing to use Runtime.exec(), you can call "vmstat". (or mpstat) The last set of fields provide info on the cpu usage. Just make sure that you use a count of more than one, as the first datapoint is a long term average -- not useful to get current usage.

If you insist on JNI, you can use the library that is used by "vmstat". I believe this is implemented by a set of kernal statistic calls -- kstat library calls. However, I would not be surprised if Linux also provides a /proc FS to the cpu usage data too. You will have to check the man pages for your particular Linux OS.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: CPU usage