| 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 "config.h" |
|---|
| 36 | #include "wandio.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 | |
|---|
| 47 | struct 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 | |
|---|
| 54 | int keep_stats = 0; |
|---|
| 55 | int force_directio_write = 0; |
|---|
| 56 | int force_directio_read = 0; |
|---|
| 57 | unsigned int use_threads = -1; |
|---|
| 58 | unsigned int max_buffers = 50; |
|---|
| 59 | |
|---|
| 60 | uint64_t read_waits = 0; |
|---|
| 61 | uint64_t write_waits = 0; |
|---|
| 62 | |
|---|
| 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 | */ |
|---|
| 70 | static void do_option(const char *option) |
|---|
| 71 | { |
|---|
| 72 | if (*option == '\0') |
|---|
| 73 | ; |
|---|
| 74 | else if (strcmp(option,"stats") == 0) |
|---|
| 75 | keep_stats = 1; |
|---|
| 76 | /* |
|---|
| 77 | else if (strcmp(option,"directwrite") == 0) |
|---|
| 78 | force_directio_write = 1; |
|---|
| 79 | else if (strcmp(option,"directread") == 0) |
|---|
| 80 | force_directio_read = 1; |
|---|
| 81 | */ |
|---|
| 82 | else if (strcmp(option,"nothreads") == 0) |
|---|
| 83 | use_threads = 0; |
|---|
| 84 | else if (strncmp(option,"threads=",8) == 0) |
|---|
| 85 | use_threads = atoi(option+8); |
|---|
| 86 | else if (strncmp(option,"buffers=",8) == 0) |
|---|
| 87 | max_buffers = atoi(option+8); |
|---|
| 88 | else { |
|---|
| 89 | fprintf(stderr,"Unknown libtraceio debug option '%s'\n", option); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | static 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 | |
|---|
| 117 | #define READ_TRACE 0 |
|---|
| 118 | #define WRITE_TRACE 0 |
|---|
| 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 |
|---|
| 126 | |
|---|
| 127 | io_t *wandio_create(const char *filename) |
|---|
| 128 | { |
|---|
| 129 | parse_env(); |
|---|
| 130 | |
|---|
| 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 */ |
|---|
| 134 | |
|---|
| 135 | DEBUG_PIPELINE("stdio"); |
|---|
| 136 | DEBUG_PIPELINE("peek"); |
|---|
| 137 | io_t *io = peek_open(stdio_open(filename)); |
|---|
| 138 | char buffer[1024]; |
|---|
| 139 | int len; |
|---|
| 140 | if (!io) |
|---|
| 141 | return NULL; |
|---|
| 142 | len = wandio_peek(io, buffer, sizeof(buffer)); |
|---|
| 143 | #if HAVE_LIBZ |
|---|
| 144 | /* Auto detect gzip compressed data */ |
|---|
| 145 | if (len>=2 && buffer[0] == '\037' && buffer[1] == '\213') { |
|---|
| 146 | DEBUG_PIPELINE("zlib"); |
|---|
| 147 | io = zlib_open(io); |
|---|
| 148 | } |
|---|
| 149 | /* Auto detect compress(1) compressed data (gzip can read this) */ |
|---|
| 150 | if (len>=2 && buffer[0] == '\037' && buffer[1] == '\235') { |
|---|
| 151 | DEBUG_PIPELINE("zlib"); |
|---|
| 152 | io = zlib_open(io); |
|---|
| 153 | } |
|---|
| 154 | #endif |
|---|
| 155 | #if HAVE_LIBBZ2 |
|---|
| 156 | /* Auto detect bzip compressed data */ |
|---|
| 157 | if (len>=3 && buffer[0] == 'B' && buffer[1] == 'Z' && buffer[2] == 'h') { |
|---|
| 158 | DEBUG_PIPELINE("bzip"); |
|---|
| 159 | io = bz_open(io); |
|---|
| 160 | } |
|---|
| 161 | #endif |
|---|
| 162 | |
|---|
| 163 | /* Now open a threaded, peekable reader using the appropriate module |
|---|
| 164 | * to read the data */ |
|---|
| 165 | |
|---|
| 166 | if (use_threads) { |
|---|
| 167 | DEBUG_PIPELINE("thread"); |
|---|
| 168 | io = thread_open(io); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | DEBUG_PIPELINE("peek"); |
|---|
| 172 | return peek_open(io); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | off_t wandio_tell(io_t *io) |
|---|
| 176 | { |
|---|
| 177 | if (!io->source->tell) { |
|---|
| 178 | errno = -ENOSYS; |
|---|
| 179 | return -1; |
|---|
| 180 | } |
|---|
| 181 | return io->source->tell(io); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | off_t wandio_seek(io_t *io, off_t offset, int whence) |
|---|
| 185 | { |
|---|
| 186 | if (!io->source->seek) { |
|---|
| 187 | errno = -ENOSYS; |
|---|
| 188 | return -1; |
|---|
| 189 | } |
|---|
| 190 | return io->source->seek(io,offset,whence); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | off_t wandio_read(io_t *io, void *buffer, off_t len) |
|---|
| 194 | { |
|---|
| 195 | off_t ret; |
|---|
| 196 | ret=io->source->read(io,buffer,len); |
|---|
| 197 | #if READ_TRACE |
|---|
| 198 | fprintf(stderr,"%p: read(%s): %d bytes = %d\n",io,io->source->name, (int)len,(int)ret); |
|---|
| 199 | #endif |
|---|
| 200 | return ret; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | off_t wandio_peek(io_t *io, void *buffer, off_t len) |
|---|
| 204 | { |
|---|
| 205 | off_t ret; |
|---|
| 206 | assert(io->source->peek); /* If this fails, it means you're calling |
|---|
| 207 | * peek on something that doesn't support |
|---|
| 208 | * peeking. Push a peek_open() on the io |
|---|
| 209 | * first. |
|---|
| 210 | */ |
|---|
| 211 | ret=io->source->peek(io, buffer, len); |
|---|
| 212 | #if READ_TRACE |
|---|
| 213 | fprintf(stderr,"%p: peek(%s): %d bytes = %d\n",io,io->source->name, (int)len, (int)ret); |
|---|
| 214 | #endif |
|---|
| 215 | return ret; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | void wandio_destroy(io_t *io) |
|---|
| 219 | { |
|---|
| 220 | if (keep_stats) |
|---|
| 221 | fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on read\n", read_waits); |
|---|
| 222 | io->source->close(io); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | iow_t *wandio_wcreate(const char *filename, int compress_type, int compression_level, int flags) |
|---|
| 226 | { |
|---|
| 227 | iow_t *iow; |
|---|
| 228 | parse_env(); |
|---|
| 229 | |
|---|
| 230 | assert ( compression_level >= 0 && compression_level <= 9 ); |
|---|
| 231 | assert (compress_type != WANDIO_COMPRESS_MASK); |
|---|
| 232 | |
|---|
| 233 | iow=stdio_wopen(filename, flags); |
|---|
| 234 | if (!iow) |
|---|
| 235 | return NULL; |
|---|
| 236 | |
|---|
| 237 | /* We prefer zlib if available, otherwise we'll use bzip. If neither |
|---|
| 238 | * are present, guess we'll just have to write uncompressed */ |
|---|
| 239 | #if HAVE_LIBZ |
|---|
| 240 | if (compression_level != 0 && |
|---|
| 241 | compress_type == WANDIO_COMPRESS_ZLIB) { |
|---|
| 242 | iow = zlib_wopen(iow,compression_level); |
|---|
| 243 | } |
|---|
| 244 | #endif |
|---|
| 245 | #if HAVE_LIBLZO2 |
|---|
| 246 | else if (compression_level != 0 && |
|---|
| 247 | compress_type == WANDIO_COMPRESS_LZO) { |
|---|
| 248 | iow = lzo_wopen(iow,compression_level); |
|---|
| 249 | } |
|---|
| 250 | #endif |
|---|
| 251 | #if HAVE_LIBBZ2 |
|---|
| 252 | else if (compression_level != 0 && |
|---|
| 253 | compress_type == WANDIO_COMPRESS_BZ2) { |
|---|
| 254 | iow = bz_wopen(iow,compression_level); |
|---|
| 255 | } |
|---|
| 256 | #endif |
|---|
| 257 | /* Open a threaded writer */ |
|---|
| 258 | if (use_threads) |
|---|
| 259 | return thread_wopen(iow); |
|---|
| 260 | else |
|---|
| 261 | return iow; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | off_t wandio_wwrite(iow_t *iow, const void *buffer, off_t len) |
|---|
| 265 | { |
|---|
| 266 | #if WRITE_TRACE |
|---|
| 267 | fprintf(stderr,"wwrite(%s): %d bytes\n",iow->source->name, (int)len); |
|---|
| 268 | #endif |
|---|
| 269 | return iow->source->write(iow,buffer,len); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | void wandio_wdestroy(iow_t *iow) |
|---|
| 273 | { |
|---|
| 274 | iow->source->close(iow); |
|---|
| 275 | if (keep_stats) |
|---|
| 276 | fprintf(stderr,"LIBTRACEIO STATS: %"PRIu64" blocks on write\n", write_waits); |
|---|
| 277 | } |
|---|
| 278 | |
|---|