source: lib/wandio.c @ b5af22c

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

Split compression type out from fileflags

  • Property mode set to 100644
File size: 6.0 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
115io_t *wandio_create(const char *filename)
116{
117        parse_env();
118
119        /* Use a peeking reader to look at the start of the trace file and
120         * determine what type of compression may have been used to write
121         * the file */
122
123        io_t *io = peek_open(stdio_open(filename));
124        char buffer[1024];
125        int len;
126        if (!io)
127                return NULL;
128        len = wandio_peek(io, buffer, sizeof(buffer));
129#if HAVE_LIBZ
130        /* Auto detect gzip compressed data */
131        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { 
132                io = zlib_open(io);
133        }
134        /* Auto detect compress(1) compressed data (gzip can read this) */
135        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') {
136                io = zlib_open(io);
137        }
138#endif
139#if HAVE_LIBBZ2
140        /* Auto detect bzip compressed data */
141        if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { 
142                io = bz_open(io);
143        }
144#endif
145       
146        /* Now open a threaded, peekable reader using the appropriate module
147         * to read the data */
148
149        if (use_threads)
150                io = thread_open(io);
151       
152        return peek_open(io);
153}
154
155off_t wandio_tell(io_t *io)
156{
157        if (!io->source->tell) {
158                errno = -ENOSYS;
159                return -1;
160        }
161        return io->source->tell(io);
162}
163
164off_t wandio_seek(io_t *io, off_t offset, int whence)
165{
166        if (!io->source->seek) {
167                errno = -ENOSYS;
168                return -1;
169        }
170        return io->source->seek(io,offset,whence);
171}
172
173off_t wandio_read(io_t *io, void *buffer, off_t len)
174{ 
175        off_t ret;
176#if READ_TRACE
177        fprintf(stderr,"read(%s): %d bytes\n",io->source->name, (int)len);
178#endif
179        ret=io->source->read(io,buffer,len); 
180        return ret;
181}
182
183off_t wandio_peek(io_t *io, void *buffer, off_t len)
184{
185        off_t ret;
186        assert(io->source->peek); /* If this fails, it means you're calling
187                                   * peek on something that doesn't support
188                                   * peeking.   Push a peek_open() on the io
189                                   * first.
190                                   */
191        ret=io->source->peek(io, buffer, len);
192        return ret;
193}
194
195void wandio_destroy(io_t *io)
196{ 
197        if (keep_stats) 
198                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits);
199        io->source->close(io); 
200}
201
202iow_t *wandio_wcreate(const char *filename, int compress_type, int compression_level, int flags)
203{
204        iow_t *iow;
205        parse_env();
206
207        assert ( compression_level >= 0 && compression_level <= 9 );
208
209        iow=stdio_wopen(filename);
210
211        /* We prefer zlib if available, otherwise we'll use bzip. If neither
212         * are present, guess we'll just have to write uncompressed */
213#if HAVE_LIBZ
214        if (compression_level != 0 && 
215            compress_type == WANDIO_COMPRESS_ZLIB) {
216                iow = zlib_wopen(iow,compression_level);
217        }
218#endif
219#if HAVE_LIBLZO2
220        else if (compression_level != 0 && 
221            compress_type == WANDIO_COMPRESS_LZO) {
222                iow = lzo_wopen(iow,compression_level);
223        }
224#endif
225#if HAVE_LIBBZ2
226        else if (compression_level != 0 && 
227            compress_type == WANDIO_COMPRESS_BZ2) {
228                iow = bz_wopen(iow,compression_level);
229        }
230#endif
231        /* Open a threaded writer */
232        if (use_threads)
233                return thread_wopen(iow);
234        else
235                return iow;
236}
237
238off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len)
239{
240#if WRITE_TRACE
241        fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len);
242#endif
243        return iow->source->write(iow,buffer,len);     
244}
245
246void wandio_wdestroy(iow_t *iow)
247{
248        iow->source->close(iow);
249        if (keep_stats) 
250                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits);
251}
252
Note: See TracBrowser for help on using the repository browser.