source: lib/wandio.c @ c3f597e

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

Deal better with errors when reading files

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34
35#include "wandio.h"
36#include "config.h"
37#include <stdlib.h>
38#include <assert.h>
39#include <errno.h>
40#include <inttypes.h>
41#include <string.h>
42
43/* This file contains the implementation of the libtrace IO API, which format
44 * modules should use to open, read from, write to, seek and close trace files.
45 */
46
47struct compression_type compression_type[]  = {
48        { "GZ",         "gz",   WANDIO_COMPRESS_ZLIB    },
49        { "BZ2",        "bz2",  WANDIO_COMPRESS_BZ2     },
50        { "LZO",        "lzo",  WANDIO_COMPRESS_LZO     },
51        { "NONE",       "",     WANDIO_COMPRESS_NONE    }
52};
53
54int keep_stats = 0;
55int force_directio_write = 0;
56int force_directio_read = 0;
57unsigned int use_threads = -1;
58
59uint64_t read_waits = 0;
60uint64_t write_waits = 0;
61
62/** Parse an option.
63 * stats -- Show summary stats
64 * directwrite -- bypass the diskcache on write
65 * directread -- bypass the diskcache on read
66 * nothreads -- Don't use threads
67 * threads=n -- Use a maximum of 'n' threads for thread farms
68 */
69static void do_option(const char *option)
70{
71        if (*option == '\0') 
72                ;
73        else if (strcmp(option,"stats") == 0)
74                keep_stats = 1;
75        else if (strcmp(option,"directwrite") == 0)
76                force_directio_write = 1;
77        else if (strcmp(option,"directread") == 0)
78                force_directio_read  = 1;
79        else if (strcmp(option,"nothreads") == 0)
80                use_threads = 0;
81        else if (strncmp(option,"threads=",8) == 0)
82                use_threads = atoi(option+8);
83        else {
84                fprintf(stderr,"Unknown libtraceio debug option '%s'\n", option);
85        }
86}
87
88static void parse_env(void)
89{
90        const char *str = getenv("LIBTRACEIO");
91        char option[1024];
92        const char *ip;
93        char *op;
94
95        if (!str)
96                return;
97
98        for(ip=str, op=option; *ip!='\0' && op < option+sizeof(option); ++ip) {
99                if (*ip == ',') {
100                        *op='\0';
101                        do_option(option);
102                        op=option;
103                }
104                else
105                        *(op++) = *ip;
106        }
107        *op='\0';
108        do_option(option);
109}
110
111
112#define READ_TRACE 0
113#define WRITE_TRACE 0
114#define PIPELINE_TRACE 0
115
116#if PIPELINE_TRACE
117#define DEBUG_PIPELINE(x) fprintf(stderr,"PIPELINE: %s\n",x)
118#else
119#define DEBUG_PIPELINE(x)
120#endif
121
122io_t *wandio_create(const char *filename)
123{
124        parse_env();
125
126        /* Use a peeking reader to look at the start of the trace file and
127         * determine what type of compression may have been used to write
128         * the file */
129
130        DEBUG_PIPELINE("stdio");
131        DEBUG_PIPELINE("peek");
132        io_t *io = peek_open(stdio_open(filename));
133        char buffer[1024];
134        int len;
135        if (!io)
136                return NULL;
137        len = wandio_peek(io, buffer, sizeof(buffer));
138#if HAVE_LIBZ
139        /* Auto detect gzip compressed data */
140        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { 
141                DEBUG_PIPELINE("zlib");
142                io = zlib_open(io);
143        }
144        /* Auto detect compress(1) compressed data (gzip can read this) */
145        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') {
146                DEBUG_PIPELINE("zlib");
147                io = zlib_open(io);
148        }
149#endif
150#if HAVE_LIBBZ2
151        /* Auto detect bzip compressed data */
152        if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { 
153                DEBUG_PIPELINE("bzip");
154                io = bz_open(io);
155        }
156#endif
157       
158        /* Now open a threaded, peekable reader using the appropriate module
159         * to read the data */
160
161        if (use_threads) {
162                DEBUG_PIPELINE("thread");
163                io = thread_open(io);
164        }
165       
166        DEBUG_PIPELINE("peek");
167        return peek_open(io);
168}
169
170off_t wandio_tell(io_t *io)
171{
172        if (!io->source->tell) {
173                errno = -ENOSYS;
174                return -1;
175        }
176        return io->source->tell(io);
177}
178
179off_t wandio_seek(io_t *io, off_t offset, int whence)
180{
181        if (!io->source->seek) {
182                errno = -ENOSYS;
183                return -1;
184        }
185        return io->source->seek(io,offset,whence);
186}
187
188off_t wandio_read(io_t *io, void *buffer, off_t len)
189{ 
190        off_t ret;
191        ret=io->source->read(io,buffer,len); 
192#if READ_TRACE
193        fprintf(stderr,"%p: read(%s): %d bytes = %d\n",io,io->source->name, (int)len,(int)ret);
194#endif
195        return ret;
196}
197
198off_t wandio_peek(io_t *io, void *buffer, off_t len)
199{
200        off_t ret;
201        assert(io->source->peek); /* If this fails, it means you're calling
202                                   * peek on something that doesn't support
203                                   * peeking.   Push a peek_open() on the io
204                                   * first.
205                                   */
206        ret=io->source->peek(io, buffer, len);
207#if READ_TRACE
208        fprintf(stderr,"%p: peek(%s): %d bytes = %d\n",io,io->source->name, (int)len, (int)ret);
209#endif
210        return ret;
211}
212
213void wandio_destroy(io_t *io)
214{ 
215        if (keep_stats) 
216                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits);
217        io->source->close(io); 
218}
219
220iow_t *wandio_wcreate(const char *filename, int compress_type, int compression_level, int flags)
221{
222        iow_t *iow;
223        parse_env();
224
225        assert ( compression_level >= 0 && compression_level <= 9 );
226
227        iow=stdio_wopen(filename, flags);
228        if (!iow)
229                return NULL;
230
231        /* We prefer zlib if available, otherwise we'll use bzip. If neither
232         * are present, guess we'll just have to write uncompressed */
233#if HAVE_LIBZ
234        if (compression_level != 0 && 
235            compress_type == WANDIO_COMPRESS_ZLIB) {
236                iow = zlib_wopen(iow,compression_level);
237        }
238#endif
239#if HAVE_LIBLZO2
240        else if (compression_level != 0 && 
241            compress_type == WANDIO_COMPRESS_LZO) {
242                iow = lzo_wopen(iow,compression_level);
243        }
244#endif
245#if HAVE_LIBBZ2
246        else if (compression_level != 0 && 
247            compress_type == WANDIO_COMPRESS_BZ2) {
248                iow = bz_wopen(iow,compression_level);
249        }
250#endif
251        /* Open a threaded writer */
252        if (use_threads)
253                return thread_wopen(iow);
254        else
255                return iow;
256}
257
258off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len)
259{
260#if WRITE_TRACE
261        fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len);
262#endif
263        return iow->source->write(iow,buffer,len);     
264}
265
266void wandio_wdestroy(iow_t *iow)
267{
268        iow->source->close(iow);
269        if (keep_stats) 
270                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits);
271}
272
Note: See TracBrowser for help on using the repository browser.