source: lib/ior-peek.c @ 14509f0

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

Be more aggressive with aggregating reads into larger blocks

This helps with reading using O_DIRECT

  • Property mode set to 100644
File size: 6.4 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;
64        int offset;
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 (len % MIN_READ_SIZE  == 0) {
113                        bytes_read = DATA(io)->child->source->read(
114                                        DATA(io)->child, buffer, len);
115                        /* Error? */
116                        if (bytes_read < 1) {
117                                /* Return if we have managed to get some data ok */
118                                if (ret > 0)
119                                        return ret;
120                                /* Return the error upstream */
121                                return bytes_read;
122                        }
123                }
124                else {
125                        /* Select the largest of "len", PEEK_SIZE and the current peek buffer size
126                         * and then round up to the nearest multiple of MIN_READ_SIZE
127                         */
128                        bytes_read = len < PEEK_SIZE ? PEEK_SIZE : len;
129                        bytes_read = bytes_read < DATA(io)->length ? DATA(io)->length : bytes_read;
130                        bytes_read += MIN_READ_SIZE - (bytes_read % MIN_READ_SIZE);
131                        /* Is the current buffer big enough? */
132                        if (DATA(io)->length < bytes_read) {
133                                if (DATA(io)->buffer)
134                                        free(DATA(io)->buffer);
135                                DATA(io)->length = bytes_read;
136                                DATA(io)->offset = 0;
137                                DATA(io)->buffer = malloc(DATA(io)->length);
138                        }
139                        /* Now actually attempt to read that many bytes */
140                        bytes_read = DATA(io)->child->source->read(     
141                                        DATA(io)->child, DATA(io)->buffer, bytes_read);
142                        /* Error? */
143                        if (bytes_read < 1) {
144                                if (ret > 0)
145                                        return ret;
146                                return bytes_read;
147                        }
148                        /* Now grab the number of bytes asked for. */
149                        len = len < bytes_read ? len : bytes_read;
150                        memcpy(buffer, DATA(io)->buffer, len);
151                        bytes_read = len;
152                }
153                ret += bytes_read;
154        }
155
156        /* Have we read past the end of the buffer? */
157        if (DATA(io)->buffer && DATA(io)->offset >= DATA(io)->length) {
158                /* If so, free the memory it used */
159                free(DATA(io)->buffer);
160                DATA(io)->buffer = NULL;
161                DATA(io)->offset = 0;
162                DATA(io)->length = 0;
163        }
164
165        return ret;
166}
167
168
169static off_t peek_peek(io_t *io, void *buffer, off_t len)
170{
171        off_t ret = 0;
172
173        /* Is there enough data in the buffer to serve this request? */
174        if (DATA(io)->length - DATA(io)->offset < len) {
175                /* No, we need to extend the buffer. */
176                off_t read_amount = len - (DATA(io)->length - DATA(io)->offset);
177                /* Round the read_amount up to the nearest MB */
178                read_amount += PEEK_SIZE - ((DATA(io)->length + read_amount) % PEEK_SIZE);
179                DATA(io)->buffer = realloc(DATA(io)->buffer, DATA(io)->length + read_amount);
180                /* Use the child reader to read more data into our managed
181                 * buffer */
182                read_amount = wandio_read(DATA(io)->child, 
183                        DATA(io)->buffer + DATA(io)->length,
184                        read_amount);
185
186                /* Pass errors up */
187                if (read_amount <1) {
188                        return read_amount;
189                }
190
191                DATA(io)->length += read_amount;
192        }
193
194        /* Right, now return data from the buffer (that now should be large
195         * enough, but might not be if we hit EOF) */
196        ret = MIN(len, DATA(io)->length - DATA(io)->offset);
197        memcpy(buffer, DATA(io)->buffer + DATA(io)->offset, ret);
198        return ret;
199}
200
201static off_t peek_tell(io_t *io)
202{
203        /* We don't actually maintain a read offset as such, so we want to
204         * return the child's read offset */
205        return wandio_tell(DATA(io)->child);
206}
207
208static off_t peek_seek(io_t *io, off_t offset, int whence)
209{
210        /* Again, we don't have a genuine read offset so we need to pass this
211         * one on to the child */
212        return wandio_seek(DATA(io)->child,offset,whence);
213}
214
215static void peek_close(io_t *io)
216{
217        /* Make sure we close the child that is doing the actual reading! */
218        wandio_destroy(DATA(io)->child);
219        if (DATA(io)->buffer)
220                free(DATA(io)->buffer);
221        free(io->data);
222        free(io);
223}
224
225io_source_t peek_source = {
226        "peek",
227        peek_read,
228        peek_peek,
229        peek_tell,
230        peek_seek,
231        peek_close
232};
233
Note: See TracBrowser for help on using the repository browser.