… | |
… | |
6 | |
6 | |
7 | use Linux::AIO; |
7 | use Linux::AIO; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | This module implements asynchronous i/o using the means available to linux |
11 | This module implements asynchronous I/O using the means available to Linux |
12 | - clone. It does not hook into the POSIX aio_* functions because linux |
12 | - clone. It does not hook into the POSIX aio_* functions because Linux |
13 | does not yet support these in the kernel (and even if, it would only allow |
13 | does not yet support these in the kernel (even as of 2.6.12, only O_DIRECT |
14 | aio_read and write, not open and stat). |
14 | files are supported) and even if, it would only allow aio_read and write, |
|
|
15 | not open, stat and so on. |
15 | |
16 | |
16 | Instead, in this module a number of (non-posix) threads are started that |
17 | 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 | 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 | thread support in your libc or perl, and the threads created by this |
19 | module will not be visible to the pthreads library. |
20 | module will not be visible to the pthreads library. |
|
|
21 | |
|
|
22 | NOTICE: the threads created by this module will automatically be killed |
|
|
23 | when the thread calling min_parallel exits. Make sure you only ever call |
|
|
24 | min_parallel from the same thread that loaded this module. |
|
|
25 | |
|
|
26 | Although the module will work with in the presence of other threads, it is |
|
|
27 | not reentrant, so use appropriate locking yourself. |
20 | |
28 | |
21 | =over 4 |
29 | =over 4 |
22 | |
30 | |
23 | =cut |
31 | =cut |
24 | |
32 | |
25 | package Linux::AIO; |
33 | package Linux::AIO; |
26 | |
34 | |
27 | use base 'Exporter'; |
35 | use base 'Exporter'; |
28 | |
36 | |
29 | BEGIN { |
37 | BEGIN { |
30 | $VERSION = '1.0'; |
38 | $VERSION = 1.6; |
31 | |
39 | |
32 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat); |
40 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink); |
33 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
41 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
34 | |
42 | |
35 | require XSLoader; |
43 | require XSLoader; |
36 | XSLoader::load Linux::AIO, $VERSION; |
44 | XSLoader::load Linux::AIO, $VERSION; |
37 | } |
45 | } |
38 | |
46 | |
39 | =item Linux::AIO::min_parallel $nthreads |
47 | =item Linux::AIO::min_parallel $nthreads |
40 | |
48 | |
41 | Set the minimum number of AIO threads to C<$nthreads>. You I<have> to call |
49 | Set the minimum number of AIO threads to C<$nthreads>. The default is |
42 | this function with a positive number at least once, otherwise no threads |
50 | C<1>, which means a single asynchronous operation can be done at one time |
43 | will be started and you aio-operations will seem to hang. |
51 | (the number of outstanding operations, however, is unlimited). |
44 | |
52 | |
45 | It is recommended to keep the number of threads low, as many linux |
53 | It is recommended to keep the number of threads low, as some linux |
46 | kernel versions will scale negatively with the number of threads (higher |
54 | kernel versions will scale negatively with the number of threads (higher |
47 | parallelity => MUCH higher latency). |
55 | parallelity => MUCH higher latency). |
|
|
56 | |
|
|
57 | =item Linux::AIO::max_parallel $nthreads |
|
|
58 | |
|
|
59 | Sets the maximum number of AIO threads to C<$nthreads>. If more than |
|
|
60 | the specified number of threads are currently running, kill them. This |
|
|
61 | function blocks until the limit is reached. |
|
|
62 | |
|
|
63 | This module automatically runs C<max_parallel 0> at program end, to ensure |
|
|
64 | that all threads are killed and that there are no outstanding requests. |
48 | |
65 | |
49 | =item $fileno = Linux::AIO::poll_fileno |
66 | =item $fileno = Linux::AIO::poll_fileno |
50 | |
67 | |
51 | Return the I<request result pipe filehandle>. This filehandle must be |
68 | Return the I<request result pipe filehandle>. This filehandle must be |
52 | polled for reading by some mechanism outside this module (e.g. Event |
69 | polled for reading by some mechanism outside this module (e.g. Event |
… | |
… | |
63 | |
80 | |
64 | Event->io (fd => Linux::AIO::poll_fileno, |
81 | Event->io (fd => Linux::AIO::poll_fileno, |
65 | poll => 'r', async => 1, |
82 | poll => 'r', async => 1, |
66 | cb => \&Linux::AIO::poll_cb ); |
83 | cb => \&Linux::AIO::poll_cb ); |
67 | |
84 | |
|
|
85 | =item Linux::AIO::poll_wait |
|
|
86 | |
|
|
87 | Wait till the result filehandle becomes ready for reading (simply does a |
|
|
88 | select on the filehandle. This is useful if you want to synchronously wait |
|
|
89 | for some requests to finish). |
68 | |
90 | |
69 | =item Linux::AIO::nreqs |
91 | =item Linux::AIO::nreqs |
70 | |
92 | |
71 | Returns the number of requests currently outstanding. |
93 | Returns the number of requests currently outstanding. |
72 | |
94 | |
… | |
… | |
98 | |
120 | |
99 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
121 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
100 | error when stat'ing a large file, the results will be silently truncated |
122 | error when stat'ing a large file, the results will be silently truncated |
101 | unless perl itself is compiled with large file support. |
123 | unless perl itself is compiled with large file support. |
102 | |
124 | |
|
|
125 | =item aio_unlink $pathname, $callback |
|
|
126 | |
|
|
127 | Asynchronously unlink a file. |
|
|
128 | |
103 | =cut |
129 | =cut |
|
|
130 | |
|
|
131 | min_parallel 1; |
104 | |
132 | |
105 | END { |
133 | END { |
106 | max_parallel 0; |
134 | max_parallel 0; |
107 | } |
135 | } |
108 | |
136 | |
… | |
… | |
121 | |
149 | |
122 | L<Coro>. |
150 | L<Coro>. |
123 | |
151 | |
124 | =head1 AUTHOR |
152 | =head1 AUTHOR |
125 | |
153 | |
126 | Marc Lehmann <pcg@goof.com> |
154 | Marc Lehmann <schmorp@schmorp.de> |
127 | http://www.goof.com/pcg/marc/ |
155 | http://home.schmorp.de/ |
128 | |
156 | |
129 | =cut |
157 | =cut |
130 | |
158 | |