… | |
… | |
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 | |
26 | |
25 | #define STACKSIZE 1024 /* yeah */ |
27 | enum { |
26 | |
28 | REQ_QUIT, |
27 | enum { 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 | |
29 | typedef struct { |
35 | typedef 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 | |
52 | typedef aio_cb *aio_req; |
58 | typedef aio_cb *aio_req; |
53 | |
59 | |
54 | static int started; |
60 | static 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 | |
|
|
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 | |
162 | static int |
181 | static int |
163 | poll_cb (pTHX) |
182 | poll_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 | } |
… | |
… | |
231 | |
239 | |
232 | static sigset_t fullsigset; |
240 | static sigset_t fullsigset; |
233 | |
241 | |
234 | #undef errno |
242 | #undef errno |
235 | #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; \ |
236 | |
272 | |
237 | static int |
273 | static int |
238 | aio_proc (void *thr_arg) |
274 | aio_proc (void *thr_arg) |
239 | { |
275 | { |
240 | aio_thread *thr = thr_arg; |
276 | aio_thread *thr = thr_arg; |
241 | aio_req req; |
277 | aio_req req; |
242 | int errno; |
278 | int errno; |
243 | |
279 | |
244 | /* this is very much x86 and kernel-specific :(:(:( */ |
280 | /* this is very much kernel-specific :(:(:( */ |
245 | /* we rely on gcc's ability to create closures. */ |
281 | /* we rely on gcc's ability to create closures. */ |
246 | _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) |
247 | _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) |
248 | |
284 | |
249 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
285 | _syscall3(long, open, char *, pathname, int, flags, int, mode) |
250 | _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); |
251 | |
290 | |
252 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
291 | #if BYTE_ORDER == LITTLE_ENDIAN |
253 | _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 |
254 | |
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 |
255 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
308 | _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf) |
256 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
309 | _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf) |
257 | _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 |
258 | |
318 | |
|
|
319 | /* the following two calls might clobber errno */ |
259 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
320 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
321 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
260 | |
322 | |
261 | /* then loop */ |
323 | /* then loop */ |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
324 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
263 | { |
325 | { |
264 | req->thread = thr; |
326 | req->thread = thr; |
265 | errno = 0; /* strictly unnecessary */ |
327 | errno = 0; /* strictly unnecessary */ |
266 | |
328 | |
267 | switch (req->type) |
329 | switch (req->type) |
268 | { |
330 | { |
269 | 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; |
270 | 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 | |
271 | 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; |
272 | case REQ_CLOSE: req->result = close (req->fd); break; |
348 | case REQ_CLOSE: req->result = close (req->fd); break; |
273 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
349 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
274 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
350 | |
|
|
351 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
275 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
352 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
276 | |
353 | |
277 | case REQ_QUIT: |
354 | case REQ_QUIT: |
278 | default: |
|
|
279 | write (respipe[1], (void *)&req, sizeof (req)); |
355 | write (respipe[1], (void *)&req, sizeof (req)); |
280 | return 0; |
356 | return 0; |
|
|
357 | |
|
|
358 | default: |
|
|
359 | req->result = ENOSYS; |
|
|
360 | break; |
281 | } |
361 | } |
282 | |
362 | |
283 | req->errorno = errno; |
363 | req->errorno = errno; |
284 | write (respipe[1], (void *)&req, sizeof (req)); |
364 | write (respipe[1], (void *)&req, sizeof (req)); |
285 | } |
365 | } |
… | |
… | |
327 | cur--; |
407 | cur--; |
328 | } |
408 | } |
329 | |
409 | |
330 | while (started > nthreads) |
410 | while (started > nthreads) |
331 | { |
411 | { |
332 | fd_set rfd; |
412 | poll_wait (); |
333 | FD_ZERO(&rfd); |
|
|
334 | FD_SET(respipe[0], &rfd); |
|
|
335 | |
|
|
336 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
337 | poll_cb (aTHX); |
413 | poll_cb (aTHX); |
338 | } |
414 | } |
339 | |
415 | |
340 | void |
416 | void |
341 | aio_open(pathname,flags,mode,callback) |
417 | aio_open(pathname,flags,mode,callback) |
… | |
… | |
364 | void |
440 | void |
365 | aio_close(fh,callback) |
441 | aio_close(fh,callback) |
366 | InputStream fh |
442 | InputStream fh |
367 | SV * callback |
443 | SV * callback |
368 | PROTOTYPE: $$ |
444 | PROTOTYPE: $$ |
|
|
445 | ALIAS: |
|
|
446 | aio_close = REQ_CLOSE |
|
|
447 | aio_fsync = REQ_FSYNC |
|
|
448 | aio_fdatasync = REQ_FDATASYNC |
369 | CODE: |
449 | CODE: |
370 | aio_req req; |
450 | aio_req req; |
371 | |
451 | |
372 | Newz (0, req, 1, aio_cb); |
452 | Newz (0, req, 1, aio_cb); |
373 | |
453 | |
374 | if (!req) |
454 | if (!req) |
375 | croak ("out of memory during aio_req allocation"); |
455 | croak ("out of memory during aio_req allocation"); |
376 | |
456 | |
377 | req->type = REQ_CLOSE; |
457 | req->type = ix; |
378 | req->fd = PerlIO_fileno (fh); |
458 | req->fd = PerlIO_fileno (fh); |
379 | req->callback = SvREFCNT_inc (callback); |
459 | req->callback = SvREFCNT_inc (callback); |
380 | |
460 | |
381 | send_req (req); |
461 | send_req (req); |
382 | |
462 | |
… | |
… | |
417 | Newz (0, req, 1, aio_cb); |
497 | Newz (0, req, 1, aio_cb); |
418 | |
498 | |
419 | if (!req) |
499 | if (!req) |
420 | croak ("out of memory during aio_req allocation"); |
500 | croak ("out of memory during aio_req allocation"); |
421 | |
501 | |
422 | New (0, req->statdata, 1, struct stat64); |
502 | New (0, req->statdata, 1, Stat_t); |
423 | |
503 | |
424 | if (!req->statdata) |
504 | if (!req->statdata) |
425 | croak ("out of memory during aio_req->statdata allocation"); |
505 | croak ("out of memory during aio_req->statdata allocation"); |
426 | |
506 | |
427 | if (SvPOK (fh_or_path)) |
507 | if (SvPOK (fh_or_path)) |
… | |
… | |
438 | |
518 | |
439 | req->callback = SvREFCNT_inc (callback); |
519 | req->callback = SvREFCNT_inc (callback); |
440 | |
520 | |
441 | send_req (req); |
521 | send_req (req); |
442 | |
522 | |
|
|
523 | void |
|
|
524 | aio_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 | |
443 | int |
543 | int |
444 | poll_fileno() |
544 | poll_fileno() |
445 | PROTOTYPE: |
545 | PROTOTYPE: |
446 | CODE: |
546 | CODE: |
447 | RETVAL = respipe[0]; |
547 | RETVAL = respipe[0]; |
… | |
… | |
454 | CODE: |
554 | CODE: |
455 | RETVAL = poll_cb (aTHX); |
555 | RETVAL = poll_cb (aTHX); |
456 | OUTPUT: |
556 | OUTPUT: |
457 | RETVAL |
557 | RETVAL |
458 | |
558 | |
|
|
559 | void |
|
|
560 | poll_wait() |
|
|
561 | PROTOTYPE: |
|
|
562 | CODE: |
|
|
563 | poll_wait (); |
|
|
564 | |
459 | int |
565 | int |
460 | nreqs() |
566 | nreqs() |
461 | PROTOTYPE: |
567 | PROTOTYPE: |
462 | CODE: |
568 | CODE: |
463 | RETVAL = nreqs; |
569 | RETVAL = nreqs; |