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.61 by root, Mon Oct 23 22:44:21 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
191 } 266 }
192} 267}
193 268
194static void poll_wait () 269static void poll_wait ()
195{ 270{
271 fd_set rfd;
272
196 while (nreqs) 273 while (nreqs)
197 { 274 {
198 aio_req req; 275 int size;
276#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
199 pthread_mutex_lock (&reslock); 277 pthread_mutex_lock (&reslock);
200 req = ress; 278#endif
279 size = res_queue.size;
280#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
201 pthread_mutex_unlock (&reslock); 281 pthread_mutex_unlock (&reslock);
282#endif
202 283
203 if (req) 284 if (size)
204 return; 285 return;
205 286
206 fd_set rfd;
207 FD_ZERO(&rfd); 287 FD_ZERO(&rfd);
208 FD_SET(respipe [0], &rfd); 288 FD_SET(respipe [0], &rfd);
209 289
210 select (respipe [0] + 1, &rfd, 0, 0, 0); 290 select (respipe [0] + 1, &rfd, 0, 0, 0);
211 } 291 }
212} 292}
213 293
214static void req_invoke (aio_req req) 294static void req_invoke (aio_req req)
215{ 295{
216 dSP; 296 dSP;
217 int errorno = errno;
218 297
219 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) 298 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
220 return; 299 {
221
222 errno = req->errorno; 300 errno = req->errorno;
223 301
224 ENTER; 302 ENTER;
225 SAVETMPS; 303 SAVETMPS;
226 PUSHMARK (SP); 304 PUSHMARK (SP);
227 EXTEND (SP, 1); 305 EXTEND (SP, 1);
228 306
229 switch (req->type) 307 switch (req->type)
230 {
231 case REQ_READDIR:
232 { 308 {
233 SV *rv = &PL_sv_undef; 309 case REQ_READDIR:
234
235 if (req->result >= 0)
236 { 310 {
237 char *buf = req->data2ptr; 311 SV *rv = &PL_sv_undef;
238 AV *av = newAV ();
239 312
240 while (req->result) 313 if (req->result >= 0)
241 { 314 {
315 char *buf = req->data2ptr;
316 AV *av = newAV ();
317
318 while (req->result)
319 {
242 SV *sv = newSVpv (buf, 0); 320 SV *sv = newSVpv (buf, 0);
243 321
244 av_push (av, sv); 322 av_push (av, sv);
245 buf += SvCUR (sv) + 1; 323 buf += SvCUR (sv) + 1;
246 req->result--; 324 req->result--;
325 }
326
327 rv = sv_2mortal (newRV_noinc ((SV *)av));
247 } 328 }
248 329
249 rv = sv_2mortal (newRV_noinc ((SV *)av)); 330 PUSHs (rv);
250 } 331 }
332 break;
251 333
252 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;
253 } 372 }
254 break;
255 373
256 case REQ_OPEN:
257 {
258 /* convert fd to fh */
259 SV *fh;
260 374
261 PUSHs (sv_2mortal (newSViv (req->result)));
262 PUTBACK; 375 PUTBACK;
263 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
264 SPAGAIN;
265
266 fh = SvREFCNT_inc (POPs);
267
268 PUSHMARK (SP);
269 XPUSHs (sv_2mortal (fh));
270 }
271 break;
272
273 case REQ_GROUP:
274 req->fd = 2; /* mark group as finished */
275
276 if (req->data)
277 {
278 int i;
279 AV *av = (AV *)req->data;
280
281 EXTEND (SP, AvFILL (av) + 1);
282 for (i = 0; i <= AvFILL (av); ++i)
283 PUSHs (*av_fetch (av, i, 0));
284 }
285 break;
286
287 case REQ_NOP:
288 case REQ_SLEEP:
289 break;
290
291 default:
292 PUSHs (sv_2mortal (newSViv (req->result)));
293 break;
294 }
295
296
297 PUTBACK;
298 call_sv (req->callback, G_VOID | G_EVAL); 376 call_sv (req->callback, G_VOID | G_EVAL);
299 SPAGAIN; 377 SPAGAIN;
300 378
301 FREETMPS; 379 FREETMPS;
302 LEAVE; 380 LEAVE;
303
304 errno = errorno;
305
306 if (SvTRUE (ERRSV))
307 { 381 }
308 req_free (req);
309 croak (0);
310 }
311}
312 382
313static void req_free (aio_req req)
314{
315 if (req->grp) 383 if (req->grp)
316 { 384 {
317 aio_req grp = req->grp; 385 aio_req grp = req->grp;
318 386
319 /* unlink request */ 387 /* unlink request */
324 grp->grp_first = req->grp_next; 392 grp->grp_first = req->grp_next;
325 393
326 aio_grp_dec (grp); 394 aio_grp_dec (grp);
327 } 395 }
328 396
397 if (SvTRUE (ERRSV))
398 {
399 req_free (req);
400 croak (0);
401 }
402}
403
404static void req_free (aio_req req)
405{
329 if (req->self) 406 if (req->self)
330 { 407 {
331 sv_unmagic (req->self, PERL_MAGIC_ext); 408 sv_unmagic (req->self, PERL_MAGIC_ext);
332 SvREFCNT_dec (req->self); 409 SvREFCNT_dec (req->self);
333 } 410 }
365 aio_req req; 442 aio_req req;
366 443
367 for (;;) 444 for (;;)
368 { 445 {
369 pthread_mutex_lock (&reslock); 446 pthread_mutex_lock (&reslock);
370 req = ress; 447 req = reqq_shift (&res_queue);
371 448
372 if (req) 449 if (req)
373 { 450 {
374 ress = req->next;
375
376 if (!ress) 451 if (!res_queue.size)
377 { 452 {
378 /* read any signals sent by the worker threads */ 453 /* read any signals sent by the worker threads */
379 char buf [32]; 454 char buf [32];
380 while (read (respipe [0], buf, 32) == 32) 455 while (read (respipe [0], buf, 32) == 32)
381 ; 456 ;
382
383 rese = 0;
384 } 457 }
385 } 458 }
386 459
387 pthread_mutex_unlock (&reslock); 460 pthread_mutex_unlock (&reslock);
388 461
451 start_thread (); 524 start_thread ();
452 525
453 ++nreqs; 526 ++nreqs;
454 527
455 pthread_mutex_lock (&reqlock); 528 pthread_mutex_lock (&reqlock);
456 529 reqq_push (&req_queue, req);
457 req->next = 0;
458
459 if (reqe)
460 {
461 reqe->next = req;
462 reqe = req;
463 }
464 else
465 reqe = reqs = req;
466
467 pthread_cond_signal (&reqwait); 530 pthread_cond_signal (&reqwait);
468 pthread_mutex_unlock (&reqlock); 531 pthread_mutex_unlock (&reqlock);
469 532
470 if (nreqs > max_outstanding) 533 if (nreqs > max_outstanding)
471 for (;;) 534 for (;;)
480} 543}
481 544
482static void end_thread (void) 545static void end_thread (void)
483{ 546{
484 aio_req req; 547 aio_req req;
548
485 Newz (0, req, 1, aio_cb); 549 Newz (0, req, 1, aio_cb);
550
486 req->type = REQ_QUIT; 551 req->type = REQ_QUIT;
552 req->pri = PRI_MAX + PRI_BIAS;
487 553
488 req_send (req); 554 req_send (req);
489} 555}
490 556
491static void min_parallel (int nthreads) 557static void min_parallel (int nthreads)
578#if !HAVE_READAHEAD 644#if !HAVE_READAHEAD
579# define readahead aio_readahead 645# define readahead aio_readahead
580 646
581static ssize_t readahead (int fd, off_t offset, size_t count) 647static ssize_t readahead (int fd, off_t offset, size_t count)
582{ 648{
583 char readahead_buf[4096]; 649 dBUF;
584 650
585 while (count > 0) 651 while (count > 0)
586 { 652 {
587 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 653 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
588 654
589 pread (fd, readahead_buf, len, offset); 655 pread (fd, aio_buf, len, offset);
590 offset += len; 656 offset += len;
591 count -= len; 657 count -= len;
592 } 658 }
659
660 fBUF;
593 661
594 errno = 0; 662 errno = 0;
595} 663}
596#endif 664#endif
597 665
683#endif 751#endif
684 ) 752 )
685 ) 753 )
686 { 754 {
687 /* emulate sendfile. this is a major pain in the ass */ 755 /* emulate sendfile. this is a major pain in the ass */
688 char buf[4096]; 756 dBUF;
757
689 res = 0; 758 res = 0;
690 759
691 while (count) 760 while (count)
692 { 761 {
693 ssize_t cnt; 762 ssize_t cnt;
694 763
695 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 764 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
696 765
697 if (cnt <= 0) 766 if (cnt <= 0)
698 { 767 {
699 if (cnt && !res) res = -1; 768 if (cnt && !res) res = -1;
700 break; 769 break;
701 } 770 }
702 771
703 cnt = write (ofd, buf, cnt); 772 cnt = write (ofd, aio_buf, cnt);
704 773
705 if (cnt <= 0) 774 if (cnt <= 0)
706 { 775 {
707 if (cnt && !res) res = -1; 776 if (cnt && !res) res = -1;
708 break; 777 break;
710 779
711 offset += cnt; 780 offset += cnt;
712 res += cnt; 781 res += cnt;
713 count -= cnt; 782 count -= cnt;
714 } 783 }
784
785 fBUF;
715 } 786 }
716 787
717 return res; 788 return res;
718} 789}
719 790
720/* read a full directory */ 791/* read a full directory */
721static int scandir_ (const char *path, void **namesp) 792static int scandir_ (const char *path, void **namesp)
722{ 793{
723 DIR *dirp = opendir (path); 794 DIR *dirp;
724 union 795 union
725 { 796 {
726 struct dirent d; 797 struct dirent d;
727 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 798 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
728 } u; 799 } *u;
729 struct dirent *entp; 800 struct dirent *entp;
730 char *name, *names; 801 char *name, *names;
731 int memlen = 4096; 802 int memlen = 4096;
732 int memofs = 0; 803 int memofs = 0;
733 int res = 0; 804 int res = 0;
734 int errorno; 805 int errorno;
735 806
807 dirp = opendir (path);
736 if (!dirp) 808 if (!dirp)
737 return -1; 809 return -1;
738 810
811 u = malloc (sizeof (*u));
739 names = malloc (memlen); 812 names = malloc (memlen);
740 813
814 if (u && names)
741 for (;;) 815 for (;;)
742 { 816 {
817 errno = 0;
743 errno = 0, readdir_r (dirp, &u.d, &entp); 818 readdir_r (dirp, &u->d, &entp);
744 819
745 if (!entp) 820 if (!entp)
746 break; 821 break;
747 822
748 name = entp->d_name; 823 name = entp->d_name;
749 824
750 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 825 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
751 { 826 {
752 int len = strlen (name) + 1; 827 int len = strlen (name) + 1;
753 828
754 res++; 829 res++;
755 830
756 while (memofs + len > memlen) 831 while (memofs + len > memlen)
757 { 832 {
758 memlen *= 2; 833 memlen *= 2;
759 names = realloc (names, memlen); 834 names = realloc (names, memlen);
760 if (!names) 835 if (!names)
761 break; 836 break;
762 } 837 }
763 838
764 memcpy (names + memofs, name, len); 839 memcpy (names + memofs, name, len);
765 memofs += len; 840 memofs += len;
766 } 841 }
767 } 842 }
768 843
769 errorno = errno; 844 errorno = errno;
845 free (u);
770 closedir (dirp); 846 closedir (dirp);
771 847
772 if (errorno) 848 if (errorno)
773 { 849 {
774 free (names); 850 free (names);
791 { 867 {
792 pthread_mutex_lock (&reqlock); 868 pthread_mutex_lock (&reqlock);
793 869
794 for (;;) 870 for (;;)
795 { 871 {
796 req = reqs; 872 req = reqq_shift (&req_queue);
797
798 if (reqs)
799 {
800 reqs = reqs->next;
801 if (!reqs) reqe = 0;
802 }
803 873
804 if (req) 874 if (req)
805 break; 875 break;
806 876
807 pthread_cond_wait (&reqwait, &reqlock); 877 pthread_cond_wait (&reqwait, &reqlock);
859 929
860 req->errorno = errno; 930 req->errorno = errno;
861 931
862 pthread_mutex_lock (&reslock); 932 pthread_mutex_lock (&reslock);
863 933
864 req->next = 0; 934 if (!reqq_push (&res_queue, req))
865
866 if (rese)
867 {
868 rese->next = req;
869 rese = req;
870 }
871 else
872 {
873 rese = ress = req;
874
875 /* write a dummy byte to the pipe so fh becomes ready */ 935 /* write a dummy byte to the pipe so fh becomes ready */
876 write (respipe [1], &respipe, 1); 936 write (respipe [1], &respipe, 1);
877 }
878 937
879 pthread_mutex_unlock (&reslock); 938 pthread_mutex_unlock (&reslock);
880 } 939 }
881 while (type != REQ_QUIT); 940 while (type != REQ_QUIT);
882 941
913{ 972{
914 aio_req prv; 973 aio_req prv;
915 974
916 started = 0; 975 started = 0;
917 976
918 while (reqs) 977 while (prv = reqq_shift (&req_queue))
919 {
920 prv = reqs;
921 reqs = prv->next;
922 req_free (prv); 978 req_free (prv);
923 }
924 979
925 reqs = reqe = 0; 980 while (prv = reqq_shift (&res_queue))
926
927 while (ress)
928 {
929 prv = ress;
930 ress = prv->next;
931 req_free (prv); 981 req_free (prv);
932 } 982
933
934 ress = rese = 0;
935
936 close (respipe [0]); 983 close (respipe [0]);
937 close (respipe [1]); 984 close (respipe [1]);
938 create_pipe (); 985 create_pipe ();
939 986
940 atfork_parent (); 987 atfork_parent ();
1255 req->type = REQ_NOP; 1302 req->type = REQ_NOP;
1256 1303
1257 REQ_SEND; 1304 REQ_SEND;
1258} 1305}
1259 1306
1260#if 0
1261
1262void 1307void
1263aio_pri (int pri = DEFAULT_PRI) 1308aioreq_pri (int pri = DEFAULT_PRI)
1264 CODE: 1309 CODE:
1265 if (pri < PRI_MIN) pri = PRI_MIN; 1310 if (pri < PRI_MIN) pri = PRI_MIN;
1266 if (pri > PRI_MAX) pri = PRI_MAX; 1311 if (pri > PRI_MAX) pri = PRI_MAX;
1267 next_pri = pri + PRI_BIAS; 1312 next_pri = pri + PRI_BIAS;
1268 1313
1269#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;
1270 1321
1271void 1322void
1272flush () 1323flush ()
1273 PROTOTYPE: 1324 PROTOTYPE:
1274 CODE: 1325 CODE:
1323 1374
1324MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1375MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1325 1376
1326void 1377void
1327cancel (aio_req_ornot req) 1378cancel (aio_req_ornot req)
1328 PROTOTYPE:
1329 CODE: 1379 CODE:
1330 req_cancel (req); 1380 req_cancel (req);
1331 1381
1332void 1382void
1333cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1383cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1383 SvREFCNT_dec (grp->data); 1433 SvREFCNT_dec (grp->data);
1384 grp->data = (SV *)av; 1434 grp->data = (SV *)av;
1385} 1435}
1386 1436
1387void 1437void
1388feed_limit (aio_req grp, int limit) 1438limit (aio_req grp, int limit)
1389 CODE: 1439 CODE:
1390 grp->fd2 = limit; 1440 grp->fd2 = limit;
1391 aio_grp_feed (grp); 1441 aio_grp_feed (grp);
1392 1442
1393void 1443void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines