source: libwandio/wandio.c @ 460855d

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 460855d was 460855d, checked in by Shane Alcock <salcock@…>, 8 years ago
  • Add an extra byte to the gzip autodetection to decrease the likelihood of genuine data, e.g. the timestamp in the ERF header from an uncompressed trace, accidentally meeting this check
  • Property mode set to 100644
File size: 7.2 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 "config.h"
36#include "wandio.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;
58unsigned int max_buffers = 50;
59
60uint64_t read_waits = 0;
61uint64_t write_waits = 0;
62
63/** Parse an option.
64 * stats -- Show summary stats
65 * directwrite -- bypass the diskcache on write
66 * directread -- bypass the diskcache on read
67 * nothreads -- Don't use threads
68 * threads=n -- Use a maximum of 'n' threads for thread farms
69 */
70static void do_option(const char *option)
71{
72        if (*option == '\0') 
73                ;
74        else if (strcmp(option,"stats") == 0)
75                keep_stats = 1;
76        /*
77        else if (strcmp(option,"directwrite") == 0)
78                force_directio_write = 1;
79        else if (strcmp(option,"directread") == 0)
80                force_directio_read  = 1;
81        */
82        else if (strcmp(option,"nothreads") == 0)
83                use_threads = 0;
84        else if (strncmp(option,"threads=",8) == 0)
85                use_threads = atoi(option+8);
86        else if (strncmp(option,"buffers=",8) == 0)
87                max_buffers = atoi(option+8);
88        else {
89                fprintf(stderr,"Unknown libtraceio debug option '%s'\n", option);
90        }
91}
92
93static void parse_env(void)
94{
95        const char *str = getenv("LIBTRACEIO");
96        char option[1024];
97        const char *ip;
98        char *op;
99
100        if (!str)
101                return;
102
103        for(ip=str, op=option; *ip!='\0' && op < option+sizeof(option); ++ip) {
104                if (*ip == ',') {
105                        *op='\0';
106                        do_option(option);
107                        op=option;
108                }
109                else
110                        *(op++) = *ip;
111        }
112        *op='\0';
113        do_option(option);
114}
115
116
117#define READ_TRACE 0
118#define WRITE_TRACE 0
119#define PIPELINE_TRACE 0
120
121#if PIPELINE_TRACE
122#define DEBUG_PIPELINE(x) fprintf(stderr,"PIPELINE: %s\n",x)
123#else
124#define DEBUG_PIPELINE(x)
125#endif
126
127DLLEXPORT io_t *wandio_create(const char *filename)
128{
129        parse_env();
130
131        /* Use a peeking reader to look at the start of the trace file and
132         * determine what type of compression may have been used to write
133         * the file */
134
135        DEBUG_PIPELINE("stdio");
136        DEBUG_PIPELINE("peek");
137        io_t *io = peek_open(stdio_open(filename));
138        char buffer[1024];
139        int len;
140        if (!io)
141                return NULL;
142        len = wandio_peek(io, buffer, sizeof(buffer));
143        /* Auto detect gzip compressed data */
144        if (len>=3 && buffer[0] == '\037' && buffer[1] == '\213' &&
145                        buffer[2] == 0x08) { 
146#if HAVE_LIBZ
147                DEBUG_PIPELINE("zlib");
148                io = zlib_open(io);
149#else
150                fprintf(stderr, "File %s is gzip compressed but libtrace has not been built with zlib support!\n", filename);
151                return NULL;
152#endif
153        }
154        /* Auto detect compress(1) compressed data (gzip can read this) */
155        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') {
156#if HAVE_LIBZ
157                DEBUG_PIPELINE("zlib");
158                io = zlib_open(io);
159#else
160                fprintf(stderr, "File %s is compress(1) compressed but libtrace has not been built with zlib support!\n", filename);
161                return NULL;
162#endif
163        }
164
165        /* Auto detect bzip compressed data */
166        if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { 
167#if HAVE_LIBBZ2
168                DEBUG_PIPELINE("bzip");
169                io = bz_open(io);
170#else
171                fprintf(stderr, "File %s is bzip compressed but libtrace has not been built with bzip2 support!\n", filename);
172                return NULL;
173#endif
174        }
175       
176        /* Now open a threaded, peekable reader using the appropriate module
177         * to read the data */
178
179        if (use_threads) {
180                DEBUG_PIPELINE("thread");
181                io = thread_open(io);
182        }
183       
184        DEBUG_PIPELINE("peek");
185        return peek_open(io);
186}
187
188DLLEXPORT off_t wandio_tell(io_t *io)
189{
190        if (!io->source->tell) {
191                errno = -ENOSYS;
192                return -1;
193        }
194        return io->source->tell(io);
195}
196
197DLLEXPORT off_t wandio_seek(io_t *io, off_t offset, int whence)
198{
199        if (!io->source->seek) {
200                errno = -ENOSYS;
201                return -1;
202        }
203        return io->source->seek(io,offset,whence);
204}
205
206DLLEXPORT off_t wandio_read(io_t *io, void *buffer, off_t len)
207{ 
208        off_t ret;
209        ret=io->source->read(io,buffer,len); 
210#if READ_TRACE
211        fprintf(stderr,"%p: read(%s): %d bytes = %d\n",io,io->source->name, (int)len,(int)ret);
212#endif
213        return ret;
214}
215
216DLLEXPORT off_t wandio_peek(io_t *io, void *buffer, off_t len)
217{
218        off_t ret;
219        assert(io->source->peek); /* If this fails, it means you're calling
220                                   * peek on something that doesn't support
221                                   * peeking.   Push a peek_open() on the io
222                                   * first.
223                                   */
224        ret=io->source->peek(io, buffer, len);
225#if READ_TRACE
226        fprintf(stderr,"%p: peek(%s): %d bytes = %d\n",io,io->source->name, (int)len, (int)ret);
227#endif
228        return ret;
229}
230
231DLLEXPORT void wandio_destroy(io_t *io)
232{ 
233        if (!io)
234                return;
235       
236        if (keep_stats) 
237                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits);
238        io->source->close(io); 
239}
240
241DLLEXPORT iow_t *wandio_wcreate(const char *filename, int compress_type, int compression_level, int flags)
242{
243        iow_t *iow;
244        parse_env();
245
246        assert ( compression_level >= 0 && compression_level <= 9 );
247        assert (compress_type != WANDIO_COMPRESS_MASK);
248
249        iow=stdio_wopen(filename, flags);
250        if (!iow)
251                return NULL;
252
253        /* We prefer zlib if available, otherwise we'll use bzip. If neither
254         * are present, guess we'll just have to write uncompressed */
255#if HAVE_LIBZ
256        if (compression_level != 0 && 
257            compress_type == WANDIO_COMPRESS_ZLIB) {
258                iow = zlib_wopen(iow,compression_level);
259        }
260#endif
261#if HAVE_LIBLZO2
262        else if (compression_level != 0 && 
263            compress_type == WANDIO_COMPRESS_LZO) {
264                iow = lzo_wopen(iow,compression_level);
265        }
266#endif
267#if HAVE_LIBBZ2
268        else if (compression_level != 0 && 
269            compress_type == WANDIO_COMPRESS_BZ2) {
270                iow = bz_wopen(iow,compression_level);
271        }
272#endif
273        /* Open a threaded writer */
274        if (use_threads)
275                return thread_wopen(iow);
276        else
277                return iow;
278}
279
280DLLEXPORT off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len)
281{
282#if WRITE_TRACE
283        fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len);
284#endif
285        return iow->source->write(iow,buffer,len);     
286}
287
288DLLEXPORT void wandio_wdestroy(iow_t *iow)
289{
290        iow->source->close(iow);
291        if (keep_stats) 
292                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits);
293}
294
Note: See TracBrowser for help on using the repository browser.