… | |
… | |
4 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
5 | #include "perl.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
6 | #include "XSUB.h" |
7 | |
7 | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
|
|
9 | |
|
|
10 | #include <pthread.h> |
9 | |
11 | |
10 | #include <sys/types.h> |
12 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
12 | |
14 | |
13 | #include <unistd.h> |
15 | #include <unistd.h> |
14 | #include <fcntl.h> |
16 | #include <fcntl.h> |
15 | #include <signal.h> |
17 | #include <signal.h> |
16 | #include <sched.h> |
18 | #include <sched.h> |
17 | |
19 | |
18 | #include <pthread.h> |
20 | #if HAVE_SENDFILE |
19 | |
21 | # if __linux |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | # include <sys/sendfile.h> |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
23 | # elif __freebsd |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
24 | # include <sys/socket.h> |
|
|
25 | # include <sys/uio.h> |
|
|
26 | # elif __hpux |
|
|
27 | # include <sys/socket.h> |
|
|
28 | # elif __solaris /* not yet */ |
|
|
29 | # include <sys/sendfile.h> |
|
|
30 | # else |
|
|
31 | # error sendfile support requested but not available |
|
|
32 | # endif |
|
|
33 | #endif |
23 | |
34 | |
24 | #if __ia64 |
35 | #if __ia64 |
25 | # define STACKSIZE 65536 |
36 | # define STACKSIZE 65536 |
26 | #else |
37 | #else |
27 | # define STACKSIZE 4096 |
38 | # define STACKSIZE 4096 |
… | |
… | |
29 | |
40 | |
30 | enum { |
41 | enum { |
31 | REQ_QUIT, |
42 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
43 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
44 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
45 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
46 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
47 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
48 | REQ_UNLINK, REQ_RMDIR, |
37 | REQ_SYMLINK, |
49 | REQ_SYMLINK, |
38 | }; |
50 | }; |
… | |
… | |
40 | typedef struct aio_cb { |
52 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
53 | struct aio_cb *volatile next; |
42 | |
54 | |
43 | int type; |
55 | int type; |
44 | |
56 | |
45 | int fd; |
57 | int fd, fd2; |
46 | off_t offset; |
58 | off_t offset; |
47 | size_t length; |
59 | size_t length; |
48 | ssize_t result; |
60 | ssize_t result; |
49 | mode_t mode; /* open */ |
61 | mode_t mode; /* open */ |
50 | int errorno; |
62 | int errorno; |
51 | SV *data, *callback, *fh; |
63 | SV *data, *callback; |
|
|
64 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
65 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
66 | STRLEN dataoffset; |
54 | |
67 | |
55 | Stat_t *statdata; |
68 | Stat_t *statdata; |
56 | } aio_cb; |
69 | } aio_cb; |
57 | |
70 | |
58 | typedef aio_cb *aio_req; |
71 | typedef aio_cb *aio_req; |
59 | |
72 | |
60 | static int started; |
73 | static int started, wanted; |
61 | static volatile int nreqs; |
74 | static volatile int nreqs; |
62 | static int max_outstanding = 1<<30; |
75 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
76 | static int respipe [2]; |
64 | |
77 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
78 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
74 | if (req->data) |
87 | if (req->data) |
75 | SvREFCNT_dec (req->data); |
88 | SvREFCNT_dec (req->data); |
76 | |
89 | |
77 | if (req->fh) |
90 | if (req->fh) |
78 | SvREFCNT_dec (req->fh); |
91 | SvREFCNT_dec (req->fh); |
|
|
92 | |
|
|
93 | if (req->fh2) |
|
|
94 | SvREFCNT_dec (req->fh2); |
79 | |
95 | |
80 | if (req->statdata) |
96 | if (req->statdata) |
81 | Safefree (req->statdata); |
97 | Safefree (req->statdata); |
82 | |
98 | |
83 | if (req->callback) |
99 | if (req->callback) |
… | |
… | |
116 | { |
132 | { |
117 | ress = req->next; |
133 | ress = req->next; |
118 | |
134 | |
119 | if (!ress) |
135 | if (!ress) |
120 | { |
136 | { |
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
137 | /* read any signals sent by the worker threads */ |
124 | char buf [32]; |
138 | char buf [32]; |
125 | while (read (respipe [0], buf, 32) == 32) |
139 | while (read (respipe [0], buf, 32) == 32) |
126 | ; |
140 | ; |
|
|
141 | |
|
|
142 | rese = 0; |
127 | } |
143 | } |
128 | } |
144 | } |
129 | |
145 | |
130 | pthread_mutex_unlock (&reslock); |
146 | pthread_mutex_unlock (&reslock); |
131 | |
147 | |
… | |
… | |
221 | } |
237 | } |
222 | |
238 | |
223 | static void |
239 | static void |
224 | send_req (aio_req req) |
240 | send_req (aio_req req) |
225 | { |
241 | { |
|
|
242 | while (started < wanted && nreqs >= started) |
|
|
243 | start_thread (); |
|
|
244 | |
226 | nreqs++; |
245 | nreqs++; |
227 | |
246 | |
228 | pthread_mutex_lock (&reqlock); |
247 | pthread_mutex_lock (&reqlock); |
229 | |
248 | |
230 | req->next = 0; |
249 | req->next = 0; |
… | |
… | |
262 | send_req (req); |
281 | send_req (req); |
263 | } |
282 | } |
264 | |
283 | |
265 | static void min_parallel (int nthreads) |
284 | static void min_parallel (int nthreads) |
266 | { |
285 | { |
267 | while (nthreads > started) |
286 | if (wanted < nthreads) |
268 | start_thread (); |
287 | wanted = nthreads; |
269 | } |
288 | } |
270 | |
289 | |
271 | static void max_parallel (int nthreads) |
290 | static void max_parallel (int nthreads) |
272 | { |
291 | { |
273 | int cur = started; |
292 | int cur = started; |
274 | |
293 | |
|
|
294 | if (wanted > nthreads) |
|
|
295 | wanted = nthreads; |
|
|
296 | |
275 | while (cur > nthreads) |
297 | while (cur > wanted) |
276 | { |
298 | { |
277 | end_thread (); |
299 | end_thread (); |
278 | cur--; |
300 | cur--; |
279 | } |
301 | } |
280 | |
302 | |
281 | while (started > nthreads) |
303 | while (started > wanted) |
282 | { |
304 | { |
283 | poll_wait (); |
305 | poll_wait (); |
284 | poll_cb (); |
306 | poll_cb (); |
285 | } |
307 | } |
286 | } |
308 | } |
… | |
… | |
311 | |
333 | |
312 | static void atfork_child (void) |
334 | static void atfork_child (void) |
313 | { |
335 | { |
314 | aio_req prv; |
336 | aio_req prv; |
315 | |
337 | |
316 | int restart = started; |
|
|
317 | started = 0; |
338 | started = 0; |
318 | |
339 | |
319 | while (reqs) |
340 | while (reqs) |
320 | { |
341 | { |
321 | prv = reqs; |
342 | prv = reqs; |
… | |
… | |
337 | close (respipe [0]); |
358 | close (respipe [0]); |
338 | close (respipe [1]); |
359 | close (respipe [1]); |
339 | create_pipe (); |
360 | create_pipe (); |
340 | |
361 | |
341 | atfork_parent (); |
362 | atfork_parent (); |
342 | |
|
|
343 | min_parallel (restart); |
|
|
344 | } |
363 | } |
345 | |
364 | |
346 | /*****************************************************************************/ |
365 | /*****************************************************************************/ |
347 | /* work around various missing functions */ |
366 | /* work around various missing functions */ |
348 | |
367 | |
… | |
… | |
413 | |
432 | |
414 | errno = 0; |
433 | errno = 0; |
415 | } |
434 | } |
416 | #endif |
435 | #endif |
417 | |
436 | |
|
|
437 | /* sendfile always needs emulation */ |
|
|
438 | static ssize_t |
|
|
439 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
440 | { |
|
|
441 | ssize_t res; |
|
|
442 | |
|
|
443 | if (!count) |
|
|
444 | return 0; |
|
|
445 | |
|
|
446 | #if HAVE_SENDFILE |
|
|
447 | # if __linux |
|
|
448 | res = sendfile (ofd, ifd, &offset, count); |
|
|
449 | |
|
|
450 | # elif __freebsd |
|
|
451 | /* |
|
|
452 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
453 | * wasted on making it similar to other I/O functions. |
|
|
454 | */ |
|
|
455 | { |
|
|
456 | off_t sbytes; |
|
|
457 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
458 | |
|
|
459 | if (res < 0 && sbytes) |
|
|
460 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
461 | res = sbytes; |
|
|
462 | } |
|
|
463 | |
|
|
464 | # elif __hpux |
|
|
465 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
466 | |
|
|
467 | # elif __solaris |
|
|
468 | { |
|
|
469 | struct sendfilevec vec; |
|
|
470 | size_t sbytes; |
|
|
471 | |
|
|
472 | vec.sfv_fd = ifd; |
|
|
473 | vec.sfv_flag = 0; |
|
|
474 | vec.sfv_off = offset; |
|
|
475 | vec.sfv_len = count; |
|
|
476 | |
|
|
477 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
478 | |
|
|
479 | if (res < 0 && sbytes) |
|
|
480 | res = sbytes; |
|
|
481 | } |
|
|
482 | |
|
|
483 | # else |
|
|
484 | res = -1; |
|
|
485 | errno = ENOSYS; |
|
|
486 | # endif |
|
|
487 | #endif |
|
|
488 | |
|
|
489 | if (res < 0 |
|
|
490 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
491 | #if __solaris |
|
|
492 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
493 | #endif |
|
|
494 | ) |
|
|
495 | ) |
|
|
496 | { |
|
|
497 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
498 | char *buf = malloc (4096); |
|
|
499 | res = 0; |
|
|
500 | |
|
|
501 | for (;;) |
|
|
502 | { |
|
|
503 | ssize_t cnt; |
|
|
504 | |
|
|
505 | cnt = pread (ifd, buf, 4096, offset); |
|
|
506 | |
|
|
507 | if (cnt <= 0) |
|
|
508 | { |
|
|
509 | if (cnt && !res) res = -1; |
|
|
510 | break; |
|
|
511 | } |
|
|
512 | |
|
|
513 | cnt = write (ofd, buf, cnt); |
|
|
514 | |
|
|
515 | if (cnt <= 0) |
|
|
516 | { |
|
|
517 | if (cnt && !res) res = -1; |
|
|
518 | break; |
|
|
519 | } |
|
|
520 | |
|
|
521 | offset += cnt; |
|
|
522 | res += cnt; |
|
|
523 | } |
|
|
524 | |
|
|
525 | { |
|
|
526 | int errorno = errno; |
|
|
527 | free (buf); |
|
|
528 | errno = errorno; |
|
|
529 | } |
|
|
530 | } |
|
|
531 | |
|
|
532 | return res; |
|
|
533 | } |
|
|
534 | |
418 | /*****************************************************************************/ |
535 | /*****************************************************************************/ |
419 | |
536 | |
420 | static void * |
537 | static void * |
421 | aio_proc (void *thr_arg) |
538 | aio_proc (void *thr_arg) |
422 | { |
539 | { |
… | |
… | |
453 | { |
570 | { |
454 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
571 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
455 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
572 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
456 | |
573 | |
457 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
574 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
575 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
458 | |
576 | |
459 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
577 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
460 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
578 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
461 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
579 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
462 | |
580 | |
… | |
… | |
585 | |
703 | |
586 | void |
704 | void |
587 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
705 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
588 | SV * fh |
706 | SV * fh |
589 | UV offset |
707 | UV offset |
590 | IV length |
708 | UV length |
591 | SV * data |
709 | SV * data |
592 | IV dataoffset |
710 | UV dataoffset |
593 | SV * callback |
711 | SV * callback |
594 | ALIAS: |
712 | ALIAS: |
595 | aio_read = REQ_READ |
713 | aio_read = REQ_READ |
596 | aio_write = REQ_WRITE |
714 | aio_write = REQ_WRITE |
597 | PROTOTYPE: $$$$$;$ |
715 | PROTOTYPE: $$$$$;$ |
… | |
… | |
643 | req->data2ptr = (void *)data; |
761 | req->data2ptr = (void *)data; |
644 | } |
762 | } |
645 | |
763 | |
646 | send_req (req); |
764 | send_req (req); |
647 | } |
765 | } |
|
|
766 | } |
|
|
767 | |
|
|
768 | void |
|
|
769 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
770 | SV * out_fh |
|
|
771 | SV * in_fh |
|
|
772 | UV in_offset |
|
|
773 | UV length |
|
|
774 | SV * callback |
|
|
775 | PROTOTYPE: $$$$;$ |
|
|
776 | CODE: |
|
|
777 | { |
|
|
778 | dREQ; |
|
|
779 | |
|
|
780 | req->type = REQ_SENDFILE; |
|
|
781 | req->fh = newSVsv (out_fh); |
|
|
782 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
783 | req->fh2 = newSVsv (in_fh); |
|
|
784 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
785 | req->offset = in_offset; |
|
|
786 | req->length = length; |
|
|
787 | |
|
|
788 | send_req (req); |
648 | } |
789 | } |
649 | |
790 | |
650 | void |
791 | void |
651 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
792 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
652 | SV * fh |
793 | SV * fh |