source: lib/ior-peek.c @ 924b0a0

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

Enforce alignment constraints

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