… | |
… | |
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 | # else |
|
|
29 | # error sendfile support requested but not available |
|
|
30 | # endif |
|
|
31 | #endif |
23 | |
32 | |
24 | #if __ia64 |
33 | #if __ia64 |
25 | # define STACKSIZE 65536 |
34 | # define STACKSIZE 65536 |
26 | #else |
35 | #else |
27 | # define STACKSIZE 4096 |
36 | # define STACKSIZE 4096 |
… | |
… | |
29 | |
38 | |
30 | enum { |
39 | enum { |
31 | REQ_QUIT, |
40 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
41 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
42 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
43 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
44 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
45 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
46 | REQ_UNLINK, REQ_RMDIR, |
37 | REQ_SYMLINK, |
47 | REQ_SYMLINK, |
38 | }; |
48 | }; |
… | |
… | |
40 | typedef struct aio_cb { |
50 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
51 | struct aio_cb *volatile next; |
42 | |
52 | |
43 | int type; |
53 | int type; |
44 | |
54 | |
45 | int fd; |
55 | int fd, fd2; |
46 | off_t offset; |
56 | off_t offset; |
47 | size_t length; |
57 | size_t length; |
48 | ssize_t result; |
58 | ssize_t result; |
49 | mode_t mode; /* open */ |
59 | mode_t mode; /* open */ |
50 | int errorno; |
60 | int errorno; |
51 | SV *data, *callback, *fh; |
61 | SV *data, *callback; |
|
|
62 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
63 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
64 | STRLEN dataoffset; |
54 | |
65 | |
55 | Stat_t *statdata; |
66 | Stat_t *statdata; |
56 | } aio_cb; |
67 | } aio_cb; |
57 | |
68 | |
58 | typedef aio_cb *aio_req; |
69 | typedef aio_cb *aio_req; |
59 | |
70 | |
60 | static int started; |
71 | static int started, wanted; |
61 | static volatile int nreqs; |
72 | static volatile int nreqs; |
62 | static int max_outstanding = 1<<30; |
73 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
74 | static int respipe [2]; |
64 | |
75 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
76 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
74 | if (req->data) |
85 | if (req->data) |
75 | SvREFCNT_dec (req->data); |
86 | SvREFCNT_dec (req->data); |
76 | |
87 | |
77 | if (req->fh) |
88 | if (req->fh) |
78 | SvREFCNT_dec (req->fh); |
89 | SvREFCNT_dec (req->fh); |
|
|
90 | |
|
|
91 | if (req->fh2) |
|
|
92 | SvREFCNT_dec (req->fh2); |
79 | |
93 | |
80 | if (req->statdata) |
94 | if (req->statdata) |
81 | Safefree (req->statdata); |
95 | Safefree (req->statdata); |
82 | |
96 | |
83 | if (req->callback) |
97 | if (req->callback) |
… | |
… | |
116 | { |
130 | { |
117 | ress = req->next; |
131 | ress = req->next; |
118 | |
132 | |
119 | if (!ress) |
133 | if (!ress) |
120 | { |
134 | { |
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
135 | /* read any signals sent by the worker threads */ |
124 | char buf [32]; |
136 | char buf [32]; |
125 | while (read (respipe [0], buf, 32) == 32) |
137 | while (read (respipe [0], buf, 32) == 32) |
126 | ; |
138 | ; |
|
|
139 | |
|
|
140 | rese = 0; |
127 | } |
141 | } |
128 | } |
142 | } |
129 | |
143 | |
130 | pthread_mutex_unlock (&reslock); |
144 | pthread_mutex_unlock (&reslock); |
131 | |
145 | |
… | |
… | |
221 | } |
235 | } |
222 | |
236 | |
223 | static void |
237 | static void |
224 | send_req (aio_req req) |
238 | send_req (aio_req req) |
225 | { |
239 | { |
|
|
240 | while (started < wanted && nreqs >= started) |
|
|
241 | start_thread (); |
|
|
242 | |
226 | nreqs++; |
243 | nreqs++; |
227 | |
244 | |
228 | pthread_mutex_lock (&reqlock); |
245 | pthread_mutex_lock (&reqlock); |
229 | |
246 | |
230 | req->next = 0; |
247 | req->next = 0; |
… | |
… | |
262 | send_req (req); |
279 | send_req (req); |
263 | } |
280 | } |
264 | |
281 | |
265 | static void min_parallel (int nthreads) |
282 | static void min_parallel (int nthreads) |
266 | { |
283 | { |
267 | while (nthreads > started) |
284 | if (wanted < nthreads) |
268 | start_thread (); |
285 | wanted = nthreads; |
269 | } |
286 | } |
270 | |
287 | |
271 | static void max_parallel (int nthreads) |
288 | static void max_parallel (int nthreads) |
272 | { |
289 | { |
273 | int cur = started; |
290 | int cur = started; |
274 | |
291 | |
|
|
292 | if (wanted > nthreads) |
|
|
293 | wanted = nthreads; |
|
|
294 | |
275 | while (cur > nthreads) |
295 | while (cur > wanted) |
276 | { |
296 | { |
277 | end_thread (); |
297 | end_thread (); |
278 | cur--; |
298 | cur--; |
279 | } |
299 | } |
280 | |
300 | |
281 | while (started > nthreads) |
301 | while (started > wanted) |
282 | { |
302 | { |
283 | poll_wait (); |
303 | poll_wait (); |
284 | poll_cb (); |
304 | poll_cb (); |
285 | } |
305 | } |
286 | } |
306 | } |
… | |
… | |
311 | |
331 | |
312 | static void atfork_child (void) |
332 | static void atfork_child (void) |
313 | { |
333 | { |
314 | aio_req prv; |
334 | aio_req prv; |
315 | |
335 | |
316 | int restart = started; |
|
|
317 | started = 0; |
336 | started = 0; |
318 | |
337 | |
319 | while (reqs) |
338 | while (reqs) |
320 | { |
339 | { |
321 | prv = reqs; |
340 | prv = reqs; |
… | |
… | |
332 | free_req (prv); |
351 | free_req (prv); |
333 | } |
352 | } |
334 | |
353 | |
335 | ress = rese = 0; |
354 | ress = rese = 0; |
336 | |
355 | |
|
|
356 | close (respipe [0]); |
|
|
357 | close (respipe [1]); |
|
|
358 | create_pipe (); |
|
|
359 | |
337 | atfork_parent (); |
360 | atfork_parent (); |
338 | |
|
|
339 | min_parallel (restart); |
|
|
340 | } |
361 | } |
341 | |
362 | |
342 | /*****************************************************************************/ |
363 | /*****************************************************************************/ |
343 | /* work around various missing functions */ |
364 | /* work around various missing functions */ |
344 | |
365 | |
… | |
… | |
409 | |
430 | |
410 | errno = 0; |
431 | errno = 0; |
411 | } |
432 | } |
412 | #endif |
433 | #endif |
413 | |
434 | |
|
|
435 | /* sendfile always needs emulation */ |
|
|
436 | static ssize_t |
|
|
437 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
438 | { |
|
|
439 | ssize_t res; |
|
|
440 | |
|
|
441 | if (!count) |
|
|
442 | return 0; |
|
|
443 | |
|
|
444 | #if __linux |
|
|
445 | res = sendfile (ofd, ifd, &offset, count); |
|
|
446 | |
|
|
447 | #elif __freebsd |
|
|
448 | /* |
|
|
449 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
450 | * wasted on making it similar to other i/o functions. |
|
|
451 | */ |
|
|
452 | { |
|
|
453 | off_t sbytes; |
|
|
454 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
455 | |
|
|
456 | if (!res && errno == EAGAIN) |
|
|
457 | /* maybe on others, too, as usual, the manpage leaves you guessing */ |
|
|
458 | res = sbytes; |
|
|
459 | } |
|
|
460 | |
|
|
461 | #elif __hpux |
|
|
462 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
463 | |
|
|
464 | #else |
|
|
465 | res = -1; |
|
|
466 | errno = ENOSYS; |
|
|
467 | #endif |
|
|
468 | |
|
|
469 | if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) |
|
|
470 | { |
|
|
471 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
472 | char *buf = malloc (4096); |
|
|
473 | res = 0; |
|
|
474 | |
|
|
475 | for (;;) |
|
|
476 | { |
|
|
477 | ssize_t cnt; |
|
|
478 | |
|
|
479 | cnt = pread (ifd, buf, 4096, offset); |
|
|
480 | |
|
|
481 | if (cnt <= 0) |
|
|
482 | { |
|
|
483 | if (cnt && !res) res = -1; |
|
|
484 | break; |
|
|
485 | } |
|
|
486 | |
|
|
487 | cnt = write (ofd, buf, cnt); |
|
|
488 | |
|
|
489 | if (cnt <= 0) |
|
|
490 | { |
|
|
491 | if (cnt && !res) res = -1; |
|
|
492 | break; |
|
|
493 | } |
|
|
494 | |
|
|
495 | offset += cnt; |
|
|
496 | res += cnt; |
|
|
497 | } |
|
|
498 | |
|
|
499 | { |
|
|
500 | int errorno = errno; |
|
|
501 | free (buf); |
|
|
502 | errno = errorno; |
|
|
503 | } |
|
|
504 | } |
|
|
505 | |
|
|
506 | return res; |
|
|
507 | } |
|
|
508 | |
414 | /*****************************************************************************/ |
509 | /*****************************************************************************/ |
415 | |
510 | |
416 | static void * |
511 | static void * |
417 | aio_proc (void *thr_arg) |
512 | aio_proc (void *thr_arg) |
418 | { |
513 | { |
… | |
… | |
449 | { |
544 | { |
450 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
545 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
451 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
546 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
452 | |
547 | |
453 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
548 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
549 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
454 | |
550 | |
455 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
551 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
456 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
552 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
457 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
553 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
458 | |
554 | |
… | |
… | |
581 | |
677 | |
582 | void |
678 | void |
583 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
679 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
584 | SV * fh |
680 | SV * fh |
585 | UV offset |
681 | UV offset |
586 | IV length |
682 | UV length |
587 | SV * data |
683 | SV * data |
588 | IV dataoffset |
684 | UV dataoffset |
589 | SV * callback |
685 | SV * callback |
590 | ALIAS: |
686 | ALIAS: |
591 | aio_read = REQ_READ |
687 | aio_read = REQ_READ |
592 | aio_write = REQ_WRITE |
688 | aio_write = REQ_WRITE |
593 | PROTOTYPE: $$$$$;$ |
689 | PROTOTYPE: $$$$$;$ |
… | |
… | |
639 | req->data2ptr = (void *)data; |
735 | req->data2ptr = (void *)data; |
640 | } |
736 | } |
641 | |
737 | |
642 | send_req (req); |
738 | send_req (req); |
643 | } |
739 | } |
|
|
740 | } |
|
|
741 | |
|
|
742 | void |
|
|
743 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
744 | SV * out_fh |
|
|
745 | SV * in_fh |
|
|
746 | UV in_offset |
|
|
747 | UV length |
|
|
748 | SV * callback |
|
|
749 | PROTOTYPE: $$$$;$ |
|
|
750 | CODE: |
|
|
751 | { |
|
|
752 | dREQ; |
|
|
753 | |
|
|
754 | req->type = REQ_SENDFILE; |
|
|
755 | req->fh = newSVsv (out_fh); |
|
|
756 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
757 | req->fh2 = newSVsv (in_fh); |
|
|
758 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
759 | req->offset = in_offset; |
|
|
760 | req->length = length; |
|
|
761 | |
|
|
762 | send_req (req); |
644 | } |
763 | } |
645 | |
764 | |
646 | void |
765 | void |
647 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
766 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
648 | SV * fh |
767 | SV * fh |