Per-thread CPU Usage JConsole Plugin
By lmalvent on Feb 02, 2007
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)
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
getTabs()method in the
JConsolePluginclass is called to retrieve a
JPanelfor 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
JConsolePluginclass 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.jarfile contains a single plugin so it defines the line
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.