ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.xs
Revision: 1.30
Committed: Sat Jul 9 23:38:07 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
Changes since 1.29: +4 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.17 #define PERL_NO_GET_CONTEXT
2    
3 root 1.1 #include "EXTERN.h"
4     #include "perl.h"
5     #include "XSUB.h"
6    
7     #include <sys/types.h>
8 root 1.13 #include <sys/stat.h>
9 root 1.1 #include <unistd.h>
10 root 1.5 #include <fcntl.h>
11 root 1.8 #include <signal.h>
12 root 1.1 #include <sched.h>
13 root 1.26 #include <endian.h>
14 root 1.1
15 root 1.21 typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16 root 1.13 typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17 root 1.21 typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
18 root 1.16
19 root 1.27 #if __i386 || __amd64
20     # define STACKSIZE ( 256 * sizeof (long))
21     #elif __ia64
22     # define STACKSIZE (8192 * sizeof (long))
23     #else
24     # define STACKSIZE ( 512 * sizeof (long))
25     #endif
26 root 1.1
27 root 1.20 enum {
28     REQ_QUIT,
29 root 1.29 REQ_OPEN, REQ_CLOSE,
30     REQ_READ, REQ_WRITE, REQ_READAHEAD,
31     REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK,
32     REQ_FSYNC, REQ_FDATASYNC,
33 root 1.20 };
34 root 1.1
35     typedef struct {
36     char stack[STACKSIZE];
37     } aio_thread;
38    
39 root 1.18 typedef struct aio_cb {
40     struct aio_cb *next;
41    
42 root 1.1 int type;
43     aio_thread *thread;
44    
45     int fd;
46     off_t offset;
47     size_t length;
48 root 1.2 ssize_t result;
49 root 1.8 mode_t mode; /* open */
50 root 1.1 int errorno;
51 root 1.5 SV *data, *callback;
52 root 1.1 void *dataptr;
53 root 1.2 STRLEN dataoffset;
54 root 1.13
55 root 1.21 Stat_t *statdata;
56 root 1.1 } aio_cb;
57    
58     typedef aio_cb *aio_req;
59    
60     static int started;
61 root 1.5 static int nreqs;
62 root 1.1 static int reqpipe[2], respipe[2];
63    
64 root 1.18 static aio_req qs, qe; /* queue start, queue end */
65    
66 root 1.2 static int aio_proc(void *arg);
67    
68 root 1.1 static void
69 root 1.18 start_thread (void)
70 root 1.1 {
71 root 1.2 aio_thread *thr;
72    
73     New (0, thr, 1, aio_thread);
74    
75     if (clone (aio_proc,
76 root 1.27 &(thr->stack[STACKSIZE - 16]),
77 root 1.20 CLONE_VM|CLONE_FS|CLONE_FILES,
78 root 1.2 thr) >= 0)
79     started++;
80     else
81     Safefree (thr);
82 root 1.1 }
83    
84     static void
85 root 1.18 send_reqs (void)
86 root 1.1 {
87 root 1.18 /* this write is atomic */
88     while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
89     {
90     qs = qs->next;
91     if (!qs) qe = 0;
92     }
93 root 1.2 }
94    
95     static void
96 root 1.8 send_req (aio_req req)
97     {
98     nreqs++;
99 root 1.18 req->next = 0;
100    
101     if (qe)
102 root 1.22 {
103     qe->next = req;
104     qe = req;
105     }
106 root 1.18 else
107     qe = qs = req;
108    
109     send_reqs ();
110     }
111    
112     static void
113     end_thread (void)
114     {
115     aio_req req;
116     New (0, req, 1, aio_cb);
117     req->type = REQ_QUIT;
118    
119     send_req (req);
120 root 1.8 }
121    
122     static void
123 root 1.17 read_write (pTHX_
124     int dowrite, int fd, off_t offset, size_t length,
125 root 1.18 SV *data, STRLEN dataoffset, SV *callback)
126 root 1.2 {
127 root 1.5 aio_req req;
128     STRLEN svlen;
129     char *svptr = SvPV (data, svlen);
130    
131 root 1.13 SvUPGRADE (data, SVt_PV);
132     SvPOK_on (data);
133    
134 root 1.5 if (dataoffset < 0)
135     dataoffset += svlen;
136    
137     if (dataoffset < 0 || dataoffset > svlen)
138     croak ("data offset outside of string");
139    
140     if (dowrite)
141     {
142     /* write: check length and adjust. */
143     if (length < 0 || length + dataoffset > svlen)
144     length = svlen - dataoffset;
145     }
146     else
147     {
148     /* read: grow scalar as necessary */
149     svptr = SvGROW (data, length + dataoffset);
150     }
151    
152     if (length < 0)
153     croak ("length must not be negative");
154    
155 root 1.14 Newz (0, req, 1, aio_cb);
156 root 1.5
157     if (!req)
158     croak ("out of memory during aio_req allocation");
159    
160     req->type = dowrite ? REQ_WRITE : REQ_READ;
161     req->fd = fd;
162     req->offset = offset;
163     req->length = length;
164     req->data = SvREFCNT_inc (data);
165 root 1.6 req->dataptr = (char *)svptr + dataoffset;
166 root 1.5 req->callback = SvREFCNT_inc (callback);
167    
168 root 1.8 send_req (req);
169 root 1.5 }
170    
171 root 1.22 static void
172     poll_wait ()
173     {
174     fd_set rfd;
175     FD_ZERO(&rfd);
176     FD_SET(respipe[0], &rfd);
177    
178     select (respipe[0] + 1, &rfd, 0, 0, 0);
179     }
180    
181 root 1.5 static int
182     poll_cb (pTHX)
183     {
184     dSP;
185     int count = 0;
186     aio_req req;
187    
188     while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
189     {
190 root 1.22 nreqs--;
191    
192 root 1.5 if (req->type == REQ_QUIT)
193     {
194     Safefree (req->thread);
195     started--;
196     }
197     else
198     {
199     int errorno = errno;
200     errno = req->errorno;
201    
202     if (req->type == REQ_READ)
203 root 1.6 SvCUR_set (req->data, req->dataoffset
204     + req->result > 0 ? req->result : 0);
205 root 1.5
206 root 1.14 if (req->data)
207     SvREFCNT_dec (req->data);
208    
209 root 1.13 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
210     {
211 root 1.21 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
212     PL_laststatval = req->result;
213     PL_statcache = *(req->statdata);
214 root 1.13
215     Safefree (req->statdata);
216     }
217    
218 root 1.5 PUSHMARK (SP);
219     XPUSHs (sv_2mortal (newSViv (req->result)));
220     PUTBACK;
221     call_sv (req->callback, G_VOID);
222     SPAGAIN;
223    
224 root 1.14 if (req->callback)
225     SvREFCNT_dec (req->callback);
226 root 1.5
227     errno = errorno;
228     count++;
229     }
230    
231     Safefree (req);
232     }
233    
234 root 1.18 if (qs)
235     send_reqs ();
236    
237 root 1.5 return count;
238 root 1.2 }
239    
240 root 1.8 static sigset_t fullsigset;
241    
242 root 1.2 #undef errno
243     #include <asm/unistd.h>
244 root 1.28 #include <linux/types.h>
245 root 1.20 #include <sys/prctl.h>
246 root 1.2
247 root 1.26 #if __alpha || __ia64 || __hppa || __v850__
248 root 1.25 # define stat kernelstat
249     # define stat64 kernelstat64
250     # include <asm/stat.h>
251     # undef stat
252     # undef stat64
253     #else
254     # define kernelstat stat
255     # define kernelstat64 stat64
256     #endif
257    
258 root 1.21 #define COPY_STATDATA \
259     req->statdata->st_dev = statdata.st_dev; \
260     req->statdata->st_ino = statdata.st_ino; \
261     req->statdata->st_mode = statdata.st_mode; \
262     req->statdata->st_nlink = statdata.st_nlink; \
263     req->statdata->st_uid = statdata.st_uid; \
264     req->statdata->st_gid = statdata.st_gid; \
265     req->statdata->st_rdev = statdata.st_rdev; \
266     req->statdata->st_size = statdata.st_size; \
267     req->statdata->st_atime = statdata.st_atime; \
268     req->statdata->st_mtime = statdata.st_mtime; \
269     req->statdata->st_ctime = statdata.st_ctime; \
270     req->statdata->st_blksize = statdata.st_blksize; \
271     req->statdata->st_blocks = statdata.st_blocks; \
272    
273 root 1.2 static int
274 root 1.18 aio_proc (void *thr_arg)
275 root 1.2 {
276     aio_thread *thr = thr_arg;
277 root 1.9 aio_req req;
278 root 1.2 int errno;
279    
280 root 1.21 /* this is very much kernel-specific :(:(:( */
281 root 1.11 /* we rely on gcc's ability to create closures. */
282 root 1.29 _syscall3(__kernel_size_t, read , unsigned int, fd, char *, buf, __kernel_size_t, count)
283     _syscall3(__kernel_size_t, write, unsigned int, fd, char *, buf, __kernel_size_t, count)
284 root 1.13
285 root 1.29 _syscall3(long, open, char *, pathname, int, flags, int, mode)
286     _syscall1(long, close, unsigned int, fd)
287     _syscall1(long, unlink, char *, filename);
288     _syscall1(long, fsync, unsigned int, fd);
289 root 1.30
290     #ifndef __NR_fdatasync
291     # define __NR_fdatasync __NR_fsync
292     #endif
293 root 1.29 _syscall1(long, fdatasync, unsigned int, fd);
294    
295     #if BYTE_ORDER == LITTLE_ENDIAN
296     # define LOFF_ARG(off) (off & 0xffffffff), (off >> 32)
297     #elif BYTE_ORDER == BIG_ENDIAN
298     # define LOFF_ARG(off) (off >> 32), (off & 0xffffffff)
299     #endif
300 root 1.11
301 root 1.28 #ifndef __NR_pread64
302     # define __NR_pread64 __NR_pread
303     # define __NR_pwrite64 __NR_write
304 root 1.21 #endif
305 root 1.29 _syscall5(__kernel_ssize_t, pread64 , unsigned int, fd, char *, buf,
306     __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
307     _syscall5(__kernel_ssize_t, pwrite64, unsigned int, fd, char *, buf,
308     __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
309     _syscall4(long, readahead, unsigned int, fd, unsigned int, offset_lh, unsigned int, offset_hl, __kernel_size_t, count);
310 root 1.21
311 root 1.25 #if __NR_stat64
312 root 1.29 _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf)
313     _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf)
314     _syscall2(long, fstat64, int , fd , struct kernelstat64 *, buf)
315 root 1.21 #elif __NR_stat
316 root 1.29 _syscall2(long, stat , const char *, filename, struct kernelstat *, buf)
317     _syscall2(long, lstat, const char *, filename, struct kernelstat *, buf)
318     _syscall2(long, fstat, int , fd , struct kernelstat *, buf)
319 root 1.21 #else
320     # error "neither stat64 nor stat defined"
321     #endif
322 root 1.13
323 root 1.29 /* the following two calls might clobber errno */
324 root 1.8 sigprocmask (SIG_SETMASK, &fullsigset, 0);
325 root 1.20 prctl (PR_SET_PDEATHSIG, SIGKILL);
326 root 1.2
327     /* then loop */
328     while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
329     {
330     req->thread = thr;
331 root 1.14 errno = 0; /* strictly unnecessary */
332 root 1.2
333 root 1.18 switch (req->type)
334 root 1.2 {
335 root 1.29 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
336     case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
337     case REQ_READAHEAD: req->result = readahead (req->fd, LOFF_ARG (req->offset), req->length); break;
338    
339 root 1.25 #if __NR_stat64
340     struct kernelstat64 statdata;
341 root 1.29 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
342     case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
343     case REQ_FSTAT: req->result = fstat64 (req->fd , &statdata); COPY_STATDATA; break;
344 root 1.21 #else
345 root 1.25 struct kernelstat statdata;
346 root 1.29 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
347     case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
348     case REQ_FSTAT: req->result = fstat (req->fd , &statdata); COPY_STATDATA; break;
349 root 1.21 #endif
350 root 1.29
351     case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
352     case REQ_CLOSE: req->result = close (req->fd); break;
353     case REQ_UNLINK: req->result = unlink (req->dataptr); break;
354    
355     case REQ_FSYNC: req->result = fsync (req->fd); break;
356     case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
357 root 1.18
358     case REQ_QUIT:
359     write (respipe[1], (void *)&req, sizeof (req));
360     return 0;
361 root 1.29
362     default:
363     req->result = ENOSYS;
364     break;
365 root 1.2 }
366    
367 root 1.8 req->errorno = errno;
368 root 1.2 write (respipe[1], (void *)&req, sizeof (req));
369     }
370    
371     return 0;
372 root 1.1 }
373    
374     MODULE = Linux::AIO PACKAGE = Linux::AIO
375    
376     BOOT:
377     {
378 root 1.8 sigfillset (&fullsigset);
379 root 1.9 sigdelset (&fullsigset, SIGTERM);
380     sigdelset (&fullsigset, SIGQUIT);
381     sigdelset (&fullsigset, SIGABRT);
382     sigdelset (&fullsigset, SIGINT);
383 root 1.8
384 root 1.1 if (pipe (reqpipe) || pipe (respipe))
385     croak ("unable to initialize request or result pipe");
386 root 1.18
387     if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
388     croak ("cannot set result pipe to nonblocking mode");
389 root 1.5
390     if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
391     croak ("cannot set result pipe to nonblocking mode");
392 root 1.1 }
393    
394     void
395     min_parallel(nthreads)
396     int nthreads
397 root 1.5 PROTOTYPE: $
398 root 1.1 CODE:
399     while (nthreads > started)
400     start_thread ();
401    
402     void
403     max_parallel(nthreads)
404     int nthreads
405 root 1.5 PROTOTYPE: $
406 root 1.1 CODE:
407 root 1.5 int cur = started;
408     while (cur > nthreads)
409     {
410     end_thread ();
411     cur--;
412     }
413    
414 root 1.1 while (started > nthreads)
415 root 1.5 {
416 root 1.22 poll_wait ();
417 root 1.17 poll_cb (aTHX);
418 root 1.5 }
419 root 1.1
420     void
421 root 1.8 aio_open(pathname,flags,mode,callback)
422 root 1.13 SV * pathname
423 root 1.8 int flags
424     int mode
425     SV * callback
426     PROTOTYPE: $$$$
427     CODE:
428     aio_req req;
429    
430 root 1.14 Newz (0, req, 1, aio_cb);
431 root 1.8
432     if (!req)
433     croak ("out of memory during aio_req allocation");
434    
435     req->type = REQ_OPEN;
436 root 1.14 req->data = newSVsv (pathname);
437     req->dataptr = SvPV_nolen (req->data);
438 root 1.8 req->fd = flags;
439     req->mode = mode;
440 root 1.10 req->callback = SvREFCNT_inc (callback);
441    
442     send_req (req);
443    
444     void
445     aio_close(fh,callback)
446 root 1.13 InputStream fh
447 root 1.10 SV * callback
448 root 1.14 PROTOTYPE: $$
449 root 1.29 ALIAS:
450     aio_close = REQ_CLOSE
451     aio_fsync = REQ_FSYNC
452     aio_fdatasync = REQ_FDATASYNC
453 root 1.10 CODE:
454     aio_req req;
455    
456 root 1.14 Newz (0, req, 1, aio_cb);
457 root 1.10
458     if (!req)
459     croak ("out of memory during aio_req allocation");
460    
461 root 1.29 req->type = ix;
462 root 1.10 req->fd = PerlIO_fileno (fh);
463 root 1.13 req->callback = SvREFCNT_inc (callback);
464    
465     send_req (req);
466    
467     void
468     aio_read(fh,offset,length,data,dataoffset,callback)
469     InputStream fh
470     UV offset
471     IV length
472     SV * data
473     IV dataoffset
474     SV * callback
475     PROTOTYPE: $$$$$$
476     CODE:
477     read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
478    
479     void
480     aio_write(fh,offset,length,data,dataoffset,callback)
481     OutputStream fh
482     UV offset
483     IV length
484     SV * data
485     IV dataoffset
486     SV * callback
487     PROTOTYPE: $$$$$$
488     CODE:
489     read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
490    
491     void
492     aio_stat(fh_or_path,callback)
493     SV * fh_or_path
494     SV * callback
495     PROTOTYPE: $$
496     ALIAS:
497     aio_lstat = 1
498     CODE:
499     aio_req req;
500    
501 root 1.14 Newz (0, req, 1, aio_cb);
502 root 1.13
503     if (!req)
504     croak ("out of memory during aio_req allocation");
505    
506 root 1.21 New (0, req->statdata, 1, Stat_t);
507 root 1.13
508     if (!req->statdata)
509     croak ("out of memory during aio_req->statdata allocation");
510    
511     if (SvPOK (fh_or_path))
512     {
513     req->type = ix ? REQ_LSTAT : REQ_STAT;
514 root 1.14 req->data = newSVsv (fh_or_path);
515     req->dataptr = SvPV_nolen (req->data);
516 root 1.13 }
517     else
518     {
519     req->type = REQ_FSTAT;
520     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
521     }
522    
523 root 1.8 req->callback = SvREFCNT_inc (callback);
524    
525     send_req (req);
526 root 1.20
527     void
528     aio_unlink(pathname,callback)
529     SV * pathname
530     SV * callback
531     PROTOTYPE: $$
532     CODE:
533     aio_req req;
534    
535     Newz (0, req, 1, aio_cb);
536    
537     if (!req)
538     croak ("out of memory during aio_req allocation");
539    
540     req->type = REQ_UNLINK;
541     req->data = newSVsv (pathname);
542     req->dataptr = SvPV_nolen (req->data);
543     req->callback = SvREFCNT_inc (callback);
544    
545     send_req (req);
546 root 1.5
547     int
548     poll_fileno()
549     PROTOTYPE:
550     CODE:
551     RETVAL = respipe[0];
552     OUTPUT:
553     RETVAL
554    
555     int
556 root 1.6 poll_cb(...)
557 root 1.5 PROTOTYPE:
558     CODE:
559     RETVAL = poll_cb (aTHX);
560     OUTPUT:
561     RETVAL
562    
563 root 1.23 void
564     poll_wait()
565     PROTOTYPE:
566     CODE:
567     poll_wait ();
568    
569 root 1.5 int
570     nreqs()
571     PROTOTYPE:
572     CODE:
573     RETVAL = nreqs;
574     OUTPUT:
575     RETVAL
576