--- IO-AIO/AIO.xs 2010/01/10 23:44:02 1.163 +++ IO-AIO/AIO.xs 2012/07/27 17:54:40 1.218 @@ -13,84 +13,79 @@ #include #include #include -#include #include #include #include -#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES +#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES # include #endif +#if __linux__ +# include +# ifdef FS_IOC_FIEMAP +# include +# include +# define HAVE_FIEMAP 1 +# endif +#endif + /* perl namespace pollution */ #undef VERSION -#ifdef _WIN32 - -# define EIO_STRUCT_DIRENT Direntry_t -# undef malloc -# undef free - -// perl overrides all those nice win32 functions -# undef open -# undef read -# undef write -# undef send -# undef recv -# undef stat -# undef fstat -# define lstat stat -# undef truncate -# undef ftruncate -# undef open -# undef close -# undef unlink -# undef rmdir -# undef rename -# undef lseek - -# define chown(a,b,c) (errno = ENOSYS, -1) -# define fchown(a,b,c) (errno = ENOSYS, -1) -# define fchmod(a,b) (errno = ENOSYS, -1) -# define symlink(a,b) (errno = ENOSYS, -1) -# define readlink(a,b,c) (errno = ENOSYS, -1) -# define mknod(a,b,c) (errno = ENOSYS, -1) -# define truncate(a,b) (errno = ENOSYS, -1) -# define ftruncate(fd,o) chsize ((fd), (o)) -# define fsync(fd) _commit (fd) -# define opendir(fd) (errno = ENOSYS, 0) -# define readdir(fd) (errno = ENOSYS, -1) -# define closedir(fd) (errno = ENOSYS, -1) -# define mkdir(a,b) mkdir (a) - -#else - -# include -# include -# include -# include -# include -# define EIO_STRUCT_DIRENT struct dirent - -#endif - /* perl stupidly overrides readdir and maybe others */ /* with thread-unsafe versions, imagine that :( */ #undef readdir #undef opendir #undef closedir -#define EIO_STRUCT_STAT Stat_t +#ifdef _WIN32 + + // perl overrides all those nice libc functions + + #undef malloc + #undef free + #undef open + #undef read + #undef write + #undef send + #undef recv + #undef stat + #undef lstat + #undef fstat + #undef truncate + #undef ftruncate + #undef open + #undef link + #undef close + #undef unlink + #undef mkdir + #undef rmdir + #undef rename + #undef lseek + #undef opendir + #undef readdir + #undef closedir + #undef chmod + #undef fchmod + #undef dup + #undef dup2 + #undef abort + #undef pipe + + #define EIO_STRUCT_STAT struct _stati64 + #define EIO_STRUCT_STATI64 -/* use NV for 32 bit perls as it allows larger offsets */ -#if IVSIZE >= 8 -# define VAL64 IV -# define SvVAL64 SvIV -# define newSVval64 newSViv #else -# define VAL64 NV -# define SvVAL64 SvNV -# define newSVval64 newSVnv + + #include + #include + #include + #include + #include + + #define EIO_STRUCT_STAT Stat_t + #endif /*****************************************************************************/ @@ -110,74 +105,33 @@ typedef int aio_rfd; /* read file desriptor */ typedef int aio_wfd; /* write file descriptor */ -static HV *aio_stash, *aio_req_stash, *aio_grp_stash; +static HV *aio_stash, *aio_req_stash, *aio_grp_stash, *aio_wd_stash; #define EIO_REQ_MEMBERS \ SV *callback; \ SV *sv1, *sv2; \ + SV *sv3, *sv4; \ STRLEN stroffset; \ SV *self; #define EIO_NO_WRAPPERS 1 +#include "libeio/config.h" #include "libeio/eio.h" -#ifndef POSIX_FADV_NORMAL -# define POSIX_FADV_NORMAL 0 -# define NO_FADVISE 1 -#endif -#ifndef POSIX_FADV_SEQUENTIAL -# define POSIX_FADV_SEQUENTIAL 0 -#endif -#ifndef POSIX_FADV_RANDOM -# define POSIX_FADV_RANDOM 0 -#endif -#ifndef POSIX_FADV_NOREUSE -# define POSIX_FADV_NOREUSE 0 -#endif -#ifndef POSIX_FADV_WILLNEED -# define POSIX_FADV_WILLNEED 0 -#endif -#ifndef POSIX_FADV_DONTNEED -# define POSIX_FADV_DONTNEED 0 -#endif +static int req_invoke (eio_req *req); +#define EIO_FINISH(req) req_invoke (req) +static void req_destroy (eio_req *grp); +#define EIO_DESTROY(req) req_destroy (req) -#ifndef ST_NODEV -# define ST_NODEV 0 -#endif -#ifndef ST_NOEXEC -# define ST_NOEXEC 0 -#endif -#ifndef ST_SYNCHRONOUS -# define ST_SYNCHRONOUS 0 -#endif -#ifndef ST_MANDLOCK -# define ST_MANDLOCK 0 -#endif -#ifndef ST_WRITE -# define ST_WRITE 0 -#endif -#ifndef ST_APPEND -# define ST_APPEND 0 -#endif -#ifndef ST_IMMUTABLE -# define ST_IMMUTABLE 0 -#endif -#ifndef ST_NOATIME -# define ST_NOATIME 0 -#endif -#ifndef ST_NODIRATIME -# define ST_NODIRATIME 0 -#endif -#ifndef ST_RELATIME -# define ST_RELATIME 0 -#endif +#include "libeio/eio.c" -#ifndef MCL_CURRENT -# define MCL_CURRENT 0 +#if !HAVE_POSIX_FADVISE +# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */ #endif -#ifndef MCL_FUTURE -# define MCL_FUTURE 0 + +#if !HAVE_POSIX_MADVISE +# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ #endif #ifndef MAP_ANONYMOUS @@ -187,39 +141,94 @@ # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ # endif #endif -#ifndef MAP_HUGETLB -# define MAP_HUGETLB 0 -#endif -#ifndef MAP_LOCKED -# define MAP_LOCKED 0 -#endif -#ifndef MAP_NORESERVE -# define MAP_NORESERVE 0 + +/* defines all sorts of constants to 0 unless they are already defined */ +#include "def0.h" + +#ifndef makedev +# define makedev(maj,min) (((maj) << 8) | (min)) #endif -#ifndef MAP_POPULATE -# define MAP_POPULATE 0 +#ifndef major +# define major(dev) ((dev) >> 8) #endif -#ifndef MAP_NONBLOCK -# define MAP_NONBLOCK 0 +#ifndef minor +# define minor(dev) ((dev) & 0xff) #endif #ifndef PAGESIZE # define PAGESIZE sysconf (_SC_PAGESIZE) #endif -static int req_invoke (eio_req *req); -#define EIO_FINISH(req) req_invoke (req) -static void req_destroy (eio_req *grp); -#define EIO_DESTROY(req) req_destroy (req) +/*****************************************************************************/ + +static void +fiemap (eio_req *req) +{ + req->result = -1; + +#if HAVE_FIEMAP + int count = req->int3; + + /* heuristic: first try with 72 extents if we don't know how many, */ + /* as most files have (hopefully) fewer than this many extents */ + /* in fact, most should have <= 2, so the 72 below is probably overkill */ + if (count < 0) + count = 72; /* for what it's worth, 72 extents fit nicely into 4kb */ + + for (;;) + { + struct fiemap *fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) + return; + + req->ptr1 = fiemap; + req->flags |= EIO_FLAG_PTR1_FREE; + + fiemap->fm_start = req->offs; + fiemap->fm_length = req->size; + fiemap->fm_flags = req->int2; + fiemap->fm_extent_count = count; + + if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) + return; + + if (req->int3 >= 0) + break; /* when not autosizing we are done */ + + if (!fiemap->fm_mapped_extents) + break; /* if file has no segments, we are done */ + + if (fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST) + break; /* autosizing successful, we are done */ + + fiemap->fm_flags = req->int2; + fiemap->fm_extent_count = 0; + + if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap)) + return; + + count = fiemap->fm_mapped_extents; + + free (fiemap); + } + + req->result = 0; + +#else + errno = ENOSYS; +#endif +} + +/*****************************************************************************/ enum { FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ }; -#include "libeio/eio.c" - typedef eio_req *aio_req; typedef eio_req *aio_req_ornot; +typedef eio_wd aio_wd; static SV *on_next_submit; static int next_pri = EIO_PRI_DEFAULT; @@ -230,20 +239,23 @@ static void req_destroy (aio_req req); static void req_cancel (aio_req req); -static void want_poll (void) +static void +want_poll (void) { /* write a dummy byte to the pipe so fh becomes ready */ s_epipe_signal (&respipe); } -static void done_poll (void) +static void +done_poll (void) { /* read any signals sent by the worker threads */ s_epipe_drain (&respipe); } /* must be called at most once */ -static SV *req_sv (aio_req req, HV *stash) +static SV * +req_sv (aio_req req, HV *stash) { if (!req->self) { @@ -254,11 +266,19 @@ return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); } -static aio_req SvAIO_REQ (SV *sv) +static SV * +newSVaio_wd (aio_wd wd) +{ + return sv_bless (newRV_noinc (newSViv ((IV)wd)), aio_wd_stash); +} + +static aio_req +SvAIO_REQ (SV *sv) { MAGIC *mg; if (!SvROK (sv) + /* for speed reasons, we do not verify that SvROK actually has a stash ptr */ || (SvSTASH (SvRV (sv)) != aio_grp_stash && SvSTASH (SvRV (sv)) != aio_req_stash && !sv_derived_from (sv, "IO::AIO::REQ"))) @@ -269,7 +289,19 @@ return mg ? (aio_req)mg->mg_ptr : 0; } -static void aio_grp_feed (aio_req grp) +static aio_wd +SvAIO_WD (SV *sv) +{ + if (!SvROK (sv) + || SvTYPE (SvRV (sv)) != SVt_PVMG + || SvSTASH (SvRV (sv)) != aio_wd_stash) + croak ("IO::AIO: expected a working directory object as returned by aio_wd"); + + return (aio_wd)(long)SvIVX (SvRV (sv)); +} + +static void +aio_grp_feed (aio_req grp) { if (grp->sv2 && SvOK (grp->sv2)) { @@ -287,7 +319,8 @@ } } -static void req_submit (eio_req *req) +static void +req_submit (eio_req *req) { eio_submit (req); @@ -304,22 +337,42 @@ } } -static int req_invoke (eio_req *req) +static int +req_invoke (eio_req *req) { - dSP; - if (req->flags & FLAG_SV2_RO_OFF) SvREADONLY_off (req->sv2); if (!EIO_CANCELLED (req) && req->callback) { + dSP; + static SV *sv_result_cache; /* caches the result integer SV */ + SV *sv_result; + ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 1); + /* do not recreate the result IV from scratch each time */ + if (expect_true (sv_result_cache)) + { + sv_result = sv_result_cache; sv_result_cache = 0; + SvIV_set (sv_result, req->result); + SvIOK_only (sv_result); + } + else + { + sv_result = newSViv (req->result); + SvREADONLY_on (sv_result); + } + switch (req->type) { + case EIO_WD_OPEN: + PUSHs (req->result ? &PL_sv_undef : sv_2mortal (newSVaio_wd (req->wd))); + break; + case EIO_READDIR: { SV *rv = &PL_sv_undef; @@ -417,6 +470,7 @@ { SV *rv = &PL_sv_undef; +#ifndef _WIN32 if (req->result >= 0) { EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); @@ -436,6 +490,7 @@ hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); } +#endif PUSHs (rv); } @@ -461,6 +516,7 @@ break; case EIO_READLINK: + case EIO_REALPATH: if (req->result > 0) PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); break; @@ -468,10 +524,17 @@ case EIO_STAT: case EIO_LSTAT: case EIO_FSTAT: - PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; - PL_laststatval = req->result; - PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); - PUSHs (sv_2mortal (newSViv (req->result))); + PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; + + if (!(PL_laststatval = req->result)) + /* if compilation fails here then perl's Stat_t is not struct _stati64 */ + PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); + + PUSHs (sv_result); + break; + + case EIO_SEEK: + PUSHs (req->result ? sv_result : sv_2mortal (newSVval64 (req->offs))); break; case EIO_READ: @@ -480,17 +543,57 @@ *SvEND (req->sv2) = 0; SvPOK_only (req->sv2); SvSETMAGIC (req->sv2); - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); } break; - case EIO_DUP2: + case EIO_CUSTOM: + if (req->feed == fiemap) + { +#if HAVE_FIEMAP + if (!req->result) + { + struct fiemap *fiemap = (struct fiemap *)req->ptr1; + + if (fiemap->fm_extent_count) + { + AV *av = newAV (); + int i; + + while (fiemap->fm_mapped_extents) + { + struct fiemap_extent *extent = &fiemap->fm_extents [--fiemap->fm_mapped_extents]; + AV *ext_av = newAV (); + + av_store (ext_av, 3, newSVuv (extent->fe_flags)); + av_store (ext_av, 2, newSVval64 (extent->fe_length)); + av_store (ext_av, 1, newSVval64 (extent->fe_physical)); + av_store (ext_av, 0, newSVval64 (extent->fe_logical)); + + av_store (av, fiemap->fm_mapped_extents, newRV_noinc ((SV *)ext_av)); + } + + PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + } + else + { + SvIV_set (sv_result, fiemap->fm_mapped_extents); + PUSHs (sv_result); + } + } +#endif + } + else + PUSHs (sv_result); + break; + + case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ if (req->result > 0) - req->result = 0; + SvIV_set (sv_result, 0); /* FALLTHROUGH */ default: - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); break; } @@ -500,6 +603,11 @@ call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); SPAGAIN; + if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) + SvREFCNT_dec (sv_result); + else + sv_result_cache = sv_result; + FREETMPS; LEAVE; @@ -509,7 +617,8 @@ return !!SvTRUE (ERRSV); } -static void req_destroy (aio_req req) +static void +req_destroy (aio_req req) { if (req->self) { @@ -519,15 +628,16 @@ SvREFCNT_dec (req->sv1); SvREFCNT_dec (req->sv2); + SvREFCNT_dec (req->sv3); + SvREFCNT_dec (req->sv4); SvREFCNT_dec (req->callback); - Safefree (req); + free (req); } -static void req_cancel_subs (aio_req grp) +static void +req_cancel_subs (aio_req grp) { - aio_req sub; - if (grp->type != EIO_GROUP) return; @@ -537,14 +647,15 @@ eio_grp_cancel (grp); } -static void +static void ecb_cold create_respipe (void) { if (s_epipe_renew (&respipe)) croak ("IO::AIO: unable to initialize result pipe"); } -static void poll_wait (void) +static void +poll_wait (void) { while (eio_nreqs ()) { @@ -563,7 +674,8 @@ } } -static int poll_cb (void) +static int +poll_cb (void) { for (;;) { @@ -579,21 +691,34 @@ } } -static void atfork_child (void) +static void ecb_cold +reinit (void) { create_respipe (); + + if (eio_init (want_poll, done_poll) < 0) + croak ("IO::AIO: unable to initialise eio library"); } /*****************************************************************************/ #if !_POSIX_MAPPED_FILES -# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) -# define munmap(addr,length) (errno = ENOSYS, -1) +# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () +# define munmap(addr,length) EIO_ENOSYS () +#endif + +#if !_POSIX_MEMORY_PROTECTION +# define mprotect(addr,len,prot) EIO_ENOSYS () +# define PROT_NONE 0 +# define PROT_WRITE 0 +# define MAP_PRIVATE 0 +# define MAP_SHARED 0 +# define MAP_FIXED 0 #endif #define MMAP_MAGIC PERL_MAGIC_ext -static int +static int ecb_cold mmap_free (pTHX_ SV *sv, MAGIC *mg) { int old_errno = errno; @@ -603,8 +728,11 @@ mg->mg_obj = 0; /* just in case */ SvREADONLY_off (sv); + + if (SvPVX (sv) != mg->mg_ptr) + croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); + SvCUR_set (sv, 0); - SvLEN_set (sv, 0); SvPVX (sv) = 0; SvOK_off (sv); @@ -624,20 +752,28 @@ return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; } +static aio_req ecb_noinline +dreq (SV *callback) +{ + SV *cb_cv; + aio_req req; + int req_pri = next_pri; + next_pri = EIO_PRI_DEFAULT; + + cb_cv = get_cb (callback); + + req = calloc (sizeof (*req), 1); + if (!req) + croak ("out of memory during eio_req allocation"); + + req->callback = SvREFCNT_inc (cb_cv); + req->pri = req_pri; + + return req; +} + #define dREQ \ - SV *cb_cv; \ - aio_req req; \ - int req_pri = next_pri; \ - next_pri = EIO_PRI_DEFAULT; \ - \ - cb_cv = get_cb (callback); \ - \ - Newz (0, req, 1, eio_req); \ - if (!req) \ - croak ("out of memory during eio_req allocation"); \ - \ - req->callback = SvREFCNT_inc (cb_cv); \ - req->pri = req_pri + aio_req req = dreq (callback); \ #define REQ_SEND \ PUTBACK; \ @@ -647,6 +783,72 @@ if (GIMME_V != G_VOID) \ XPUSHs (req_sv (req, aio_req_stash)); +ecb_inline void +req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr) +{ + if (expect_false (SvROK (path))) + { + SV *rv = SvRV (path); + SV *wdob; + + if (SvTYPE (rv) == SVt_PVAV && AvFILLp (rv) == 1) + { + path = AvARRAY (rv)[1]; + wdob = AvARRAY (rv)[0]; + + if (SvOK (wdob)) + { + *wd = SvAIO_WD (wdob); + *wdsv = SvREFCNT_inc_NN (SvRV (wdob)); + } + else + *wd = EIO_INVALID_WD; + } + else if (SvTYPE (rv) == SVt_PVMG && SvSTASH (rv) == aio_wd_stash) + { + *wd = (aio_wd)(long)SvIVX (rv); + *wdsv = SvREFCNT_inc_NN (rv); + *ptr = "."; + return; /* path set to "." */ + } + else + croak ("IO::AIO: pathname arguments must be specified as a string, an IO::AIO::WD object or a [IO::AIO::WD, path] pair"); + } + + *pathsv = newSVsv (path); + *ptr = SvPVbyte_nolen (*pathsv); +} + +static void ecb_noinline +req_set_path1 (aio_req req, SV *path) +{ + req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1); +} + +static void ecb_noinline +req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path) +{ + SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path; + + switch (SvTYPE (rv)) + { + case SVt_PVIO: + case SVt_PVLV: + case SVt_PVGV: + req->type = type_fh; + req->sv1 = newSVsv (fh_or_path); + req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); + break; + + default: + req->type = type_path; + req_set_path1 (req, fh_or_path); + break; + } +} + +XS(boot_IO__AIO) ecb_cold; + MODULE = IO::AIO PACKAGE = IO::AIO PROTOTYPES: ENABLE @@ -660,15 +862,47 @@ # define const_niv(name, value) { # name, (IV) value }, # define const_iv(name) { # name, (IV) name }, # define const_eio(name) { # name, (IV) EIO_ ## name }, - const_iv (EXDEV) + + /* you have to re-run ./gendef0 after adding/Removing any constants here */ + const_iv (ENOSYS) + const_iv (EXDEV) + const_iv (EBADR) + const_iv (O_RDONLY) const_iv (O_WRONLY) + const_iv (O_RDWR) const_iv (O_CREAT) const_iv (O_TRUNC) -#ifndef _WIN32 + const_iv (O_EXCL) + const_iv (O_APPEND) + + const_iv (O_ASYNC) + const_iv (O_DIRECT) + const_iv (O_NOATIME) + + const_iv (O_CLOEXEC) + const_iv (O_NOCTTY) + const_iv (O_NOFOLLOW) + const_iv (O_NONBLOCK) + const_iv (O_EXEC) + const_iv (O_SEARCH) + const_iv (O_DIRECTORY) + const_iv (O_DSYNC) + const_iv (O_RSYNC) + const_iv (O_SYNC) + const_iv (O_TTY_INIT) + const_iv (S_IFIFO) -#endif + const_iv (S_IFCHR) + const_iv (S_IFBLK) + const_iv (S_IFLNK) + const_iv (S_IFREG) + const_iv (S_IFDIR) + const_iv (S_IFWHT) + const_iv (S_IFSOCK) + const_iv (S_IFMT) + const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) @@ -676,6 +910,12 @@ const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) + const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) + const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) + const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) + const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) + const_iv (ST_RDONLY) const_iv (ST_NOSUID) const_iv (ST_NODEV) @@ -689,8 +929,8 @@ const_iv (ST_NODIRATIME) const_iv (ST_RELATIME) - const_iv (PROT_EXEC) const_iv (PROT_NONE) + const_iv (PROT_EXEC) const_iv (PROT_READ) const_iv (PROT_WRITE) @@ -706,8 +946,36 @@ const_iv (MAP_POPULATE) const_iv (MAP_NONBLOCK) - const_iv (MCL_FUTURE) - const_iv (MCL_CURRENT) + const_iv (FIEMAP_FLAG_SYNC) + const_iv (FIEMAP_FLAG_XATTR) + const_iv (FIEMAP_FLAGS_COMPAT) + const_iv (FIEMAP_EXTENT_LAST) + const_iv (FIEMAP_EXTENT_UNKNOWN) + const_iv (FIEMAP_EXTENT_DELALLOC) + const_iv (FIEMAP_EXTENT_ENCODED) + const_iv (FIEMAP_EXTENT_DATA_ENCRYPTED) + const_iv (FIEMAP_EXTENT_NOT_ALIGNED) + const_iv (FIEMAP_EXTENT_DATA_INLINE) + const_iv (FIEMAP_EXTENT_DATA_TAIL) + const_iv (FIEMAP_EXTENT_UNWRITTEN) + const_iv (FIEMAP_EXTENT_MERGED) + const_iv (FIEMAP_EXTENT_SHARED) + + const_iv (SPLICE_F_MOVE) + const_iv (SPLICE_F_NONBLOCK) + const_iv (SPLICE_F_MORE) + const_iv (SPLICE_F_GIFT) + + const_iv (SEEK_DATA) + const_iv (SEEK_HOLE) + + /* libeio constants */ + const_eio (SEEK_SET) + const_eio (SEEK_CUR) + const_eio (SEEK_END) + + const_eio (MCL_FUTURE) + const_eio (MCL_CURRENT) const_eio (MS_ASYNC) const_eio (MS_INVALIDATE) @@ -719,6 +987,9 @@ const_eio (SYNC_FILE_RANGE_WRITE) const_eio (SYNC_FILE_RANGE_WAIT_AFTER) + const_eio (FALLOC_FL_KEEP_SIZE) + const_eio (FALLOC_FL_PUNCH_HOLE) + const_eio (READDIR_DENTS) const_eio (READDIR_DIRS_FIRST) const_eio (READDIR_STAT_ORDER) @@ -738,23 +1009,22 @@ aio_stash = gv_stashpv ("IO::AIO" , 1); aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); + aio_wd_stash = gv_stashpv ("IO::AIO::WD" , 1); - for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) - newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); + for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) + newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); - create_respipe (); - - if (eio_init (want_poll, done_poll) < 0) - croak ("IO::AIO: unable to initialise eio library"); - - /* atfork child called in fifo order, so before eio's handler */ - X_THREAD_ATFORK (0, 0, atfork_child); + reinit (); } void -max_poll_reqs (int nreqs) +reinit () + PROTOTYPE: + +void +max_poll_reqs (unsigned int nreqs) PROTOTYPE: $ CODE: eio_set_max_poll_reqs (nreqs); @@ -766,39 +1036,55 @@ eio_set_max_poll_time (nseconds); void -min_parallel (int nthreads) +min_parallel (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_min_parallel (nthreads); void -max_parallel (int nthreads) +max_parallel (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_max_parallel (nthreads); void -max_idle (int nthreads) +max_idle (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_max_idle (nthreads); void -max_outstanding (int maxreqs) +idle_timeout (unsigned int seconds) + PROTOTYPE: $ + CODE: + eio_set_idle_timeout (seconds); + +void +max_outstanding (unsigned int maxreqs) PROTOTYPE: $ CODE: max_outstanding = maxreqs; void +aio_wd (SV8 *pathname, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_WD_OPEN; + req_set_path1 (req, pathname); + + REQ_SEND; +} + +void aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { dREQ; req->type = EIO_OPEN; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); + req_set_path1 (req, pathname); req->int1 = flags; req->int2 = mode; @@ -807,10 +1093,10 @@ void aio_fsync (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ ALIAS: aio_fsync = EIO_FSYNC aio_fdatasync = EIO_FDATASYNC + aio_syncfs = EIO_SYNCFS PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -820,12 +1106,11 @@ req->sv1 = newSVsv (fh); req->int1 = fd; - REQ_SEND (req); + REQ_SEND; } void aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -838,36 +1123,74 @@ req->size = nbytes; req->int2 = flags; - REQ_SEND (req); + REQ_SEND; +} + +void +aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_FALLOCATE; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->int2 = mode; + req->offs = offset; + req->size = len; + + REQ_SEND; } void aio_close (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ PPCODE: { - static int close_pipe = -1; /* dummy fd to close fds via dup2 */ + static int close_fd = -1; /* dummy fd to close fds via dup2 */ int fd = s_fileno_croak (fh, 0); dREQ; - if (close_pipe < 0) + if (expect_false (close_fd < 0)) { int pipefd [2]; - if (pipe (pipefd) < 0 - || close (pipefd [1]) < 0 - || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) + if ( +#ifdef _WIN32 + _pipe (pipefd, 1, _O_BINARY) < 0 +#else + pipe (pipefd) < 0 + || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 +#endif + || close (pipefd [1]) < 0 + ) abort (); /*D*/ - close_pipe = pipefd [0]; + close_fd = pipefd [0]; } req->type = EIO_DUP2; - req->int1 = close_pipe; + req->int1 = close_fd; req->sv2 = newSVsv (fh); req->int2 = fd; - REQ_SEND (req); + REQ_SEND; +} + +void +aio_seek (SV *fh, SV *offset, int whence, SV *callback=&PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_SEEK; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->offs = SvVAL64 (offset); + req->int2 = whence; + + REQ_SEND; } void @@ -875,7 +1198,6 @@ ALIAS: aio_read = EIO_READ aio_write = EIO_WRITE - PROTOTYPE: $$$$$;$ PPCODE: { STRLEN svlen; @@ -899,7 +1221,10 @@ { /* read: check type and grow scalar as necessary */ SvUPGRADE (data, SVt_PV); - svptr = SvGROW (data, len + dataoffset + 1); + if (SvLEN (data) >= SvCUR (data)) + svptr = SvGROW (data, len + dataoffset + 1); + else if (SvCUR (data) < len + dataoffset) + croak ("length + dataoffset outside of scalar, and cannot grow"); } { @@ -925,23 +1250,22 @@ } void -aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) - PROTOTYPE: $$;$ +aio_readlink (SV8 *pathname, SV *callback=&PL_sv_undef) + ALIAS: + aio_readlink = EIO_READLINK + aio_realpath = EIO_REALPATH PPCODE: { - SV *data; dREQ; - req->type = EIO_READLINK; - req->sv1 = newSVsv (path); - req->ptr1 = SvPVbyte_nolen (req->sv1); + req->type = ix; + req_set_path1 (req, pathname); REQ_SEND; } void aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ PPCODE: { int ifd = s_fileno_croak (in_fh , 0); @@ -961,7 +1285,6 @@ void aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -986,22 +1309,27 @@ { dREQ; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = ix; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); REQ_SEND; } +UV +major (UV dev) + ALIAS: + minor = 1 + CODE: + RETVAL = ix ? major (dev) : minor (dev); + OUTPUT: + RETVAL + +UV +makedev (UV maj, UV min) + CODE: + RETVAL = makedev (maj, min); + OUTPUT: + RETVAL + void aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) PPCODE: @@ -1010,18 +1338,7 @@ req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = EIO_UTIME; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FUTIME; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_UTIME, EIO_FUTIME, fh_or_path); REQ_SEND; } @@ -1032,45 +1349,20 @@ { dREQ; - req->sv1 = newSVsv (fh_or_path); req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; - - if (SvPOK (req->sv1)) - { - req->type = EIO_TRUNCATE; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FTRUNCATE; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_TRUNCATE, EIO_FTRUNCATE, fh_or_path); REQ_SEND; } void aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) - ALIAS: - aio_chmod = EIO_CHMOD - aio_mkdir = EIO_MKDIR PPCODE: { dREQ; req->int2 = mode; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = ix; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FCHMOD; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_CHMOD, EIO_FCHMOD, fh_or_path); REQ_SEND; } @@ -1083,18 +1375,7 @@ req->int2 = SvOK (uid) ? SvIV (uid) : -1; req->int3 = SvOK (gid) ? SvIV (gid) : -1; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = EIO_CHOWN; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FCHOWN; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_CHOWN, EIO_FCHOWN, fh_or_path); REQ_SEND; } @@ -1106,17 +1387,30 @@ dREQ; req->type = EIO_READDIR; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; if (flags & EIO_READDIR_DENTS) req->int1 |= EIO_READDIR_CUSTOM2; + req_set_path1 (req, pathname); + REQ_SEND; } void +aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_MKDIR; + req->int2 = mode; + req_set_path1 (req, pathname); + + REQ_SEND; +} + +void aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) ALIAS: aio_unlink = EIO_UNLINK @@ -1127,8 +1421,7 @@ dREQ; req->type = ix; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); + req_set_path1 (req, pathname); REQ_SEND; } @@ -1141,13 +1434,13 @@ aio_rename = EIO_RENAME PPCODE: { + eio_wd wd2 = 0; dREQ; req->type = ix; - req->sv1 = newSVsv (oldpath); - req->ptr1 = SvPVbyte_nolen (req->sv1); - req->sv2 = newSVsv (newpath); - req->ptr2 = SvPVbyte_nolen (req->sv2); + req_set_path1 (req, oldpath); + req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2); + req->int3 = (long)wd2; REQ_SEND; } @@ -1159,10 +1452,9 @@ dREQ; req->type = EIO_MKNOD; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); req->int2 = (mode_t)mode; req->offs = dev; + req_set_path1 (req, pathname); REQ_SEND; } @@ -1172,12 +1464,11 @@ ALIAS: aio_mtouch = EIO_MTOUCH aio_msync = EIO_MSYNC - PROTOTYPE: $$$$;$ PPCODE: { STRLEN svlen; - UV len = SvUV (length); char *svptr = SvPVbyte (data, svlen); + UV len = SvUV (length); if (offset < 0) offset += svlen; @@ -1192,9 +1483,9 @@ dREQ; req->type = ix; - req->size = len; req->sv2 = SvREFCNT_inc (data); req->ptr2 = (char *)svptr + offset; + req->size = len; req->int1 = flags; REQ_SEND; @@ -1202,6 +1493,70 @@ } void +aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef) + PPCODE: +{ + STRLEN svlen; + char *svptr = SvPVbyte (data, svlen); + UV len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + { + dREQ; + + req->type = EIO_MLOCK; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + offset; + req->size = len; + + REQ_SEND; + } +} + +void +aio_mlockall (IV flags, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_MLOCKALL; + req->int1 = flags; + + REQ_SEND; +} + +void +aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_CUSTOM; + req->sv1 = newSVsv (fh); + req->int1 = fd; + + req->feed = fiemap; +#if HAVE_FIEMAP + /* keep our fingers crossed that the next two types are 64 bit */ + req->offs = start; + req->size = SvOK (length) ? SvVAL64 (length) : ~0ULL; + req->int2 = flags; + req->int3 = SvOK (count) ? SvIV (count) : -1; +#endif + + REQ_SEND; +} + +void aio_busy (double delay, SV *callback=&PL_sv_undef) PPCODE: { @@ -1215,7 +1570,6 @@ void aio_group (SV *callback=&PL_sv_undef) - PROTOTYPE: ;$ PPCODE: { dREQ; @@ -1242,7 +1596,6 @@ int aioreq_pri (int pri = 0) - PROTOTYPE: ;$ CODE: RETVAL = next_pri; if (items > 0) @@ -1264,7 +1617,6 @@ void flush () - PROTOTYPE: CODE: while (eio_nreqs ()) { @@ -1273,8 +1625,7 @@ } int -poll() - PROTOTYPE: +poll () CODE: poll_wait (); RETVAL = poll_cb (); @@ -1282,15 +1633,14 @@ RETVAL int -poll_fileno() - PROTOTYPE: +poll_fileno () CODE: RETVAL = s_epipe_fd (&respipe); OUTPUT: RETVAL int -poll_cb(...) +poll_cb (...) PROTOTYPE: CODE: RETVAL = poll_cb (); @@ -1298,38 +1648,33 @@ RETVAL void -poll_wait() - PROTOTYPE: +poll_wait () CODE: poll_wait (); int -nreqs() - PROTOTYPE: +nreqs () CODE: RETVAL = eio_nreqs (); OUTPUT: RETVAL int -nready() - PROTOTYPE: +nready () CODE: RETVAL = eio_nready (); OUTPUT: RETVAL int -npending() - PROTOTYPE: +npending () CODE: RETVAL = eio_npending (); OUTPUT: RETVAL int -nthreads() - PROTOTYPE: +nthreads () CODE: RETVAL = eio_nthreads (); OUTPUT: @@ -1337,27 +1682,20 @@ int fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) - PROTOTYPE: $$$$ CODE: -#if _XOPEN_SOURCE >= 600 && !NO_FADVISE RETVAL = posix_fadvise (fh, offset, length, advice); -#else - RETVAL = errno = ENOSYS; /* yes, this is actually correct */ -#endif OUTPUT: RETVAL -ssize_t +IV sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) - PROTOTYPE: $$$$ CODE: RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); OUTPUT: RETVAL void -mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) - PROTOTYPE: $$$$$;$ +mmap (SV *scalar, size_t length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0) PPCODE: sv_unmagic (scalar, MMAP_MAGIC); { @@ -1366,14 +1704,20 @@ if (addr == (void *)-1) XSRETURN_NO; + sv_force_normal (scalar); + /* we store the length in mg_obj, as namlen is I32 :/ */ sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) ->mg_obj = (SV *)length; SvUPGRADE (scalar, SVt_PV); /* nop... */ + if (!(prot & PROT_WRITE)) SvREADONLY_on (scalar); + if (SvLEN (scalar)) + Safefree (SvPVX (scalar)); + SvPVX (scalar) = (char *)addr; SvCUR_set (scalar, length); SvLEN_set (scalar, 0); @@ -1384,40 +1728,108 @@ void munmap (SV *scalar) - PROTOTYPE: $ CODE: sv_unmagic (scalar, MMAP_MAGIC); int -mlockall (int flags) - PROTOTYPE: $ +madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) + ALIAS: + mprotect = 1 CODE: -#if _POSIX_MEMLOCK -#if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 - extern int mallopt (int, int); - mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ -#endif - mlockall (flags); +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + size_t len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + addr = (void *)(((intptr_t)addr) + offset); + eio_page_align (&addr, &len); + + switch (ix) + { + case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break; + case 1: RETVAL = mprotect (addr, len, advice_or_prot); break; + } +} + OUTPUT: + RETVAL + +int +munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) + CODE: +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + size_t len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + addr = (void *)(((intptr_t)addr) + offset); + eio_page_align (&addr, &len); +#if _POSIX_MEMLOCK_RANGE + RETVAL = munlock (addr, len); #else - RETVAL = -1; - errno = ENOSYS; + RETVAL = EIO_ENOSYS (); #endif +} OUTPUT: RETVAL int munlockall () - PROTOTYPE: CODE: #if _POSIX_MEMLOCK munlockall (); #else - RETVAL = -1; - errno = ENOSYS; + RETVAL = EIO_ENOSYS (); #endif OUTPUT: RETVAL +int +splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags) + CODE: +{ +#if HAVE_LINUX_SPLICE + loff_t off_in_, off_out_; + RETVAL = splice ( + rfh, SvOK (off_in ) ? (off_in_ = SvVAL64 (off_in )), &off_in_ : 0, + wfh, SvOK (off_out) ? (off_out_ = SvVAL64 (off_out)), &off_out_ : 0, + length, flags + ); +#else + RETVAL = EIO_ENOSYS (); +#endif +} + OUTPUT: + RETVAL + +int +tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags) + CODE: +#if HAVE_LINUX_SPLICE + RETVAL = tee (rfh, wfh, length, flags); +#else + RETVAL = EIO_ENOSYS (); +#endif + OUTPUT: + RETVAL + void _on_next_submit (SV *cb) CODE: SvREFCNT_dec (on_next_submit); @@ -1425,6 +1837,34 @@ PROTOTYPES: DISABLE +MODULE = IO::AIO PACKAGE = IO::AIO::WD + +BOOT: +{ + newCONSTSUB (aio_stash, "CWD" , newSVaio_wd (EIO_CWD )); + newCONSTSUB (aio_stash, "INVALID_WD", newSVaio_wd (EIO_INVALID_WD)); +} + +void +DESTROY (SV *self) + CODE: +{ + aio_wd wd = SvAIO_WD (self); +#if HAVE_AT + { + SV *callback = &PL_sv_undef; + dREQ; /* clobbers next_pri :/ */ + next_pri = req->pri; /* restore next_pri */ + req->pri = EIO_PRI_MAX; /* better use max. priority to conserve fds */ + req->type = EIO_WD_CLOSE; + req->wd = wd; + REQ_SEND; + } +#else + eio_wd_close_sync (wd); +#endif +} + MODULE = IO::AIO PACKAGE = IO::AIO::REQ void