source: lib/ior-peek.c @ c6f41d8

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

Correctly update peek buffer offsets

  • Property mode set to 100644
File size: 6.6 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#include "wandio.h"
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <fcntl.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <string.h>
41#include <assert.h>
42
43/* Libtrace IO module implementing a peeking reader.
44 *
45 * Assuming my understanding of Perry's code is correct, this module provides
46 * generic support for "peeking" that can be used in concert with any other
47 * implemented IO reader.
48 *
49 * The other IO reader is a "child" to the peeking reader and is used to read
50 * the data into a buffer managed by the peeking reader. Any actual "peeks"
51 * are serviced from the managed buffer, which means that we do not have to
52 * manipulate the read offsets directly in zlib or bzip, for instance.
53 */
54
55/* for O_DIRECT we have to read in multiples of this */
56#define MIN_READ_SIZE 4096
57/* Round reads for peeks into the buffer up to this size */
58#define PEEK_SIZE (1024*1024)
59
60struct peek_t {
61        io_t *child;
62        char *buffer;
63        int length; /* Length of buffer */
64        int offset; /* Offset into buffer */
65};
66
67extern io_source_t peek_source;
68
69#define DATA(io) ((struct peek_t *)((io)->data))
70#define MIN(a,b) ((a) < (b) ? (a) : (b))
71
72io_t *peek_open(io_t *child)
73{
74        io_t *io;
75        if (!child)
76                return NULL;
77        io =  malloc(sizeof(io_t));
78        io->data = malloc(sizeof(struct peek_t));
79        io->source = &peek_source;
80
81        /* Wrap the peeking reader around the "child" */
82        DATA(io)->child = child;
83        DATA(io)->buffer = NULL;
84        DATA(io)->length = 0;
85        DATA(io)->offset = 0;   
86
87        return io;
88}
89
90static off_t peek_read(io_t *io, void *buffer, off_t len)
91{
92        off_t ret = 0;
93
94        /* Is some of this data in the buffer? */
95        if (DATA(io)->buffer) {
96                ret = MIN(len,DATA(io)->length - DATA(io)->offset);
97
98                /* Copy anything we've got into their buffer, and shift our
99                 * offset so that we don't peek at the data we've read again */
100                memcpy(buffer, 
101                        DATA(io)->buffer + DATA(io)->offset,
102                        ret);
103                buffer += ret;
104                DATA(io)->offset += ret;
105                len -= ret;
106        }
107        /* Use the child reader to get the rest of the required data */
108        if (len>0) {
109                /* To get here, the buffer must be empty */
110                assert(DATA(io)->length-DATA(io)->offset == 0);
111                off_t bytes_read;
112                /* If they're reading a block size, use that */
113                if (len % MIN_READ_SIZE  == 0) {
114                        bytes_read = DATA(io)->child->source->read(
115                                        DATA(io)->child, buffer, len);
116                        /* Error? */
117                        if (bytes_read < 1) {
118                                /* Return if we have managed to get some data ok */
119                                if (ret > 0)
120                                        return ret;
121                                /* Return the error upstream */
122                                return bytes_read;
123                        }
124                }
125                else {
126                        /* Select the largest of "len", PEEK_SIZE and the current peek buffer size
127                         * and then round up to the nearest multiple of MIN_READ_SIZE
128                         */
129                        bytes_read = len < PEEK_SIZE ? PEEK_SIZE : len;
130                        bytes_read = bytes_read < DATA(io)->length ? DATA(io)->length : bytes_read;
131                        bytes_read += MIN_READ_SIZE - (bytes_read % MIN_READ_SIZE);
132                        /* Is the current buffer big enough? */
133                        if (DATA(io)->length < bytes_read) {
134                                if (DATA(io)->buffer)
135                                        free(DATA(io)->buffer);
136                                DATA(io)->length = bytes_read;
137                                DATA(io)->offset = 0;
138                                DATA(io)->buffer = malloc(DATA(io)->length);
139                        }
140                        /* Now actually attempt to read that many bytes */
141                        bytes_read = DATA(io)->child->source->read(     
142                                        DATA(io)->child, DATA(io)->buffer, bytes_read);
143                        /* Error? */
144                        if (bytes_read < 1) {
145                                if (ret > 0)
146                                        return ret;
147                                return bytes_read;
148                        }
149                        /* Now grab the number of bytes asked for. */
150                        len = len < bytes_read ? len : bytes_read;
151                        memcpy(buffer, DATA(io)->buffer, len);
152                        DATA(io)->offset = len;
153                        bytes_read = len;
154                }
155                ret += bytes_read;
156        }
157
158        /* Have we read past the end of the buffer? */
159        if (DATA(io)->buffer && DATA(io)->offset >= DATA(io)->length) {
160                /* If so, free the memory it used */
161                free(DATA(io)->buffer);
162                DATA(io)->buffer = NULL;
163                DATA(io)->offset = 0;
164                DATA(io)->length = 0;
165        }
166
167        return ret;
168}
169
170
171static off_t peek_peek(io_t *io, void *buffer, off_t len)
172{
173        off_t ret = 0;
174
175        /* Is there enough data in the buffer to serve this request? */
176        if (DATA(io)->length - DATA(io)->offset < len) {
177                /* No, we need to extend the buffer. */
178                off_t read_amount = len - (DATA(io)->length - DATA(io)->offset);
179                /* Round the read_amount up to the nearest MB */
180                read_amount += PEEK_SIZE - ((DATA(io)->length + read_amount) % PEEK_SIZE);
181                DATA(io)->buffer = realloc(DATA(io)->buffer, DATA(io)->length + read_amount);
182                /* Use the child reader to read more data into our managed
183                 * buffer */
184                read_amount = wandio_read(DATA(io)->child, 
185                        DATA(io)->buffer + DATA(io)->length,
186                        read_amount);
187
188                /* Pass errors up */
189                if (read_amount <1) {
190                        return read_amount;
191                }
192
193                DATA(io)->length += read_amount;
194        }
195
196        /* Right, now return data from the buffer (that now should be large
197         * enough, but might not be if we hit EOF) */
198        ret = MIN(len, DATA(io)->length - DATA(io)->offset);
199        memcpy(buffer, DATA(io)->buffer + DATA(io)->offset, ret);
200        return ret;
201}
202
203static off_t peek_tell(io_t *io)
204{
205        /* We don't actually maintain a read offset as such, so we want to
206         * return the child's read offset */
207        return wandio_tell(DATA(io)->child);
208}
209
210static off_t peek_seek(io_t *io, off_t offset, int whence)
211{
212        /* Again, we don't have a genuine read offset so we need to pass this
213         * one on to the child */
214        return wandio_seek(DATA(io)->child,offset,whence);
215}
216
217static void peek_close(io_t *io)
218{
219        /* Make sure we close the child that is doing the actual reading! */
220        wandio_destroy(DATA(io)->child);
221        if (DATA(io)->buffer)
222                free(DATA(io)->buffer);
223        free(io->data);
224        free(io);
225}
226
227io_source_t peek_source = {
228        "peek",
229        peek_read,
230        peek_peek,
231        peek_tell,
232        peek_seek,
233        peek_close
234};
235
Note: See TracBrowser for help on using the repository browser.