source: lib/wandio.c @ 29d4438

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

Support an "undocumented" libtraceio debug interface

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