Recording Java Metrics with the Windows Performance Counters

Example Java Code 
Library Performance 
Library and Source Code
Classspath 
CreatingCountersWindowsOperatingSystem 
Repository Organization
Related Posts

Java provides good monitoring through the MBean interface and monitoring tooling. It is nice because it works the same no matter what platform you run on.  There are times when you want to plug into the native performance recording tools especially when running on Windows machines because this makes it easy to integrate your Java application with your Windows based operational consoles.

A Java test program generates 2,000,000 performance counter events per second in a single thread and up to 6,500,000 updates per second multi-threaded. (source available on github) This means that counter updates can be made at normal volumes with very little effect on the system.

I've created a simple C# library and Java / JNI wrapper that lets you record native Microsoft Windows Performance Counters directly from your Java code.  Those events can then be viewed with perfmon.exe or other Microsoft Windows tools.  You can also view performance counters on remote machines using perfmon.exe.

The graph on the right shows how the rate operations per second counter behaves when feed a square wave of traffic: 200 messages per second for 2 seconds followed by 0 messages per second for 2 second.  The counter averages the rate/second across time resulting in a saw tooth pattern.


Example Java Code

The previous perfmon.exe image was built by the following code that calls the increment() functions on a single counter at regular intervals.

  1. The first portion initializes the bridge. 
  2. The second registers the category/counter combination to obtain a key. 
  3. The final section updates the counter in a loop.


...

Library Performance


This table describes the library's maximum performance as measured using a circa 2011 I7 Macbook Pro. The test programs are in the source tree on GitHub.  The C# library has two public interfaces.

  • WindowsPerformanceLiason is an instance method based interface that uses string Category, Instance and Counter keys in the same way the underlying calls to the windows system do. This approach suffers from continual string mapping and copying as strings are passed across the Java / C# boundary.
  • WindowsPerformanceFacade is a static class wrapper accepts integer keys representing a Category, Instance and Counter.  Callers first requests an integer key using those three strings and then uses the integer key from then on. The previous example code demonstrates use of this interface.
The JNI code was generated with JNI4Net.  Initial performance was pretty disappointing both from a maximum Counters / Second and from CPU usage point of view.  I found what I think is a small problem with the JNI4Net support library. I wrote a small powershell script that massages the generated C# JNI code to get the "Patched JNI4Net" numbers you see below.



ThreadsC# LibraryInstance Methods
String Keys
Default JNI4Net
Instance Methods
String Keys
Patched JNI4Net
Static Methods
Integer Counter Key
Default JNI4Net
Static Methods
Integer Counter Key
Patched JNI4Net
13,600,00014,000270,00014,0002,000,000
26,400,00026,000500,00026,0003,500,000
39,300,00040,000700,00040,0005,300,000
410,700,00046,000960,00046,0006,500,000
611,300,00047,500900,00047,5006,300,000
812,500,00048,00048,000

Library and Source Code

You can find the library on GitHub https://github.com/freemansoft/java-to-windows-performance-counters  C# source, Java test program source, powershell build and counter configuration scripts and pre-built binaries available on GitHub.  Pre-built dll and jar files are located in the Packages directory.

Most java clients will use the WindowsPerformanceCounterFacade class in green.  It totally encapsulates the functionality for updating Windows Performance Counters from a Java prog




Classpath

Your program's classpath needs to include the WindowsPerformanceCounter jar file, the jni4net jar file and the directory where the two dlls are on the file system. Here is my Eclipse .classpath file when working with the examples.

It should work for most folks building the examples because of the relative path to the libraries.

Creating Counters in the Windows Operating System

You must create the counters with using an account with Administrator privilge.  Powershell scripts are the simplest way to do this. You can find a powershell script in the java directory on Github.  A sample can also be found in an earlier blog article. You can create as many counters as you wish in a single Powershell script.  Compound or "bonded" counters must be created as sequential counters with the primary counter first and the base counter second.

Repository Organization

-- This section is incomplete --


Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Fraps Converter which is I often use is good product because of its specific things. In Farps converter, it is a kind of video format conversion to another fast and convenient. In fact, it's free don't hurt, either. Even is without any advertising, pop-up window or watermark. the original source


    ReplyDelete

Post a Comment

Popular posts from this blog

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

DNS for Azure Point to Site (P2S) VPN - getting the internal IPs