1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2004 Daniel McNeil <daniel@osdl.org>
4 * 2004 Open Source Development Lab
6 * Copy file by using a async I/O state machine.
7 * 1. Start read request
8 * 2. When read completes turn it into a write request
9 * 3. When write completes decrement counter and free resources
11 * Usage: aiocp [-b blksize] -n [num_aio] [-w] [-z] [-s filesize]
12 * [-f DIRECT|TRUNC|CREAT|SYNC|LARGEFILE] src dest
16 * version of copy command using async I/O
17 * From: Stephen Hemminger <shemminger@osdl.org>
18 * Modified by Daniel McNeil <daniel@osdl.org> for testing aio.
19 * - added -a alignment
20 * - added -b blksize option
21 * - added -s size option
22 * - added -f open_flag option
23 * - added -w (no write) option (reads from source only)
24 * - added -n (num aio) option
25 * - added -z (zero dest) opton (writes zeros to dest only)
26 * - added -D delay_ms option
27 * - 2/2004 Marty Ridgeway (mridge@us.ibm.com) Changes to adapt to LTP
30 /* #define _GNU_SOURCE */
36 #include <sys/types.h>
38 #include <sys/param.h>
42 #include <sys/select.h>
43 #include <lustre/lustreapi.h>
46 #define AIO_BLKSIZE (64*1024)
49 static int aio_blksize = AIO_BLKSIZE;
50 static int aio_maxio = AIO_MAXIO;
52 static int busy; /* # of I/O's in flight */
53 static int tocopy; /* # of blocks left to copy */
54 static int srcfd; /* source fd */
55 static int dstfd = -1; /* destination file descriptor */
56 static const char *dstname;
57 static const char *srcname;
58 static int source_open_flag = O_RDONLY; /* open flags on source file */
59 static int dest_open_flag = O_WRONLY; /* open flags on dest file */
60 static int no_write; /* do not write */
61 static int zero; /* write zero's only */
64 static int count_io_q_waits; /* how many times io_queue_wait called */
66 struct iocb **iocb_free; /* array of pointers to iocb */
67 int iocb_free_count; /* current free count */
68 int alignment = 512; /* buffer alignment */
70 struct timeval delay; /* delay between I/O */
72 static int init_iocb(int n, int iosize)
77 iocb_free = malloc(n * sizeof(struct iocb *));
81 for (i = 0; i < n; i++) {
82 iocb_free[i] = (struct iocb *) malloc(sizeof(struct iocb));
85 if (posix_memalign(&buf, alignment, iosize))
88 printf("buf allocated at 0x%p, align:%d\n",
93 * We are writing zero's to dstfd
95 memset(buf, 0, iosize);
97 io_prep_pread(iocb_free[i], -1, buf, iosize, 0);
103 static struct iocb *alloc_iocb()
105 if (!iocb_free_count)
107 return iocb_free[--iocb_free_count];
110 static void free_iocb(struct iocb *io)
112 iocb_free[iocb_free_count++] = io;
116 * io_wait_run() - wait for an io_event and then call the callback.
118 static int io_wait_run(io_context_t ctx, struct timespec *to)
120 struct io_event events[aio_maxio];
125 * get up to aio_maxio events at a time.
127 ret = n = io_getevents(ctx, 1, aio_maxio, events, to);
130 * Call the callback functions for each event.
132 for (ep = events; n-- > 0; ep++) {
133 io_callback_t cb = (io_callback_t)ep->data;
134 struct iocb *iocb = (struct iocb *)ep->obj;
136 cb(ctx, iocb, ep->res, ep->res2);
141 /* Fatal error handler */
142 static void io_error(const char *func, int rc)
145 fprintf(stderr, "%s: %s\n", func, strerror(-rc));
147 fprintf(stderr, "%s: error %d\n", func, rc);
151 if (dstname && dest_open_flag & O_CREAT)
157 * Write complete callback.
158 * Adjust counts and free resources
160 static void wr_done(io_context_t ctx, struct iocb *iocb, long res, long res2)
163 io_error("aio write", res2);
165 if (res != iocb->u.c.nbytes) {
167 "write missed bytes at %llu expected %lu got %ld\n",
168 iocb->u.c.offset, iocb->u.c.nbytes, res);
175 fprintf(stderr, "w");
179 * Read complete callback.
180 * Change read iocb into a write iocb and start it.
182 static void rd_done(io_context_t ctx, struct iocb *iocb, long res, long res2)
184 /* library needs accessors to look at iocb? */
185 int iosize = iocb->u.c.nbytes;
186 char *buf = iocb->u.c.buf;
187 off_t offset = iocb->u.c.offset;
190 io_error("aio read", res2);
193 "read missed bytes at %llu expected %lu got %ld\n",
194 iocb->u.c.offset, iocb->u.c.nbytes, res);
198 /* turn read into write */
204 io_prep_pwrite(iocb, dstfd, buf, iosize, offset);
205 io_set_callback(iocb, wr_done);
206 res = io_submit(ctx, 1, &iocb);
208 io_error("io_submit write", res);
211 fprintf(stderr, "r");
213 printf("%d", iosize);
216 static void usage(void)
219 "Usage: aiocp [-a align] [-s size] [-b blksize] [-n num_io] [-f open_flag] SOURCE DEST\n"
220 "This copies from SOURCE to DEST using AIO.\n\n"
221 "Usage: aiocp [options] -w SOURCE\n"
222 "This does sequential AIO reads (no writes).\n\n"
223 "Usage: aiocp [options] -z DEST\n"
224 "This does sequential AIO writes of zeros.\n");
229 * Scale value by kilo, mega, or giga.
231 static long long scale_by_kmg(long long value, char scale)
255 int main(int argc, char *const *argv)
258 off_t length = 0, offset = 0;
262 while ((c = getopt(argc, argv, "a:b:df:n:s:wzD:")) != -1) {
266 case 'a': /* alignment of data buffer */
267 alignment = strtol(optarg, &endp, 0);
268 alignment = (long)scale_by_kmg((long long)alignment,
271 case 'f': /* use these open flags */
272 if (strcmp(optarg, "LARGEFILE") == 0 ||
273 strcmp(optarg, "O_LARGEFILE") == 0) {
274 source_open_flag |= O_LARGEFILE;
275 dest_open_flag |= O_LARGEFILE;
276 } else if (strcmp(optarg, "TRUNC") == 0 ||
277 strcmp(optarg, "O_TRUNC") == 0) {
278 dest_open_flag |= O_TRUNC;
279 } else if (strcmp(optarg, "SYNC") == 0 ||
280 strcmp(optarg, "O_SYNC") == 0) {
281 dest_open_flag |= O_SYNC | O_NONBLOCK;
282 } else if (strcmp(optarg, "DIRECT") == 0 ||
283 strcmp(optarg, "O_DIRECT") == 0) {
284 source_open_flag |= O_DIRECT;
285 dest_open_flag |= O_DIRECT;
286 } else if (strncmp(optarg, "CREAT", 5) == 0 ||
287 strncmp(optarg, "O_CREAT", 5) == 0) {
288 dest_open_flag |= O_CREAT;
295 delay.tv_usec = atoi(optarg);
297 case 'b': /* block size */
298 aio_blksize = strtol(optarg, &endp, 0);
299 aio_blksize = (long)scale_by_kmg(
300 (long long)aio_blksize, *endp);
302 case 'n': /* num io */
303 aio_maxio = strtol(optarg, &endp, 0);
305 case 's': /* size to transfer */
306 length = strtoll(optarg, &endp, 0);
307 length = scale_by_kmg(length, *endp);
309 case 'w': /* no write */
312 case 'z': /* write zero's */
328 source_open_flag |= O_DIRECT;
329 dest_open_flag |= O_DIRECT;
332 srcname = "junkdata";
337 srcfd = open(srcname = *argv, source_open_flag);
340 srcfd = open(srcname, source_open_flag);
348 if (fstat(srcfd, &st) < 0) {
358 * We are either copying or writing zeros to dstname
363 dstfd = open(dstname = *argv, dest_open_flag, 0666);
366 dstfd = open(dstname, dest_open_flag, 0666);
374 * get size of dest, if we are zeroing it.
375 * TODO: handle devices.
377 if (fstat(dstfd, &st) < 0) {
386 /* initialize state machine */
387 memset(&myctx, 0, sizeof(myctx));
388 io_queue_init(aio_maxio, &myctx);
389 tocopy = howmany(length, aio_blksize);
390 if (init_iocb(aio_maxio, aio_blksize) < 0) {
391 fprintf(stderr, "Error allocating the I/O buffers\n");
397 /* Submit as many reads as once as possible upto aio_maxio */
398 int n = MIN(MIN(aio_maxio - busy, aio_maxio),
399 howmany(length - offset, aio_blksize));
403 for (i = 0; i < n; i++) {
404 struct iocb *io = alloc_iocb();
405 int iosize = MIN(length - offset, aio_blksize);
409 * We are writing zero's to dstfd
411 io_prep_pwrite(io, dstfd, io->u.c.buf,
413 io_set_callback(io, wr_done);
415 io_prep_pread(io, srcfd, io->u.c.buf,
417 io_set_callback(io, rd_done);
423 rc = io_submit(myctx, n, ioq);
425 io_error("io_submit", rc);
429 printf("io_submit(%d) busy:%d\n", n, busy);
431 struct timeval t = delay;
432 (void) select(0, 0, 0, 0, &t);
437 * We have submitted all the I/O requests.
438 * Wait for at least one to complete and call the callbacks.
441 rc = io_wait_run(myctx, 0);
443 io_error("io_wait_run", rc);
446 printf("io_wait_run: rc == %d\n", rc);
447 printf("busy:%d aio_maxio:%d tocopy:%d\n",
448 busy, aio_maxio, tocopy);
461 * [alanm@toolbox ~/MOT3]$ ../taio -d kernel-source-2.4.8-0.4g.ppc.rpm abc
462 * rrrrrrrrrrrrrrrwwwrwrrwwrrwrwwrrwrwrwwrrwrwrrrrwwrwwwrrwrrrwwwwwwwwwwwwwwwww
463 * rrrrrrrrrrrrrrwwwrrwrwrwrwrrwwwwwwwwwwwwwwrrrrrrrrrrrrrrrrrrwwwwrwrwwrwrwrwr
464 * wrrrrrrrwwwwwwwwwwwwwrrrwrrrwrrwrwwwwwwwwwwrrrrwwrwrrrrrrrrrrrwwwwwwwwwwwrww
465 * wwwrrrrrrrrwwrrrwwrwrwrwwwrrrrrrrwwwrrwwwrrwrwwwwwwwwrrrrrrrwwwrrrrrrrwwwwww
466 * wwwwwwwrwrrrrrrrrwrrwrrwrrwrwrrrwrrrwrrrwrwwwwwwwwwwwwwwwwwwrrrwwwrrrrrrrrrr
467 * rrwrrrrrrwrrwwwwwwwwwwwwwwwwrwwwrrwrwwrrrrrrrrrrrrrrrrrrrwwwwwwwwwwwwwwwwwww
468 * rrrrrwrrwrwrwrrwrrrwwwwwwwwrrrrwrrrwrwwrwrrrwrrwrrrrwwwwwwwrwrwwwwrwwrrrwrrr
469 * rrrwwwwwwwrrrrwwrrrrrrrrrrrrwrwrrrrwwwwwwwwwwwwwwrwrrrrwwwwrwrrrrwrwwwrrrwww
470 * rwwrrrrrrrwrrrrrrrrrrrrwwwwrrrwwwrwrrwwwwwwwwwwwwwwwwwwwwwrrrrrrrwwwwwwwrw