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
36 #include <sys/types.h>
38 #include <sys/param.h>
42 #include <sys/select.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 time 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 expect %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 missing bytes expect %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)
252 int main(int argc, char *const *argv)
255 off_t length = 0, offset = 0;
259 while ((c = getopt(argc, argv, "a:b:df:n:s:wzD:")) != -1) {
263 case 'a': /* alignment of data buffer */
264 alignment = strtol(optarg, &endp, 0);
265 alignment = (long)scale_by_kmg((long long)alignment,
268 case 'f': /* use these open flags */
269 if (strcmp(optarg, "LARGEFILE") == 0 ||
270 strcmp(optarg, "O_LARGEFILE") == 0) {
271 source_open_flag |= O_LARGEFILE;
272 dest_open_flag |= O_LARGEFILE;
273 } else if (strcmp(optarg, "TRUNC") == 0 ||
274 strcmp(optarg, "O_TRUNC") == 0) {
275 dest_open_flag |= O_TRUNC;
276 } else if (strcmp(optarg, "SYNC") == 0 ||
277 strcmp(optarg, "O_SYNC") == 0) {
278 dest_open_flag |= O_SYNC | O_NONBLOCK;
279 } else if (strcmp(optarg, "DIRECT") == 0 ||
280 strcmp(optarg, "O_DIRECT") == 0) {
281 source_open_flag |= O_DIRECT;
282 dest_open_flag |= O_DIRECT;
283 } else if (strncmp(optarg, "CREAT", 5) == 0 ||
284 strncmp(optarg, "O_CREAT", 5) == 0) {
285 dest_open_flag |= O_CREAT;
292 delay.tv_usec = atoi(optarg);
294 case 'b': /* block size */
295 aio_blksize = strtol(optarg, &endp, 0);
296 aio_blksize = (long)scale_by_kmg(
297 (long long)aio_blksize, *endp);
299 case 'n': /* num io */
300 aio_maxio = strtol(optarg, &endp, 0);
302 case 's': /* size to transfer */
303 length = strtoll(optarg, &endp, 0);
304 length = scale_by_kmg(length, *endp);
306 case 'w': /* no write */
309 case 'z': /* write zero's */
325 source_open_flag |= O_DIRECT;
326 dest_open_flag |= O_DIRECT;
329 srcname = "junkdata";
334 srcfd = open(srcname = *argv, source_open_flag);
337 srcfd = open(srcname, source_open_flag);
345 if (fstat(srcfd, &st) < 0) {
355 * We are either copying or writing zeros to dstname
360 dstfd = open(dstname = *argv, dest_open_flag, 0666);
363 dstfd = open(dstname, dest_open_flag, 0666);
371 * get size of dest, if we are zeroing it.
372 * TODO: handle devices.
374 if (fstat(dstfd, &st) < 0) {
383 /* initialize state machine */
384 memset(&myctx, 0, sizeof(myctx));
385 io_queue_init(aio_maxio, &myctx);
386 tocopy = howmany(length, aio_blksize);
387 if (init_iocb(aio_maxio, aio_blksize) < 0) {
388 fprintf(stderr, "Error allocating the i/o buffers\n");
394 /* Submit as many reads as once as possible upto aio_maxio */
395 int n = MIN(MIN(aio_maxio - busy, aio_maxio),
396 howmany(length - offset, aio_blksize));
400 for (i = 0; i < n; i++) {
401 struct iocb *io = alloc_iocb();
402 int iosize = MIN(length - offset, aio_blksize);
406 * We are writing zero's to dstfd
408 io_prep_pwrite(io, dstfd, io->u.c.buf,
410 io_set_callback(io, wr_done);
412 io_prep_pread(io, srcfd, io->u.c.buf,
414 io_set_callback(io, rd_done);
420 rc = io_submit(myctx, n, ioq);
422 io_error("io_submit", rc);
426 printf("io_submit(%d) busy:%d\n", n, busy);
428 struct timeval t = delay;
429 (void) select(0, 0, 0, 0, &t);
434 * We have submitted all the i/o requests.
435 * Wait for at least one to complete and call the callbacks.
438 rc = io_wait_run(myctx, 0);
440 io_error("io_wait_run", rc);
443 printf("io_wait_run: rc == %d\n", rc);
444 printf("busy:%d aio_maxio:%d tocopy:%d\n",
445 busy, aio_maxio, tocopy);
458 * [alanm@toolbox ~/MOT3]$ ../taio -d kernel-source-2.4.8-0.4g.ppc.rpm abc
459 * rrrrrrrrrrrrrrrwwwrwrrwwrrwrwwrrwrwrwwrrwrwrrrrwwrwwwrrwrrrwwwwwwwwwwwwwwwww
460 * rrrrrrrrrrrrrrwwwrrwrwrwrwrrwwwwwwwwwwwwwwrrrrrrrrrrrrrrrrrrwwwwrwrwwrwrwrwr
461 * wrrrrrrrwwwwwwwwwwwwwrrrwrrrwrrwrwwwwwwwwwwrrrrwwrwrrrrrrrrrrrwwwwwwwwwwwrww
462 * wwwrrrrrrrrwwrrrwwrwrwrwwwrrrrrrrwwwrrwwwrrwrwwwwwwwwrrrrrrrwwwrrrrrrrwwwwww
463 * wwwwwwwrwrrrrrrrrwrrwrrwrrwrwrrrwrrrwrrrwrwwwwwwwwwwwwwwwwwwrrrwwwrrrrrrrrrr
464 * rrwrrrrrrwrrwwwwwwwwwwwwwwwwrwwwrrwrwwrrrrrrrrrrrrrrrrrrrwwwwwwwwwwwwwwwwwww
465 * rrrrrwrrwrwrwrrwrrrwwwwwwwwrrrrwrrrwrwwrwrrrwrrwrrrrwwwwwwwrwrwwwwrwwrrrwrrr
466 * rrrwwwwwwwrrrrwwrrrrrrrrrrrrwrwrrrrwwwwwwwwwwwwwwrwrrrrwwwwrwrrrrwrwwwrrrwww
467 * rwwrrrrrrrwrrrrrrrrrrrrwwwwrrrwwwrwrrwwwwwwwwwwwwwwwwwwwwwrrrrrrrwwwwwwwrw