… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Linux::AIO; |
7 | use Linux::AIO; |
8 | |
8 | |
|
|
9 | # This module has been mostly superseded by IO::AIO. |
|
|
10 | |
9 | =head1 DESCRIPTION |
11 | =head1 DESCRIPTION |
|
|
12 | |
|
|
13 | I<This module has been mostly superseded by IO::AIO, which is API |
|
|
14 | compatible.> |
10 | |
15 | |
11 | This module implements asynchronous I/O using the means available to Linux |
16 | 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 |
17 | - clone. It does not hook into the POSIX aio_* functions because Linux |
13 | does not yet support these in the kernel (even as of 2.6.12, only O_DIRECT |
18 | does not yet support these in the kernel (even as of 2.6.12, only O_DIRECT |
14 | files are supported) and even if, it would only allow aio_read and write, |
19 | files are supported) and even if, it would only allow aio_read and write, |
… | |
… | |
24 | min_parallel from the same thread that loaded this module. |
29 | min_parallel from the same thread that loaded this module. |
25 | |
30 | |
26 | Although the module will work with in the presence of other threads, it is |
31 | Although the module will work with in the presence of other threads, it is |
27 | not reentrant, so use appropriate locking yourself. |
32 | not reentrant, so use appropriate locking yourself. |
28 | |
33 | |
|
|
34 | =head2 API NOTES |
|
|
35 | |
|
|
36 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
|
|
37 | with the same name (sans C<aio_>). The arguments are similar or identical, |
|
|
38 | and they all accept an additional C<$callback> argument which must be |
|
|
39 | a code reference. This code reference will get called with the syscall |
|
|
40 | return code (e.g. most syscalls return C<-1> on error, unlike perl, which |
|
|
41 | usually delivers "false") as it's sole argument when the given syscall has |
|
|
42 | been executed asynchronously. |
|
|
43 | |
|
|
44 | All functions that expect a filehandle will also accept a file descriptor. |
|
|
45 | |
|
|
46 | The filenames you pass to these routines I<must> be absolute. The reason |
|
|
47 | is that at the time the request is being executed, the current working |
|
|
48 | directory could have changed. Alternatively, you can make sure that you |
|
|
49 | never change the current working directory. |
|
|
50 | |
29 | =over 4 |
51 | =over 4 |
30 | |
52 | |
31 | =cut |
53 | =cut |
32 | |
54 | |
33 | package Linux::AIO; |
55 | package Linux::AIO; |
34 | |
56 | |
35 | use base 'Exporter'; |
57 | use base 'Exporter'; |
36 | |
58 | |
37 | BEGIN { |
59 | BEGIN { |
38 | $VERSION = 1.7; |
60 | $VERSION = 1.8; |
39 | |
61 | |
40 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
62 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
41 | aio_fsync aio_fdatasync aio_readahead); |
63 | aio_fsync aio_fdatasync aio_readahead); |
42 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
64 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
43 | |
65 | |
… | |
… | |
53 | |
75 | |
54 | It is recommended to keep the number of threads low, as some linux |
76 | It is recommended to keep the number of threads low, as some linux |
55 | kernel versions will scale negatively with the number of threads (higher |
77 | kernel versions will scale negatively with the number of threads (higher |
56 | parallelity => MUCH higher latency). |
78 | parallelity => MUCH higher latency). |
57 | |
79 | |
|
|
80 | Under normal circumstances you don't need to call this function, as this |
|
|
81 | module automatically starts a single async thread. |
|
|
82 | |
58 | =item Linux::AIO::max_parallel $nthreads |
83 | =item Linux::AIO::max_parallel $nthreads |
59 | |
84 | |
60 | Sets the maximum number of AIO threads to C<$nthreads>. If more than |
85 | Sets the maximum number of AIO threads to C<$nthreads>. If more than |
61 | the specified number of threads are currently running, kill them. This |
86 | the specified number of threads are currently running, kill them. This |
62 | function blocks until the limit is reached. |
87 | function blocks until the limit is reached. |
63 | |
88 | |
64 | This module automatically runs C<max_parallel 0> at program end, to ensure |
89 | This module automatically runs C<max_parallel 0> at program end, to ensure |
65 | that all threads are killed and that there are no outstanding requests. |
90 | that all threads are killed and that there are no outstanding requests. |
|
|
91 | |
|
|
92 | Under normal circumstances you don't need to call this function. |
66 | |
93 | |
67 | =item $fileno = Linux::AIO::poll_fileno |
94 | =item $fileno = Linux::AIO::poll_fileno |
68 | |
95 | |
69 | Return the I<request result pipe filehandle>. This filehandle must be |
96 | Return the I<request result pipe filehandle>. This filehandle must be |
70 | polled for reading by some mechanism outside this module (e.g. Event |
97 | polled for reading by some mechanism outside this module (e.g. Event |
71 | or select, see below). If the pipe becomes readable you have to call |
98 | or select, see below). If the pipe becomes readable you have to call |
72 | C<poll_cb> to check the results. |
99 | C<poll_cb> to check the results. |
73 | |
100 | |
|
|
101 | See C<poll_cb> for an example. |
|
|
102 | |
74 | =item Linux::AIO::poll_cb |
103 | =item Linux::AIO::poll_cb |
75 | |
104 | |
76 | Process all outstanding events on the result pipe. You have to call this |
105 | Process all outstanding events on the result pipe. You have to call this |
77 | regularly. Returns the number of events processed. Returns immediately |
106 | regularly. Returns the number of events processed. Returns immediately |
78 | when no events are outstanding. |
107 | when no events are outstanding. |
79 | |
108 | |
80 | You can use Event to multiplex, e.g.: |
109 | You can use Event to multiplex, e.g.: |
81 | |
110 | |
82 | Event->io (fd => Linux::AIO::poll_fileno, |
111 | Event->io (fd => Linux::AIO::poll_fileno, |
83 | poll => 'r', async => 1, |
112 | poll => 'r', async => 1, |
84 | cb => \&Linux::AIO::poll_cb ); |
113 | cb => \&Linux::AIO::poll_cb); |
85 | |
114 | |
86 | =item Linux::AIO::poll_wait |
115 | =item Linux::AIO::poll_wait |
87 | |
116 | |
88 | Wait till the result filehandle becomes ready for reading (simply does a |
117 | Wait till the result filehandle becomes ready for reading (simply does a |
89 | select on the filehandle. This is useful if you want to synchronously wait |
118 | select on the filehandle. This is useful if you want to synchronously wait |
90 | for some requests to finish). |
119 | for some requests to finish). |
91 | |
120 | |
|
|
121 | See C<nreqs> for an example. |
|
|
122 | |
92 | =item Linux::AIO::nreqs |
123 | =item Linux::AIO::nreqs |
93 | |
124 | |
94 | Returns the number of requests currently outstanding. |
125 | Returns the number of requests currently outstanding. |
95 | |
126 | |
|
|
127 | Example: wait till there are no outstanding requests anymore: |
|
|
128 | |
|
|
129 | Linux::AIO::poll_wait, Linux::AIO::poll_cb |
|
|
130 | while Linux::AIO::nreqs; |
|
|
131 | |
96 | =item aio_open $pathname, $flags, $mode, $callback |
132 | =item aio_open $pathname, $flags, $mode, $callback |
97 | |
133 | |
98 | Asynchronously open or create a file and call the callback with the |
134 | Asynchronously open or create a file and call the callback with the |
99 | filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
135 | filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
100 | might change in the future). |
136 | might change in the future). |
101 | |
137 | |
|
|
138 | The pathname passed to C<aio_open> must be absolute. See API NOTES, above, |
|
|
139 | for an explanation. |
|
|
140 | |
|
|
141 | The C<$mode> argument is a bitmask. See the C<Fcntl> module for a |
|
|
142 | list. They are the same as used in C<sysopen>. |
|
|
143 | |
|
|
144 | Example: |
|
|
145 | |
|
|
146 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
|
|
147 | if ($_[0] >= 0) { |
|
|
148 | open my $fh, "<&=$_[0]"; |
|
|
149 | print "open successful, fh is $fh\n"; |
|
|
150 | ... |
|
|
151 | } else { |
|
|
152 | die "open failed: $!\n"; |
|
|
153 | } |
|
|
154 | }; |
|
|
155 | |
102 | =item aio_close $fh, $callback |
156 | =item aio_close $fh, $callback |
103 | |
157 | |
104 | Asynchronously close a file and call the callback with the result code. |
158 | Asynchronously close a file and call the callback with the result code. |
105 | |
159 | |
106 | =item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
160 | =item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
107 | |
161 | |
108 | =item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
162 | =item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
109 | |
163 | |
110 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
164 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
111 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
165 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
112 | callback without the actual number of bytes read (or C<undef> on error). |
166 | callback without the actual number of bytes read (or -1 on error, just |
|
|
167 | like the syscall). |
|
|
168 | |
|
|
169 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, strating at |
|
|
170 | offset C<0> within the scalar: |
|
|
171 | |
|
|
172 | aio_read $fh, 7, 15, $buffer, 0, sub { |
|
|
173 | $_[0] >= 0 or die "read error: $!"; |
|
|
174 | print "read <$buffer>\n"; |
|
|
175 | }; |
|
|
176 | |
|
|
177 | =item aio_readahead $fh,$offset,$length, $callback |
|
|
178 | |
|
|
179 | Asynchronously reads the specified byte range into the page cache, using |
|
|
180 | the C<readahead> syscall. |
|
|
181 | |
|
|
182 | readahead() populates the page cache with data from a file so that |
|
|
183 | subsequent reads from that file will not block on disk I/O. The C<$offset> |
|
|
184 | argument specifies the starting point from which data is to be read and |
|
|
185 | C<$length> specifies the number of bytes to be read. I/O is performed in |
|
|
186 | whole pages, so that offset is effectively rounded down to a page boundary |
|
|
187 | and bytes are read up to the next page boundary greater than or equal to |
|
|
188 | (off-set+length). aio_readahead() does not read beyond the end of the |
|
|
189 | file. The current file offset of the file is left unchanged. |
113 | |
190 | |
114 | =item aio_stat $fh_or_path, $callback |
191 | =item aio_stat $fh_or_path, $callback |
115 | |
192 | |
116 | =item aio_lstat $fh, $callback |
193 | =item aio_lstat $fh, $callback |
117 | |
194 | |
118 | Works like perl's C<stat> or C<lstat> in void context. The callback will |
195 | Works like perl's C<stat> or C<lstat> in void context. The callback will |
119 | be called after the stat and the results will be available using C<stat _> |
196 | be called after the stat and the results will be available using C<stat _> |
120 | or C<-s _> etc... |
197 | or C<-s _> etc... |
121 | |
198 | |
|
|
199 | The pathname passed to C<aio_stat> must be absolute. See API NOTES, above, |
|
|
200 | for an explanation. |
|
|
201 | |
122 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
202 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
123 | error when stat'ing a large file, the results will be silently truncated |
203 | error when stat'ing a large file, the results will be silently truncated |
124 | unless perl itself is compiled with large file support. |
204 | unless perl itself is compiled with large file support. |
125 | |
205 | |
|
|
206 | Example: Print the length of F</etc/passwd>: |
|
|
207 | |
|
|
208 | aio_stat "/etc/passwd", sub { |
|
|
209 | $_[0] and die "stat failed: $!"; |
|
|
210 | print "size is ", -s _, "\n"; |
|
|
211 | }; |
|
|
212 | |
126 | =item aio_unlink $pathname, $callback |
213 | =item aio_unlink $pathname, $callback |
127 | |
214 | |
128 | Asynchronously unlink a file. |
215 | Asynchronously unlink (delete) a file and call the callback with the |
|
|
216 | result code. |
|
|
217 | |
|
|
218 | =item aio_fsync $fh, $callback |
|
|
219 | |
|
|
220 | Asynchronously call fsync on the given filehandle and call the callback |
|
|
221 | with the fsync result code. |
|
|
222 | |
|
|
223 | =item aio_fdatasync $fh, $callback |
|
|
224 | |
|
|
225 | Asynchronously call fdatasync on the given filehandle and call the |
|
|
226 | callback with the fdatasync result code. |
129 | |
227 | |
130 | =cut |
228 | =cut |
131 | |
229 | |
132 | min_parallel 1; |
230 | min_parallel 1; |
133 | |
231 | |
… | |
… | |
146 | |
244 | |
147 | - aio_open gives a fd, but all other functions expect a perl filehandle. |
245 | - aio_open gives a fd, but all other functions expect a perl filehandle. |
148 | |
246 | |
149 | =head1 SEE ALSO |
247 | =head1 SEE ALSO |
150 | |
248 | |
151 | L<Coro>. |
249 | L<Coro>, L<IO::AIO>. |
152 | |
250 | |
153 | =head1 AUTHOR |
251 | =head1 AUTHOR |
154 | |
252 | |
155 | Marc Lehmann <schmorp@schmorp.de> |
253 | Marc Lehmann <schmorp@schmorp.de> |
156 | http://home.schmorp.de/ |
254 | http://home.schmorp.de/ |