source: examples/parallel/trivial_skeleton.c @ 624c2da

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 624c2da was ccb89e4, checked in by Shane Alcock <salcock@…>, 6 years ago

Update trivial and timedemo examples to use new parallel API

Also fixed a few documentation errors (spelling, grammar etc).

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/* Trivial parallel libtrace skeleton program
2 *
3 * This libtrace skeleton has the bare minimum required to write a useful
4 * libtrace program, including error handling.
5 *
6 *
7 */
8/* Note we include libtrace_parallel.h rather then libtrace.h */
9#include "libtrace_parallel.h"
10#include <stdio.h>
11#include <assert.h>
12
13
14/* Every time a packet becomes ready this function will be called. This
15 * function is run in parallel, so multiple packets can be processed at once.
16 *
17 * Parameters:
18 *   trace -- the input source that the packet was read from
19 *   t -- a pointer to the current processing thread
20 *   global -- a pointer to the global variable passed in to trace_start
21 *   tls -- a pointer to the thread local storage for this thread
22 *   packet -- the packet itself
23 */
24static libtrace_packet_t *process_packet(libtrace_t *trace,
25                libtrace_thread_t *t,
26                void *global, void *tls, libtrace_packet_t *packet) {
27
28        /* Note that in this example, global and tls will both be NULL.
29         * global is NULL because we passed NULL as the second argument
30         * for trace_pstart. tls is NULL because we did not set a
31         * starting callback for our per packet threads.
32         */
33
34
35        assert(packet);
36
37        /* Your code goes here */
38
39        /* If we've finished with the packet, we should return it to
40         * libtrace so that it can be reused. */
41        return packet;
42}
43
44int main(int argc, char *argv[])
45{
46        libtrace_t *trace;
47        libtrace_callback_set_t *pktcbs;
48
49        if (argc<2) {
50                fprintf(stderr,"usage: %s libtraceuri\n",argv[0]);
51                return 1;
52        }
53
54        trace = trace_create(argv[1]);
55
56        if (trace_is_err(trace)) {
57                trace_perror(trace,"Opening trace file");
58                return 1;
59        }
60
61        /* Create a callback set for our per packet threads */
62        pktcbs = trace_create_callback_set();
63
64        /* Set the packet callback to be our packet processing function */
65        trace_set_packet_cb(pktcbs, process_packet);
66
67        /* We use a new version of trace_start(), trace_pstart()
68         * The reporter function argument is optional and can be NULL.
69         * We've also set the second argument to NULL because we have no
70         * global data that we want to be available to all threads. */
71        if (trace_pstart(trace, NULL, pktcbs, NULL)) {
72                trace_perror(trace,"Starting trace");
73                trace_destroy(trace);
74                return 1;
75        }
76
77        /* Wait for the trace to finish */
78        trace_join(trace);
79
80        if (trace_is_err(trace)) {
81                trace_perror(trace,"Reading packets");
82                trace_destroy(trace);
83                return 1;
84        }
85
86        trace_destroy(trace);
87        trace_destroy_callback_set(pktcbs);
88
89        return 0;
90}
Note: See TracBrowser for help on using the repository browser.