public class FooTraceEventHandler extends TraceEventHandlerBase { public FooTraceEventHandler() { super(); this.hooks.add(new TraceHook("sched_switch")); } public void handle_sched_switch(TraceReader reader, CtfTmfEvent event) { // Process event } }
By convention, the function "handle_sched_switch" is called only with an instance of "sched_switch" event as argument. The magic happens by first getting a reference the method by it's name (done once), and then call it dynamically later in the processing path, similar to the following small example.
method = dummy.getClass().getMethod("foo", (Class[]) null); ... method.invoke(dummy);
To compare dynamic invocation to compile-time method call, I did a trivial benchmark program. Here are the results for 10 billion calls.
StaticMethodBenchmark 9,653000 DynamicMethodBenchmark 47,625000
This experiment shows that dynamic invocation is roughly 5x slower than compile-time method call. Dynamic invocations is also weaker, because a missing method for the corresponding hook will be detected only at runtime.
There are benefits to using hooks. The test for the event type is done only once for all handlers. It avoid quite a lot of repetition. Also, it allows to list events required for a specific analysis, and then enable only this subset in the tracing session, reducing the runtime overhead and the trace size compared to enabling all events. So, we need to preserve these properties, but with compile-time invocations.
Aucun commentaire:
Enregistrer un commentaire