Date: Mon, 1 Dec 1997 14:26:34 -0500 (EST) From: "John S. Dyson" <toor@dyson.iquest.net> To: current@freebsd.org Subject: FYI: usage of new AIO calls Message-ID: <199712011926.OAA00376@dyson.iquest.net>
next in thread | raw e-mail | index | archive | help
For fun (and actually as a result of a comment by Justin Gibbs), I have put together a small program that could maybe be used for streaming tape drives using the new AIO code. Note that if you try this, it must be used only on current kernels on/after 19:00 GMT, Dec 1. This code is NOT pretty, but does show example usage. The code will likely have problems on SMP kernels (unless you are writing to a VCHR device.) -- John dyson@freebsd.org jdyson@nc.com #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/fcntl.h> #include <sys/time.h> #include <sys/errno.h> #include <sys/aio.h> #include <sys/syscall.h> #define NBUFS 16 #define BUFSZ (1024*64) struct aiocb *olist[NBUFS]; void *bufaddrs[NBUFS]; int activeflags[NBUFS]; int nactive; int eof; int main( int argc, char *argv[]) { int i; int fd1, fd2; off_t curptr; int nread; int rtval; int errval; if (argc != 2) { fprintf(stderr, "usage: fcp destfile"); exit(0); } fd1 = 0; fd2 = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if (fd2 == -1) { perror(argv[1]); exit(1); } for(i=0;i<NBUFS;i++) { olist[i] = malloc(sizeof (struct aiocb)); bzero(olist[i], sizeof (struct aiocb)); bufaddrs[i] = malloc(BUFSZ); activeflags[i] = 0; } nactive = 0; curptr = 0; eof = 0; while ((eof == 0) || (nactive > 0)) { if ((eof && (nactive > 0)) || (nactive == NBUFS)) { for (i = 0; i < NBUFS; i++ ) { if (activeflags[i]) { errval = aio_suspend(&olist[i], 1, NULL); if (errval) { perror("aio_suspend"); exit(1); } break; } } } if (nactive > 0) { for (i = 0; i < NBUFS; i++) { if (activeflags[i] == 0) continue; errval = aio_error(olist[i]); if (errval) { if (errval == EINPROGRESS) continue; perror("aio_error1"); exit(1); } activeflags[i] = 0; --nactive; rtval = aio_return(olist[i]); if (rtval != olist[i]->aio_nbytes) { perror("buffer not fully written"); exit(1); } } } for (i = 0; ((eof == 0) && (nactive < NBUFS) && (i < NBUFS)); i++) { if (activeflags[i] == 0) { nread = read( fd1, bufaddrs[i], BUFSZ); if (nread == 0) { eof = 1; break; } if (nread == -1) { perror("read"); exit(1); } olist[i]->aio_offset = curptr; curptr += nread; olist[i]->aio_nbytes = nread; olist[i]->aio_fildes = fd2; olist[i]->aio_buf = bufaddrs[i]; errval = aio_write(olist[i]); activeflags[i] = 1; nactive++; if (errval) { perror("aio_write"); exit(1); } } } } }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199712011926.OAA00376>