source: libwandio/wandio.c @ 91ea76c

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 91ea76c was 91ea76c, checked in by Shane Alcock <salcock@…>, 9 years ago
  • Detect if a user is attempting to read a compressed format that their libtrace build does not support and provide an appropriate error. Previously, we'd just try to read the trace with a stdout reader, resulting in some very confusing results.
  • 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>=2 && buffer[0] == '\037' && buffer[1] == '\213') { 
145#if HAVE_LIBZ
146                DEBUG_PIPELINE("zlib");
147                io = zlib_open(io);
148#else
149                fprintf(stderr, "File %s is gzip compressed but libtrace has not been built with zlib support!\n", filename);
150                return NULL;
151#endif
152        }
153        /* Auto detect compress(1) compressed data (gzip can read this) */
154        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') {
155#if HAVE_LIBZ
156                DEBUG_PIPELINE("zlib");
157                io = zlib_open(io);
158#else
159                fprintf(stderr, "File %s is compress(1) compressed but libtrace has not been built with zlib support!\n", filename);
160                return NULL;
161#endif
162        }
163
164        /* Auto detect bzip compressed data */
165        if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { 
166#if HAVE_LIBBZ2
167                DEBUG_PIPELINE("bzip");
168                io = bz_open(io);
169#else
170                fprintf(stderr, "File %s is bzip compressed but libtrace has not been built with bzip2 support!\n", filename);
171                return NULL;
172#endif
173        }
174       
175        /* Now open a threaded, peekable reader using the appropriate module
176         * to read the data */
177
178        if (use_threads) {
179                DEBUG_PIPELINE("thread");
180                io = thread_open(io);
181        }
182       
183        DEBUG_PIPELINE("peek");
184        return peek_open(io);
185}
186
187DLLEXPORT off_t wandio_tell(io_t *io)
188{
189        if (!io->source->tell) {
190                errno = -ENOSYS;
191                return -1;
192        }
193        return io->source->tell(io);
194}
195
196DLLEXPORT off_t wandio_seek(io_t *io, off_t offset, int whence)
197{
198        if (!io->source->seek) {
199                errno = -ENOSYS;
200                return -1;
201        }
202        return io->source->seek(io,offset,whence);
203}
204
205DLLEXPORT off_t wandio_read(io_t *io, void *buffer, off_t len)
206{ 
207        off_t ret;
208        ret=io->source->read(io,buffer,len); 
209#if READ_TRACE
210        fprintf(stderr,"%p: read(%s): %d bytes = %d\n",io,io->source->name, (int)len,(int)ret);
211#endif
212        return ret;
213}
214
215DLLEXPORT off_t wandio_peek(io_t *io, void *buffer, off_t len)
216{
217        off_t ret;
218        assert(io->source->peek); /* If this fails, it means you're calling
219                                   * peek on something that doesn't support
220                                   * peeking.   Push a peek_open() on the io
221                                   * first.
222                                   */
223        ret=io->source->peek(io, buffer, len);
224#if READ_TRACE
225        fprintf(stderr,"%p: peek(%s): %d bytes = %d\n",io,io->source->name, (int)len, (int)ret);
226#endif
227        return ret;
228}
229
230DLLEXPORT void wandio_destroy(io_t *io)
231{ 
232        if (!io)
233                return;
234       
235        if (keep_stats) 
236                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits);
237        io->source->close(io); 
238}
239
240DLLEXPORT iow_t *wandio_wcreate(const char *filename, int compress_type, int compression_level, int flags)
241{
242        iow_t *iow;
243        parse_env();
244
245        assert ( compression_level >= 0 && compression_level <= 9 );
246        assert (compress_type != WANDIO_COMPRESS_MASK);
247
248        iow=stdio_wopen(filename, flags);
249        if (!iow)
250                return NULL;
251
252        /* We prefer zlib if available, otherwise we'll use bzip. If neither
253         * are present, guess we'll just have to write uncompressed */
254#if HAVE_LIBZ
255        if (compression_level != 0 && 
256            compress_type == WANDIO_COMPRESS_ZLIB) {
257                iow = zlib_wopen(iow,compression_level);
258        }
259#endif
260#if HAVE_LIBLZO2
261        else if (compression_level != 0 && 
262            compress_type == WANDIO_COMPRESS_LZO) {
263                iow = lzo_wopen(iow,compression_level);
264        }
265#endif
266#if HAVE_LIBBZ2
267        else if (compression_level != 0 && 
268            compress_type == WANDIO_COMPRESS_BZ2) {
269                iow = bz_wopen(iow,compression_level);
270        }
271#endif
272        /* Open a threaded writer */
273        if (use_threads)
274                return thread_wopen(iow);
275        else
276                return iow;
277}
278
279DLLEXPORT off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len)
280{
281#if WRITE_TRACE
282        fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len);
283#endif
284        return iow->source->write(iow,buffer,len);     
285}
286
287DLLEXPORT void wandio_wdestroy(iow_t *iow)
288{
289        iow->source->close(iow);
290        if (keep_stats) 
291                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits);
292}
293
Note: See TracBrowser for help on using the repository browser.