Skip to content

Direct vs Generic instrumentation

Ignacio del Valle Alles edited this page Jan 21, 2020 · 8 revisions

bctrace defines two different approaches to instrumentation; "generic" and "direct", each one better suited to a different scenario.

These categories are relected in the agent API as different base implementations of hooks, filters and listeners.

Generic instrumentation

Generic Listeners expose a fixed general purpose interface capable of handling events coming from any method. They are suited for creating listeners able to be injected in multiple places, like it's the case in a logging or tracing agent, and handling events coming from any method.

Overhead

At runtime, generic instrumentation comes at the cost of creating an additional object array to pass the arguments to the generic listener method as well as the needed boxing/unboxing of the primitive values. Argument passing to generic listeners can be disabled (in order to reduce this overhead) by overriding requiresArguments()

Method identification

The bctrace runtime passes a method-id to the generic listeners, that can be used by the listener implementation to identify the instrumented method triggering the event, with the help of MethodRegistry. This operation is lightweight and don't add significant overhead, given at runtime each instrumented method just moves an integer literal as a parameter to the listener. Listener implementations can get the method information in O(1) time if needed.

Entry point

Currently only methods can be instrumented in a generic way. Call-site generic instrumentation has not been added yet.

The entry point in the API for using generic instrumentation is GenericMethodHook.

Direct listeners

Direct listeners on the contrary, have no fixed signature. They define a listener method annotated with @DirectListener.ListenerMethod whose signature must match (plus some additional parameters) the signature of the method or call-site being instrumented.

At runtime, they are called with minimal overhead given that both the arguments and return values are passed to or from them as they are, without needed transformations.

Entry point

Both methods and call-sites can be instrumented in a direct way. The entry points in the API are: