• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CPU usage

 
Vu Pham
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic