-
Notifications
You must be signed in to change notification settings - Fork 6
/
aiocache.c
316 lines (266 loc) · 5.85 KB
/
aiocache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <aio.h>
#include <getopt.h>
#include <sys/time.h>
#include <time.h>
static struct timeval tp1,tp2;
static void start_timer()
{
gettimeofday(&tp1,NULL);
}
static double end_timer()
{
gettimeofday(&tp2,NULL);
return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
(tp1.tv_sec + (tp1.tv_usec*1.0e-6));
}
struct aio_block {
struct aiocb *cb;
unsigned char *buffer;
int nread;
int done;
int blk;
};
struct aio_ring {
int fd;
unsigned block_size;
unsigned num_blocks;
struct aio_block *blocks;
};
static int a_schedule(struct aio_ring *ring, int blk)
{
struct aio_block *b = &ring->blocks[blk % ring->num_blocks];
if (blk == b->blk) return 0;
if (b->buffer == NULL) {
b->buffer = malloc(ring->block_size);
if (b->buffer == NULL) goto failed;
}
if (b->cb == NULL) {
b->cb = malloc(sizeof(*b->cb));
if (b->cb == NULL) goto failed;
}
if (b->blk != -1 && !b->done) {
int ret = aio_cancel(ring->fd, b->cb);
if (ret == AIO_NOTCANCELED) {
aio_suspend(&b->cb, 1, NULL);
}
aio_error(b->cb);
aio_return(b->cb);
}
b->blk = blk;
memset(b->cb, 0, sizeof(*b->cb));
b->cb->aio_fildes = ring->fd;
b->cb->aio_buf = b->buffer;
b->cb->aio_nbytes = ring->block_size;
b->cb->aio_offset = blk * (off_t)ring->block_size;
if (aio_read(b->cb) != 0) goto failed;
b->done = 0;
return 0;
failed:
free(b->buffer);
free(b->cb);
b->buffer = NULL;
b->cb = NULL;
b->blk = -1;
return -1;
}
static int a_wait(struct aio_ring *ring, int blk)
{
struct aio_block *b = &ring->blocks[blk % ring->num_blocks];
if (b->blk != blk) return -1;
if (b->done) return 0;
if (aio_suspend(&b->cb, 1, NULL) != 0 ||
aio_error(b->cb) != 0) {
free(b->buffer);
free(b->cb);
b->buffer = NULL;
b->cb = NULL;
b->blk = -1;
return -1;
}
b->done = 1;
b->nread = aio_return(b->cb);
if (b->nread < 0) return -1;
return 0;
}
static ssize_t a_read(struct aio_ring *ring, void *buf, size_t count, off_t offset)
{
int blk_start = offset / ring->block_size;
int blk_end = (offset+count-1) / ring->block_size;
int blk_sched = blk_start + (ring->num_blocks/2);
int i;
ssize_t total=0;
if (blk_sched < blk_end) blk_sched = blk_end;
for (i=blk_start;i<=blk_sched;i++) {
a_schedule(ring, i);
}
while (count) {
int n = count;
int blk_offset = offset % ring->block_size;
int blk = offset / ring->block_size;
struct aio_block *b = &ring->blocks[blk % ring->num_blocks];
ssize_t nread;
if (n > ring->block_size - blk_offset) {
n = ring->block_size - blk_offset;
}
if (a_wait(ring, blk) != 0) {
nread = pread(ring->fd, buf, n, offset);
printf("sync fallback\n");
} else {
if (n > b->nread) n = b->nread;
memcpy(buf, b->buffer + blk_offset, n);
nread = n;
}
if (nread <= 0) {
if (total > 0) return total;
return nread;
}
total += nread;
buf = (void *)(nread + (char *)buf);
count -= nread;
offset += nread;
}
return total;
}
static struct aio_ring *a_init(int fd, int block_size, int nblocks)
{
struct aio_ring *ring;
int i;
ring = malloc(sizeof(*ring));
if (ring == NULL) return NULL;
ring->fd = fd;
ring->num_blocks = nblocks;
ring->block_size = block_size;
ring->blocks = calloc(nblocks, sizeof(ring->blocks[0]));
if (ring->blocks == NULL) {
free(ring);
return NULL;
}
for (i=0;i<nblocks;i++) {
ring->blocks[i].blk = -1;
}
return ring;
}
static void a_close(struct aio_ring *ring)
{
int i;
for (i=0;i<ring->num_blocks;i++) {
struct aio_block *b = &ring->blocks[i];
if (b->blk != -1 && !b->done) {
int ret = aio_cancel(ring->fd, b->cb);
if (ret == AIO_NOTCANCELED) {
aio_suspend(&b->cb, 1, NULL);
}
aio_error(b->cb);
aio_return(b->cb);
}
if (b->cb) free(b->cb);
if (b->buffer) free(b->buffer);
}
free(ring);
}
static void async_test(const char *fname, int block_size, int nblocks, int rsize)
{
int fd;
struct aio_ring *ring;
off_t offset=0;
char buf[rsize];
int i=0;
offset=0;
start_timer();
fd = open(fname, O_RDONLY);
if (fd == -1) {
perror(fname);
exit(1);
}
ring = a_init(fd, block_size, nblocks);
if (ring == NULL) {
fprintf(stderr, "a_init faild\n");
exit(1);
}
while (a_read(ring, buf, sizeof(buf), offset) > 0) {
offset += sizeof(buf);
if (++i % 5 == 0) {
offset -= 2*sizeof(buf);
}
}
printf("async: %.3f MByte/sec\n", 1.0e-6 * offset / end_timer());
a_close(ring);
}
static void sync_test(const char *fname, int rsize)
{
int fd;
off_t offset=0;
char buf[rsize];
int i=0;
offset=0;
start_timer();
fd = open(fname, O_RDONLY);
if (fd == -1) {
perror(fname);
exit(1);
}
while (pread(fd, buf, sizeof(buf), offset) > 0) {
offset += sizeof(buf);
if (++i % 5 == 0) {
offset -= 2*sizeof(buf);
}
}
printf("sync : %.3f MByte/sec\n", 1.0e-6 * offset / end_timer());
close(fd);
}
static void usage(void)
{
printf("Usage: aiocache [options] <filename>\n");
printf(
"Options:\n" \
" -b block_size\n" \
" -n nblocks\n" \
" -r readsize\n");
}
int main(int argc, const char *argv[])
{
const char *fname;
int opt;
int block_size = 1024*1024;
int nblocks = 100;
int rsize = 32*1024;
while ((opt = getopt(argc, argv, "b:n:r:")) != -1) {
switch (opt) {
case 'b':
block_size = strtol(optarg, NULL, 0);
break;
case 'n':
nblocks = strtol(optarg, NULL, 0);
break;
case 'r':
rsize = strtol(optarg, NULL, 0);
break;
default:
printf("Invalid option '%c'\n", opt);
usage();
exit(1);
}
}
argv += optind;
argc -= optind;
if (argc < 1) {
usage();
exit(1);
}
fname = argv[0];
printf("Testing with block_size=%d nblocks=%d rsize=%d\n",
block_size,nblocks,rsize);
async_test(fname, block_size, nblocks, rsize);
sync_test(fname, rsize);
async_test(fname, block_size, nblocks, rsize);
sync_test(fname, rsize);
return 0;
}