source: lib/format_helper.c @ 5b91b48

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 5b91b48 was 0a6638f, checked in by Perry Lorier <perry@…>, 15 years ago

Fixed up erf output
Force trace_create_packet to be called, don't let people cheat.

  • Property mode set to 100644
File size: 5.0 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#include <errno.h>
39#include <fcntl.h> /* for O_LARGEFILE */
40#ifdef HAVE_INTTYPES_H
41#  include <inttypes.h>
42#else
43#  error "Can't find inttypes.h - this needs to be fixed"
44#endif
45#include  "format_helper.h"
46
47#include <sys/ioctl.h>
48#include <assert.h>
49
50struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
51        struct libtrace_eventobj_t event = {0,0,0.0,0};
52        int data;
53
54        if (packet->trace->format->get_fd) {
55                event.fd = packet->trace->format->get_fd(packet);
56        } else {
57                event.fd = 0;
58        }
59        if (ioctl(event.fd,FIONREAD,&data)==-1) {
60                event.type = TRACE_EVENT_TERMINATE;
61                return event;
62        }
63        if (data>0) {
64                event.size = trace_read_packet(trace,packet);
65                event.type = TRACE_EVENT_PACKET;
66                return event;
67        }
68        event.type= TRACE_EVENT_IOWAIT;
69        return event;
70}
71
72struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
73        struct libtrace_eventobj_t event = {0,0,0.0,0};
74        double ts;
75        double now;
76        struct timeval stv;
77
78        if (!trace->event.packet.buffer) {
79                trace->event.packet.buffer = (void *)malloc(4096);
80                trace->event.packet.size=
81                        trace_read_packet(trace,packet);
82                event.size = trace->event.packet.size;
83                if (trace->event.packet.size > 0 ) {
84                        memcpy(trace->event.packet.buffer,
85                                        packet->buffer,
86                                        trace->event.packet.size);
87                } else {
88                        /* return here, the test for
89                         * event.size will sort out the error
90                         */
91                        event.type = TRACE_EVENT_TERMINATE;
92                        return event;
93                }
94        }
95
96        ts=trace_get_seconds(packet);
97        if (trace->event.tdelta!=0) {
98                /* Get the adjusted current time */
99                gettimeofday(&stv, NULL);
100                now = stv.tv_sec + 
101                        ((double)stv.tv_usec / 1000000.0);
102                /* adjust for trace delta */
103                now -= trace->event.tdelta; 
104
105                /*if the trace timestamp is still in the
106                //future, return a SLEEP event,
107                //otherwise fire the packet
108                 */
109                if (ts > now) {
110                        event.seconds = ts - 
111                                trace->event.trace_last_ts;
112                        event.type = TRACE_EVENT_SLEEP;
113                        return event;
114                }
115        } else {
116                gettimeofday(&stv, NULL);
117                /* work out the difference between the
118                // start of trace replay, and the first
119                // packet in the trace
120                 */
121                trace->event.tdelta = stv.tv_sec + 
122                        ((double)stv.tv_usec / 1000000.0);
123                trace->event.tdelta -= ts;
124        }
125
126        /* This is the first packet, so just fire away. */
127        packet->size = trace->event.packet.size;
128        memcpy(packet->buffer,
129                        trace->event.packet.buffer,
130                        trace->event.packet.size);
131        free(trace->event.packet.buffer);
132        trace->event.packet.buffer = 0;
133        event.type = TRACE_EVENT_PACKET;
134
135        trace->event.trace_last_ts = ts;
136
137        return event;
138       
139}
140
141/* Catch undefined O_LARGEFILE on *BSD etc */
142#ifndef O_LARGEFILE
143#  define O_LARGEFILE 0
144#endif
145
146LIBTRACE_FILE trace_open_file(libtrace_t *trace)
147{
148        int fd;
149        LIBTRACE_FILE ret;
150
151
152        if (strcmp(trace->uridata,"-")==0) {
153                ret=LIBTRACE_FDOPEN(fileno(stdin),"r");
154                return ret;
155        }
156
157        /* We open the file with open(2), so we can provide O_LARGEFILE
158         * as zlib doesn't always do it itself
159         */
160        fd=open(trace->uridata,O_LARGEFILE);
161        if (fd==-1) {
162                trace_set_err(errno,"Unable to open %s",trace->uridata);
163                return 0;
164        }
165        ret=LIBTRACE_FDOPEN(fd,"r");
166        return ret;
167}
168
169LIBTRACE_FILE trace_open_file_out(libtrace_out_t *trace,int level, int fileflag)
170{
171        int fd;
172        LIBTRACE_FILE ret;
173        char filemode[4]; /* wb9\0 */
174        assert(level<10);
175        assert(level>=0);
176#if HAVE_ZLIB
177        sprintf(filemode,"wb%d",level);
178#else
179        sprintf(filemode,"w");
180#endif
181
182        if (strcmp(trace->uridata,"-")==0) {
183                ret=LIBTRACE_FDOPEN(fileno(stdout),filemode);
184                return ret;
185        }
186
187        /* We open the file with open(2), so we can provide O_LARGEFILE
188         * as zlib doesn't always do it itself
189         */
190        fd=open(trace->uridata,fileflag,0666);
191        if (fd==-1) {
192                trace_set_err(errno,"Unable to open %s",trace->uridata);
193                return 0;
194        }
195        ret=LIBTRACE_FDOPEN(fd,filemode);
196        if (ret==NULL) {
197                printf("%s\n",filemode);
198                trace_set_err(TRACE_ERR_INIT_FAILED,"gz out of memory");
199        }
200        return ret;
201}
202
203
204
Note: See TracBrowser for help on using the repository browser.