httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marc Slemko <ma...@znep.com>
Subject FYI: usage of new AIO calls (fwd)
Date Tue, 02 Dec 1997 06:34:20 GMT
Very cool.

If I were only writing for FreeBSD (well, or the other systems that
support these calls; they are POSIX; what else implements them right now?)
I would love playing with the aio stuff.

So, how does this fit into Dean's process model?  <g> (guess I should read
it to be sure he doesn't mention it, but I prefer to study right now...) 

---------- Forwarded message ----------
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


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);
				}
			}
		}
	}
}


Mime
View raw message