Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JConsole CPU Usage

807588Nov 29 2007 — edited Jun 3 2009
Hey I am writting a JConsole plugin and for this plugin I need to be able to calculate the CPU Usage. I see that it is being determined and displayed on the Overview Tab of JConsole and I was wondering if anyone knew how I could access that value or calculate it myself. I have tried a few different methods, all of which have had bad results.

My best attempt to do this involves me gathering all the thread ID's from a ThreadMXBean. The short of it is I have a loop that executes approximetly every four seconds. In this loop it gathers the total execution time for all my threads by first gathering the thread ID's of each thread from a ThreadMXBean and then using the ID's to get the Thread CPU time of each thread (all the while it sums the Thread CPU times to get a total CPU time value). It's important to note here that after calculating the total CPU time i store the value in a variable so that it can be used in later loops. The reason I use it in later loops is because the next thing my program does is it subtracts the total execuation time of the threads from the previous time the loop ran from the total execuation time that I had just calculated. This way I know how much time it has spent working in these threads since the loop last ran. After this I calculate how much real world time has passed in a smilare method using System.currentTimeMillis() as my method for gathering time to begin with. Once I have how much CPU execution time has passed i divide it by how much real world time has passed and then multiply that result by 100 in order to get a percentage. Problem is the percentage i am getting does not match the percentage that the JConsole it self calculates.

I also have noticed that my CPU time difference does not match the difference in time of JConsoles Process CPU time. That is I watch JConsoles Process CPU time too see how much it is incrementing by and compare it to how much my calcualted time difference is and they do not match. Mine is always larger then the difference in time that JConsole shows, usually around 2 times as much but not always.

Anyways, I know that's kind of confusing to read but my hope is that there is just a much simpler or well defined way for Calculating how much CPU usage my program is putting on the system. Thanks in advance for anyones help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2009
Added on Nov 29 2007
1 comment
354 views