source: examples/parallel/trivial_skeleton.c @ 9346e4a

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 9346e4a was 16cb2a2, checked in by Richard Sanger <rsangerarj@…>, 7 years ago

Add some example programs.

trivial_skeleton.c
timedemo.c - Shows how to deal with tracetime results (say printing statistics every X seconds) using TICK_INTERVAL

  • Property mode set to 100644
File size: 1.7 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
13static void process_packet(libtrace_packet_t *packet)
14{
15        /* You really should consider using complete_parallel.c instead */
16        assert(packet);
17
18        /* Your code goes here */
19
20}
21
22/* Every time a packet becomes ready this function will be called. It will also
23 * be called when messages from the library are received. This function
24 * is run in parallel.
25 */
26static void* per_packet(libtrace_t *trace UNUSED, libtrace_thread_t *t UNUSED,
27                        int mesg, libtrace_generic_t data,
28                        libtrace_thread_t *sender UNUSED)
29{
30
31        switch (mesg) {
32        case MESSAGE_PACKET:
33                process_packet(data.pkt);
34                /* If we have finished processing this packet return it */
35                return data.pkt;
36        default:
37                return NULL;
38        }
39        return NULL;
40}
41
42
43int main(int argc, char *argv[])
44{
45        libtrace_t *trace;
46
47        if (argc<2) {
48                fprintf(stderr,"usage: %s libtraceuri\n",argv[0]);
49                return 1;
50        }
51
52        trace = trace_create(argv[1]);
53
54        if (trace_is_err(trace)) {
55                trace_perror(trace,"Opening trace file");
56                return 1;
57        }
58
59        /* We use a new version of trace_start(), trace_pstart()
60         * The reporter function argument is optional and can be NULL */
61        if (trace_pstart(trace, NULL, per_packet, NULL)) {
62                trace_perror(trace,"Starting trace");
63                trace_destroy(trace);
64                return 1;
65        }
66
67        /* Wait for the trace to finish */
68        trace_join(trace);
69
70        if (trace_is_err(trace)) {
71                trace_perror(trace,"Reading packets");
72                trace_destroy(trace);
73                return 1;
74        }
75
76        trace_destroy(trace);
77
78        return 0;
79}
Note: See TracBrowser for help on using the repository browser.