source: lib/wandio.c @ aa0c5c5

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

Fix missed conflict in previous commit

  • Property mode set to 100644
File size: 6.1 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;
57int use_threads = 1;
58
59uint64_t read_waits = 0;
60uint64_t write_waits = 0;
61
62static void do_option(const char *option)
63{
64        if (*option == '\0') 
65                ;
66        else if (strcmp(option,"stats") == 0)
67                keep_stats = 1;
68        else if (strcmp(option,"directwrite") == 0)
69                force_directio_write = 1;
70        else if (strcmp(option,"directread") == 0)
71                force_directio_read  = 1;
72        else if (strcmp(option,"nothreads") == 0)
73                use_threads = 0;
74        else {
75                fprintf(stderr,"Unknown libtraceio debug option '%s'\n", option);
76        }
77}
78
79static void parse_env(void)
80{
81        const char *str = getenv("LIBTRACEIO");
82        char option[1024];
83        const char *ip;
84        char *op;
85
86        if (!str)
87                return;
88
89        for(ip=str, op=option; *ip!='\0' && op < option+sizeof(option); ++ip) {
90                if (*ip == ',') {
91                        *op='\0';
92                        do_option(option);
93                        op=option;
94                }
95                else
96                        *(op++) = *ip;
97        }
98        *op='\0';
99        do_option(option);
100}
101
102
103>>>>>>> .r1529
104#define READ_TRACE 0
105#define WRITE_TRACE 0
106
107io_t *wandio_create(const char *filename)
108{
109        parse_env();
110
111        /* Use a peeking reader to look at the start of the trace file and
112         * determine what type of compression may have been used to write
113         * the file */
114
115        io_t *io = peek_open(stdio_open(filename));
116        char buffer[1024];
117        int len;
118        if (!io)
119                return NULL;
120        len = wandio_peek(io, buffer, sizeof(buffer));
121#if HAVE_LIBZ
122        /* Auto detect gzip compressed data */
123        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { 
124                io = zlib_open(io);
125        }
126        /* Auto detect compress(1) compressed data (gzip can read this) */
127        if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') {
128                io = zlib_open(io);
129        }
130#endif
131#if HAVE_LIBBZ2
132        /* Auto detect bzip compressed data */
133        if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { 
134                io = bz_open(io);
135        }
136#endif
137       
138        /* Now open a threaded, peekable reader using the appropriate module
139         * to read the data */
140
141        if (use_threads)
142                io = thread_open(io);
143       
144        return peek_open(io);
145}
146
147off_t wandio_tell(io_t *io)
148{
149        if (!io->source->tell) {
150                errno = -ENOSYS;
151                return -1;
152        }
153        return io->source->tell(io);
154}
155
156off_t wandio_seek(io_t *io, off_t offset, int whence)
157{
158        if (!io->source->seek) {
159                errno = -ENOSYS;
160                return -1;
161        }
162        return io->source->seek(io,offset,whence);
163}
164
165off_t wandio_read(io_t *io, void *buffer, off_t len)
166{ 
167        off_t ret;
168#if READ_TRACE
169        fprintf(stderr,"read(%s): %d bytes\n",io->source->name, (int)len);
170#endif
171        ret=io->source->read(io,buffer,len); 
172        return ret;
173}
174
175off_t wandio_peek(io_t *io, void *buffer, off_t len)
176{
177        off_t ret;
178        assert(io->source->peek); /* If this fails, it means you're calling
179                                   * peek on something that doesn't support
180                                   * peeking.   Push a peek_open() on the io
181                                   * first.
182                                   */
183        ret=io->source->peek(io, buffer, len);
184        return ret;
185}
186
187void wandio_destroy(io_t *io)
188{ 
189        if (keep_stats) 
190                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits);
191        io->source->close(io); 
192}
193
194iow_t *wandio_wcreate(const char *filename, int compression_level, int flags)
195{
196        iow_t *iow;
197
198        assert ( compression_level >= 0 && compression_level <= 9 );
199        fprintf(stderr,"Compression level= %d\n",compression_level);
200        fprintf(stderr,"flags=%d\n",flags);
201
202        iow=stdio_wopen(filename);
203
204        /* We prefer zlib if available, otherwise we'll use bzip. If neither
205         * are present, guess we'll just have to write uncompressed */
206#if HAVE_LIBZ
207        if (compression_level != 0 && 
208            (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_ZLIB) {
209                fprintf(stderr,"GZip compression\n");
210                iow = zlib_wopen(iow,compression_level);
211        }
212#endif
213#if HAVE_LIBLZO2
214        else if (compression_level != 0 && 
215            (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_LZO) {
216                fprintf(stderr,"Using LZO\n");
217                iow = lzo_wopen(iow,compression_level);
218        }
219#endif
220#if HAVE_LIBBZ2
221        else if (compression_level != 0 && 
222            (flags & WANDIO_COMPRESS_MASK) == WANDIO_COMPRESS_BZ2) {
223                fprintf(stderr,"BZ compression\n");
224                iow = bz_wopen(iow,compression_level);
225        }
226#endif
227        else if (compression_level != 0) {
228                fprintf(stderr,"Compression %d unavailable\n",flags);
229        }
230        else {
231                fprintf(stderr,"No compression requested\n");
232        }
233        /* Open a threaded writer */
234        if (use_threads)
235                return thread_wopen(iow);
236        else
237                return iow;
238}
239
240off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len)
241{
242#if WRITE_TRACE
243        fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len);
244#endif
245        return iow->source->write(iow,buffer,len);     
246}
247
248void wandio_wdestroy(iow_t *iow)
249{
250        iow->source->close(iow);
251        if (keep_stats) 
252                fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits);
253}
254
Note: See TracBrowser for help on using the repository browser.