Skip to content

Parallel Libtrace HOWTO: Callback Sets

Shane Alcock edited this page Jan 12, 2017 · 2 revisions

A callback set describes all of the callbacks for a thread, either a processing thread or a reporting thread. To tell libtrace which callbacks that a thread should invoke, you will first need to create and populate a callback set. You'll need one callback set for your processing threads and one for your reporter thread (if you intend to have a reporter thread). Multiple input traces may share the same callback set, so if you are processing a set of trace files in sequence you will only need to configure your callback sets once.

You should create and configure your callback set in the configuration step between creating your input trace and starting your input trace.

In our example, we are going to need to populate a processing callback set with our start, stop and packet callbacks and a reporting callback set with our start, stop and result callbacks. The code fragment below shows how to do this:

/* One callback set for processing threads, one for the reporter 
 * thread.
 */
libtrace_callback_set_t *processing = NULL;
libtrace_callback_set_t *reporter = NULL;

/* Setting up the processing callbacks.
 * First, create the callback set */
processing = trace_create_callback_set();

/* There is a special function for setting each of the possible
 * callback functions. You simply need to call the right function
 * with the callback set as the first parameter and the name of
 * the callback function as the second parameter. If your callback
 * function is incorrect, i.e. it has the wrong arguments or return
 * type, you will get an error at compile time.
 */
trace_set_starting_cb(processing, start_processing);
trace_set_stopping_cb(processing, stop_processing);
trace_set_packet_cb(processing, per_packet);

/* Repeat for the reporter callback set, but make sure we set a 
 * result callback instead of a packet callback.
 */
reporter = trace_create_callback_set();
trace_set_starting_cb(reporter, start_reporter);
trace_set_stopping_cb(reporter, stop_reporter);
trace_set_result_cb(reporter, per_result);

If a callback is not set, then nothing will happen when the event that would otherwise trigger that callback occurs. This means that you only have to worry about implementing callbacks for events that you care about. For instance, if you did not require thread local storage in the reporter, you could skip adding start and stop callbacks for the reporter callback set and nothing untoward will happen.

The final step in getting our program running is now to create and start our parallel input trace.

Clone this wiki locally