… | |
… | |
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 | |
29 | =over 4 |
41 | =over 4 |
30 | |
42 | |
31 | =cut |
43 | =cut |
32 | |
44 | |
33 | package Linux::AIO; |
45 | package Linux::AIO; |
… | |
… | |
53 | |
65 | |
54 | It is recommended to keep the number of threads low, as some linux |
66 | 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 |
67 | kernel versions will scale negatively with the number of threads (higher |
56 | parallelity => MUCH higher latency). |
68 | parallelity => MUCH higher latency). |
57 | |
69 | |
|
|
70 | Under normal circumstances you don't need to call this function, as this |
|
|
71 | module automatically starts a single async thread. |
|
|
72 | |
58 | =item Linux::AIO::max_parallel $nthreads |
73 | =item Linux::AIO::max_parallel $nthreads |
59 | |
74 | |
60 | Sets the maximum number of AIO threads to C<$nthreads>. If more than |
75 | 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 |
76 | the specified number of threads are currently running, kill them. This |
62 | function blocks until the limit is reached. |
77 | function blocks until the limit is reached. |
63 | |
78 | |
64 | This module automatically runs C<max_parallel 0> at program end, to ensure |
79 | 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. |
80 | that all threads are killed and that there are no outstanding requests. |
|
|
81 | |
|
|
82 | Under normal circumstances you don't need to call this function. |
66 | |
83 | |
67 | =item $fileno = Linux::AIO::poll_fileno |
84 | =item $fileno = Linux::AIO::poll_fileno |
68 | |
85 | |
69 | Return the I<request result pipe filehandle>. This filehandle must be |
86 | Return the I<request result pipe filehandle>. This filehandle must be |
70 | polled for reading by some mechanism outside this module (e.g. Event |
87 | 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 |
88 | or select, see below). If the pipe becomes readable you have to call |
72 | C<poll_cb> to check the results. |
89 | C<poll_cb> to check the results. |
73 | |
90 | |
|
|
91 | See C<poll_cb> for an example. |
|
|
92 | |
74 | =item Linux::AIO::poll_cb |
93 | =item Linux::AIO::poll_cb |
75 | |
94 | |
76 | Process all outstanding events on the result pipe. You have to call this |
95 | Process all outstanding events on the result pipe. You have to call this |
77 | regularly. Returns the number of events processed. Returns immediately |
96 | regularly. Returns the number of events processed. Returns immediately |
78 | when no events are outstanding. |
97 | when no events are outstanding. |
79 | |
98 | |
80 | You can use Event to multiplex, e.g.: |
99 | You can use Event to multiplex, e.g.: |
81 | |
100 | |
82 | Event->io (fd => Linux::AIO::poll_fileno, |
101 | Event->io (fd => Linux::AIO::poll_fileno, |
83 | poll => 'r', async => 1, |
102 | poll => 'r', async => 1, |
84 | cb => \&Linux::AIO::poll_cb ); |
103 | cb => \&Linux::AIO::poll_cb); |
85 | |
104 | |
86 | =item Linux::AIO::poll_wait |
105 | =item Linux::AIO::poll_wait |
87 | |
106 | |
88 | Wait till the result filehandle becomes ready for reading (simply does a |
107 | 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 |
108 | select on the filehandle. This is useful if you want to synchronously wait |
90 | for some requests to finish). |
109 | for some requests to finish). |
91 | |
110 | |
|
|
111 | See C<nreqs> for an example. |
|
|
112 | |
92 | =item Linux::AIO::nreqs |
113 | =item Linux::AIO::nreqs |
93 | |
114 | |
94 | Returns the number of requests currently outstanding. |
115 | Returns the number of requests currently outstanding. |
95 | |
116 | |
|
|
117 | Example: wait till there are no outstanding requests anymore: |
|
|
118 | |
|
|
119 | Linux::AIO::poll_wait while Linux::AIO::nreqs; |
|
|
120 | |
96 | =item aio_open $pathname, $flags, $mode, $callback |
121 | =item aio_open $pathname, $flags, $mode, $callback |
97 | |
122 | |
98 | Asynchronously open or create a file and call the callback with the |
123 | 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 |
124 | filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
100 | might change in the future). |
125 | might change in the future). |
101 | |
126 | |
|
|
127 | The C<$mode> argument is a bitmask. See the C<Fcntl> module for a |
|
|
128 | list. They are the same as used in C<sysopen>. |
|
|
129 | |
|
|
130 | Example: |
|
|
131 | |
|
|
132 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
|
|
133 | if ($_[0] >= 0) { |
|
|
134 | open my $fh, "<&$_[0]"; # create a copy for perl |
|
|
135 | aio_close $_[0], sub { }; # close the aio handle |
|
|
136 | print "open successful, fh is $fh\n"; |
|
|
137 | ... |
|
|
138 | } else { |
|
|
139 | die "open failed: $!\n"; |
|
|
140 | } |
|
|
141 | }; |
|
|
142 | |
102 | =item aio_close $fh, $callback |
143 | =item aio_close $fh, $callback |
103 | |
144 | |
104 | Asynchronously close a file and call the callback with the result code. |
145 | Asynchronously close a file and call the callback with the result code. |
105 | |
146 | |
106 | =item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
147 | =item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
107 | |
148 | |
108 | =item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
149 | =item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
109 | |
150 | |
110 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
151 | 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 |
152 | 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). |
153 | callback without the actual number of bytes read (or -1 on error, just |
|
|
154 | like the syscall). |
|
|
155 | |
|
|
156 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, strating at |
|
|
157 | offset C<0> within the scalar: |
|
|
158 | |
|
|
159 | aio_read $fh, 7, 15, $buffer, 0, sub { |
|
|
160 | $_[0] >= 0 or die "read error: $!"; |
|
|
161 | print "read <$buffer>\n"; |
|
|
162 | }; |
|
|
163 | |
|
|
164 | =item aio_readahead $fh,$offset,$length, $callback |
|
|
165 | |
|
|
166 | Asynchronously reads the specified byte range into the page cache, using |
|
|
167 | the C<readahead> syscall. |
|
|
168 | |
|
|
169 | readahead() populates the page cache with data from a file so that |
|
|
170 | subsequent reads from that file will not block on disk I/O. The C<$offset> |
|
|
171 | argument specifies the starting point from which data is to be read and |
|
|
172 | C<$length> specifies the number of bytes to be read. I/O is performed in |
|
|
173 | whole pages, so that offset is effectively rounded down to a page boundary |
|
|
174 | and bytes are read up to the next page boundary greater than or equal to |
|
|
175 | (off-set+length). aio_readahead() does not read beyond the end of the |
|
|
176 | file. The current file offset of the file is left unchanged. |
113 | |
177 | |
114 | =item aio_stat $fh_or_path, $callback |
178 | =item aio_stat $fh_or_path, $callback |
115 | |
179 | |
116 | =item aio_lstat $fh, $callback |
180 | =item aio_lstat $fh, $callback |
117 | |
181 | |
… | |
… | |
121 | |
185 | |
122 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
186 | 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 |
187 | error when stat'ing a large file, the results will be silently truncated |
124 | unless perl itself is compiled with large file support. |
188 | unless perl itself is compiled with large file support. |
125 | |
189 | |
|
|
190 | Example: Print the length of F</etc/passwd>: |
|
|
191 | |
|
|
192 | aio_stat "/etc/passwd", sub { |
|
|
193 | $_[0] and die "stat failed: $!"; |
|
|
194 | print "size is ", -s _, "\n"; |
|
|
195 | }; |
|
|
196 | |
126 | =item aio_unlink $pathname, $callback |
197 | =item aio_unlink $pathname, $callback |
127 | |
198 | |
128 | Asynchronously unlink a file. |
199 | Asynchronously unlink (delete) a file and call the callback with the |
|
|
200 | result code. |
|
|
201 | |
|
|
202 | =item aio_fsync $fh, $callback |
|
|
203 | |
|
|
204 | Asynchronously call fsync on the given filehandle and call the callback |
|
|
205 | with the fsync result code. |
|
|
206 | |
|
|
207 | =item aio_fdatasync $fh, $callback |
|
|
208 | |
|
|
209 | Asynchronously call fdatasync on the given filehandle and call the |
|
|
210 | callback with the fdatasync result code. |
129 | |
211 | |
130 | =cut |
212 | =cut |
131 | |
213 | |
132 | min_parallel 1; |
214 | min_parallel 1; |
133 | |
215 | |