Wednesday, February 12, 2020

[BTrace Bits: Part 1] Templated variables in BTrace

Foreword

There have been a fair number of additions to BTrace in the last year or two which, I am afraid, might have slipped the user attention as they were not given a lot of publicity at the time they were introduced.

The followup series of mini blog posts covering the potentially obscure but cool functionality available in BTrace - whimsically named 'BTrace Bits' - intends to fix it.


Ok. Let's start with something simple.


[1] Templated Variables


Since version 1.3.11 it has been possible to use template variables in the probe declarations. This allows delayed configuration at deployment time and makes the probe definition much more flexible.

A template variable has a value defined at deployment time and can be referenced using ant-like format - eg. ${refresh}. The variable value will be resolved as a plain string and will be the subject of any conversions necessary for the corresponding place of use.


Example: Templated uptime probe

The following probe will run the uptime check each uptime_period_ms milliseconds. 

@BTrace
public class Uptime {
    @OnTimer(from  = "${uptime_period_ms}")
    public static void f() {
        println("uptime: " + Sys.VM.vmUptime());
    }
}

The uptime check period value must be provided to BTrace agent - either as a javaagent argument or an argument to btrace dynamic attach launcher.

Attach on launch:
java -jar application.jar -javaagent:btrace-agent.jar=script=Uptime.class,uptime_period_ms=3000

Dynamic attach:
btrace <pid> Uptime.java uptime_period_ms=3000


Elements supporting templated variables

  • @OnMethod
    Templating similar to what was shown in the previous example is available for all @OnMethod attributes - clazz, method, type and location whereas again the templated variables can be used in its clazz, method and type attributes in turn.
  • @OnTimer
    The timer period can be defined via templated variable and is to be provided in the annotation's from attribute. The usage is shown in the previous example.

Tuesday, February 11, 2020

BTrace update for JPMS

BTrace 2.0.0


JPMS (Java Platform Module System) support

After long-long time BTrace finally got updated to work with JPMS

What does this mean? Well, now you can use BTrace for Java with JPMS (8, 9, 10, 11 and theoretically anything newer than 11, although no version after 11 was explicitly tested).

As for testing - the functional test suite is now executed for Java 8, 9 and 11 to make sure BTrace keeps on working with modules as well as with a number of internal APIs removed or made inaccessible. Java 8 testing ensures the ongoing backward compatibility without resorting to multiple agent binaries. You can see all the test rounds in the Travis CI.

This brings a slight complication to build process. BTrace now requires three environment variables defined - JAVA_8_HOME, JAVA_9_HOME and JAVA_11_HOME. They need to point to valid Java installations in order for the tests to work correctly. To make the setup easier there is `config_build.sh` script which tries automating the part of location and downloading JDK versions using SDKMAN!.


Preparation for move to OpenJDK GitHub (Skara)

In anticipation of moving the development to OpenJDK GitHub environment (project Skara, and yes, BTrace is an OpenJDK project) the sources were re-packaged not to use obsolete `com.sun.btrace` and go to `org.openjdk.btrace` instead.

For new probes you will have to use new package names but previously compiled probes should keep on working - there is a compatibility layer in place taking care of translating the package names.


Project restructuring

This item was long due. The project project was re-structured to have proper Gradle submodules and contain less 'after-build-magic'. As a bonus it will not build the DTrace lib when on supported OS (Solaris) and use a pre-built binary to include in the distribution image otherwise. Since the DTrace lib is barely changing this should be fine for most of the users.




Getting to BTrace 2.0.0 took some time. The release contain quite extensive changes and while it is passing all tests just fine and brief manual testing didn't show any problems either, please, keep in mind that there might be some wrinkles to iron and if you encounter any problems feel free to report them.