1 |
#define PERL_NO_GET_CONTEXT |
2 |
|
3 |
#include "EXTERN.h" |
4 |
#include "perl.h" |
5 |
#include "XSUB.h" |
6 |
|
7 |
#include <sys/types.h> |
8 |
#include <sys/stat.h> |
9 |
#include <unistd.h> |
10 |
#include <fcntl.h> |
11 |
#include <signal.h> |
12 |
#include <sched.h> |
13 |
|
14 |
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 *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 |
|
18 |
#ifndef __NR_pread64 |
19 |
# define __NR_pread64 __NR_pread |
20 |
#endif |
21 |
#ifndef __NR_pwrite64 |
22 |
# define __NR_pwrite64 __NR_pwrite |
23 |
#endif |
24 |
|
25 |
#define STACKSIZE 1024 /* yeah */ |
26 |
|
27 |
enum { |
28 |
REQ_QUIT, |
29 |
REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
30 |
REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
31 |
}; |
32 |
|
33 |
typedef struct { |
34 |
char stack[STACKSIZE]; |
35 |
} aio_thread; |
36 |
|
37 |
typedef struct aio_cb { |
38 |
struct aio_cb *next; |
39 |
|
40 |
int type; |
41 |
aio_thread *thread; |
42 |
|
43 |
int fd; |
44 |
off_t offset; |
45 |
size_t length; |
46 |
ssize_t result; |
47 |
mode_t mode; /* open */ |
48 |
int errorno; |
49 |
SV *data, *callback; |
50 |
void *dataptr; |
51 |
STRLEN dataoffset; |
52 |
|
53 |
struct stat64 *statdata; |
54 |
} aio_cb; |
55 |
|
56 |
typedef aio_cb *aio_req; |
57 |
|
58 |
static int started; |
59 |
static int nreqs; |
60 |
static int reqpipe[2], respipe[2]; |
61 |
|
62 |
static aio_req qs, qe; /* queue start, queue end */ |
63 |
|
64 |
static int aio_proc(void *arg); |
65 |
|
66 |
static void |
67 |
start_thread (void) |
68 |
{ |
69 |
aio_thread *thr; |
70 |
|
71 |
New (0, thr, 1, aio_thread); |
72 |
|
73 |
if (clone (aio_proc, |
74 |
&(thr->stack[STACKSIZE]), |
75 |
CLONE_VM|CLONE_FS|CLONE_FILES, |
76 |
thr) >= 0) |
77 |
started++; |
78 |
else |
79 |
Safefree (thr); |
80 |
} |
81 |
|
82 |
static void |
83 |
send_reqs (void) |
84 |
{ |
85 |
/* this write is atomic */ |
86 |
while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
87 |
{ |
88 |
qs = qs->next; |
89 |
if (!qs) qe = 0; |
90 |
} |
91 |
} |
92 |
|
93 |
static void |
94 |
send_req (aio_req req) |
95 |
{ |
96 |
nreqs++; |
97 |
req->next = 0; |
98 |
|
99 |
if (qe) |
100 |
qe->next = req; |
101 |
else |
102 |
qe = qs = req; |
103 |
|
104 |
send_reqs (); |
105 |
} |
106 |
|
107 |
static void |
108 |
end_thread (void) |
109 |
{ |
110 |
aio_req req; |
111 |
New (0, req, 1, aio_cb); |
112 |
req->type = REQ_QUIT; |
113 |
|
114 |
send_req (req); |
115 |
} |
116 |
|
117 |
static void |
118 |
read_write (pTHX_ |
119 |
int dowrite, int fd, off_t offset, size_t length, |
120 |
SV *data, STRLEN dataoffset, SV *callback) |
121 |
{ |
122 |
aio_req req; |
123 |
STRLEN svlen; |
124 |
char *svptr = SvPV (data, svlen); |
125 |
|
126 |
SvUPGRADE (data, SVt_PV); |
127 |
SvPOK_on (data); |
128 |
|
129 |
if (dataoffset < 0) |
130 |
dataoffset += svlen; |
131 |
|
132 |
if (dataoffset < 0 || dataoffset > svlen) |
133 |
croak ("data offset outside of string"); |
134 |
|
135 |
if (dowrite) |
136 |
{ |
137 |
/* write: check length and adjust. */ |
138 |
if (length < 0 || length + dataoffset > svlen) |
139 |
length = svlen - dataoffset; |
140 |
} |
141 |
else |
142 |
{ |
143 |
/* read: grow scalar as necessary */ |
144 |
svptr = SvGROW (data, length + dataoffset); |
145 |
} |
146 |
|
147 |
if (length < 0) |
148 |
croak ("length must not be negative"); |
149 |
|
150 |
Newz (0, req, 1, aio_cb); |
151 |
|
152 |
if (!req) |
153 |
croak ("out of memory during aio_req allocation"); |
154 |
|
155 |
req->type = dowrite ? REQ_WRITE : REQ_READ; |
156 |
req->fd = fd; |
157 |
req->offset = offset; |
158 |
req->length = length; |
159 |
req->data = SvREFCNT_inc (data); |
160 |
req->dataptr = (char *)svptr + dataoffset; |
161 |
req->callback = SvREFCNT_inc (callback); |
162 |
|
163 |
send_req (req); |
164 |
} |
165 |
|
166 |
static int |
167 |
poll_cb (pTHX) |
168 |
{ |
169 |
dSP; |
170 |
int count = 0; |
171 |
aio_req req; |
172 |
|
173 |
while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
174 |
{ |
175 |
if (req->type == REQ_QUIT) |
176 |
{ |
177 |
Safefree (req->thread); |
178 |
started--; |
179 |
} |
180 |
else |
181 |
{ |
182 |
int errorno = errno; |
183 |
errno = req->errorno; |
184 |
|
185 |
if (req->type == REQ_READ) |
186 |
SvCUR_set (req->data, req->dataoffset |
187 |
+ req->result > 0 ? req->result : 0); |
188 |
|
189 |
if (req->data) |
190 |
SvREFCNT_dec (req->data); |
191 |
|
192 |
if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
193 |
{ |
194 |
PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
195 |
PL_laststatval = req->result; |
196 |
PL_statcache.st_dev = req->statdata->st_dev; |
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 |
|
210 |
Safefree (req->statdata); |
211 |
} |
212 |
|
213 |
PUSHMARK (SP); |
214 |
XPUSHs (sv_2mortal (newSViv (req->result))); |
215 |
PUTBACK; |
216 |
call_sv (req->callback, G_VOID); |
217 |
SPAGAIN; |
218 |
|
219 |
if (req->callback) |
220 |
SvREFCNT_dec (req->callback); |
221 |
|
222 |
errno = errorno; |
223 |
nreqs--; |
224 |
count++; |
225 |
} |
226 |
|
227 |
Safefree (req); |
228 |
} |
229 |
|
230 |
if (qs) |
231 |
send_reqs (); |
232 |
|
233 |
return count; |
234 |
} |
235 |
|
236 |
static sigset_t fullsigset; |
237 |
|
238 |
#undef errno |
239 |
#include <asm/unistd.h> |
240 |
#include <sys/prctl.h> |
241 |
|
242 |
static int |
243 |
aio_proc (void *thr_arg) |
244 |
{ |
245 |
aio_thread *thr = thr_arg; |
246 |
aio_req req; |
247 |
int errno; |
248 |
|
249 |
/* this is very much x86 and kernel-specific :(:(:( */ |
250 |
/* we rely on gcc's ability to create closures. */ |
251 |
_syscall3(int,read,int,fd,char *,buf,size_t,count) |
252 |
_syscall3(int,write,int,fd,char *,buf,size_t,count) |
253 |
|
254 |
_syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
255 |
_syscall1(int,close,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); |
265 |
|
266 |
sigprocmask (SIG_SETMASK, &fullsigset, 0); |
267 |
prctl (PR_SET_PDEATHSIG, SIGKILL); |
268 |
|
269 |
/* then loop */ |
270 |
while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
271 |
{ |
272 |
req->thread = thr; |
273 |
errno = 0; /* strictly unnecessary */ |
274 |
|
275 |
switch (req->type) |
276 |
{ |
277 |
case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
278 |
case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
279 |
case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
280 |
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; |
285 |
|
286 |
case REQ_QUIT: |
287 |
default: |
288 |
write (respipe[1], (void *)&req, sizeof (req)); |
289 |
return 0; |
290 |
} |
291 |
|
292 |
req->errorno = errno; |
293 |
write (respipe[1], (void *)&req, sizeof (req)); |
294 |
} |
295 |
|
296 |
return 0; |
297 |
} |
298 |
|
299 |
MODULE = Linux::AIO PACKAGE = Linux::AIO |
300 |
|
301 |
BOOT: |
302 |
{ |
303 |
sigfillset (&fullsigset); |
304 |
sigdelset (&fullsigset, SIGTERM); |
305 |
sigdelset (&fullsigset, SIGQUIT); |
306 |
sigdelset (&fullsigset, SIGABRT); |
307 |
sigdelset (&fullsigset, SIGINT); |
308 |
|
309 |
if (pipe (reqpipe) || pipe (respipe)) |
310 |
croak ("unable to initialize request or result pipe"); |
311 |
|
312 |
if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
313 |
croak ("cannot set result pipe to nonblocking mode"); |
314 |
|
315 |
if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
316 |
croak ("cannot set result pipe to nonblocking mode"); |
317 |
} |
318 |
|
319 |
void |
320 |
min_parallel(nthreads) |
321 |
int nthreads |
322 |
PROTOTYPE: $ |
323 |
CODE: |
324 |
while (nthreads > started) |
325 |
start_thread (); |
326 |
|
327 |
void |
328 |
max_parallel(nthreads) |
329 |
int nthreads |
330 |
PROTOTYPE: $ |
331 |
CODE: |
332 |
int cur = started; |
333 |
while (cur > nthreads) |
334 |
{ |
335 |
end_thread (); |
336 |
cur--; |
337 |
} |
338 |
|
339 |
while (started > nthreads) |
340 |
{ |
341 |
fd_set rfd; |
342 |
FD_ZERO(&rfd); |
343 |
FD_SET(respipe[0], &rfd); |
344 |
|
345 |
select (respipe[0] + 1, &rfd, 0, 0, 0); |
346 |
poll_cb (aTHX); |
347 |
} |
348 |
|
349 |
void |
350 |
aio_open(pathname,flags,mode,callback) |
351 |
SV * pathname |
352 |
int flags |
353 |
int mode |
354 |
SV * callback |
355 |
PROTOTYPE: $$$$ |
356 |
CODE: |
357 |
aio_req req; |
358 |
|
359 |
Newz (0, req, 1, aio_cb); |
360 |
|
361 |
if (!req) |
362 |
croak ("out of memory during aio_req allocation"); |
363 |
|
364 |
req->type = REQ_OPEN; |
365 |
req->data = newSVsv (pathname); |
366 |
req->dataptr = SvPV_nolen (req->data); |
367 |
req->fd = flags; |
368 |
req->mode = mode; |
369 |
req->callback = SvREFCNT_inc (callback); |
370 |
|
371 |
send_req (req); |
372 |
|
373 |
void |
374 |
aio_close(fh,callback) |
375 |
InputStream fh |
376 |
SV * callback |
377 |
PROTOTYPE: $$ |
378 |
CODE: |
379 |
aio_req req; |
380 |
|
381 |
Newz (0, req, 1, aio_cb); |
382 |
|
383 |
if (!req) |
384 |
croak ("out of memory during aio_req allocation"); |
385 |
|
386 |
req->type = REQ_CLOSE; |
387 |
req->fd = PerlIO_fileno (fh); |
388 |
req->callback = SvREFCNT_inc (callback); |
389 |
|
390 |
send_req (req); |
391 |
|
392 |
void |
393 |
aio_read(fh,offset,length,data,dataoffset,callback) |
394 |
InputStream fh |
395 |
UV offset |
396 |
IV length |
397 |
SV * data |
398 |
IV dataoffset |
399 |
SV * callback |
400 |
PROTOTYPE: $$$$$$ |
401 |
CODE: |
402 |
read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
403 |
|
404 |
void |
405 |
aio_write(fh,offset,length,data,dataoffset,callback) |
406 |
OutputStream fh |
407 |
UV offset |
408 |
IV length |
409 |
SV * data |
410 |
IV dataoffset |
411 |
SV * callback |
412 |
PROTOTYPE: $$$$$$ |
413 |
CODE: |
414 |
read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
415 |
|
416 |
void |
417 |
aio_stat(fh_or_path,callback) |
418 |
SV * fh_or_path |
419 |
SV * callback |
420 |
PROTOTYPE: $$ |
421 |
ALIAS: |
422 |
aio_lstat = 1 |
423 |
CODE: |
424 |
aio_req req; |
425 |
|
426 |
Newz (0, req, 1, aio_cb); |
427 |
|
428 |
if (!req) |
429 |
croak ("out of memory during aio_req allocation"); |
430 |
|
431 |
New (0, req->statdata, 1, struct stat64); |
432 |
|
433 |
if (!req->statdata) |
434 |
croak ("out of memory during aio_req->statdata allocation"); |
435 |
|
436 |
if (SvPOK (fh_or_path)) |
437 |
{ |
438 |
req->type = ix ? REQ_LSTAT : REQ_STAT; |
439 |
req->data = newSVsv (fh_or_path); |
440 |
req->dataptr = SvPV_nolen (req->data); |
441 |
} |
442 |
else |
443 |
{ |
444 |
req->type = REQ_FSTAT; |
445 |
req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
446 |
} |
447 |
|
448 |
req->callback = SvREFCNT_inc (callback); |
449 |
|
450 |
send_req (req); |
451 |
|
452 |
void |
453 |
aio_unlink(pathname,callback) |
454 |
SV * pathname |
455 |
SV * callback |
456 |
PROTOTYPE: $$ |
457 |
CODE: |
458 |
aio_req req; |
459 |
|
460 |
Newz (0, req, 1, aio_cb); |
461 |
|
462 |
if (!req) |
463 |
croak ("out of memory during aio_req allocation"); |
464 |
|
465 |
req->type = REQ_UNLINK; |
466 |
req->data = newSVsv (pathname); |
467 |
req->dataptr = SvPV_nolen (req->data); |
468 |
req->callback = SvREFCNT_inc (callback); |
469 |
|
470 |
send_req (req); |
471 |
|
472 |
int |
473 |
poll_fileno() |
474 |
PROTOTYPE: |
475 |
CODE: |
476 |
RETVAL = respipe[0]; |
477 |
OUTPUT: |
478 |
RETVAL |
479 |
|
480 |
int |
481 |
poll_cb(...) |
482 |
PROTOTYPE: |
483 |
CODE: |
484 |
RETVAL = poll_cb (aTHX); |
485 |
OUTPUT: |
486 |
RETVAL |
487 |
|
488 |
int |
489 |
nreqs() |
490 |
PROTOTYPE: |
491 |
CODE: |
492 |
RETVAL = nreqs; |
493 |
OUTPUT: |
494 |
RETVAL |
495 |
|