source: tools/tracereplay/tracereplay.c @ 550be94

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 550be94 was 550be94, checked in by Andreas Löf <andreas.lof@…>, 12 years ago

start of a trace replay tool.

The tool is supposed to play back a trace in trace time, using the libtrace events to manage the actual timings. The tool is based on the event and output examples.

This is the very start of the tool, and it currently segfaults whenever it is run.
The order of business is to:

  1. Make it work.]
  2. Refactor it so that work is done _before_ the next packet needs to be sent, unlike now. This will lead to less missed deadlines.
  3. Investigate if it is possible to use a resulotion wait function for greater accuracy.
  • Property mode set to 100644
File size: 3.4 KB
Line 
1
2
3
4/* This is a simple example program that demonstrates how to use the libtrace
5 * event framework. The event framework is ideal for reading from devices and
6 * interfaces in a non-blocking manner, and for reading from a trace in
7 * "tracetime" as opposed to as fast as possible.
8 */
9
10
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <assert.h>
15#include <sys/types.h>
16#include <unistd.h>
17#include <string.h>
18#include <libtrace.h>
19
20static libtrace_packet_t * per_packet(libtrace_packet_t *packet) {
21 
22  uint32_t remaining;
23  libtrace_linktype_t linktype;
24  void * pkt_buffer = trace_get_packet_buffer(packet,&linktype,&remaining);
25  libtrace_packet_t *new_packet;
26
27  size_t wire_length = trace_get_wire_length(packet);
28
29  trace_construct_packet(new_packet,linktype,pkt_buffer,wire_length);
30
31
32  return new_packet;
33 
34}
35
36static uint32_t event_read_packet(libtrace_t *trace, libtrace_packet_t *packet) 
37{
38        libtrace_eventobj_t obj;
39        fd_set rfds;
40        struct timeval sleep_tv;
41       
42        FD_ZERO(&rfds);
43       
44        for (;;) {
45                obj = trace_event(trace, packet);
46
47                switch(obj.type) {
48                       
49                        /* Device has no packets at present - lets wait until
50                         * it does get something */
51                        case TRACE_EVENT_IOWAIT:
52                                FD_ZERO(&rfds);
53                                FD_SET(obj.fd, &rfds);
54                                select(obj.fd + 1, &rfds, NULL, NULL, 0);
55                                continue;
56                               
57                        /* Replaying a trace in tracetime and the next packet
58                         * is not due yet */
59                        case TRACE_EVENT_SLEEP:
60                                /* select offers good precision for sleeping */
61                                sleep_tv.tv_sec = (int)obj.seconds;
62                                sleep_tv.tv_usec = (int) ((obj.seconds - sleep_tv.tv_sec) * 1000000.0);
63                                select(0, NULL, NULL, NULL, &sleep_tv);
64                                continue;
65                               
66                        /* We've got a packet! */
67                        case TRACE_EVENT_PACKET:
68                                /* Check for error first */
69                                if (obj.size == -1)
70                                        return -1;
71                                return 1;
72                               
73                        /* End of trace has been reached */
74                        case TRACE_EVENT_TERMINATE:
75                                return -1;
76                               
77                        /* An event we don't know about has occured */
78                        default:
79                                fprintf(stderr, "Unknown event type occured\n");
80                                return -1;
81                }
82        }
83}
84               
85int main(int argc, char *argv[]) {
86       
87        libtrace_t *trace;
88        libtrace_out_t *output;
89        libtrace_packet_t *packet;
90        int psize = 0;
91        char *uri = 0;
92       
93        if (argc == 3) {
94                uri = strdup(argv[1]);
95        } else {
96                fprintf(stderr,"usage: %s <input uri> <outputuri>\n",argv[0]);
97                return -1;
98        }
99
100        /* Create the trace */
101        trace = trace_create(uri);
102        if (trace_is_err(trace)) {
103                trace_perror(trace, "trace_create");
104                return 1;
105        }
106       
107        /* Starting the trace */
108        if (trace_start(trace) != 0) {
109                trace_perror(trace, "trace_start");
110                return 1;
111        }
112
113        /* Creating output trace */
114        output = trace_create_output(argv[2]);
115       
116        if (trace_is_err_output(output)) {
117                trace_perror_output(output, "Opening output trace");
118                return 1;
119        }
120        if (trace_start_output(output)) {
121                trace_perror_output(output, "Starting output trace");
122                trace_destroy_output(output);
123                trace_destroy(trace);
124                return 1;
125        }
126               
127
128        packet = trace_create_packet();
129
130        for (;;) {
131                if ((psize = event_read_packet(trace, packet)) <= 0) {
132                        break;
133                }
134
135                /* Got a packet - let's do something with it */
136                libtrace_packet_t * new = per_packet(packet);
137
138                if (trace_write_packet(output, new) < 0) {
139                        trace_perror_output(output, "Writing packet");
140                        trace_destroy(trace);
141                        trace_destroy_output(output);
142                        trace_destroy_packet(packet);
143                        return 1;
144                }
145                trace_destroy_packet(new);
146        }
147        free(uri);
148        trace_destroy(trace);
149        trace_destroy_output(output);
150        trace_destroy_packet(packet);
151        return 0;
152
153}
Note: See TracBrowser for help on using the repository browser.