Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Access Windows Performance Monitor counters from Java, Part 1

Use a simple Java API to gather valuable performance statistics

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

For those running Java applications or services, the Windows Performance Monitor (WPM) contains a treasure trove of valuable information—see Figure 1. There are hundreds (or thousands) of performance statistics called counters that are available in real time and measure critical system performance factors. This article outlines a simple way to access those performance statistics from Java.

Figure 1. Windows Performance Manager. Click on thumbnail to view full-sized image.

The problem is that accessing WPM can be quite challenging as it is a native Windows service and few, if any, Java APIs are available for accessing the information it provides from Java. Some research revealed three main ways of reading the WPM data with Java:

  • Configure WPM to log specific performance counters: WPM can take a configurable set of counters and log them to a file on a configured frequency. Windows XP and Windows 2003 also allow WPM data to be logged directly to an ODBC (Open Database Connectivity) datasource. Once this data has been logged to its final destination, it should be trivial enough to read the logging file using the java.io or java.nio packages. If you are fortunate enough to be able to log directly to an ODBC datasource, then the WPM counter data will be accessible through the java.sql JDBC (Java Database Connectivity) packages. The deficiencies of this approach are:

    • No real time or frequency control: With this approach, you cannot simply request data when you want it. You must wait for WPM to log a counter to the persisted source. Then you must go out and get the data. These two factors typically involve some sort of polling process, which grows tiresome since you cannot get some data exactly when you want it, and you will get other data even when you don't want it. Worst of all, you may spend more time grappling with I/O or JDBC issues than you will actually reading WPM counter data.
    • Inflexible counter configuration: Every time you decide to add or remove a counter to the list persisted to the WPM counter log, you will need to go to the WPM console and reconfigure the logging options. This may also require a configuration (or code) change in your monitor application that retrieves data from the persisted store.
  • Native (Java Native Interface) interface to WPM: I have to fess up and say I generally hate working with Windows-native APIs. Any day of the week, I would rather hook into a nice Java jar file than a nasty dynamic link library hidden three layers deep. However, if you enjoy this sort of thing, several resources are available for guiding you through using a native interface to read WPM counters from Java. They usually involve either using Microsoft's own JVM with its native Windows interfaces or using JNI to directly communicate with the Windows-native API. Say what you will about proprietary Microsoft Java technology, it is certainly not portable. As far as the JNI solutions go, they appear to need constant changes depending on the versions of Windows and Java you use. The end product is not particularly portable either, since it will only run on Win32.
  • WMI: Windows Management Interface is a scripting interface to the management internals of all Win32 platforms since Windows 98. Some analogize it with Java Management Extensions (JMX) for Windows. WMI makes all sorts of Win32 internals accessible, including WPM counters. The integration of WMI and Java is not provided for, with the exception of Microsoft's proprietary Java VM implementation.


A simple and pure Java API provides a better approach for accessing WPM.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

thanksBy Anonymous on October 28, 2009, 11:30 amthanks

Reply | Read entire comment

thanksBy Anonymous on October 28, 2009, 11:30 amthanks

Reply | Read entire comment

thanksBy Anonymous on October 28, 2009, 11:34 amthanks

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources