source: lib/ior-peek.c @ d08c691

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since d08c691 was d08c691, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Actually check the return value of posix_memalign
  • Property mode set to 100644
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
108                if (DATA(io)->buffer)
109                        free(DATA(io)->buffer);
110                DATA(io)->length = bytes_read;
111                DATA(io)->offset = 0;
112#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
113                /* We need to do this as read() of O_DIRECT might happen into
114                 * this buffer.  The docs suggest 512 bytes is all we need to
115                 * align to, but I'm suspicious. I expect disks with 4k blocks
116                 * will arrive soon, and thus 4k is the minimum I'm willing to
117                 * live with.
118                 */
119                res = posix_memalign((void **)&DATA(io)->buffer, 4096, 
120                                DATA(io)->length);
121                if (res != 0) {
122                        fprintf(stderr, "Error aligning IO buffer: %d\n",
123                                        res);
124                        return res;
125                }
126#else
127                DATA(io)->buffer = malloc(DATA(io)->length);
128#endif
129        }
130        else
131                DATA(io)->length = bytes_read;
132
133        assert(DATA(io)->buffer);
134
135        /* Now actually attempt to read that many bytes */
136        bytes_read = DATA(io)->child->source->read(     
137                        DATA(io)->child, DATA(io)->buffer, bytes_read);
138
139        DATA(io)->offset = 0;
140        DATA(io)->length = bytes_read;
141
142        /* Error? */
143        if (bytes_read < 1)
144                return bytes_read;
145
146        return bytes_read;
147       
148}
149
150static off_t peek_read(io_t *io, void *buffer, off_t len)
151{
152        off_t ret = 0;
153
154        /* Is some of this data in the buffer? */
155        if (DATA(io)->buffer) {
156                ret = MIN(len,DATA(io)->length - DATA(io)->offset);
157
158                /* Copy anything we've got into their buffer, and shift our
159                 * offset so that we don't peek at the data we've read again */
160                memcpy(buffer, 
161                        DATA(io)->buffer + DATA(io)->offset,
162                        ret);
163                buffer += ret;
164                DATA(io)->offset += ret;
165                len -= ret;
166        }
167
168        /* Use the child reader to get the rest of the required data */
169        if (len>0) {
170                /* To get here, the buffer must be empty */
171                assert(DATA(io)->length-DATA(io)->offset == 0);
172                off_t bytes_read;
173                /* If they're reading exactly a block size, just use that, no point in malloc'ing
174                 * and memcpy()ing needlessly.  However, if the buffer isn't aligned, we need to
175                 * pass on an aligning buffer, skip this and do it into our own aligned buffer.
176                 */
177                if ((len % MIN_READ_SIZE  == 0) && ((ptrdiff_t)buffer % 4096)==0) {
178                        assert(((ptrdiff_t)buffer % 4096) == 0);
179                        bytes_read = DATA(io)->child->source->read(
180                                        DATA(io)->child, buffer, len);
181                        /* Error? */
182                        if (bytes_read < 1) {
183                                /* Return if we have managed to get some data ok */
184                                if (ret > 0)
185                                        return ret;
186                                /* Return the error upstream */
187                                return bytes_read;
188                        }
189                }
190                else {
191                        bytes_read = refill_buffer(io, len);
192                        if (bytes_read < 1) {
193                                /* Return if we have managed to get some data ok */
194                                if (ret > 0)
195                                        return ret;
196                                /* Return the error upstream */
197                                return bytes_read;
198                        }
199                        /* Now grab the number of bytes asked for. */
200                        len = len < bytes_read ? len : bytes_read;
201                        memcpy(buffer, DATA(io)->buffer, len);
202
203                        DATA(io)->offset = len;
204                        bytes_read = len;
205                }
206                ret += bytes_read;
207        }
208
209        /* Have we read past the end of the buffer? */
210        if (DATA(io)->buffer && DATA(io)->offset >= DATA(io)->length) {
211                /* If so, free the memory it used */
212                free(DATA(io)->buffer);
213                DATA(io)->buffer = NULL;
214                DATA(io)->offset = 0;
215                DATA(io)->length = 0;
216        }
217
218        return ret;
219}
220
221static void *alignedrealloc(void *old, size_t oldsize, size_t size, int *res)
222{
223#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
224        void *new;
225        /* Shortcut resizing */
226        if (size < oldsize)
227                return old;
228        *res = posix_memalign(&new, 4096, size);
229        if (*res != 0) {
230                fprintf(stderr, "Error aligning IO buffer: %d\n", *res);
231               
232                return NULL;
233        }
234        assert(oldsize<size);
235        memcpy(new,old,oldsize);
236        free(old);
237        return new;
238#else
239        return realloc(old,size);
240#endif
241}
242
243
244static off_t peek_peek(io_t *io, void *buffer, off_t len)
245{
246        off_t ret = 0;
247        int res = 0;
248
249        /* Is there enough data in the buffer to serve this request? */
250        if (DATA(io)->length - DATA(io)->offset < len) {
251                /* No, we need to extend the buffer. */
252                off_t read_amount = len - (DATA(io)->length - DATA(io)->offset);
253                /* Round the read_amount up to the nearest MB */
254                read_amount += PEEK_SIZE - ((DATA(io)->length + read_amount) % PEEK_SIZE);
255                DATA(io)->buffer = alignedrealloc(DATA(io)->buffer, 
256                        DATA(io)->length, 
257                        DATA(io)->length + read_amount, &res);
258
259                if (DATA(io)->buffer == NULL) {
260                        return res;     
261                }
262
263                /* Use the child reader to read more data into our managed
264                 * buffer */
265                read_amount = wandio_read(DATA(io)->child, 
266                        DATA(io)->buffer + DATA(io)->length,
267                        read_amount);
268
269                /* Pass errors up */
270                if (read_amount <1) {
271                        return read_amount;
272                }
273
274                DATA(io)->length += read_amount;
275        }
276
277        /* Right, now return data from the buffer (that now should be large
278         * enough, but might not be if we hit EOF) */
279        ret = MIN(len, DATA(io)->length - DATA(io)->offset);
280        memcpy(buffer, DATA(io)->buffer + DATA(io)->offset, ret);
281        return ret;
282}
283
284static off_t peek_tell(io_t *io)
285{
286        /* We don't actually maintain a read offset as such, so we want to
287         * return the child's read offset */
288        return wandio_tell(DATA(io)->child);
289}
290
291static off_t peek_seek(io_t *io, off_t offset, int whence)
292{
293        /* Again, we don't have a genuine read offset so we need to pass this
294         * one on to the child */
295        return wandio_seek(DATA(io)->child,offset,whence);
296}
297
298static void peek_close(io_t *io)
299{
300        /* Make sure we close the child that is doing the actual reading! */
301        wandio_destroy(DATA(io)->child);
302        if (DATA(io)->buffer)
303                free(DATA(io)->buffer);
304        free(io->data);
305        free(io);
306}
307
308io_source_t peek_source = {
309        "peek",
310        peek_read,
311        peek_peek,
312        peek_tell,
313        peek_seek,
314        peek_close
315};
316
Note: See TracBrowser for help on using the repository browser.