source: lib/format_helper.c @ 0d57541

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

Add preliminary seek support

  • Property mode set to 100644
File size: 6.1 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#include <stdarg.h>
50
51struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
52        struct libtrace_eventobj_t event = {0,0,0.0,0};
53        int data;
54
55        assert(trace);
56        assert(packet);
57       
58        if (trace->format->get_fd) {
59                event.fd = trace->format->get_fd(trace);
60        } else {
61                event.fd = 0;
62        }
63        if (ioctl(event.fd,FIONREAD,&data)==-1) {
64                event.type = TRACE_EVENT_TERMINATE;
65                return event;
66        }
67        if (data>0) {
68                event.size = trace_read_packet(trace,packet);
69                event.type = TRACE_EVENT_PACKET;
70                return event;
71        }
72        event.type= TRACE_EVENT_IOWAIT;
73        return event;
74}
75
76struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
77        struct libtrace_eventobj_t event = {0,0,0.0,0};
78        double ts;
79        double now;
80        struct timeval stv;
81
82        if (!trace->event.packet) {
83                trace->event.packet = trace_create_packet();
84                trace->event.psize=
85                        trace_read_packet(trace,trace->event.packet);
86                if (trace->event.psize<1) {
87                        /* return here, the test for
88                         * event.size will sort out the error
89                         */
90                        event.type = TRACE_EVENT_TERMINATE;
91                        return event;
92                }
93        }
94
95        ts=trace_get_seconds(trace->event.packet);
96        if (trace->event.tdelta!=0) {
97                /* Get the adjusted current time */
98                gettimeofday(&stv, NULL);
99                now = stv.tv_sec + 
100                        ((double)stv.tv_usec / 1000000.0);
101                /* adjust for trace delta */
102                now -= trace->event.tdelta; 
103
104                /* if the trace timestamp is still in the
105                 * future, return a SLEEP event,
106                 * otherwise fire the packet
107                 */
108                if (ts > now) {
109                        event.seconds = ts - 
110                                trace->event.trace_last_ts;
111                        event.type = TRACE_EVENT_SLEEP;
112                        return event;
113                }
114        } else {
115                gettimeofday(&stv, NULL);
116                /* work out the difference between the
117                 * start of trace replay, and the first
118                 * packet in the trace
119                 */
120                trace->event.tdelta = stv.tv_sec + 
121                        ((double)stv.tv_usec / 1000000.0);
122                trace->event.tdelta -= ts;
123        }
124
125        /* This is the first packet, so just fire away. */
126        /* TODO: finalise packet */
127        *packet = *trace->event.packet;
128        trace->event.packet = NULL;
129
130        event.type = TRACE_EVENT_PACKET;
131
132        trace->event.trace_last_ts = ts;
133
134        return event;
135}
136
137/* Catch undefined O_LARGEFILE on *BSD etc */
138#ifndef O_LARGEFILE
139#  define O_LARGEFILE 0
140#endif
141
142/* open a file or stdin using gzip compression if necessary (and supported)
143 * @internal
144 */
145LIBTRACE_FILE trace_open_file(libtrace_t *trace)
146{
147        int fd;
148        LIBTRACE_FILE ret;
149
150
151        if (strcmp(trace->uridata,"-")==0) {
152                ret=LIBTRACE_FDOPEN(fileno(stdin),"r");
153                return ret;
154        }
155
156        /* We open the file with open(2), so we can provide O_LARGEFILE
157         * as zlib doesn't always do it itself
158         */
159        fd=open(trace->uridata,O_LARGEFILE);
160        if (fd==-1) {
161                trace_set_err(trace,errno,"Unable to open %s",trace->uridata);
162                return 0;
163        }
164        ret=LIBTRACE_FDOPEN(fd,"r");
165        return ret;
166}
167
168/* Create a file or write to stdout using compression if requested
169 * @internal
170 */
171LIBTRACE_FILE trace_open_file_out(libtrace_out_t *trace,int level, int fileflag)
172{
173        int fd;
174        LIBTRACE_FILE ret;
175        char filemode[4]; /* wb9\0 */
176        assert(level<10);
177        assert(level>=0);
178#if HAVE_ZLIB
179        sprintf(filemode,"wb%d",level);
180#else
181        sprintf(filemode,"w");
182#endif
183
184        if (strcmp(trace->uridata,"-")==0) {
185                ret=LIBTRACE_FDOPEN(fileno(stdout),filemode);
186                return ret;
187        }
188
189        /* We open the file with open(2), so we can provide O_LARGEFILE
190         * as zlib doesn't always do it itself
191         */
192        fd=open(trace->uridata,fileflag,0666);
193        if (fd==-1) {
194                trace_set_err_out(trace,
195                                errno,"Unable to open %s",trace->uridata);
196                return 0;
197        }
198        ret=LIBTRACE_FDOPEN(fd,filemode);
199        if (ret==NULL) {
200                printf("%s\n",filemode);
201                trace_set_err_out(trace,
202                                TRACE_ERR_INIT_FAILED,"gz out of memory");
203        }
204        return ret;
205}
206
207
208/** Update the libtrace error
209 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
210 * @param msg a plaintext error message
211 * @internal
212 */
213void trace_set_err(libtrace_t *trace,int errcode,const char *msg,...)
214{
215        char buf[256];
216        va_list va;
217        va_start(va,msg);
218        trace->err.err_num=errcode;
219        if (errcode>0) {
220                vsnprintf(buf,sizeof(buf),msg,va);
221                snprintf(trace->err.problem,sizeof(trace->err.problem),
222                                "%s: %s",buf,strerror(errno));
223        } else {
224                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
225                                msg,va);
226        }
227        va_end(va);
228}
229
230/** Update the libtrace for output traces error
231 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
232 * @param msg a plaintext error message
233 * @internal
234 */
235void trace_set_err_out(libtrace_out_t *trace,int errcode,const char *msg,...)
236{
237        char buf[256];
238        va_list va;
239        va_start(va,msg);
240        trace->err.err_num=errcode;
241        if (errcode>0) {
242                vsnprintf(buf,sizeof(buf),msg,va);
243                snprintf(trace->err.problem,sizeof(trace->err.problem),
244                                "%s: %s",buf,strerror(errno));
245        } else {
246                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
247                                msg,va);
248        }
249        va_end(va);
250}
251
Note: See TracBrowser for help on using the repository browser.