source: lib/wandio.c @ b4fd5bc

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

Oops, missed a bit from a previous conflict

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