--- IO-AIO/AIO.xs 2006/10/22 10:33:19 1.48 +++ IO-AIO/AIO.xs 2006/10/24 00:26:32 1.65 @@ -1,4 +1,9 @@ +#if __linux +# define _GNU_SOURCE +#endif + #define _REENTRANT 1 + #include #include "EXTERN.h" @@ -43,10 +48,22 @@ #if __ia64 # define STACKSIZE 65536 +#elif __i386 || __x86_64 /* 16k is unreasonably high :( */ +# define STACKSIZE PTHREAD_STACK_MIN #else -# define STACKSIZE 8192 +# define STACKSIZE 16384 #endif +/* buffer size for various temporary buffers */ +#define AIO_BUFSIZE 65536 + +#define dBUF \ + char *aio_buf = malloc (AIO_BUFSIZE); \ + if (!aio_buf) \ + return -1; + +#define fBUF free (aio_buf) + enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, @@ -57,8 +74,8 @@ REQ_UNLINK, REQ_RMDIR, REQ_RENAME, REQ_READDIR, REQ_LINK, REQ_SYMLINK, + REQ_GROUP, REQ_NOP, REQ_SLEEP, - REQ_GROUP, }; #define AIO_REQ_KLASS "IO::AIO::REQ" @@ -66,12 +83,8 @@ typedef struct aio_cb { - struct aio_cb *grp, *grp_prev, *grp_next; - struct aio_cb *volatile next; - SV *self; /* the perl counterpart of this request, if any */ - SV *data, *callback; SV *fh, *fh2; void *dataptr, *data2ptr; @@ -80,55 +93,145 @@ size_t length; ssize_t result; + STRLEN dataoffset; int type; int fd, fd2; int errorno; - STRLEN dataoffset; mode_t mode; /* open */ - unsigned char cancelled; + + unsigned char flags; + unsigned char pri; + + SV *self; /* the perl counterpart of this request, if any */ + struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; } aio_cb; +enum { + FLAG_CANCELLED = 0x01, +}; + typedef aio_cb *aio_req; typedef aio_cb *aio_req_ornot; +enum { + PRI_MIN = -4, + PRI_MAX = 4, + + DEFAULT_PRI = 0, + PRI_BIAS = -PRI_MIN, +}; + +static int next_pri = DEFAULT_PRI + PRI_BIAS; + static int started, wanted; static volatile int nreqs; static int max_outstanding = 1<<30; static int respipe [2]; -static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; +#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) +# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP +#else +# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER +#endif + +static pthread_mutex_t reslock = AIO_MUTEX_INIT; +static pthread_mutex_t reqlock = AIO_MUTEX_INIT; static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; static volatile aio_req reqs, reqe; /* queue start, queue end */ static volatile aio_req ress, rese; /* queue start, queue end */ +static void req_invoke (aio_req req); static void req_free (aio_req req); /* must be called at most once */ static SV *req_sv (aio_req req, const char *klass) { - req->self = (SV *)newHV (); - sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); + if (!req->self) + { + req->self = (SV *)newHV (); + sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); + } return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); } static aio_req SvAIO_REQ (SV *sv) { + MAGIC *mg; + if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) croak ("object of class " AIO_REQ_KLASS " expected"); - MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); + mg = mg_find (SvRV (sv), PERL_MAGIC_ext); return mg ? (aio_req)mg->mg_ptr : 0; } +static void aio_grp_feed (aio_req grp) +{ + while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) + { + int old_len = grp->length; + + if (grp->fh2 && SvOK (grp->fh2)) + { + dSP; + + ENTER; + SAVETMPS; + PUSHMARK (SP); + XPUSHs (req_sv (grp, AIO_GRP_KLASS)); + PUTBACK; + call_sv (grp->fh2, G_VOID | G_EVAL); + SPAGAIN; + FREETMPS; + LEAVE; + } + + /* stop if no progress has been made */ + if (old_len == grp->length) + { + SvREFCNT_dec (grp->fh2); + grp->fh2 = 0; + break; + } + } +} + +static void aio_grp_dec (aio_req grp) +{ + --grp->length; + + /* call feeder, if applicable */ + aio_grp_feed (grp); + + /* finish, if done */ + if (!grp->length && grp->fd) + { + req_invoke (grp); + req_free (grp); + } +} + static void poll_wait () { - if (nreqs && !ress) + fd_set rfd; + + while (nreqs) { - fd_set rfd; + aio_req req; +#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ + pthread_mutex_lock (&reslock); +#endif + req = ress; +#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ + pthread_mutex_unlock (&reslock); +#endif + + if (req) + return; + FD_ZERO(&rfd); FD_SET(respipe [0], &rfd); @@ -141,12 +244,13 @@ dSP; int errorno = errno; - if (req->cancelled || !SvOK (req->callback)) + if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) return; errno = req->errorno; ENTER; + SAVETMPS; PUSHMARK (SP); EXTEND (SP, 1); @@ -195,6 +299,8 @@ break; case REQ_GROUP: + req->fd = 2; /* mark group as finished */ + if (req->data) { int i; @@ -206,6 +312,7 @@ } break; + case REQ_NOP: case REQ_SLEEP: break; @@ -219,15 +326,16 @@ call_sv (req->callback, G_VOID | G_EVAL); SPAGAIN; + FREETMPS; + LEAVE; + + errno = errorno; + if (SvTRUE (ERRSV)) { req_free (req); croak (0); } - - LEAVE; - - errno = errorno; } static void req_free (aio_req req) @@ -237,14 +345,13 @@ aio_req grp = req->grp; /* unlink request */ - req->grp_next->grp_prev = req->grp_prev; - req->grp_prev->grp_next = req->grp_next; + if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; + if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; - if (grp->grp_next == grp && grp->fd) - { - req_invoke (grp); - req_free (grp); - } + if (grp->grp_first == req) + grp->grp_first = req->grp_next; + + aio_grp_dec (grp); } if (req->self) @@ -253,20 +360,11 @@ SvREFCNT_dec (req->self); } - if (req->data) - SvREFCNT_dec (req->data); - - if (req->fh) - SvREFCNT_dec (req->fh); - - if (req->fh2) - SvREFCNT_dec (req->fh2); - - if (req->statdata) - Safefree (req->statdata); - - if (req->callback) - SvREFCNT_dec (req->callback); + SvREFCNT_dec (req->data); + SvREFCNT_dec (req->fh); + SvREFCNT_dec (req->fh2); + SvREFCNT_dec (req->callback); + Safefree (req->statdata); if (req->type == REQ_READDIR && req->result >= 0) free (req->data2ptr); @@ -276,13 +374,13 @@ static void req_cancel (aio_req req) { - req->cancelled = 1; + req->flags |= FLAG_CANCELLED; if (req->type == REQ_GROUP) { aio_req sub; - for (sub = req->grp_next; sub != req; sub = sub->grp_next) + for (sub = req->grp_first; sub; sub = sub->grp_next) req_cancel (sub); } } @@ -319,11 +417,11 @@ if (!req) break; - nreqs--; + --nreqs; if (req->type == REQ_QUIT) started--; - else if (req->type == REQ_GROUP && req->grp_next != req) + else if (req->type == REQ_GROUP && req->length) { req->fd = 1; /* mark request as delayed */ continue; @@ -380,7 +478,7 @@ while (started < wanted && nreqs >= started) start_thread (); - nreqs++; + ++nreqs; pthread_mutex_lock (&reqlock); @@ -510,17 +608,19 @@ static ssize_t readahead (int fd, off_t offset, size_t count) { - char readahead_buf[4096]; + dBUF; while (count > 0) { - size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); + size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; - pread (fd, readahead_buf, len, offset); + pread (fd, aio_buf, len, offset); offset += len; count -= len; } + fBUF; + errno = 0; } #endif @@ -615,14 +715,15 @@ ) { /* emulate sendfile. this is a major pain in the ass */ - char buf[4096]; + dBUF; + res = 0; while (count) { ssize_t cnt; - cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); + cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); if (cnt <= 0) { @@ -630,7 +731,7 @@ break; } - cnt = write (ofd, buf, cnt); + cnt = write (ofd, aio_buf, cnt); if (cnt <= 0) { @@ -642,6 +743,8 @@ res += cnt; count -= cnt; } + + fBUF; } return res; @@ -650,12 +753,12 @@ /* read a full directory */ static int scandir_ (const char *path, void **namesp) { - DIR *dirp = opendir (path); + DIR *dirp; union { struct dirent d; char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; - } u; + } *u; struct dirent *entp; char *name, *names; int memlen = 4096; @@ -663,40 +766,45 @@ int res = 0; int errorno; + dirp = opendir (path); if (!dirp) return -1; + u = malloc (sizeof (*u)); names = malloc (memlen); - for (;;) - { - errno = 0, readdir_r (dirp, &u.d, &entp); + if (u && names) + for (;;) + { + errno = 0; + readdir_r (dirp, &u->d, &entp); - if (!entp) - break; + if (!entp) + break; - name = entp->d_name; + name = entp->d_name; - if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) - { - int len = strlen (name) + 1; + if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) + { + int len = strlen (name) + 1; - res++; + res++; - while (memofs + len > memlen) - { - memlen *= 2; - names = realloc (names, memlen); - if (!names) - break; - } + while (memofs + len > memlen) + { + memlen *= 2; + names = realloc (names, memlen); + if (!names) + break; + } - memcpy (names + memofs, name, len); - memofs += len; - } - } + memcpy (names + memofs, name, len); + memofs += len; + } + } errorno = errno; + free (u); closedir (dirp); if (errorno) @@ -740,9 +848,10 @@ pthread_mutex_unlock (&reqlock); errno = 0; /* strictly unnecessary */ + type = req->type; /* remember type for QUIT check */ - if (!req->cancelled) - switch (req->type) + if (!(req->flags & FLAG_CANCELLED)) + switch (type) { case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; @@ -776,6 +885,8 @@ req->result = select (0, 0, 0, 0, &tv); } + case REQ_GROUP: + case REQ_NOP: case REQ_QUIT: break; @@ -869,6 +980,8 @@ #define dREQ \ aio_req req; \ + int req_pri = next_pri; \ + next_pri = DEFAULT_PRI + PRI_BIAS; \ \ if (SvOK (callback) && !SvROK (callback)) \ croak ("callback must be undef or of reference type"); \ @@ -877,7 +990,8 @@ if (!req) \ croak ("out of memory during aio_req allocation"); \ \ - req->callback = newSVsv (callback) + req->callback = newSVsv (callback); \ + req->pri = req_pri #define REQ_SEND \ req_send (req); \ @@ -1162,15 +1276,37 @@ PPCODE: { dREQ; - req->type = REQ_GROUP; - req->grp_next = req; - req->grp_prev = req; + req->type = REQ_GROUP; req_send (req); + XPUSHs (req_sv (req, AIO_GRP_KLASS)); } void +aio_nop (callback=&PL_sv_undef) + SV * callback + PPCODE: +{ + dREQ; + + req->type = REQ_NOP; + + REQ_SEND; +} + +#if 0 + +void +aio_pri (int pri = DEFAULT_PRI) + CODE: + if (pri < PRI_MIN) pri = PRI_MIN; + if (pri > PRI_MAX) pri = PRI_MAX; + next_pri = pri + PRI_BIAS; + +#endif + +void flush () PROTOTYPE: CODE: @@ -1231,6 +1367,12 @@ CODE: req_cancel (req); +void +cb (aio_req_ornot req, SV *callback=&PL_sv_undef) + CODE: + SvREFCNT_dec (req->callback); + req->callback = newSVsv (callback); + MODULE = IO::AIO PACKAGE = IO::AIO::GRP void @@ -1238,39 +1380,64 @@ PPCODE: { int i; + aio_req req; + + if (grp->fd == 2) + croak ("cannot add requests to IO::AIO::GRP after the group finished"); for (i = 1; i < items; ++i ) { if (GIMME_V != G_VOID) XPUSHs (sv_2mortal (newSVsv (ST (i)))); - aio_req req = SvAIO_REQ (ST (i)); + req = SvAIO_REQ (ST (i)); if (req) { - req->grp_prev = grp; - req->grp_next = grp->grp_next; - grp->grp_next->grp_prev = req; - grp->grp_next = req; - + ++grp->length; req->grp = grp; + + req->grp_prev = 0; + req->grp_next = grp->grp_first; + + if (grp->grp_first) + grp->grp_first->grp_prev = req; + + grp->grp_first = req; } } } void result (aio_req grp, ...) - CODE: + CODE: { - int i; - AV *av = newAV (); + int i; + AV *av = newAV (); - if (grp->data) - SvREFCNT_dec (grp->data); - - for (i = 1; i < items; ++i ) + for (i = 1; i < items; ++i ) av_push (av, newSVsv (ST (i))); + SvREFCNT_dec (grp->data); grp->data = (SV *)av; } +void +feed_limit (aio_req grp, int limit) + CODE: + grp->fd2 = limit; + aio_grp_feed (grp); + +void +feed (aio_req grp, SV *callback=&PL_sv_undef) + CODE: +{ + SvREFCNT_dec (grp->fh2); + grp->fh2 = newSVsv (callback); + + if (grp->fd2 <= 0) + grp->fd2 = 2; + + aio_grp_feed (grp); +} +