… | |
… | |
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 | |
14 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
16 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
16 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | typedef 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 | |
|
|
25 | #define STACKSIZE 1024 /* yeah */ |
|
|
26 | |
26 | |
27 | enum { |
27 | enum { |
28 | REQ_QUIT, |
28 | REQ_QUIT, |
29 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
29 | REQ_OPEN, REQ_CLOSE, |
|
|
30 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
30 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
31 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
|
|
32 | REQ_FSYNC, REQ_FDATASYNC, |
31 | }; |
33 | }; |
32 | |
34 | |
33 | typedef struct { |
35 | typedef struct { |
34 | char stack[STACKSIZE]; |
36 | char stack[STACKSIZE]; |
35 | } aio_thread; |
37 | } aio_thread; |
… | |
… | |
48 | int errorno; |
50 | int errorno; |
49 | SV *data, *callback; |
51 | SV *data, *callback; |
50 | void *dataptr; |
52 | void *dataptr; |
51 | STRLEN dataoffset; |
53 | STRLEN dataoffset; |
52 | |
54 | |
53 | struct stat64 *statdata; |
55 | Stat_t *statdata; |
54 | } aio_cb; |
56 | } aio_cb; |
55 | |
57 | |
56 | typedef aio_cb *aio_req; |
58 | typedef aio_cb *aio_req; |
57 | |
59 | |
58 | static int started; |
60 | static int started; |
… | |
… | |
69 | aio_thread *thr; |
71 | aio_thread *thr; |
70 | |
72 | |
71 | New (0, thr, 1, aio_thread); |
73 | New (0, thr, 1, aio_thread); |
72 | |
74 | |
73 | if (clone (aio_proc, |
75 | if (clone (aio_proc, |
74 | &(thr->stack[STACKSIZE]), |
76 | &(thr->stack[STACKSIZE - 16]), |
75 | CLONE_VM|CLONE_FS|CLONE_FILES, |
77 | CLONE_VM|CLONE_FS|CLONE_FILES, |
76 | thr) >= 0) |
78 | thr) >= 0) |
77 | started++; |
79 | started++; |
78 | else |
80 | else |
79 | Safefree (thr); |
81 | Safefree (thr); |
… | |
… | |
95 | { |
97 | { |
96 | nreqs++; |
98 | nreqs++; |
97 | req->next = 0; |
99 | req->next = 0; |
98 | |
100 | |
99 | if (qe) |
101 | if (qe) |
|
|
102 | { |
100 | qe->next = req; |
103 | qe->next = req; |
|
|
104 | qe = req; |
|
|
105 | } |
101 | else |
106 | else |
102 | qe = qs = req; |
107 | qe = qs = req; |
103 | |
108 | |
104 | send_reqs (); |
109 | send_reqs (); |
105 | } |
110 | } |
… | |
… | |
161 | req->callback = SvREFCNT_inc (callback); |
166 | req->callback = SvREFCNT_inc (callback); |
162 | |
167 | |
163 | send_req (req); |
168 | send_req (req); |
164 | } |
169 | } |
165 | |
170 | |
|
|
171 | static void |
|
|
172 | poll_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 | |
166 | static int |
181 | static int |
167 | poll_cb (pTHX) |
182 | poll_cb (pTHX) |
168 | { |
183 | { |
169 | dSP; |
184 | dSP; |
170 | int count = 0; |
185 | int count = 0; |
171 | aio_req req; |
186 | aio_req req; |
172 | |
187 | |
173 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
188 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
174 | { |
189 | { |
|
|
190 | nreqs--; |
|
|
191 | |
175 | if (req->type == REQ_QUIT) |
192 | if (req->type == REQ_QUIT) |
176 | { |
193 | { |
177 | Safefree (req->thread); |
194 | Safefree (req->thread); |
178 | started--; |
195 | started--; |
179 | } |
196 | } |
… | |
… | |
189 | if (req->data) |
206 | if (req->data) |
190 | SvREFCNT_dec (req->data); |
207 | SvREFCNT_dec (req->data); |
191 | |
208 | |
192 | 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) |
193 | { |
210 | { |
194 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
211 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
195 | PL_laststatval = req->result; |
212 | PL_laststatval = req->result; |
196 | PL_statcache.st_dev = req->statdata->st_dev; |
213 | PL_statcache = *(req->statdata); |
197 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
198 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
199 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
200 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
201 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
202 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
203 | PL_statcache.st_size = req->statdata->st_size; |
|
|
204 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
205 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
206 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
207 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
208 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
209 | |
214 | |
210 | Safefree (req->statdata); |
215 | Safefree (req->statdata); |
211 | } |
216 | } |
212 | |
217 | |
213 | PUSHMARK (SP); |
218 | PUSHMARK (SP); |
… | |
… | |
218 | |
223 | |
219 | if (req->callback) |
224 | if (req->callback) |
220 | SvREFCNT_dec (req->callback); |
225 | SvREFCNT_dec (req->callback); |
221 | |
226 | |
222 | errno = errorno; |
227 | errno = errorno; |
223 | nreqs--; |
|
|
224 | count++; |
228 | count++; |
225 | } |
229 | } |
226 | |
230 | |
227 | Safefree (req); |
231 | Safefree (req); |
228 | } |
232 | } |
… | |
… | |
235 | |
239 | |
236 | static sigset_t fullsigset; |
240 | static sigset_t fullsigset; |
237 | |
241 | |
238 | #undef errno |
242 | #undef errno |
239 | #include <asm/unistd.h> |
243 | #include <asm/unistd.h> |
|
|
244 | #include <linux/types.h> |
240 | #include <sys/prctl.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; \ |
241 | |
272 | |
242 | static int |
273 | static int |
243 | aio_proc (void *thr_arg) |
274 | aio_proc (void *thr_arg) |
244 | { |
275 | { |
245 | aio_thread *thr = thr_arg; |
276 | aio_thread *thr = thr_arg; |
246 | aio_req req; |
277 | aio_req req; |
247 | int errno; |
278 | int errno; |
248 | |
279 | |
249 | /* this is very much x86 and kernel-specific :(:(:( */ |
280 | /* this is very much kernel-specific :(:(:( */ |
250 | /* we rely on gcc's ability to create closures. */ |
281 | /* we rely on gcc's ability to create closures. */ |
251 | _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) |
252 | _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) |
253 | |
284 | |
254 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
285 | _syscall3(long, open, char *, pathname, int, flags, int, mode) |
255 | _syscall1(int,close,int,fd) |
286 | _syscall1(long, close, unsigned int, fd) |
256 | |
|
|
257 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
258 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
259 | |
|
|
260 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
|
|
261 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
|
|
262 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
|
|
263 | |
|
|
264 | _syscall1(int,unlink, char *, filename); |
287 | _syscall1(long, unlink, char *, filename); |
|
|
288 | _syscall1(long, fsync, unsigned int, fd); |
|
|
289 | _syscall1(long, fdatasync, unsigned int, fd); |
265 | |
290 | |
|
|
291 | #if BYTE_ORDER == LITTLE_ENDIAN |
|
|
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 |
|
|
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 |
|
|
308 | _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf) |
|
|
309 | _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, 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 |
|
|
318 | |
|
|
319 | /* the following two calls might clobber errno */ |
266 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
320 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
267 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
321 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
268 | |
322 | |
269 | /* then loop */ |
323 | /* then loop */ |
270 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
324 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
… | |
… | |
272 | req->thread = thr; |
326 | req->thread = thr; |
273 | errno = 0; /* strictly unnecessary */ |
327 | errno = 0; /* strictly unnecessary */ |
274 | |
328 | |
275 | switch (req->type) |
329 | switch (req->type) |
276 | { |
330 | { |
277 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
331 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break; |
278 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); 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 | |
279 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
347 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
280 | case REQ_CLOSE: req->result = close (req->fd); break; |
348 | case REQ_CLOSE: req->result = close (req->fd); break; |
281 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
282 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
|
|
283 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
284 | case REQ_UNLINK: req->result = unlink (req->dataptr); 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; |
285 | |
353 | |
286 | case REQ_QUIT: |
354 | case REQ_QUIT: |
287 | default: |
|
|
288 | write (respipe[1], (void *)&req, sizeof (req)); |
355 | write (respipe[1], (void *)&req, sizeof (req)); |
289 | return 0; |
356 | return 0; |
|
|
357 | |
|
|
358 | default: |
|
|
359 | req->result = ENOSYS; |
|
|
360 | break; |
290 | } |
361 | } |
291 | |
362 | |
292 | req->errorno = errno; |
363 | req->errorno = errno; |
293 | write (respipe[1], (void *)&req, sizeof (req)); |
364 | write (respipe[1], (void *)&req, sizeof (req)); |
294 | } |
365 | } |
… | |
… | |
336 | cur--; |
407 | cur--; |
337 | } |
408 | } |
338 | |
409 | |
339 | while (started > nthreads) |
410 | while (started > nthreads) |
340 | { |
411 | { |
341 | fd_set rfd; |
412 | poll_wait (); |
342 | FD_ZERO(&rfd); |
|
|
343 | FD_SET(respipe[0], &rfd); |
|
|
344 | |
|
|
345 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
346 | poll_cb (aTHX); |
413 | poll_cb (aTHX); |
347 | } |
414 | } |
348 | |
415 | |
349 | void |
416 | void |
350 | aio_open(pathname,flags,mode,callback) |
417 | aio_open(pathname,flags,mode,callback) |
… | |
… | |
373 | void |
440 | void |
374 | aio_close(fh,callback) |
441 | aio_close(fh,callback) |
375 | InputStream fh |
442 | InputStream fh |
376 | SV * callback |
443 | SV * callback |
377 | PROTOTYPE: $$ |
444 | PROTOTYPE: $$ |
|
|
445 | ALIAS: |
|
|
446 | aio_close = REQ_CLOSE |
|
|
447 | aio_fsync = REQ_FSYNC |
|
|
448 | aio_fdatasync = REQ_FDATASYNC |
378 | CODE: |
449 | CODE: |
379 | aio_req req; |
450 | aio_req req; |
380 | |
451 | |
381 | Newz (0, req, 1, aio_cb); |
452 | Newz (0, req, 1, aio_cb); |
382 | |
453 | |
383 | if (!req) |
454 | if (!req) |
384 | croak ("out of memory during aio_req allocation"); |
455 | croak ("out of memory during aio_req allocation"); |
385 | |
456 | |
386 | req->type = REQ_CLOSE; |
457 | req->type = ix; |
387 | req->fd = PerlIO_fileno (fh); |
458 | req->fd = PerlIO_fileno (fh); |
388 | req->callback = SvREFCNT_inc (callback); |
459 | req->callback = SvREFCNT_inc (callback); |
389 | |
460 | |
390 | send_req (req); |
461 | send_req (req); |
391 | |
462 | |
… | |
… | |
426 | Newz (0, req, 1, aio_cb); |
497 | Newz (0, req, 1, aio_cb); |
427 | |
498 | |
428 | if (!req) |
499 | if (!req) |
429 | croak ("out of memory during aio_req allocation"); |
500 | croak ("out of memory during aio_req allocation"); |
430 | |
501 | |
431 | New (0, req->statdata, 1, struct stat64); |
502 | New (0, req->statdata, 1, Stat_t); |
432 | |
503 | |
433 | if (!req->statdata) |
504 | if (!req->statdata) |
434 | croak ("out of memory during aio_req->statdata allocation"); |
505 | croak ("out of memory during aio_req->statdata allocation"); |
435 | |
506 | |
436 | if (SvPOK (fh_or_path)) |
507 | if (SvPOK (fh_or_path)) |
… | |
… | |
483 | CODE: |
554 | CODE: |
484 | RETVAL = poll_cb (aTHX); |
555 | RETVAL = poll_cb (aTHX); |
485 | OUTPUT: |
556 | OUTPUT: |
486 | RETVAL |
557 | RETVAL |
487 | |
558 | |
|
|
559 | void |
|
|
560 | poll_wait() |
|
|
561 | PROTOTYPE: |
|
|
562 | CODE: |
|
|
563 | poll_wait (); |
|
|
564 | |
488 | int |
565 | int |
489 | nreqs() |
566 | nreqs() |
490 | PROTOTYPE: |
567 | PROTOTYPE: |
491 | CODE: |
568 | CODE: |
492 | RETVAL = nreqs; |
569 | RETVAL = nreqs; |