… | |
… | |
6 | |
6 | |
7 | use Linux::AIO; |
7 | use Linux::AIO; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | This module implements asynchroneous i/o using the means available to |
11 | This module implements asynchronous i/o using the means available to linux |
12 | linux - clone. It does not hook into the POSIX aio_* functions because |
12 | - clone. It does not hook into the POSIX aio_* functions because linux |
13 | linux does not yet support these in the kernel. Instead, a number of |
13 | does not yet support these in the kernel (and even if, it would only allow |
14 | threads are started that execute your read/writes and signal their |
14 | aio_read and write, not open and stat). |
15 | completion. |
15 | |
|
|
16 | Instead, in this module a number of (non-posix) threads are started that |
|
|
17 | execute your read/writes and signal their completion. You don't need |
|
|
18 | thread support in your libc or perl, and the threads created by this |
|
|
19 | module will not be visible to the pthreads library. |
16 | |
20 | |
17 | =over 4 |
21 | =over 4 |
18 | |
22 | |
19 | =cut |
23 | =cut |
20 | |
24 | |
21 | package Linux::AIO; |
25 | package Linux::AIO; |
22 | |
26 | |
23 | use base 'Exporter'; |
27 | use base 'Exporter'; |
24 | |
28 | |
25 | BEGIN { |
29 | BEGIN { |
26 | $VERSION = 0.2; |
30 | $VERSION = '1.0'; |
27 | |
31 | |
28 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat); |
32 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat); |
29 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
33 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
30 | |
34 | |
31 | require XSLoader; |
35 | require XSLoader; |
32 | XSLoader::load Linux::AIO, $VERSION; |
36 | XSLoader::load Linux::AIO, $VERSION; |
33 | } |
37 | } |
34 | |
38 | |
35 | =item Linux::AIO::min_parallel($nthreads) |
39 | =item Linux::AIO::min_parallel $nthreads |
36 | |
40 | |
37 | Set the minimum number of AIO threads to $nthreads. You I<have> to call |
41 | Set the minimum number of AIO threads to C<$nthreads>. You I<have> to call |
38 | this function with a positive number at leats once, otherwise no threads |
42 | this function with a positive number at least once, otherwise no threads |
39 | will be started and you aio-operations will seem to hang. |
43 | will be started and you aio-operations will seem to hang. |
|
|
44 | |
|
|
45 | It is recommended to keep the number of threads low, as many linux |
|
|
46 | kernel versions will scale negatively with the number of threads (higher |
|
|
47 | parallelity => MUCH higher latency). |
40 | |
48 | |
41 | =item $fileno = Linux::AIO::poll_fileno |
49 | =item $fileno = Linux::AIO::poll_fileno |
42 | |
50 | |
43 | Return the request result pipe filehandle. This filehandle must be polled |
51 | Return the I<request result pipe filehandle>. This filehandle must be |
44 | for reading. If the pipe becomes readable you have to call C<poll_cb>. |
52 | polled for reading by some mechanism outside this module (e.g. Event |
|
|
53 | or select, see below). If the pipe becomes readable you have to call |
|
|
54 | C<poll_cb> to check the results. |
45 | |
55 | |
46 | =item Linux::AIO::poll_cb |
56 | =item Linux::AIO::poll_cb |
47 | |
57 | |
48 | Process all outstanding events on the result pipe. You have to call this |
58 | Process all outstanding events on the result pipe. You have to call this |
49 | regularly. Returns the number of events processed. |
59 | regularly. Returns the number of events processed. Returns immediately |
|
|
60 | when no events are outstanding. |
50 | |
61 | |
51 | You can use Event to multiplex, e.g.: |
62 | You can use Event to multiplex, e.g.: |
52 | |
63 | |
53 | Event->io(fd => Linux::AIO::poll_fileno, |
64 | Event->io (fd => Linux::AIO::poll_fileno, |
54 | poll => 'r', async => 1, |
65 | poll => 'r', async => 1, |
55 | cb => \&Linux::AIO::poll_cb ); |
66 | cb => \&Linux::AIO::poll_cb ); |
56 | |
67 | |
57 | |
68 | |
58 | =item Linux::AIO::nreqs |
69 | =item Linux::AIO::nreqs |
59 | |
70 | |
60 | Returns the number of requests currently outstanding. |
71 | Returns the number of requests currently outstanding. |
61 | |
72 | |
62 | =item aio_open($pathname, $flags, $mode, $callback) |
73 | =item aio_open $pathname, $flags, $mode, $callback |
63 | |
74 | |
64 | Asynchronously open or create a file and call the callback with the |
75 | Asynchronously open or create a file and call the callback with the |
65 | filedescriptor. |
76 | filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
|
|
77 | might change in the future). |
66 | |
78 | |
67 | =item aio_close($fh, $callback) |
79 | =item aio_close $fh, $callback |
68 | |
80 | |
69 | Asynchronously close a file and call the callback with the result code. |
81 | Asynchronously close a file and call the callback with the result code. |
70 | |
82 | |
71 | =item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) |
83 | =item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
72 | |
84 | |
73 | =item aio_write($fh,$offset,$length, $data,$dataoffset,$callback) |
85 | =item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
74 | |
86 | |
75 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
87 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
76 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
88 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
77 | callback without the actual number of bytes read (or undef on error). |
89 | callback without the actual number of bytes read (or C<undef> on error). |
78 | |
90 | |
79 | =item aio_stat($fh_or_path,$callback) |
91 | =item aio_stat $fh_or_path, $callback |
80 | |
92 | |
81 | =item aio_lstat($fh,$callback) |
93 | =item aio_lstat $fh, $callback |
82 | |
94 | |
83 | Works like perl's C<stat> or C<lstat> in void context, i.e. the callback |
95 | Works like perl's C<stat> or C<lstat> in void context. The callback will |
84 | will be called after the stat and the results will be available using |
96 | be called after the stat and the results will be available using C<stat _> |
85 | C<stat _> or C<-s _> etc... |
97 | or C<-s _> etc... |
86 | |
98 | |
87 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
99 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
88 | error when stat'ing a large file, the results will be silently truncated |
100 | error when stat'ing a large file, the results will be silently truncated |
89 | unless perl itself is compiled with large file support. |
101 | unless perl itself is compiled with large file support. |
90 | |
102 | |
… | |
… | |
98 | |
110 | |
99 | =back |
111 | =back |
100 | |
112 | |
101 | =head1 BUGS |
113 | =head1 BUGS |
102 | |
114 | |
103 | This module has not yet been extensively tested. Watch out! |
115 | This module has been extensively tested in a large and very busy webserver |
|
|
116 | for many years now. |
104 | |
117 | |
105 | - perl-threads/fork interaction poorly tested. |
|
|
106 | - aio_open gives a fd, but all other functions expect a filehandle. |
118 | - aio_open gives a fd, but all other functions expect a perl filehandle. |
107 | |
119 | |
108 | =head1 SEE ALSO |
120 | =head1 SEE ALSO |
109 | |
121 | |
110 | L<Coro>. |
122 | L<Coro>. |
111 | |
123 | |