ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.62 by root, Mon Oct 23 22:45:18 2006 UTC vs.
Revision 1.68 by root, Tue Oct 24 03:40:25 2006 UTC

1#if __linux
2# define _GNU_SOURCE
3#endif
4
1#define _REENTRANT 1 5#define _REENTRANT 1
6
2#include <errno.h> 7#include <errno.h>
3 8
4#include "EXTERN.h" 9#include "EXTERN.h"
5#include "perl.h" 10#include "perl.h"
6#include "XSUB.h" 11#include "XSUB.h"
41# define NAME_MAX 4096 46# define NAME_MAX 4096
42#endif 47#endif
43 48
44#if __ia64 49#if __ia64
45# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
46#else 53#else
47# define STACKSIZE 8192 54# define STACKSIZE 16384
48#endif 55#endif
56
57/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536
59
60#define dBUF \
61 char *aio_buf = malloc (AIO_BUFSIZE); \
62 if (!aio_buf) \
63 return -1;
64
65#define fBUF free (aio_buf)
49 66
50enum { 67enum {
51 REQ_QUIT, 68 REQ_QUIT,
52 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
53 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
100 PRI_MIN = -4, 117 PRI_MIN = -4,
101 PRI_MAX = 4, 118 PRI_MAX = 4,
102 119
103 DEFAULT_PRI = 0, 120 DEFAULT_PRI = 0,
104 PRI_BIAS = -PRI_MIN, 121 PRI_BIAS = -PRI_MIN,
122 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
105}; 123};
106 124
107static int next_pri = DEFAULT_PRI + PRI_BIAS; 125static int next_pri = DEFAULT_PRI + PRI_BIAS;
108 126
109static int started, wanted; 127static int started, wanted;
110static volatile int nreqs; 128static volatile int nreqs;
111static int max_outstanding = 1<<30; 129static int max_outstanding = 1<<30;
112static int respipe [2]; 130static int respipe [2];
113 131
132#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
133# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
134#else
135# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
136#endif
137
114static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reslock = AIO_MUTEX_INIT;
115static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 139static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
116static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 140static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
117 141
118static volatile aio_req reqs, reqe; /* queue start, queue end */ 142/*
119static volatile aio_req ress, rese; /* queue start, queue end */ 143 * a somewhat faster data structure might be nice, but
144 * with 8 priorities this actually needs <20 insns
145 * per shift, the most expensive operation.
146 */
147typedef struct {
148 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
149 int size;
150} reqq;
151
152static reqq req_queue;
153static reqq res_queue;
154
155int reqq_push (reqq *q, aio_req req)
156{
157 int pri = req->pri;
158 req->next = 0;
159
160 if (q->qe[pri])
161 {
162 q->qe[pri]->next = req;
163 q->qe[pri] = req;
164 }
165 else
166 q->qe[pri] = q->qs[pri] = req;
167
168 return q->size++;
169}
170
171aio_req reqq_shift (reqq *q)
172{
173 int pri;
174
175 if (!q->size)
176 return 0;
177
178 --q->size;
179
180 for (pri = NUM_PRI; pri--; )
181 {
182 aio_req req = q->qs[pri];
183
184 if (req)
185 {
186 if (!(q->qs[pri] = req->next))
187 q->qe[pri] = 0;
188
189 return req;
190 }
191 }
192
193 abort ();
194}
120 195
121static void req_invoke (aio_req req); 196static void req_invoke (aio_req req);
122static void req_free (aio_req req); 197static void req_free (aio_req req);
123 198
124/* must be called at most once */ 199/* must be called at most once */
158 ENTER; 233 ENTER;
159 SAVETMPS; 234 SAVETMPS;
160 PUSHMARK (SP); 235 PUSHMARK (SP);
161 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 236 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
162 PUTBACK; 237 PUTBACK;
163 call_sv (grp->fh2, G_VOID | G_EVAL); 238 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
164 SPAGAIN; 239 SPAGAIN;
165 FREETMPS; 240 FREETMPS;
166 LEAVE; 241 LEAVE;
167 } 242 }
168 243
195{ 270{
196 fd_set rfd; 271 fd_set rfd;
197 272
198 while (nreqs) 273 while (nreqs)
199 { 274 {
200 aio_req req; 275 int size;
276#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
201 pthread_mutex_lock (&reslock); 277 pthread_mutex_lock (&reslock);
202 req = ress; 278#endif
279 size = res_queue.size;
280#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
203 pthread_mutex_unlock (&reslock); 281 pthread_mutex_unlock (&reslock);
282#endif
204 283
205 if (req) 284 if (size)
206 return; 285 return;
207 286
208 FD_ZERO(&rfd); 287 FD_ZERO(&rfd);
209 FD_SET(respipe [0], &rfd); 288 FD_SET(respipe [0], &rfd);
210 289
213} 292}
214 293
215static void req_invoke (aio_req req) 294static void req_invoke (aio_req req)
216{ 295{
217 dSP; 296 dSP;
218 int errorno = errno;
219 297
220 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) 298 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
221 return; 299 {
222
223 errno = req->errorno; 300 errno = req->errorno;
224 301
225 ENTER; 302 ENTER;
226 SAVETMPS; 303 SAVETMPS;
227 PUSHMARK (SP); 304 PUSHMARK (SP);
228 EXTEND (SP, 1); 305 EXTEND (SP, 1);
229 306
230 switch (req->type) 307 switch (req->type)
231 {
232 case REQ_READDIR:
233 { 308 {
234 SV *rv = &PL_sv_undef; 309 case REQ_READDIR:
235
236 if (req->result >= 0)
237 { 310 {
238 char *buf = req->data2ptr; 311 SV *rv = &PL_sv_undef;
239 AV *av = newAV ();
240 312
241 while (req->result) 313 if (req->result >= 0)
242 { 314 {
315 char *buf = req->data2ptr;
316 AV *av = newAV ();
317
318 while (req->result)
319 {
243 SV *sv = newSVpv (buf, 0); 320 SV *sv = newSVpv (buf, 0);
244 321
245 av_push (av, sv); 322 av_push (av, sv);
246 buf += SvCUR (sv) + 1; 323 buf += SvCUR (sv) + 1;
247 req->result--; 324 req->result--;
325 }
326
327 rv = sv_2mortal (newRV_noinc ((SV *)av));
248 } 328 }
249 329
250 rv = sv_2mortal (newRV_noinc ((SV *)av)); 330 PUSHs (rv);
251 } 331 }
332 break;
252 333
253 PUSHs (rv); 334 case REQ_OPEN:
335 {
336 /* convert fd to fh */
337 SV *fh;
338
339 PUSHs (sv_2mortal (newSViv (req->result)));
340 PUTBACK;
341 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
342 SPAGAIN;
343
344 fh = SvREFCNT_inc (POPs);
345
346 PUSHMARK (SP);
347 XPUSHs (sv_2mortal (fh));
348 }
349 break;
350
351 case REQ_GROUP:
352 req->fd = 2; /* mark group as finished */
353
354 if (req->data)
355 {
356 int i;
357 AV *av = (AV *)req->data;
358
359 EXTEND (SP, AvFILL (av) + 1);
360 for (i = 0; i <= AvFILL (av); ++i)
361 PUSHs (*av_fetch (av, i, 0));
362 }
363 break;
364
365 case REQ_NOP:
366 case REQ_SLEEP:
367 break;
368
369 default:
370 PUSHs (sv_2mortal (newSViv (req->result)));
371 break;
254 } 372 }
255 break;
256 373
257 case REQ_OPEN:
258 {
259 /* convert fd to fh */
260 SV *fh;
261 374
262 PUSHs (sv_2mortal (newSViv (req->result)));
263 PUTBACK; 375 PUTBACK;
264 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
265 SPAGAIN;
266
267 fh = SvREFCNT_inc (POPs);
268
269 PUSHMARK (SP);
270 XPUSHs (sv_2mortal (fh));
271 }
272 break;
273
274 case REQ_GROUP:
275 req->fd = 2; /* mark group as finished */
276
277 if (req->data)
278 {
279 int i;
280 AV *av = (AV *)req->data;
281
282 EXTEND (SP, AvFILL (av) + 1);
283 for (i = 0; i <= AvFILL (av); ++i)
284 PUSHs (*av_fetch (av, i, 0));
285 }
286 break;
287
288 case REQ_NOP:
289 case REQ_SLEEP:
290 break;
291
292 default:
293 PUSHs (sv_2mortal (newSViv (req->result)));
294 break;
295 }
296
297
298 PUTBACK;
299 call_sv (req->callback, G_VOID | G_EVAL); 376 call_sv (req->callback, G_VOID | G_EVAL);
300 SPAGAIN; 377 SPAGAIN;
301 378
302 FREETMPS; 379 FREETMPS;
303 LEAVE; 380 LEAVE;
304
305 errno = errorno;
306
307 if (SvTRUE (ERRSV))
308 { 381 }
309 req_free (req);
310 croak (0);
311 }
312}
313 382
314static void req_free (aio_req req)
315{
316 if (req->grp) 383 if (req->grp)
317 { 384 {
318 aio_req grp = req->grp; 385 aio_req grp = req->grp;
319 386
320 /* unlink request */ 387 /* unlink request */
325 grp->grp_first = req->grp_next; 392 grp->grp_first = req->grp_next;
326 393
327 aio_grp_dec (grp); 394 aio_grp_dec (grp);
328 } 395 }
329 396
397 if (SvTRUE (ERRSV))
398 {
399 req_free (req);
400 croak (0);
401 }
402}
403
404static void req_free (aio_req req)
405{
330 if (req->self) 406 if (req->self)
331 { 407 {
332 sv_unmagic (req->self, PERL_MAGIC_ext); 408 sv_unmagic (req->self, PERL_MAGIC_ext);
333 SvREFCNT_dec (req->self); 409 SvREFCNT_dec (req->self);
334 } 410 }
366 aio_req req; 442 aio_req req;
367 443
368 for (;;) 444 for (;;)
369 { 445 {
370 pthread_mutex_lock (&reslock); 446 pthread_mutex_lock (&reslock);
371 req = ress; 447 req = reqq_shift (&res_queue);
372 448
373 if (req) 449 if (req)
374 { 450 {
375 ress = req->next;
376
377 if (!ress) 451 if (!res_queue.size)
378 { 452 {
379 /* read any signals sent by the worker threads */ 453 /* read any signals sent by the worker threads */
380 char buf [32]; 454 char buf [32];
381 while (read (respipe [0], buf, 32) == 32) 455 while (read (respipe [0], buf, 32) == 32)
382 ; 456 ;
383
384 rese = 0;
385 } 457 }
386 } 458 }
387 459
388 pthread_mutex_unlock (&reslock); 460 pthread_mutex_unlock (&reslock);
389 461
452 start_thread (); 524 start_thread ();
453 525
454 ++nreqs; 526 ++nreqs;
455 527
456 pthread_mutex_lock (&reqlock); 528 pthread_mutex_lock (&reqlock);
457 529 reqq_push (&req_queue, req);
458 req->next = 0;
459
460 if (reqe)
461 {
462 reqe->next = req;
463 reqe = req;
464 }
465 else
466 reqe = reqs = req;
467
468 pthread_cond_signal (&reqwait); 530 pthread_cond_signal (&reqwait);
469 pthread_mutex_unlock (&reqlock); 531 pthread_mutex_unlock (&reqlock);
470 532
471 if (nreqs > max_outstanding) 533 if (nreqs > max_outstanding)
472 for (;;) 534 for (;;)
481} 543}
482 544
483static void end_thread (void) 545static void end_thread (void)
484{ 546{
485 aio_req req; 547 aio_req req;
548
486 Newz (0, req, 1, aio_cb); 549 Newz (0, req, 1, aio_cb);
550
487 req->type = REQ_QUIT; 551 req->type = REQ_QUIT;
552 req->pri = PRI_MAX + PRI_BIAS;
488 553
489 req_send (req); 554 req_send (req);
490} 555}
491 556
492static void min_parallel (int nthreads) 557static void min_parallel (int nthreads)
579#if !HAVE_READAHEAD 644#if !HAVE_READAHEAD
580# define readahead aio_readahead 645# define readahead aio_readahead
581 646
582static ssize_t readahead (int fd, off_t offset, size_t count) 647static ssize_t readahead (int fd, off_t offset, size_t count)
583{ 648{
584 char readahead_buf[4096]; 649 dBUF;
585 650
586 while (count > 0) 651 while (count > 0)
587 { 652 {
588 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 653 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
589 654
590 pread (fd, readahead_buf, len, offset); 655 pread (fd, aio_buf, len, offset);
591 offset += len; 656 offset += len;
592 count -= len; 657 count -= len;
593 } 658 }
659
660 fBUF;
594 661
595 errno = 0; 662 errno = 0;
596} 663}
597#endif 664#endif
598 665
684#endif 751#endif
685 ) 752 )
686 ) 753 )
687 { 754 {
688 /* emulate sendfile. this is a major pain in the ass */ 755 /* emulate sendfile. this is a major pain in the ass */
689 char buf[4096]; 756 dBUF;
757
690 res = 0; 758 res = 0;
691 759
692 while (count) 760 while (count)
693 { 761 {
694 ssize_t cnt; 762 ssize_t cnt;
695 763
696 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 764 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
697 765
698 if (cnt <= 0) 766 if (cnt <= 0)
699 { 767 {
700 if (cnt && !res) res = -1; 768 if (cnt && !res) res = -1;
701 break; 769 break;
702 } 770 }
703 771
704 cnt = write (ofd, buf, cnt); 772 cnt = write (ofd, aio_buf, cnt);
705 773
706 if (cnt <= 0) 774 if (cnt <= 0)
707 { 775 {
708 if (cnt && !res) res = -1; 776 if (cnt && !res) res = -1;
709 break; 777 break;
711 779
712 offset += cnt; 780 offset += cnt;
713 res += cnt; 781 res += cnt;
714 count -= cnt; 782 count -= cnt;
715 } 783 }
784
785 fBUF;
716 } 786 }
717 787
718 return res; 788 return res;
719} 789}
720 790
721/* read a full directory */ 791/* read a full directory */
722static int scandir_ (const char *path, void **namesp) 792static int scandir_ (const char *path, void **namesp)
723{ 793{
724 DIR *dirp = opendir (path); 794 DIR *dirp;
725 union 795 union
726 { 796 {
727 struct dirent d; 797 struct dirent d;
728 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 798 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
729 } u; 799 } *u;
730 struct dirent *entp; 800 struct dirent *entp;
731 char *name, *names; 801 char *name, *names;
732 int memlen = 4096; 802 int memlen = 4096;
733 int memofs = 0; 803 int memofs = 0;
734 int res = 0; 804 int res = 0;
735 int errorno; 805 int errorno;
736 806
807 dirp = opendir (path);
737 if (!dirp) 808 if (!dirp)
738 return -1; 809 return -1;
739 810
811 u = malloc (sizeof (*u));
740 names = malloc (memlen); 812 names = malloc (memlen);
741 813
814 if (u && names)
742 for (;;) 815 for (;;)
743 { 816 {
817 errno = 0;
744 errno = 0, readdir_r (dirp, &u.d, &entp); 818 readdir_r (dirp, &u->d, &entp);
745 819
746 if (!entp) 820 if (!entp)
747 break; 821 break;
748 822
749 name = entp->d_name; 823 name = entp->d_name;
750 824
751 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 825 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
752 { 826 {
753 int len = strlen (name) + 1; 827 int len = strlen (name) + 1;
754 828
755 res++; 829 res++;
756 830
757 while (memofs + len > memlen) 831 while (memofs + len > memlen)
758 { 832 {
759 memlen *= 2; 833 memlen *= 2;
760 names = realloc (names, memlen); 834 names = realloc (names, memlen);
761 if (!names) 835 if (!names)
762 break; 836 break;
763 } 837 }
764 838
765 memcpy (names + memofs, name, len); 839 memcpy (names + memofs, name, len);
766 memofs += len; 840 memofs += len;
767 } 841 }
768 } 842 }
769 843
770 errorno = errno; 844 errorno = errno;
845 free (u);
771 closedir (dirp); 846 closedir (dirp);
772 847
773 if (errorno) 848 if (errorno)
774 { 849 {
775 free (names); 850 free (names);
792 { 867 {
793 pthread_mutex_lock (&reqlock); 868 pthread_mutex_lock (&reqlock);
794 869
795 for (;;) 870 for (;;)
796 { 871 {
797 req = reqs; 872 req = reqq_shift (&req_queue);
798
799 if (reqs)
800 {
801 reqs = reqs->next;
802 if (!reqs) reqe = 0;
803 }
804 873
805 if (req) 874 if (req)
806 break; 875 break;
807 876
808 pthread_cond_wait (&reqwait, &reqlock); 877 pthread_cond_wait (&reqwait, &reqlock);
860 929
861 req->errorno = errno; 930 req->errorno = errno;
862 931
863 pthread_mutex_lock (&reslock); 932 pthread_mutex_lock (&reslock);
864 933
865 req->next = 0; 934 if (!reqq_push (&res_queue, req))
866
867 if (rese)
868 {
869 rese->next = req;
870 rese = req;
871 }
872 else
873 {
874 rese = ress = req;
875
876 /* write a dummy byte to the pipe so fh becomes ready */ 935 /* write a dummy byte to the pipe so fh becomes ready */
877 write (respipe [1], &respipe, 1); 936 write (respipe [1], &respipe, 1);
878 }
879 937
880 pthread_mutex_unlock (&reslock); 938 pthread_mutex_unlock (&reslock);
881 } 939 }
882 while (type != REQ_QUIT); 940 while (type != REQ_QUIT);
883 941
914{ 972{
915 aio_req prv; 973 aio_req prv;
916 974
917 started = 0; 975 started = 0;
918 976
919 while (reqs) 977 while (prv = reqq_shift (&req_queue))
920 {
921 prv = reqs;
922 reqs = prv->next;
923 req_free (prv); 978 req_free (prv);
924 }
925 979
926 reqs = reqe = 0; 980 while (prv = reqq_shift (&res_queue))
927
928 while (ress)
929 {
930 prv = ress;
931 ress = prv->next;
932 req_free (prv); 981 req_free (prv);
933 } 982
934
935 ress = rese = 0;
936
937 close (respipe [0]); 983 close (respipe [0]);
938 close (respipe [1]); 984 close (respipe [1]);
939 create_pipe (); 985 create_pipe ();
940 986
941 atfork_parent (); 987 atfork_parent ();
1256 req->type = REQ_NOP; 1302 req->type = REQ_NOP;
1257 1303
1258 REQ_SEND; 1304 REQ_SEND;
1259} 1305}
1260 1306
1261#if 0
1262
1263void 1307void
1264aio_pri (int pri = DEFAULT_PRI) 1308aioreq_pri (int pri = DEFAULT_PRI)
1265 CODE: 1309 CODE:
1266 if (pri < PRI_MIN) pri = PRI_MIN; 1310 if (pri < PRI_MIN) pri = PRI_MIN;
1267 if (pri > PRI_MAX) pri = PRI_MAX; 1311 if (pri > PRI_MAX) pri = PRI_MAX;
1268 next_pri = pri + PRI_BIAS; 1312 next_pri = pri + PRI_BIAS;
1269 1313
1270#endif 1314void
1315aioreq_nice (int nice = 0)
1316 CODE:
1317 nice = next_pri - nice;
1318 if (nice < PRI_MIN) nice = PRI_MIN;
1319 if (nice > PRI_MAX) nice = PRI_MAX;
1320 next_pri = nice + PRI_BIAS;
1271 1321
1272void 1322void
1273flush () 1323flush ()
1274 PROTOTYPE: 1324 PROTOTYPE:
1275 CODE: 1325 CODE:
1324 1374
1325MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1375MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1326 1376
1327void 1377void
1328cancel (aio_req_ornot req) 1378cancel (aio_req_ornot req)
1329 PROTOTYPE:
1330 CODE: 1379 CODE:
1331 req_cancel (req); 1380 req_cancel (req);
1332 1381
1333void 1382void
1334cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1383cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1384 SvREFCNT_dec (grp->data); 1433 SvREFCNT_dec (grp->data);
1385 grp->data = (SV *)av; 1434 grp->data = (SV *)av;
1386} 1435}
1387 1436
1388void 1437void
1389feed_limit (aio_req grp, int limit) 1438limit (aio_req grp, int limit)
1390 CODE: 1439 CODE:
1391 grp->fd2 = limit; 1440 grp->fd2 = limit;
1392 aio_grp_feed (grp); 1441 aio_grp_feed (grp);
1393 1442
1394void 1443void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines