• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CPU usage

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic