ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.pm
(Generate patch)

Comparing Linux-AIO/AIO.pm (file contents):
Revision 1.19 by root, Fri Aug 6 17:18:08 2004 UTC vs.
Revision 1.34 by root, Sun Jul 10 17:07:34 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines