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.18 by root, Thu May 6 15:05:57 2004 UTC vs.
Revision 1.30 by root, Sat Jul 9 23:38:07 2005 UTC

8#include <sys/stat.h> 8#include <sys/stat.h>
9#include <unistd.h> 9#include <unistd.h>
10#include <fcntl.h> 10#include <fcntl.h>
11#include <signal.h> 11#include <signal.h>
12#include <sched.h> 12#include <sched.h>
13#include <endian.h>
13 14
14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
17 18
18#ifndef __NR_pread64 19#if __i386 || __amd64
19# 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))
20#endif 25#endif
21#ifndef __NR_pwrite64
22# define __NR_pwrite64 __NR_pwrite
23#endif
24 26
25#define STACKSIZE 1024 /* yeah */ 27enum {
26 28 REQ_QUIT,
27enum { 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};
28 34
29typedef struct { 35typedef struct {
30 char stack[STACKSIZE]; 36 char stack[STACKSIZE];
31} aio_thread; 37} aio_thread;
32 38
44 int errorno; 50 int errorno;
45 SV *data, *callback; 51 SV *data, *callback;
46 void *dataptr; 52 void *dataptr;
47 STRLEN dataoffset; 53 STRLEN dataoffset;
48 54
49 struct stat64 *statdata; 55 Stat_t *statdata;
50} aio_cb; 56} aio_cb;
51 57
52typedef aio_cb *aio_req; 58typedef aio_cb *aio_req;
53 59
54static int started; 60static int started;
65 aio_thread *thr; 71 aio_thread *thr;
66 72
67 New (0, thr, 1, aio_thread); 73 New (0, thr, 1, aio_thread);
68 74
69 if (clone (aio_proc, 75 if (clone (aio_proc,
70 &(thr->stack[STACKSIZE]), 76 &(thr->stack[STACKSIZE - 16]),
71 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 77 CLONE_VM|CLONE_FS|CLONE_FILES,
72 thr) >= 0) 78 thr) >= 0)
73 started++; 79 started++;
74 else 80 else
75 Safefree (thr); 81 Safefree (thr);
76} 82}
91{ 97{
92 nreqs++; 98 nreqs++;
93 req->next = 0; 99 req->next = 0;
94 100
95 if (qe) 101 if (qe)
102 {
96 qe->next = req; 103 qe->next = req;
104 qe = req;
105 }
97 else 106 else
98 qe = qs = req; 107 qe = qs = req;
99 108
100 send_reqs (); 109 send_reqs ();
101} 110}
157 req->callback = SvREFCNT_inc (callback); 166 req->callback = SvREFCNT_inc (callback);
158 167
159 send_req (req); 168 send_req (req);
160} 169}
161 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
162static int 181static int
163poll_cb (pTHX) 182poll_cb (pTHX)
164{ 183{
165 dSP; 184 dSP;
166 int count = 0; 185 int count = 0;
167 aio_req req; 186 aio_req req;
168 187
169 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 188 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
170 { 189 {
190 nreqs--;
191
171 if (req->type == REQ_QUIT) 192 if (req->type == REQ_QUIT)
172 { 193 {
173 Safefree (req->thread); 194 Safefree (req->thread);
174 started--; 195 started--;
175 } 196 }
185 if (req->data) 206 if (req->data)
186 SvREFCNT_dec (req->data); 207 SvREFCNT_dec (req->data);
187 208
188 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)
189 { 210 {
190 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 211 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
191 PL_laststatval = req->result; 212 PL_laststatval = req->result;
192 PL_statcache.st_dev = req->statdata->st_dev; 213 PL_statcache = *(req->statdata);
193 PL_statcache.st_ino = req->statdata->st_ino;
194 PL_statcache.st_mode = req->statdata->st_mode;
195 PL_statcache.st_nlink = req->statdata->st_nlink;
196 PL_statcache.st_uid = req->statdata->st_uid;
197 PL_statcache.st_gid = req->statdata->st_gid;
198 PL_statcache.st_rdev = req->statdata->st_rdev;
199 PL_statcache.st_size = req->statdata->st_size;
200 PL_statcache.st_atime = req->statdata->st_atime;
201 PL_statcache.st_mtime = req->statdata->st_mtime;
202 PL_statcache.st_ctime = req->statdata->st_ctime;
203 PL_statcache.st_blksize = req->statdata->st_blksize;
204 PL_statcache.st_blocks = req->statdata->st_blocks;
205 214
206 Safefree (req->statdata); 215 Safefree (req->statdata);
207 } 216 }
208 217
209 PUSHMARK (SP); 218 PUSHMARK (SP);
214 223
215 if (req->callback) 224 if (req->callback)
216 SvREFCNT_dec (req->callback); 225 SvREFCNT_dec (req->callback);
217 226
218 errno = errorno; 227 errno = errorno;
219 nreqs--;
220 count++; 228 count++;
221 } 229 }
222 230
223 Safefree (req); 231 Safefree (req);
224 } 232 }
225 233
226 if (qs) 234 if (qs)
227 {
228 printf ("outstanding %p %d\n", qs, nreqs);
229 send_reqs (); 235 send_reqs ();
230 }
231 236
232 return count; 237 return count;
233} 238}
234 239
235static sigset_t fullsigset; 240static sigset_t fullsigset;
236 241
237#undef errno 242#undef errno
238#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; \
239 272
240static int 273static int
241aio_proc (void *thr_arg) 274aio_proc (void *thr_arg)
242{ 275{
243 aio_thread *thr = thr_arg; 276 aio_thread *thr = thr_arg;
244 aio_req req; 277 aio_req req;
245 int errno; 278 int errno;
246 279
247 /* this is very much x86 and kernel-specific :(:(:( */ 280 /* this is very much kernel-specific :(:(:( */
248 /* we rely on gcc's ability to create closures. */ 281 /* we rely on gcc's ability to create closures. */
249 _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)
250 _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)
251 284
252 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 285 _syscall3(long, open, char *, pathname, int, flags, int, mode)
253 _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);
254 289
255 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 290#ifndef __NR_fdatasync
256 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 291# define __NR_fdatasync __NR_fsync
292#endif
293 _syscall1(long, fdatasync, unsigned int, fd);
257 294
295#if BYTE_ORDER == LITTLE_ENDIAN
296# define LOFF_ARG(off) (off & 0xffffffff), (off >> 32)
297#elif BYTE_ORDER == BIG_ENDIAN
298# define LOFF_ARG(off) (off >> 32), (off & 0xffffffff)
299#endif
300
301#ifndef __NR_pread64
302# define __NR_pread64 __NR_pread
303# define __NR_pwrite64 __NR_write
304#endif
305 _syscall5(__kernel_ssize_t, pread64 , unsigned int, fd, char *, buf,
306 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
307 _syscall5(__kernel_ssize_t, pwrite64, unsigned int, fd, char *, buf,
308 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
309 _syscall4(long, readahead, unsigned int, fd, unsigned int, offset_lh, unsigned int, offset_hl, __kernel_size_t, count);
310
311#if __NR_stat64
258 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 312 _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf)
259 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 313 _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf)
260 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 314 _syscall2(long, fstat64, int , fd , struct kernelstat64 *, buf)
315#elif __NR_stat
316 _syscall2(long, stat , const char *, filename, struct kernelstat *, buf)
317 _syscall2(long, lstat, const char *, filename, struct kernelstat *, buf)
318 _syscall2(long, fstat, int , fd , struct kernelstat *, buf)
319#else
320# error "neither stat64 nor stat defined"
321#endif
261 322
323 /* the following two calls might clobber errno */
262 sigprocmask (SIG_SETMASK, &fullsigset, 0); 324 sigprocmask (SIG_SETMASK, &fullsigset, 0);
325 prctl (PR_SET_PDEATHSIG, SIGKILL);
263 326
264 /* then loop */ 327 /* then loop */
265 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 328 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
266 { 329 {
267 req->thread = thr; 330 req->thread = thr;
268 errno = 0; /* strictly unnecessary */ 331 errno = 0; /* strictly unnecessary */
269 332
270 switch (req->type) 333 switch (req->type)
271 { 334 {
272 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 335 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
273 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 336 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
337 case REQ_READAHEAD: req->result = readahead (req->fd, LOFF_ARG (req->offset), req->length); break;
338
339#if __NR_stat64
340 struct kernelstat64 statdata;
341 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
342 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
343 case REQ_FSTAT: req->result = fstat64 (req->fd , &statdata); COPY_STATDATA; break;
344#else
345 struct kernelstat statdata;
346 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
347 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
348 case REQ_FSTAT: req->result = fstat (req->fd , &statdata); COPY_STATDATA; break;
349#endif
350
274 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 351 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
275 case REQ_CLOSE: req->result = close (req->fd); break; 352 case REQ_CLOSE: req->result = close (req->fd); break;
276 case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; 353 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
277 case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; 354
355 case REQ_FSYNC: req->result = fsync (req->fd); break;
278 case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; 356 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
279 357
280 case REQ_QUIT: 358 case REQ_QUIT:
281 default:
282 write (respipe[1], (void *)&req, sizeof (req)); 359 write (respipe[1], (void *)&req, sizeof (req));
283 return 0; 360 return 0;
361
362 default:
363 req->result = ENOSYS;
364 break;
284 } 365 }
285 366
286 req->errorno = errno; 367 req->errorno = errno;
287 write (respipe[1], (void *)&req, sizeof (req)); 368 write (respipe[1], (void *)&req, sizeof (req));
288 } 369 }
330 cur--; 411 cur--;
331 } 412 }
332 413
333 while (started > nthreads) 414 while (started > nthreads)
334 { 415 {
335 fd_set rfd; 416 poll_wait ();
336 FD_ZERO(&rfd);
337 FD_SET(respipe[0], &rfd);
338
339 select (respipe[0] + 1, &rfd, 0, 0, 0);
340 poll_cb (aTHX); 417 poll_cb (aTHX);
341 } 418 }
342 419
343void 420void
344aio_open(pathname,flags,mode,callback) 421aio_open(pathname,flags,mode,callback)
367void 444void
368aio_close(fh,callback) 445aio_close(fh,callback)
369 InputStream fh 446 InputStream fh
370 SV * callback 447 SV * callback
371 PROTOTYPE: $$ 448 PROTOTYPE: $$
449 ALIAS:
450 aio_close = REQ_CLOSE
451 aio_fsync = REQ_FSYNC
452 aio_fdatasync = REQ_FDATASYNC
372 CODE: 453 CODE:
373 aio_req req; 454 aio_req req;
374 455
375 Newz (0, req, 1, aio_cb); 456 Newz (0, req, 1, aio_cb);
376 457
377 if (!req) 458 if (!req)
378 croak ("out of memory during aio_req allocation"); 459 croak ("out of memory during aio_req allocation");
379 460
380 req->type = REQ_CLOSE; 461 req->type = ix;
381 req->fd = PerlIO_fileno (fh); 462 req->fd = PerlIO_fileno (fh);
382 req->callback = SvREFCNT_inc (callback); 463 req->callback = SvREFCNT_inc (callback);
383 464
384 send_req (req); 465 send_req (req);
385 466
420 Newz (0, req, 1, aio_cb); 501 Newz (0, req, 1, aio_cb);
421 502
422 if (!req) 503 if (!req)
423 croak ("out of memory during aio_req allocation"); 504 croak ("out of memory during aio_req allocation");
424 505
425 New (0, req->statdata, 1, struct stat64); 506 New (0, req->statdata, 1, Stat_t);
426 507
427 if (!req->statdata) 508 if (!req->statdata)
428 croak ("out of memory during aio_req->statdata allocation"); 509 croak ("out of memory during aio_req->statdata allocation");
429 510
430 if (SvPOK (fh_or_path)) 511 if (SvPOK (fh_or_path))
441 522
442 req->callback = SvREFCNT_inc (callback); 523 req->callback = SvREFCNT_inc (callback);
443 524
444 send_req (req); 525 send_req (req);
445 526
527void
528aio_unlink(pathname,callback)
529 SV * pathname
530 SV * callback
531 PROTOTYPE: $$
532 CODE:
533 aio_req req;
534
535 Newz (0, req, 1, aio_cb);
536
537 if (!req)
538 croak ("out of memory during aio_req allocation");
539
540 req->type = REQ_UNLINK;
541 req->data = newSVsv (pathname);
542 req->dataptr = SvPV_nolen (req->data);
543 req->callback = SvREFCNT_inc (callback);
544
545 send_req (req);
546
446int 547int
447poll_fileno() 548poll_fileno()
448 PROTOTYPE: 549 PROTOTYPE:
449 CODE: 550 CODE:
450 RETVAL = respipe[0]; 551 RETVAL = respipe[0];
457 CODE: 558 CODE:
458 RETVAL = poll_cb (aTHX); 559 RETVAL = poll_cb (aTHX);
459 OUTPUT: 560 OUTPUT:
460 RETVAL 561 RETVAL
461 562
563void
564poll_wait()
565 PROTOTYPE:
566 CODE:
567 poll_wait ();
568
462int 569int
463nreqs() 570nreqs()
464 PROTOTYPE: 571 PROTOTYPE:
465 CODE: 572 CODE:
466 RETVAL = nreqs; 573 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines