source: trunk/lib/ior-peek.c @ 1632

Last change on this file since 1632 was 1632, checked in by salcock, 3 years ago
  • Fixed yet another strict aliasing warning
  • Property svn:keywords set to Id
File size: 8.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#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#include <stddef.h>
43
44/* Libtrace IO module implementing a peeking reader.
45 *
46 * Assuming my understanding of Perry's code is correct, this module provides
47 * generic support for "peeking" that can be used in concert with any other
48 * implemented IO reader.
49 *
50 * The other IO reader is a "child" to the peeking reader and is used to read
51 * the data into a buffer managed by the peeking reader. Any actual "peeks"
52 * are serviced from the managed buffer, which means that we do not have to
53 * manipulate the read offsets directly in zlib or bzip, for instance.
54 */
55
56/* for O_DIRECT we have to read in multiples of this */
57#define MIN_READ_SIZE 4096
58/* Round reads for peeks into the buffer up to this size */
59#define PEEK_SIZE (1024*1024)
60
61struct peek_t {
62        io_t *child;
63        char *buffer;
64        int length; /* Length of buffer */
65        int offset; /* Offset into buffer */
66};
67
68extern io_source_t peek_source;
69
70#define DATA(io) ((struct peek_t *)((io)->data))
71#define MIN(a,b) ((a) < (b) ? (a) : (b))
72
73io_t *peek_open(io_t *child)
74{
75        io_t *io;
76        if (!child)
77                return NULL;
78        io =  malloc(sizeof(io_t));
79        io->data = malloc(sizeof(struct peek_t));
80        io->source = &peek_source;
81
82        /* Wrap the peeking reader around the "child" */
83        DATA(io)->child = child;
84        DATA(io)->buffer = NULL;
85        DATA(io)->length = 0;
86        DATA(io)->offset = 0;   
87
88        return io;
89}
90
91/* Read at least "len" bytes from the child io into the internal buffer, and return how many
92   bytes was actually read.
93 */
94static off_t refill_buffer(io_t *io, off_t len)
95{
96        off_t bytes_read;
97        assert(DATA(io)->length - DATA(io)->offset == 0);
98        /* Select the largest of "len", PEEK_SIZE and the current peek buffer size
99         * and then round up to the nearest multiple of MIN_READ_SIZE
100         */
101        bytes_read = len < PEEK_SIZE ? PEEK_SIZE : len;
102        bytes_read = bytes_read < DATA(io)->length ? DATA(io)->length : bytes_read;
103        bytes_read += MIN_READ_SIZE - (bytes_read % MIN_READ_SIZE);
104        /* Is the current buffer big enough? */
105        if (DATA(io)->length < bytes_read) {
106                int res = 0;
107                void *buf_ptr = (void *)(DATA(io)->buffer);
108
109                if (buf_ptr)
110                        free(buf_ptr);
111                DATA(io)->length = bytes_read;
112                DATA(io)->offset = 0;
113#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
114                /* We need to do this as read() of O_DIRECT might happen into
115                 * this buffer.  The docs suggest 512 bytes is all we need to
116                 * align to, but I'm suspicious. I expect disks with 4k blocks
117                 * will arrive soon, and thus 4k is the minimum I'm willing to
118                 * live with.
119                 */
120                res = posix_memalign(&buf_ptr, 4096, DATA(io)->length);
121                if (res != 0) {
122                        fprintf(stderr, "Error aligning IO buffer: %d\n",
123                                        res);
124                        return res;
125                }
126                DATA(io)->buffer = buf_ptr;
127#else
128                DATA(io)->buffer = malloc(DATA(io)->length);
129#endif
130        }
131        else
132                DATA(io)->length = bytes_read;
133
134        assert(DATA(io)->buffer);
135
136        /* Now actually attempt to read that many bytes */
137        bytes_read = DATA(io)->child->source->read(     
138                        DATA(io)->child, DATA(io)->buffer, bytes_read);
139
140        DATA(io)->offset = 0;
141        DATA(io)->length = bytes_read;
142
143        /* Error? */
144        if (bytes_read < 1)
145                return bytes_read;
146
147        return bytes_read;
148       
149}
150
151static off_t peek_read(io_t *io, void *buffer, off_t len)
152{
153        off_t ret = 0;
154
155        /* Is some of this data in the buffer? */
156        if (DATA(io)->buffer) {
157                ret = MIN(len,DATA(io)->length - DATA(io)->offset);
158
159                /* Copy anything we've got into their buffer, and shift our
160                 * offset so that we don't peek at the data we've read again */
161                memcpy(buffer, 
162                        DATA(io)->buffer + DATA(io)->offset,
163                        ret);
164                buffer += ret;
165                DATA(io)->offset += ret;
166                len -= ret;
167        }
168
169        /* Use the child reader to get the rest of the required data */
170        if (len>0) {
171                /* To get here, the buffer must be empty */
172                assert(DATA(io)->length-DATA(io)->offset == 0);
173                off_t bytes_read;
174                /* If they're reading exactly a block size, just use that, no point in malloc'ing
175                 * and memcpy()ing needlessly.  However, if the buffer isn't aligned, we need to
176                 * pass on an aligning buffer, skip this and do it into our own aligned buffer.
177                 */
178                if ((len % MIN_READ_SIZE  == 0) && ((ptrdiff_t)buffer % 4096)==0) {
179                        assert(((ptrdiff_t)buffer % 4096) == 0);
180                        bytes_read = DATA(io)->child->source->read(
181                                        DATA(io)->child, buffer, len);
182                        /* Error? */
183                        if (bytes_read < 1) {
184                                /* Return if we have managed to get some data ok */
185                                if (ret > 0)
186                                        return ret;
187                                /* Return the error upstream */
188                                return bytes_read;
189                        }
190                }
191                else {
192                        bytes_read = refill_buffer(io, len);
193                        if (bytes_read < 1) {
194                                /* Return if we have managed to get some data ok */
195                                if (ret > 0)
196                                        return ret;
197                                /* Return the error upstream */
198                                return bytes_read;
199                        }
200                        /* Now grab the number of bytes asked for. */
201                        len = len < bytes_read ? len : bytes_read;
202                        memcpy(buffer, DATA(io)->buffer, len);
203
204                        DATA(io)->offset = len;
205                        bytes_read = len;
206                }
207                ret += bytes_read;
208        }
209
210        /* Have we read past the end of the buffer? */
211        if (DATA(io)->buffer && DATA(io)->offset >= DATA(io)->length) {
212                /* If so, free the memory it used */
213                free(DATA(io)->buffer);
214                DATA(io)->buffer = NULL;
215                DATA(io)->offset = 0;
216                DATA(io)->length = 0;
217        }
218
219        return ret;
220}
221
222static void *alignedrealloc(void *old, size_t oldsize, size_t size, int *res)
223{
224#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
225        void *new;
226        /* Shortcut resizing */
227        if (size < oldsize)
228                return old;
229        *res = posix_memalign(&new, 4096, size);
230        if (*res != 0) {
231                fprintf(stderr, "Error aligning IO buffer: %d\n", *res);
232               
233                return NULL;
234        }
235        assert(oldsize<size);
236        memcpy(new,old,oldsize);
237        free(old);
238        return new;
239#else
240        return realloc(old,size);
241#endif
242}
243
244
245static off_t peek_peek(io_t *io, void *buffer, off_t len)
246{
247        off_t ret = 0;
248        int res = 0;
249
250        /* Is there enough data in the buffer to serve this request? */
251        if (DATA(io)->length - DATA(io)->offset < len) {
252                /* No, we need to extend the buffer. */
253                off_t read_amount = len - (DATA(io)->length - DATA(io)->offset);
254                /* Round the read_amount up to the nearest MB */
255                read_amount += PEEK_SIZE - ((DATA(io)->length + read_amount) % PEEK_SIZE);
256                DATA(io)->buffer = alignedrealloc(DATA(io)->buffer, 
257                        DATA(io)->length, 
258                        DATA(io)->length + read_amount, &res);
259
260                if (DATA(io)->buffer == NULL) {
261                        return res;     
262                }
263
264                /* Use the child reader to read more data into our managed
265                 * buffer */
266                read_amount = wandio_read(DATA(io)->child, 
267                        DATA(io)->buffer + DATA(io)->length,
268                        read_amount);
269
270                /* Pass errors up */
271                if (read_amount <1) {
272                        return read_amount;
273                }
274
275                DATA(io)->length += read_amount;
276        }
277
278        /* Right, now return data from the buffer (that now should be large
279         * enough, but might not be if we hit EOF) */
280        ret = MIN(len, DATA(io)->length - DATA(io)->offset);
281        memcpy(buffer, DATA(io)->buffer + DATA(io)->offset, ret);
282        return ret;
283}
284
285static off_t peek_tell(io_t *io)
286{
287        /* We don't actually maintain a read offset as such, so we want to
288         * return the child's read offset */
289        return wandio_tell(DATA(io)->child);
290}
291
292static off_t peek_seek(io_t *io, off_t offset, int whence)
293{
294        /* Again, we don't have a genuine read offset so we need to pass this
295         * one on to the child */
296        return wandio_seek(DATA(io)->child,offset,whence);
297}
298
299static void peek_close(io_t *io)
300{
301        /* Make sure we close the child that is doing the actual reading! */
302        wandio_destroy(DATA(io)->child);
303        if (DATA(io)->buffer)
304                free(DATA(io)->buffer);
305        free(io->data);
306        free(io);
307}
308
309io_source_t peek_source = {
310        "peek",
311        peek_read,
312        peek_peek,
313        peek_tell,
314        peek_seek,
315        peek_close
316};
317
Note: See TracBrowser for help on using the repository browser.