source: lib/format_helper.c @ 880aa58

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 880aa58 was 880aa58, checked in by Perry Lorier <perry@…>, 16 years ago

Added libtrace_set_err() to everything (except format_rt, that's shane's job)

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id$
28 *
29 */
30
31#include "libtrace.h"
32#include "libtrace_int.h"
33#include "config.h"
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#ifdef HAVE_INTTYPES_H
39#  include <inttypes.h>
40#else
41#  error "Can't find inttypes.h - this needs to be fixed"
42#endif
43#include  "format_helper.h"
44
45#include <sys/ioctl.h>
46
47struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
48        struct libtrace_eventobj_t event = {0,0,0.0,0};
49        int data;
50
51        if (packet->trace->format->get_fd) {
52                event.fd = packet->trace->format->get_fd(packet);
53        } else {
54                event.fd = 0;
55        }
56        if (ioctl(event.fd,FIONREAD,&data)==-1) {
57                event.type = TRACE_EVENT_TERMINATE;
58                return event;
59        }
60        if (data>0) {
61                event.size = trace_read_packet(trace,packet);
62                event.type = TRACE_EVENT_PACKET;
63                return event;
64        }
65        event.type= TRACE_EVENT_IOWAIT;
66        return event;
67}
68
69struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
70        struct libtrace_eventobj_t event = {0,0,0.0,0};
71        double ts;
72        double now;
73        struct timeval stv;
74
75        if (!trace->event.packet.buffer) {
76                trace->event.packet.buffer = (void *)malloc(4096);
77                trace->event.packet.size=
78                        trace_read_packet(trace,packet);
79                event.size = trace->event.packet.size;
80                if (trace->event.packet.size > 0 ) {
81                        memcpy(trace->event.packet.buffer,
82                                        packet->buffer,
83                                        trace->event.packet.size);
84                } else {
85                        /* return here, the test for
86                         * event.size will sort out the error
87                         */
88                        event.type = TRACE_EVENT_TERMINATE;
89                        return event;
90                }
91        }
92
93        ts=trace_get_seconds(packet);
94        if (trace->event.tdelta!=0) {
95                /* Get the adjusted current time */
96                gettimeofday(&stv, NULL);
97                now = stv.tv_sec + 
98                        ((double)stv.tv_usec / 1000000.0);
99                /* adjust for trace delta */
100                now -= trace->event.tdelta; 
101
102                /*if the trace timestamp is still in the
103                //future, return a SLEEP event,
104                //otherwise fire the packet
105                 */
106                if (ts > now) {
107                        event.seconds = ts - 
108                                trace->event.trace_last_ts;
109                        event.type = TRACE_EVENT_SLEEP;
110                        return event;
111                }
112        } else {
113                gettimeofday(&stv, NULL);
114                /* work out the difference between the
115                // start of trace replay, and the first
116                // packet in the trace
117                 */
118                trace->event.tdelta = stv.tv_sec + 
119                        ((double)stv.tv_usec / 1000000.0);
120                trace->event.tdelta -= ts;
121        }
122
123        /* This is the first packet, so just fire away. */
124        packet->size = trace->event.packet.size;
125        memcpy(packet->buffer,
126                        trace->event.packet.buffer,
127                        trace->event.packet.size);
128        free(trace->event.packet.buffer);
129        trace->event.packet.buffer = 0;
130        event.type = TRACE_EVENT_PACKET;
131
132        trace->event.trace_last_ts = ts;
133
134        return event;
135       
136}
Note: See TracBrowser for help on using the repository browser.