source: libwandio/wandio.c @ 954577b9

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 954577b9 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
RevLine 
[eda2def]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
[c66068d]35#include "config.h"
[ef07202]36#include "wandio.h"
[c66068d]37#include <stdlib.h>
38#include <assert.h>
39#include <errno.h>
[29d4438]40#include <inttypes.h>
41#include <string.h>
[c66068d]42
[eda2def]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
[c66068d]47struct compression_type compression_type[]  = {
[15e9390]48        { "GZ",         "gz",   WANDIO_COMPRESS_ZLIB    },
49        { "BZ2",        "bz2",  WANDIO_COMPRESS_BZ2     },
[aa0c5c5]50        { "LZO",        "lzo",  WANDIO_COMPRESS_LZO     },
[15e9390]51        { "NONE",       "",     WANDIO_COMPRESS_NONE    }
[c66068d]52};
53
[29d4438]54int keep_stats = 0;
55int force_directio_write = 0;
56int force_directio_read = 0;
[6f8745b]57unsigned int use_threads = -1;
[f258932]58unsigned int max_buffers = 50;
[29d4438]59
60uint64_t read_waits = 0;
61uint64_t write_waits = 0;
62
[6f8745b]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 */
[29d4438]70static void do_option(const char *option)
71{
72        if (*option == '\0') 
73                ;
74        else if (strcmp(option,"stats") == 0)
75                keep_stats = 1;
[a8d184f]76        /*
[29d4438]77        else if (strcmp(option,"directwrite") == 0)
78                force_directio_write = 1;
79        else if (strcmp(option,"directread") == 0)
80                force_directio_read  = 1;
[a8d184f]81        */
[29d4438]82        else if (strcmp(option,"nothreads") == 0)
83                use_threads = 0;
[6f8745b]84        else if (strncmp(option,"threads=",8) == 0)
85                use_threads = atoi(option+8);
[f258932]86        else if (strncmp(option,"buffers=",8) == 0)
87                max_buffers = atoi(option+8);
[29d4438]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
[c66068d]117#define READ_TRACE 0
118#define WRITE_TRACE 0
[baef9df]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
[c66068d]126
[bd6576d]127DLLEXPORT io_t *wandio_create(const char *filename)
[c66068d]128{
[29d4438]129        parse_env();
130
[eda2def]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 */
[aa0c5c5]134
[baef9df]135        DEBUG_PIPELINE("stdio");
136        DEBUG_PIPELINE("peek");
[c66068d]137        io_t *io = peek_open(stdio_open(filename));
138        char buffer[1024];
[cf30639]139        int len;
[c66068d]140        if (!io)
141                return NULL;
[cf30639]142        len = wandio_peek(io, buffer, sizeof(buffer));
[eda2def]143        /* Auto detect gzip compressed data */
[cf30639]144        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { 
[91ea76c]145#if HAVE_LIBZ
[baef9df]146                DEBUG_PIPELINE("zlib");
[c66068d]147                io = zlib_open(io);
[91ea76c]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
[c66068d]152        }
[eda2def]153        /* Auto detect compress(1) compressed data (gzip can read this) */
[cf30639]154        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') {
[91ea76c]155#if HAVE_LIBZ
[baef9df]156                DEBUG_PIPELINE("zlib");
[c66068d]157                io = zlib_open(io);
[91ea76c]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;
[c66068d]161#endif
[91ea76c]162        }
163
[eda2def]164        /* Auto detect bzip compressed data */
[cf30639]165        if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { 
[91ea76c]166#if HAVE_LIBBZ2
[baef9df]167                DEBUG_PIPELINE("bzip");
[c66068d]168                io = bz_open(io);
[91ea76c]169#else
170                fprintf(stderr, "File %s is bzip compressed but libtrace has not been built with bzip2 support!\n", filename);
171                return NULL;
[c66068d]172#endif
[91ea76c]173        }
[eda2def]174       
175        /* Now open a threaded, peekable reader using the appropriate module
176         * to read the data */
[29d4438]177
[baef9df]178        if (use_threads) {
179                DEBUG_PIPELINE("thread");
[29d4438]180                io = thread_open(io);
[baef9df]181        }
[eda2def]182       
[baef9df]183        DEBUG_PIPELINE("peek");
[29d4438]184        return peek_open(io);
[c66068d]185}
186
[bd6576d]187DLLEXPORT off_t wandio_tell(io_t *io)
[c66068d]188{
189        if (!io->source->tell) {
190                errno = -ENOSYS;
191                return -1;
192        }
193        return io->source->tell(io);
194}
195
[bd6576d]196DLLEXPORT off_t wandio_seek(io_t *io, off_t offset, int whence)
[c66068d]197{
198        if (!io->source->seek) {
199                errno = -ENOSYS;
200                return -1;
201        }
202        return io->source->seek(io,offset,whence);
203}
204
[bd6576d]205DLLEXPORT off_t wandio_read(io_t *io, void *buffer, off_t len)
[c66068d]206{ 
207        off_t ret;
[baef9df]208        ret=io->source->read(io,buffer,len); 
[c66068d]209#if READ_TRACE
[baef9df]210        fprintf(stderr,"%p: read(%s): %d bytes = %d\n",io,io->source->name, (int)len,(int)ret);
[c66068d]211#endif
212        return ret;
213}
214
[bd6576d]215DLLEXPORT off_t wandio_peek(io_t *io, void *buffer, off_t len)
[c66068d]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);
[c3f597e]224#if READ_TRACE
225        fprintf(stderr,"%p: peek(%s): %d bytes = %d\n",io,io->source->name, (int)len, (int)ret);
226#endif
[c66068d]227        return ret;
228}
229
[bd6576d]230DLLEXPORT void wandio_destroy(io_t *io)
[29d4438]231{ 
[91ea76c]232        if (!io)
233                return;
234       
[29d4438]235        if (keep_stats) 
236                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits);
237        io->source->close(io); 
238}
[c66068d]239
[bd6576d]240DLLEXPORT iow_t *wandio_wcreate(const char *filename, int compress_type, int compression_level, int flags)
[c66068d]241{
242        iow_t *iow;
[6f8745b]243        parse_env();
[c66068d]244
245        assert ( compression_level >= 0 && compression_level <= 9 );
[bd1e5fc]246        assert (compress_type != WANDIO_COMPRESS_MASK);
[c66068d]247
[baef9df]248        iow=stdio_wopen(filename, flags);
[c3f597e]249        if (!iow)
250                return NULL;
[eda2def]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 */
[c66068d]254#if HAVE_LIBZ
255        if (compression_level != 0 && 
[b5af22c]256            compress_type == WANDIO_COMPRESS_ZLIB) {
[c66068d]257                iow = zlib_wopen(iow,compression_level);
258        }
259#endif
[aa0c5c5]260#if HAVE_LIBLZO2
261        else if (compression_level != 0 && 
[b5af22c]262            compress_type == WANDIO_COMPRESS_LZO) {
[aa0c5c5]263                iow = lzo_wopen(iow,compression_level);
264        }
265#endif
[c66068d]266#if HAVE_LIBBZ2
267        else if (compression_level != 0 && 
[b5af22c]268            compress_type == WANDIO_COMPRESS_BZ2) {
[c66068d]269                iow = bz_wopen(iow,compression_level);
270        }
271#endif
[eda2def]272        /* Open a threaded writer */
[29d4438]273        if (use_threads)
274                return thread_wopen(iow);
275        else
276                return iow;
[c66068d]277}
278
[bd6576d]279DLLEXPORT off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len)
[c66068d]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
[bd6576d]287DLLEXPORT void wandio_wdestroy(iow_t *iow)
[c66068d]288{
289        iow->source->close(iow);
[29d4438]290        if (keep_stats) 
291                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits);
[c66068d]292}
293
Note: See TracBrowser for help on using the repository browser.