--- IO-AIO/AIO.xs 2006/10/24 03:40:25 1.68 +++ IO-AIO/AIO.xs 2006/10/26 16:28:33 1.79 @@ -1,7 +1,11 @@ -#if __linux +/* solaris */ +#define _POSIX_PTHREAD_SEMANTICS 1 + +#if __linux && !defined(_GNU_SOURCE) # define _GNU_SOURCE #endif +/* just in case */ #define _REENTRANT 1 #include @@ -46,6 +50,11 @@ # define NAME_MAX 4096 #endif +#ifndef PTHREAD_STACK_MIN +/* care for broken platforms, e.g. windows */ +# define PTHREAD_STACK_MIN 16384 +#endif + #if __ia64 # define STACKSIZE 65536 #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ @@ -54,16 +63,29 @@ # define STACKSIZE 16384 #endif +/* wether word reads are potentially non-atomic. + * this is conservatice, likely most arches this runs + * on have atomic word read/writes. + */ +#ifndef WORDREAD_UNSAFE +# if __i386 || __x86_64 +# define WORDREAD_UNSAFE 0 +# else +# define WORDREAD_UNSAFE 1 +# endif +#endif + /* buffer size for various temporary buffers */ #define AIO_BUFSIZE 65536 #define dBUF \ - char *aio_buf = malloc (AIO_BUFSIZE); \ + char *aio_buf; \ + LOCK (wrklock); \ + self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ + UNLOCK (wrklock); \ if (!aio_buf) \ return -1; -#define fBUF free (aio_buf) - enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, @@ -75,7 +97,7 @@ REQ_READDIR, REQ_LINK, REQ_SYMLINK, REQ_GROUP, REQ_NOP, - REQ_SLEEP, + REQ_BUSY, }; #define AIO_REQ_KLASS "IO::AIO::REQ" @@ -124,9 +146,9 @@ static int next_pri = DEFAULT_PRI + PRI_BIAS; -static int started, wanted; -static volatile int nreqs; -static int max_outstanding = 1<<30; +static unsigned int started, wanted; +static volatile unsigned int nreqs, nready, npending; +static volatile unsigned int max_outstanding = 0xffffffff; static int respipe [2]; #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) @@ -135,6 +157,49 @@ # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER #endif +#define LOCK(mutex) pthread_mutex_lock (&(mutex)) +#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) + +/* worker threads management */ +static pthread_mutex_t wrklock = AIO_MUTEX_INIT; + +typedef struct worker { + /* locked by wrklock */ + struct worker *prev, *next; + + pthread_t tid; + + /* locked by reslock, reqlock or wrklock */ + aio_req req; /* currently processed request */ + void *dbuf; + DIR *dirp; +} worker; + +static worker wrk_first = { &wrk_first, &wrk_first, 0 }; + +static void worker_clear (worker *wrk) +{ + if (wrk->dirp) + { + closedir (wrk->dirp); + wrk->dirp = 0; + } + + if (wrk->dbuf) + { + free (wrk->dbuf); + wrk->dbuf = 0; + } +} + +static void worker_free (worker *wrk) +{ + wrk->next->prev = wrk->prev; + wrk->prev->next = wrk->next; + + free (wrk); +} + static pthread_mutex_t reslock = AIO_MUTEX_INIT; static pthread_mutex_t reqlock = AIO_MUTEX_INIT; static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; @@ -193,8 +258,10 @@ abort (); } +static int poll_cb (int max); static void req_invoke (aio_req req); static void req_free (aio_req req); +static void req_cancel (aio_req req); /* must be called at most once */ static SV *req_sv (aio_req req, const char *klass) @@ -273,13 +340,9 @@ while (nreqs) { int size; -#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ - pthread_mutex_lock (&reslock); -#endif + if (WORDREAD_UNSAFE) LOCK (reslock); size = res_queue.size; -#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ - pthread_mutex_unlock (&reslock); -#endif + if (WORDREAD_UNSAFE) UNLOCK (reslock); if (size) return; @@ -297,8 +360,6 @@ if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) { - errno = req->errorno; - ENTER; SAVETMPS; PUSHMARK (SP); @@ -312,16 +373,18 @@ if (req->result >= 0) { + int i; char *buf = req->data2ptr; AV *av = newAV (); - while (req->result) + av_extend (av, req->result - 1); + + for (i = 0; i < req->result; ++i) { SV *sv = newSVpv (buf, 0); - av_push (av, sv); + av_store (av, i, sv); buf += SvCUR (sv) + 1; - req->result--; } rv = sv_2mortal (newRV_noinc ((SV *)av)); @@ -363,7 +426,7 @@ break; case REQ_NOP: - case REQ_SLEEP: + case REQ_BUSY: break; default: @@ -371,6 +434,7 @@ break; } + errno = req->errorno; PUTBACK; call_sv (req->callback, G_VOID | G_EVAL); @@ -415,26 +479,34 @@ SvREFCNT_dec (req->callback); Safefree (req->statdata); - if (req->type == REQ_READDIR && req->result >= 0) + if (req->type == REQ_READDIR) free (req->data2ptr); Safefree (req); } +static void req_cancel_subs (aio_req grp) +{ + aio_req sub; + + if (grp->type != REQ_GROUP) + return; + + SvREFCNT_dec (grp->fh2); + grp->fh2 = 0; + + for (sub = grp->grp_first; sub; sub = sub->grp_next) + req_cancel (sub); +} + static void req_cancel (aio_req req) { req->flags |= FLAG_CANCELLED; - if (req->type == REQ_GROUP) - { - aio_req sub; - - for (sub = req->grp_first; sub; sub = sub->grp_next) - req_cancel (sub); - } + req_cancel_subs (req); } -static int poll_cb () +static int poll_cb (int max) { dSP; int count = 0; @@ -443,55 +515,67 @@ for (;;) { - pthread_mutex_lock (&reslock); - req = reqq_shift (&res_queue); - - if (req) + while (max <= 0 || count < max) { - if (!res_queue.size) - { - /* read any signals sent by the worker threads */ - char buf [32]; - while (read (respipe [0], buf, 32) == 32) - ; - } - } + LOCK (reslock); + req = reqq_shift (&res_queue); - pthread_mutex_unlock (&reslock); + if (req) + { + --npending; - if (!req) - break; + if (!res_queue.size) + { + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) == 32) + ; + } + } - --nreqs; + UNLOCK (reslock); - if (req->type == REQ_QUIT) - started--; - else if (req->type == REQ_GROUP && req->length) - { - req->fd = 1; /* mark request as delayed */ - continue; - } - else - { - if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); + if (!req) + break; - if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) - SvREADONLY_off (req->data); + --nreqs; - if (req->statdata) + if (req->type == REQ_QUIT) + --started; + else if (req->type == REQ_GROUP && req->length) { - PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; - PL_laststatval = req->result; - PL_statcache = *(req->statdata); + req->fd = 1; /* mark request as delayed */ + continue; } + else + { + if (req->type == REQ_READ) + SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); + + if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) + SvREADONLY_off (req->data); + + if (req->statdata) + { + PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; + PL_laststatval = req->result; + PL_statcache = *(req->statdata); + } + + req_invoke (req); - req_invoke (req); + count++; + } - count++; + req_free (req); } - req_free (req); + if (nreqs <= max_outstanding) + break; + + poll_wait (); + + max = 0; } return count; @@ -502,20 +586,35 @@ static void start_thread (void) { sigset_t fullsigset, oldsigset; - pthread_t tid; pthread_attr_t attr; + worker *wrk = calloc (1, sizeof (worker)); + + if (!wrk) + croak ("unable to allocate worker thread data"); + pthread_attr_init (&attr); pthread_attr_setstacksize (&attr, STACKSIZE); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); sigfillset (&fullsigset); + + LOCK (wrklock); sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); - if (pthread_create (&tid, &attr, aio_proc, 0) == 0) - started++; + if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) + { + wrk->prev = &wrk_first; + wrk->next = wrk_first.next; + wrk_first.next->prev = wrk; + wrk_first.next = wrk; + ++started; + } + else + free (wrk); sigprocmask (SIG_SETMASK, &oldsigset, 0); + UNLOCK (wrklock); } static void req_send (aio_req req) @@ -525,21 +624,11 @@ ++nreqs; - pthread_mutex_lock (&reqlock); + LOCK (reqlock); + ++nready; reqq_push (&req_queue, req); pthread_cond_signal (&reqwait); - pthread_mutex_unlock (&reqlock); - - if (nreqs > max_outstanding) - for (;;) - { - poll_cb (); - - if (nreqs <= max_outstanding) - break; - - poll_wait (); - } + UNLOCK (reqlock); } static void end_thread (void) @@ -576,7 +665,7 @@ while (started > wanted) { poll_wait (); - poll_cb (); + poll_cb (0); } } @@ -611,12 +700,12 @@ ssize_t res; off_t ooffset; - pthread_mutex_lock (&preadwritelock); + LOCK (preadwritelock); ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = read (fd, buf, count); lseek (fd, ooffset, SEEK_SET); - pthread_mutex_unlock (&preadwritelock); + UNLOCK (preadwritelock); return res; } @@ -626,12 +715,12 @@ ssize_t res; off_t ooffset; - pthread_mutex_lock (&preadwritelock); + LOCK (preadwritelock); ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = write (fd, buf, count); lseek (fd, offset, SEEK_SET); - pthread_mutex_unlock (&preadwritelock); + UNLOCK (preadwritelock); return res; } @@ -642,9 +731,9 @@ #endif #if !HAVE_READAHEAD -# define readahead aio_readahead +# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) -static ssize_t readahead (int fd, off_t offset, size_t count) +static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) { dBUF; @@ -657,10 +746,9 @@ count -= len; } - fBUF; - errno = 0; } + #endif #if !HAVE_READDIR_R @@ -673,7 +761,7 @@ struct dirent *e; int errorno; - pthread_mutex_lock (&readdirlock); + LOCK (readdirlock); e = readdir (dirp); errorno = errno; @@ -686,7 +774,7 @@ else *res = 0; - pthread_mutex_unlock (&readdirlock); + UNLOCK (readdirlock); errno = errorno; return e ? 0 : -1; @@ -694,7 +782,7 @@ #endif /* sendfile always needs emulation */ -static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) +static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) { ssize_t res; @@ -715,7 +803,7 @@ res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); if (res < 0 && sbytes) - /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ + /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ res = sbytes; } @@ -781,15 +869,13 @@ res += cnt; count -= cnt; } - - fBUF; } return res; } /* read a full directory */ -static int scandir_ (const char *path, void **namesp) +static void scandir_ (aio_req req, worker *self) { DIR *dirp; union @@ -804,14 +890,13 @@ int res = 0; int errorno; - dirp = opendir (path); - if (!dirp) - return -1; + LOCK (wrklock); + self->dirp = dirp = opendir (req->dataptr); + self->dbuf = u = malloc (sizeof (*u)); + req->data2ptr = names = malloc (memlen); + UNLOCK (wrklock); - u = malloc (sizeof (*u)); - names = malloc (memlen); - - if (u && names) + if (dirp && u && names) for (;;) { errno = 0; @@ -831,7 +916,10 @@ while (memofs + len > memlen) { memlen *= 2; - names = realloc (names, memlen); + LOCK (wrklock); + req->data2ptr = names = realloc (names, memlen); + UNLOCK (wrklock); + if (!names) break; } @@ -841,19 +929,10 @@ } } - errorno = errno; - free (u); - closedir (dirp); - - if (errorno) - { - free (names); - errno = errorno; - res = -1; - } - - *namesp = (void *)names; - return res; + if (errno) + res = -1; + + req->result = res; } /*****************************************************************************/ @@ -862,14 +941,15 @@ { aio_req req; int type; + worker *self = (worker *)thr_arg; do { - pthread_mutex_lock (&reqlock); + LOCK (reqlock); for (;;) { - req = reqq_shift (&req_queue); + self->req = req = reqq_shift (&req_queue); if (req) break; @@ -877,7 +957,9 @@ pthread_cond_wait (&reqwait, &reqlock); } - pthread_mutex_unlock (&reqlock); + --nready; + + UNLOCK (reqlock); errno = 0; /* strictly unnecessary */ type = req->type; /* remember type for QUIT check */ @@ -889,7 +971,7 @@ case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; - case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; + case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; @@ -905,9 +987,9 @@ case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; case REQ_FSYNC: req->result = fsync (req->fd); break; - case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; + case REQ_READDIR: scandir_ (req, self); break; - case REQ_SLEEP: + case REQ_BUSY: { struct timeval tv; @@ -929,16 +1011,25 @@ req->errorno = errno; - pthread_mutex_lock (&reslock); + LOCK (reslock); + + ++npending; if (!reqq_push (&res_queue, req)) /* write a dummy byte to the pipe so fh becomes ready */ write (respipe [1], &respipe, 1); - pthread_mutex_unlock (&reslock); + self->req = 0; + worker_clear (self); + + UNLOCK (reslock); } while (type != REQ_QUIT); + LOCK (wrklock); + worker_free (self); + UNLOCK (wrklock); + return 0; } @@ -946,40 +1037,54 @@ static void atfork_prepare (void) { - pthread_mutex_lock (&reqlock); - pthread_mutex_lock (&reslock); + LOCK (wrklock); + LOCK (reqlock); + LOCK (reslock); #if !HAVE_PREADWRITE - pthread_mutex_lock (&preadwritelock); + LOCK (preadwritelock); #endif #if !HAVE_READDIR_R - pthread_mutex_lock (&readdirlock); + LOCK (readdirlock); #endif } static void atfork_parent (void) { #if !HAVE_READDIR_R - pthread_mutex_unlock (&readdirlock); + UNLOCK (readdirlock); #endif #if !HAVE_PREADWRITE - pthread_mutex_unlock (&preadwritelock); + UNLOCK (preadwritelock); #endif - pthread_mutex_unlock (&reslock); - pthread_mutex_unlock (&reqlock); + UNLOCK (reslock); + UNLOCK (reqlock); + UNLOCK (wrklock); } static void atfork_child (void) { aio_req prv; - started = 0; - while (prv = reqq_shift (&req_queue)) req_free (prv); while (prv = reqq_shift (&res_queue)) req_free (prv); - + + while (wrk_first.next != &wrk_first) + { + worker *wrk = wrk_first.next; + + if (wrk->req) + req_free (wrk->req); + + worker_clear (wrk); + worker_free (wrk); + } + + started = 0; + nreqs = 0; + close (respipe [0]); close (respipe [1]); create_pipe (); @@ -1024,22 +1129,21 @@ } void -min_parallel (nthreads) - int nthreads +min_parallel (int nthreads) PROTOTYPE: $ void -max_parallel (nthreads) - int nthreads +max_parallel (int nthreads) PROTOTYPE: $ int -max_outstanding (nreqs) - int nreqs - PROTOTYPE: $ +max_outstanding (int maxreqs) + PROTOTYPE: $ CODE: RETVAL = max_outstanding; - max_outstanding = nreqs; + max_outstanding = maxreqs; + OUTPUT: + RETVAL void aio_open (pathname,flags,mode,callback=&PL_sv_undef) @@ -1264,14 +1368,14 @@ } void -aio_sleep (delay,callback=&PL_sv_undef) +aio_busy (delay,callback=&PL_sv_undef) double delay SV * callback PPCODE: { dREQ; - req->type = REQ_SLEEP; + req->type = REQ_BUSY; req->fd = delay < 0. ? 0 : delay; req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); @@ -1304,20 +1408,27 @@ REQ_SEND; } -void -aioreq_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; +int +aioreq_pri (int pri = 0) + PROTOTYPE: ;$ + CODE: + RETVAL = next_pri - PRI_BIAS; + if (items > 0) + { + if (pri < PRI_MIN) pri = PRI_MIN; + if (pri > PRI_MAX) pri = PRI_MAX; + next_pri = pri + PRI_BIAS; + } + OUTPUT: + RETVAL void aioreq_nice (int nice = 0) - CODE: - nice = next_pri - nice; - if (nice < PRI_MIN) nice = PRI_MIN; - if (nice > PRI_MAX) nice = PRI_MAX; - next_pri = nice + PRI_BIAS; + CODE: + nice = next_pri - nice; + if (nice < PRI_MIN) nice = PRI_MIN; + if (nice > PRI_MAX) nice = PRI_MAX; + next_pri = nice + PRI_BIAS; void flush () @@ -1326,7 +1437,7 @@ while (nreqs) { poll_wait (); - poll_cb (); + poll_cb (0); } void @@ -1336,7 +1447,7 @@ if (nreqs) { poll_wait (); - poll_cb (); + poll_cb (0); } int @@ -1351,7 +1462,15 @@ poll_cb(...) PROTOTYPE: CODE: - RETVAL = poll_cb (); + RETVAL = poll_cb (0); + OUTPUT: + RETVAL + +int +poll_some(int max = 0) + PROTOTYPE: $ + CODE: + RETVAL = poll_cb (max); OUTPUT: RETVAL @@ -1370,6 +1489,26 @@ OUTPUT: RETVAL +int +nready() + PROTOTYPE: + CODE: + if (WORDREAD_UNSAFE) LOCK (reqlock); + RETVAL = nready; + if (WORDREAD_UNSAFE) UNLOCK (reqlock); + OUTPUT: + RETVAL + +int +npending() + PROTOTYPE: + CODE: + if (WORDREAD_UNSAFE) LOCK (reslock); + RETVAL = npending; + if (WORDREAD_UNSAFE) UNLOCK (reslock); + OUTPUT: + RETVAL + PROTOTYPES: DISABLE MODULE = IO::AIO PACKAGE = IO::AIO::REQ @@ -1421,11 +1560,20 @@ } void +cancel_subs (aio_req_ornot req) + CODE: + req_cancel_subs (req); + +void result (aio_req grp, ...) CODE: { int i; - AV *av = newAV (); + AV *av; + + grp->errorno = errno; + + av = newAV (); for (i = 1; i < items; ++i ) av_push (av, newSVsv (ST (i))); @@ -1435,6 +1583,11 @@ } void +errno (aio_req grp, int errorno = errno) + CODE: + grp->errorno = errorno; + +void limit (aio_req grp, int limit) CODE: grp->fd2 = limit;