• 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

Calculation of CPU load

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I want to calculate the CPU load using a single process in JAVA. The load can be in % and as shown in the task manager (CPU usage) in Windows.

Can you give me hint or brief notes on it.

Thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no way to calculate it, but you can certainly ask Windows what it is. There are multiple ugly ways you might do that. For example, on Windows XP Pro (and presumably some of your more Pro-life versions of Vista, as well) there's a program called "tasklist" which reports process info to standard output. You could run it with a Runtime.exec() call and extract the number(s) you want from the output.

You could also use JNI, the Java Native Interface, to call the appropriate Win32 function. You might be able to think of some even more hideous ways to do it.

Or you could use the right tool for the job, and use a Windows language like C#, C++, etc, to write the whole thing and talk to the API directly.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Thanks for the reply.
I have to use native method which is written in C++, etc to get the CPU load.

How can I use the JNI API to accomplish the task ?


Thanks
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijit:
How can I use the JNI API to accomplish the task ?



JNI is just a way to call native programs from java. So, the assumption is that you have written something in C/C++ to talk to the api for getting CPU usage details. JNI will just help you to call this C/C++ program from Java and get the results.
[ December 08, 2008: Message edited by: Nitesh Kant ]
 
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
An easier way is probably to do a runtime.exec() to the typeperf command. This command report on OS counters -- including the counters for the CPU usage.

Henry
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I have come across API of JRobin to calculate the CPU load. It takes the support of NET-SMPT jars. But I cannot able to configure the SMPT. Resgistration and Start service are done . But the log files show it is not properly started.
If any body use these two JARs to acomplish the CPU load, then please help to configure all.

While running the main function , Timeout exception is comming.


 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Open Source forum now.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use
ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime()
and
ManagementFactory.getThreadMXBean().getThreadCpuTime()
to get the CPU time consumed by threads.

If you use a Custom ThreadGroup or poll you Thread periodically, you can collect this information for Threads which die.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic