Skip to content

Parallel Libtrace HOWTO: Pausing and Resuming Callbacks

Shane Alcock edited this page Apr 29, 2019 · 3 revisions

One thing you may have noticed so far is that our examples only give you one chance to configure your libtrace program, i.e. once you call trace_pstart, none of the configuration or callback setting functions will have any effect.

However, it is possible to pause a parallel libtrace program which will cause the hasher thread(s) to stop assigning packets to the processing threads. During this time, you can alter or apply any configuration and then call trace_pstart again to resume the hasher thread. As long as a trace is paused, no packet callbacks will be triggered.

The below code demonstrates how we can pause a trace and increase the tick frequency (maybe in response to a user pushing '+' on the keyboard, for instance). Note that we pass a lot of NULLs into trace_pstart() when we resume the trace; this is to indicate that we have not modified the global variable or callback sets so the trace can continue to use the original values. If we had changed any of these parameters, we would need to pass the new values into trace_pstart().

int interval = 10000;

/* Decreases the tick interval by 1 second */
static void pushed_plus(libtrace_t *trace) {
    /* Pause the trace so we can adjust our tick interval safely */
    trace_ppause(trace);    

    if (interval > 1000)
        interval -= 1000;
    
    trace_set_tick_interval(trace, interval);
    
    /* Configuration updated, resume the trace. Since we haven't
     * changed any of the arguments we passed in to trace_pstart
     * originally, we can pass NULL arguments to tell libtrace to
     * continue using the old values.
     */
    trace_pstart(trace, NULL, NULL, NULL);
}

Both processing and reporter threads support callback functions that can be triggered whenever an input trace is paused or resumed. For example, it may be a good idea to reset any counters whenever a trace is resumed in case a configuration change has caused any previous values in the counters to be invalid.

Pause and resume callbacks take the following form:

static void pause_processing(libtrace_t *trace, libtrace_thread_t *thread,
        void *global, void *tls) {

    /* Insert code for pause callback here */
}

static void resume_processing(libtrace_t *trace, libtrace_thread_t *thread,
        void *global, void *tls) {

    /* Insert code for resume callback here */
}


    /* somewhere in main where we create our callback sets */
    trace_set_pausing_cb(processing, pause_processing);
    trace_set_resuming_cb(processing, resume_processing);

Many libtrace programs won't need to support pausing and restarting, so only add these callbacks if you think we are going to need them.

The next callback that we will introduce is the first packet callback.

Clone this wiki locally