--- IO-AIO/AIO.xs 2005/08/07 03:26:10 1.21 +++ IO-AIO/AIO.xs 2005/08/23 01:16:50 1.35 @@ -7,6 +7,8 @@ #include "autoconf/config.h" +#include + #include #include @@ -15,11 +17,20 @@ #include #include -#include - -typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ -typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ -typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ +#if HAVE_SENDFILE +# if __linux +# include +# elif __freebsd +# include +# include +# elif __hpux +# include +# elif __solaris /* not yet */ +# include +# else +# error sendfile support requested but not available +# endif +#endif #if __ia64 # define STACKSIZE 65536 @@ -31,8 +42,11 @@ REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_READAHEAD, - REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, + REQ_SENDFILE, + REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_FSYNC, REQ_FDATASYNC, + REQ_UNLINK, REQ_RMDIR, + REQ_SYMLINK, }; typedef struct aio_cb { @@ -40,14 +54,15 @@ int type; - int fd; + int fd, fd2; off_t offset; size_t length; ssize_t result; mode_t mode; /* open */ int errorno; - SV *data, *callback, *fh; - void *dataptr; + SV *data, *callback; + SV *fh, *fh2; + void *dataptr, *data2ptr; STRLEN dataoffset; Stat_t *statdata; @@ -55,7 +70,7 @@ typedef aio_cb *aio_req; -static int started; +static int started, wanted; static volatile int nreqs; static int max_outstanding = 1<<30; static int respipe [2]; @@ -67,6 +82,26 @@ static volatile aio_req reqs, reqe; /* queue start, queue end */ static volatile aio_req ress, rese; /* queue start, queue end */ +static void free_req (aio_req req) +{ + 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); + + Safefree (req); +} + static void poll_wait () { @@ -85,24 +120,34 @@ { dSP; int count = 0; - aio_req req, prv; + int do_croak = 0; + aio_req req; - pthread_mutex_lock (&reslock); + for (;;) + { + pthread_mutex_lock (&reslock); + req = ress; - { - /* read any signals sent by the worker threads */ - char buf [32]; - while (read (respipe [0], buf, 32) > 0) - ; - } + if (req) + { + ress = req->next; - req = ress; - ress = rese = 0; + if (!ress) + { + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) == 32) + ; - pthread_mutex_unlock (&reslock); + rese = 0; + } + } + + pthread_mutex_unlock (&reslock); + + if (!req) + break; - while (req) - { nreqs--; if (req->type == REQ_QUIT) @@ -113,22 +158,16 @@ errno = req->errorno; if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset - + req->result > 0 ? req->result : 0); + SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); - if (req->data) - SvREFCNT_dec (req->data); + if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) + SvREADONLY_off (req->data); - if (req->fh) - SvREFCNT_dec (req->fh); - - if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) + if (req->statdata) { PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; PL_laststatval = req->result; PL_statcache = *(req->statdata); - - Safefree (req->statdata); } ENTER; @@ -155,22 +194,21 @@ PUTBACK; call_sv (req->callback, G_VOID | G_EVAL); SPAGAIN; + + if (SvTRUE (ERRSV)) + { + free_req (req); + croak (0); + } } LEAVE; - - if (req->callback) - SvREFCNT_dec (req->callback); errno = errorno; count++; } - prv = req; - req = req->next; - Safefree (prv); - - /* TODO: croak on errors? */ + free_req (req); } return count; @@ -201,6 +239,9 @@ static void send_req (aio_req req) { + while (started < wanted && nreqs >= started) + start_thread (); + nreqs++; pthread_mutex_lock (&reqlock); @@ -218,23 +259,114 @@ pthread_cond_signal (&reqwait); pthread_mutex_unlock (&reqlock); - while (nreqs > max_outstanding) - { - poll_wait (); - poll_cb (); - } + if (nreqs > max_outstanding) + for (;;) + { + poll_cb (); + + if (nreqs <= max_outstanding) + break; + + poll_wait (); + } } static void end_thread (void) { aio_req req; - New (0, req, 1, aio_cb); + Newz (0, req, 1, aio_cb); req->type = REQ_QUIT; send_req (req); } +static void min_parallel (int nthreads) +{ + if (wanted < nthreads) + wanted = nthreads; +} + +static void max_parallel (int nthreads) +{ + int cur = started; + + if (wanted > nthreads) + wanted = nthreads; + + while (cur > wanted) + { + end_thread (); + cur--; + } + + while (started > wanted) + { + poll_wait (); + poll_cb (); + } +} + +static void create_pipe () +{ + if (pipe (respipe)) + croak ("unable to initialize result pipe"); + + if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); + + if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); +} + +static void atfork_prepare (void) +{ + pthread_mutex_lock (&reqlock); + pthread_mutex_lock (&reslock); +} + +static void atfork_parent (void) +{ + pthread_mutex_unlock (&reslock); + pthread_mutex_unlock (&reqlock); +} + +static void atfork_child (void) +{ + aio_req prv; + + started = 0; + + while (reqs) + { + prv = reqs; + reqs = prv->next; + free_req (prv); + } + + reqs = reqe = 0; + + while (ress) + { + prv = ress; + ress = prv->next; + free_req (prv); + } + + ress = rese = 0; + + close (respipe [0]); + close (respipe [1]); + create_pipe (); + + atfork_parent (); +} + +/* currently noops */ +#define LOCK_FD(fd) do { } while (0) +#define UNLOCK_FD(fd) do { } while (0) + +/*****************************************************************************/ /* work around various missing functions */ #if !HAVE_PREADWRITE @@ -254,12 +386,14 @@ ssize_t res; off_t ooffset; - pthread_mutex_lock (&iolock); + LOCK_FD (fd); + pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */ ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = read (fd, buf, count); lseek (fd, ooffset, SEEK_SET); pthread_mutex_unlock (&iolock); + UNLOCK_FD (d); return res; } @@ -270,12 +404,14 @@ ssize_t res; off_t ooffset; - pthread_mutex_lock (&iolock); + LOCK_FD (fd); + pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */ ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = write (fd, buf, count); lseek (fd, offset, SEEK_SET); pthread_mutex_unlock (&iolock); + UNLOCK_FD (d); return res; } @@ -306,6 +442,110 @@ } #endif +/* sendfile always needs emulation */ +static ssize_t +sendfile_ (int ofd, int ifd, off_t offset, size_t count) +{ + ssize_t res; + + if (!count) + return 0; + + LOCK_FD (ofd); + +#if HAVE_SENDFILE +# if __linux + res = sendfile (ofd, ifd, &offset, count); + +# elif __freebsd + /* + * Of course, the freebsd sendfile is a dire hack with no thoughts + * wasted on making it similar to other I/O functions. + */ + { + off_t sbytes; + 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 */ + res = sbytes; + } + +# elif __hpux + res = sendfile (ofd, ifd, offset, count, 0, 0); + +# elif __solaris + { + struct sendfilevec vec; + size_t sbytes; + + vec.sfv_fd = ifd; + vec.sfv_flag = 0; + vec.sfv_off = offset; + vec.sfv_len = count; + + res = sendfilev (ofd, &vec, 1, &sbytes); + + if (res < 0 && sbytes) + res = sbytes; + } + +# else + res = -1; + errno = ENOSYS; +# endif +#endif + + if (res < 0 + && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK +#if __solaris + || errno == EAFNOSUPPORT || errno == EPROTOTYPE +#endif + ) + ) + { + /* emulate sendfile. this is a major pain in the ass */ + char *buf = malloc (4096); + res = 0; + + for (;;) + { + ssize_t cnt; + + cnt = pread (ifd, buf, 4096, offset); + + if (cnt <= 0) + { + if (cnt && !res) res = -1; + break; + } + + cnt = write (ofd, buf, cnt); + + if (cnt <= 0) + { + if (cnt && !res) res = -1; + break; + } + + offset += cnt; + res += cnt; + } + + { + int errorno = errno; + free (buf); + errno = errorno; + } + } + + UNLOCK_FD (ofd); + + return res; +} + +/*****************************************************************************/ + static void * aio_proc (void *thr_arg) { @@ -344,6 +584,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_STAT: req->result = stat (req->dataptr, req->statdata); break; case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; @@ -352,6 +593,8 @@ case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; case REQ_CLOSE: req->result = close (req->fd); break; case REQ_UNLINK: req->result = unlink (req->dataptr); break; + case REQ_RMDIR: req->result = rmdir (req->dataptr); break; + case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; case REQ_FSYNC: req->result = fsync (req->fd); break; @@ -390,49 +633,37 @@ return 0; } +#define dREQ \ + aio_req req; \ + \ + if (SvOK (callback) && !SvROK (callback)) \ + croak ("clalback must be undef or of reference type"); \ + \ + Newz (0, req, 1, aio_cb); \ + if (!req) \ + croak ("out of memory during aio_req allocation"); \ + \ + req->callback = newSVsv (callback); + MODULE = IO::AIO PACKAGE = IO::AIO PROTOTYPES: ENABLE BOOT: { - if (pipe (respipe)) - croak ("unable to initialize result pipe"); - - if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); - - if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); + create_pipe (); + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); } void min_parallel(nthreads) int nthreads PROTOTYPE: $ - CODE: - while (nthreads > started) - start_thread (); void max_parallel(nthreads) int nthreads PROTOTYPE: $ - CODE: -{ - int cur = started; - while (cur > nthreads) - { - end_thread (); - cur--; - } - - while (started > nthreads) - { - poll_wait (); - poll_cb (); - } -} int max_outstanding(nreqs) @@ -451,19 +682,13 @@ PROTOTYPE: $$$;$ CODE: { - aio_req req; - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; req->type = REQ_OPEN; req->data = newSVsv (pathname); - req->dataptr = SvPV_nolen (req->data); + req->dataptr = SvPVbyte_nolen (req->data); req->fd = flags; req->mode = mode; - req->callback = SvREFCNT_inc (callback); send_req (req); } @@ -479,17 +704,11 @@ aio_fdatasync = REQ_FDATASYNC CODE: { - aio_req req; - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; req->type = ix; req->fh = newSVsv (fh); req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); - req->callback = SvREFCNT_inc (callback); send_req (req); } @@ -498,9 +717,9 @@ aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) SV * fh UV offset - IV length + UV length SV * data - IV dataoffset + UV dataoffset SV * callback ALIAS: aio_read = REQ_READ @@ -536,20 +755,47 @@ if (length < 0) croak ("length must not be negative"); - Newz (0, req, 1, aio_cb); + { + dREQ; + + req->type = ix; + req->fh = newSVsv (fh); + req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) + : IoOFP (sv_2io (fh))); + req->offset = offset; + req->length = length; + req->data = SvREFCNT_inc (data); + req->dataptr = (char *)svptr + dataoffset; - if (!req) - croak ("out of memory during aio_req allocation"); + if (!SvREADONLY (data)) + { + SvREADONLY_on (data); + req->data2ptr = (void *)data; + } - req->type = ix; - req->fh = newSVsv (fh); - req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) - : IoOFP (sv_2io (fh))); - req->offset = offset; + send_req (req); + } +} + +void +aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) + SV * out_fh + SV * in_fh + UV in_offset + UV length + SV * callback + PROTOTYPE: $$$$;$ + CODE: +{ + dREQ; + + req->type = REQ_SENDFILE; + req->fh = newSVsv (out_fh); + req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); + req->fh2 = newSVsv (in_fh); + req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); + req->offset = in_offset; req->length = length; - req->data = SvREFCNT_inc (data); - req->dataptr = (char *)svptr + dataoffset; - req->callback = SvREFCNT_inc (callback); send_req (req); } @@ -563,22 +809,13 @@ PROTOTYPE: $$$;$ CODE: { - aio_req req; - - if (length < 0) - croak ("length must not be negative"); - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; req->type = REQ_READAHEAD; req->fh = newSVsv (fh); req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); req->offset = offset; req->length = length; - req->callback = SvREFCNT_inc (callback); send_req (req); } @@ -592,23 +829,20 @@ aio_lstat = REQ_LSTAT CODE: { - aio_req req; - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; New (0, req->statdata, 1, Stat_t); - if (!req->statdata) - croak ("out of memory during aio_req->statdata allocation"); + { + free_req (req); + croak ("out of memory during aio_req->statdata allocation"); + } if (SvPOK (fh_or_path)) { req->type = ix; req->data = newSVsv (fh_or_path); - req->dataptr = SvPV_nolen (req->data); + req->dataptr = SvPVbyte_nolen (req->data); } else { @@ -617,8 +851,6 @@ req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); } - req->callback = SvREFCNT_inc (callback); - send_req (req); } @@ -626,19 +858,34 @@ aio_unlink(pathname,callback=&PL_sv_undef) SV * pathname SV * callback + ALIAS: + aio_unlink = REQ_UNLINK + aio_rmdir = REQ_RMDIR CODE: { - aio_req req; + dREQ; - Newz (0, req, 1, aio_cb); + req->type = ix; + req->data = newSVsv (pathname); + req->dataptr = SvPVbyte_nolen (req->data); - if (!req) - croak ("out of memory during aio_req allocation"); + send_req (req); +} + +void +aio_symlink(oldpath,newpath,callback=&PL_sv_undef) + SV * oldpath + SV * newpath + SV * callback + CODE: +{ + dREQ; - req->type = REQ_UNLINK; - req->data = newSVsv (pathname); - req->dataptr = SvPV_nolen (req->data); - req->callback = SvREFCNT_inc (callback); + req->type = REQ_SYMLINK; + req->fh = newSVsv (oldpath); + req->data2ptr = SvPVbyte_nolen (req->fh); + req->data = newSVsv (newpath); + req->dataptr = SvPVbyte_nolen (req->data); send_req (req); }