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

Comparing Linux-AIO/AIO.xs (file contents):
Revision 1.16 by root, Wed May 5 10:13:30 2004 UTC vs.
Revision 1.29 by root, Sat Jul 9 22:45:06 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines