Instrumenting Programs with Probes

Many instrumentation tasks can be accomplished by adding instrumentation code at particular points in the program that is triggered when that part of the program executes. A probe is an a piece of instrumentation that can be added to any instruction in the program. For example, if we are interested in recording all calls to a particular function, we can add a probe to the first instruction in the function. The probe will fire when this instruction executes and can produce output, record the time, or collect statistics about the call that we are interested in.


Probe Example: Branch Frequency Counter

Probes inserted at locations in the code of an executing program implement a special interface, the Simulator.Probe interface. This interface is nested inside the Simulator class and looks like this:


Suppose we are interested in the branch frequency for a particular branch in the program (for example in a performance-critical loop). We can write a new Java class that implements this interface and insert it at that particular instruction in the program. In our implementation, the fireAfter() method will check the program counter after the instruction executes. If the program counter points to the instruction following the branch, the branch was not taken. If the program counter points to any other location, then we can assume the branch must have been taken (ignoring interrupts for the sake of simplicity).



Probes can be as simple or as complex as your instrumentation task requires. For example, to profile the whole program, you can write a simple counter and simply insert it so that it executes after each instruction in the program. Or, a more complex task may be to measure the time to execute a procedure in your program. You could insert a "start" probe at the beginning of the procedure that records the time when the method is entered, and then an "end" probe at the end of the procedure that records the time difference. You might also want a probe to enable other probes or watches or disable them. For example to profile an interrupt handler's execution, but not the main program, you could enable the counter probe at the beginning of the interrupt handler and disable it just before exitting. Probes can insert watches or events, as well. The possibilities are limited only by your imagination.

More information: