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