Per-thread CPU Usage JConsole Plugin

JConsole plugin that allows to plot the CPU usage of a set of threads running in a remote VM. It could be seen as an add-on to the existing JTop JConsole plugin in Java SE 6 (<JDK6_HOME>/demo/management/JTop). The aim of this plugin is to show how easy it is to add a custom UI to JConsole based on the Java SE platform instrumentation MXBeans in conjunction with the JFreeChart chart library.

Before going into the implementation details let's see how this new JConsole tab looks like:

(this example monitors the CPU consumption of the threads GreedyThread-1 and GreedyThread-2)

Per-thread CPU Usage JConsole Plugin

This JConsole plugin is made of two Java classes:

  • ThreadCpuUsage.java: This is the standalone Swing-based application which contains all the thread instrumentation semantics and directly uses the JFreeChart chart library.
  • ThreadCpuUsagePlugin.java: This is the wrapper class that extends the JConsolePlugin abstract class and implements its abstract methods.

The main differences between the standalone Swing-based application and the JConsole plugin are the following:

  • The plugin no longer creates a JFrame. The getTabs() method in the JConsolePlugin class is called to retrieve a JPanel for each of the tabs to be added to JConsole.
  • The plugin no longer needs to handle the JMX connection to the remote VM because JConsole already handles it.
  • The plugin no longer needs to start its own timer because the internal JConsole's timer is used. The newSwingWorker() method in the JConsolePlugin class is called at the same interval as JConsole updates the GUI.
  • JConsole uses the service provider mechanism to search the JConsole plugins so the jar file containing the JConsole plugin needs to specify the file META-INF/services/com.sun.tools.jconsole.JConsolePlugin. This file must contain one line for each plugin. The TheadCpuUsage.jar file contains a single plugin so it defines the line com.sun.example.ThreadCpuUsagePlugin.

Have a look at the readme.txt file in the resource bundle for more detailed info on how to use this plugin. The source code is also included in the bundle.

Comments:

This demo is very useful to add our own tab to their existing tab.

Posted by Raghavanantham.V on February 22, 2007 at 03:27 AM CET #

I have been using JDK6 Jconsole and plugins to run against JDK5 runtimes. I compiled and ran your code against a JDK5 runtime and received a: java.lang.reflect.UndeclaredThrowableException when calling: threadProxy.dumpAllThreads(flase,false)

I made the following change to that line of ThreadCpuUsage.updateTimeSeries():

< ThreadInfo tis[] = threadProxy.dumpAllThreads(false, false);
---
> ThreadInfo tis[] = threadProxy.getThreadInfo(threadProxy.getAllThreadIds()) ;

This change allows me to run your code in JDK6 Jconsole against JDK5 runtimes.<> Thanks for putting this together and posting this.

Posted by Michael Ottati on March 27, 2007 at 05:35 PM CEST #

This is excellent trend from sun to improve jmx management, will be nice if there are more samples like this from you.

Posted by Larry on January 06, 2008 at 08:02 PM CET #

Hi Luis-Miguel,

I'm looking for a way to compute the global CPU Usage of my JBoss Server. Do you think I can find an easy solution by reading the JConsole sources (about CPU Usage) ? Maybe this sources are not accesibles ?

Cordially

Antoine Verger

Posted by Antoine Verger on May 14, 2008 at 09:51 AM CEST #

Hi Antoine, the JConsole source code is available at http://openjdk.java.net.

Posted by Luis-Miguel Alventosa on May 26, 2008 at 07:03 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

lmalvent

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today