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 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 struct iocb *alloc_iocb()
105 if (!iocb_free_count)
107 return iocb_free[--iocb_free_count];
110 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 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) {
166 fprintf(stderr, "write missed bytes expected %lu got %ld\n",
167 iocb->u.c.nbytes, res2);
174 fprintf(stderr, "w");
178 * Read complete callback.
179 * Change read iocb into a write iocb and start it.
181 static void rd_done(io_context_t ctx, struct iocb *iocb, long res, long res2)
183 /* library needs accessors to look at iocb? */
184 int iosize = iocb->u.c.nbytes;
185 char *buf = iocb->u.c.buf;
186 off_t offset = iocb->u.c.offset;
189 io_error("aio read", res2);
191 fprintf(stderr, "read missed bytes expected %lu got %ld\n",
192 iocb->u.c.nbytes, res);
197 /* turn read into write */
203 io_prep_pwrite(iocb, dstfd, buf, iosize, offset);
204 io_set_callback(iocb, wr_done);
205 res = io_submit(ctx, 1, &iocb);
207 io_error("io_submit write", res);
210 fprintf(stderr, "r");
212 printf("%d", iosize);
218 "Usage: aiocp [-a align] [-s size] [-b blksize] [-n num_io] [-f open_flag] SOURCE DEST\n"
219 "This copies from SOURCE to DEST using AIO.\n\n"
220 "Usage: aiocp [options] -w SOURCE\n"
221 "This does sequential AIO reads (no writes).\n\n"
222 "Usage: aiocp [options] -z DEST\n"
223 "This does sequential AIO writes of zeros.\n");
228 * Scale value by kilo, mega, or giga.
230 long long scale_by_kmg(long long value, char scale)
254 int main(int argc, char *const *argv)
257 off_t length = 0, offset = 0;
261 while ((c = getopt(argc, argv, "a:b:df:n:s:wzD:")) != -1) {
265 case 'a': /* alignment of data buffer */
266 alignment = strtol(optarg, &endp, 0);
267 alignment = (long)scale_by_kmg((long long)alignment,
270 case 'f': /* use these open flags */
271 if (strcmp(optarg, "LARGEFILE") == 0 ||
272 strcmp(optarg, "O_LARGEFILE") == 0) {
273 source_open_flag |= O_LARGEFILE;
274 dest_open_flag |= O_LARGEFILE;
275 } else if (strcmp(optarg, "TRUNC") == 0 ||
276 strcmp(optarg, "O_TRUNC") == 0) {
277 dest_open_flag |= O_TRUNC;
278 } else if (strcmp(optarg, "SYNC") == 0 ||
279 strcmp(optarg, "O_SYNC") == 0) {
280 dest_open_flag |= O_SYNC | O_NONBLOCK;
281 } else if (strcmp(optarg, "DIRECT") == 0 ||
282 strcmp(optarg, "O_DIRECT") == 0) {
283 source_open_flag |= O_DIRECT;
284 dest_open_flag |= O_DIRECT;
285 } else if (strncmp(optarg, "CREAT", 5) == 0 ||
286 strncmp(optarg, "O_CREAT", 5) == 0) {
287 dest_open_flag |= O_CREAT;
294 delay.tv_usec = atoi(optarg);
296 case 'b': /* block size */
297 aio_blksize = strtol(optarg, &endp, 0);
298 aio_blksize = (long)scale_by_kmg(
299 (long long)aio_blksize, *endp);
301 case 'n': /* num io */
302 aio_maxio = strtol(optarg, &endp, 0);
304 case 's': /* size to transfer */
305 length = strtoll(optarg, &endp, 0);
306 length = scale_by_kmg(length, *endp);
308 case 'w': /* no write */
311 case 'z': /* write zero's */
327 source_open_flag |= O_DIRECT;
328 dest_open_flag |= O_DIRECT;
331 srcname = "junkdata";
336 srcfd = open(srcname = *argv, source_open_flag);
339 srcfd = open(srcname, source_open_flag);
347 if (fstat(srcfd, &st) < 0) {
357 * We are either copying or writing zeros to dstname
362 dstfd = open(dstname = *argv, dest_open_flag, 0666);
365 dstfd = open(dstname, dest_open_flag, 0666);
373 * get size of dest, if we are zeroing it.
374 * TODO: handle devices.
376 if (fstat(dstfd, &st) < 0) {
385 /* initialize state machine */
386 memset(&myctx, 0, sizeof(myctx));
387 io_queue_init(aio_maxio, &myctx);
388 tocopy = howmany(length, aio_blksize);
389 if (init_iocb(aio_maxio, aio_blksize) < 0) {
390 fprintf(stderr, "Error allocating the I/O buffers\n");
396 /* Submit as many reads as once as possible upto aio_maxio */
397 int n = MIN(MIN(aio_maxio - busy, aio_maxio),
398 howmany(length - offset, aio_blksize));
402 for (i = 0; i < n; i++) {
403 struct iocb *io = alloc_iocb();
404 int iosize = MIN(length - offset, aio_blksize);
408 * We are writing zero's to dstfd
410 io_prep_pwrite(io, dstfd, io->u.c.buf,
412 io_set_callback(io, wr_done);
414 io_prep_pread(io, srcfd, io->u.c.buf,
416 io_set_callback(io, rd_done);
422 rc = io_submit(myctx, n, ioq);
424 io_error("io_submit", rc);
428 printf("io_submit(%d) busy:%d\n", n, busy);
430 struct timeval t = delay;
431 (void) select(0, 0, 0, 0, &t);
436 * We have submitted all the I/O requests.
437 * Wait for at least one to complete and call the callbacks.
440 rc = io_wait_run(myctx, 0);
442 io_error("io_wait_run", rc);
445 printf("io_wait_run: rc == %d\n", rc);
446 printf("busy:%d aio_maxio:%d tocopy:%d\n",
447 busy, aio_maxio, tocopy);
460 * [alanm@toolbox ~/MOT3]$ ../taio -d kernel-source-2.4.8-0.4g.ppc.rpm abc
461 * rrrrrrrrrrrrrrrwwwrwrrwwrrwrwwrrwrwrwwrrwrwrrrrwwrwwwrrwrrrwwwwwwwwwwwwwwwww
462 * rrrrrrrrrrrrrrwwwrrwrwrwrwrrwwwwwwwwwwwwwwrrrrrrrrrrrrrrrrrrwwwwrwrwwrwrwrwr
463 * wrrrrrrrwwwwwwwwwwwwwrrrwrrrwrrwrwwwwwwwwwwrrrrwwrwrrrrrrrrrrrwwwwwwwwwwwrww
464 * wwwrrrrrrrrwwrrrwwrwrwrwwwrrrrrrrwwwrrwwwrrwrwwwwwwwwrrrrrrrwwwrrrrrrrwwwwww
465 * wwwwwwwrwrrrrrrrrwrrwrrwrrwrwrrrwrrrwrrrwrwwwwwwwwwwwwwwwwwwrrrwwwrrrrrrrrrr
466 * rrwrrrrrrwrrwwwwwwwwwwwwwwwwrwwwrrwrwwrrrrrrrrrrrrrrrrrrrwwwwwwwwwwwwwwwwwww
467 * rrrrrwrrwrwrwrrwrrrwwwwwwwwrrrrwrrrwrwwrwrrrwrrwrrrrwwwwwwwrwrwwwwrwwrrrwrrr
468 * rrrwwwwwwwrrrrwwrrrrrrrrrrrrwrwrrrrwwwwwwwwwwwwwwrwrrrrwwwwrwrrrrwrwwwrrrwww
469 * rwwrrrrrrrwrrrrrrrrrrrrwwwwrrrwwwrwrrwwwwwwwwwwwwwwwwwwwwwrrrrrrrwwwwwwwrw