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.16 by root, Thu May 6 15:05:57 2004 UTC vs.
Revision 1.31 by root, Sun Jul 10 01:03:06 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
21Although the module will work with threads, it is not reentrant, so use 22NOTICE: the threads created by this module will automatically be killed
22appropriate locking yourself. 23when the thread calling min_parallel exits. Make sure you only ever call
24min_parallel from the same thread that loaded this module.
25
26Although the module will work with in the presence of other threads, it is
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.
23 40
24=over 4 41=over 4
25 42
26=cut 43=cut
27 44
28package Linux::AIO; 45package Linux::AIO;
29 46
30use base 'Exporter'; 47use base 'Exporter';
31 48
32BEGIN { 49BEGIN {
33 $VERSION = 1.1; 50 $VERSION = 1.71;
34 51
35 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat); 52 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink
53 aio_fsync aio_fdatasync aio_readahead);
36 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); 54 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs);
37 55
38 require XSLoader; 56 require XSLoader;
39 XSLoader::load Linux::AIO, $VERSION; 57 XSLoader::load Linux::AIO, $VERSION;
40} 58}
43 61
44Set the minimum number of AIO threads to C<$nthreads>. The default is 62Set the minimum number of AIO threads to C<$nthreads>. The default is
45C<1>, which means a single asynchronous operation can be done at one time 63C<1>, which means a single asynchronous operation can be done at one time
46(the number of outstanding operations, however, is unlimited). 64(the number of outstanding operations, however, is unlimited).
47 65
48It is recommended to keep the number of threads low, as many linux 66It is recommended to keep the number of threads low, as some linux
49kernel versions will scale negatively with the number of threads (higher 67kernel versions will scale negatively with the number of threads (higher
50parallelity => MUCH higher latency). 68parallelity => MUCH higher latency).
69
70Under normal circumstances you don't need to call this function, as this
71module automatically starts a single async thread.
72
73=item Linux::AIO::max_parallel $nthreads
74
75Sets the maximum number of AIO threads to C<$nthreads>. If more than
76the specified number of threads are currently running, kill them. This
77function blocks until the limit is reached.
78
79This module automatically runs C<max_parallel 0> at program end, to ensure
80that all threads are killed and that there are no outstanding requests.
81
82Under normal circumstances you don't need to call this function.
51 83
52=item $fileno = Linux::AIO::poll_fileno 84=item $fileno = Linux::AIO::poll_fileno
53 85
54Return the I<request result pipe filehandle>. This filehandle must be 86Return the I<request result pipe filehandle>. This filehandle must be
55polled for reading by some mechanism outside this module (e.g. Event 87polled for reading by some mechanism outside this module (e.g. Event
56or select, see below). If the pipe becomes readable you have to call 88or select, see below). If the pipe becomes readable you have to call
57C<poll_cb> to check the results. 89C<poll_cb> to check the results.
58 90
91See C<poll_cb> for an example.
92
59=item Linux::AIO::poll_cb 93=item Linux::AIO::poll_cb
60 94
61Process all outstanding events on the result pipe. You have to call this 95Process all outstanding events on the result pipe. You have to call this
62regularly. Returns the number of events processed. Returns immediately 96regularly. Returns the number of events processed. Returns immediately
63when no events are outstanding. 97when no events are outstanding.
64 98
65You can use Event to multiplex, e.g.: 99You can use Event to multiplex, e.g.:
66 100
67 Event->io (fd => Linux::AIO::poll_fileno, 101 Event->io (fd => Linux::AIO::poll_fileno,
68 poll => 'r', async => 1, 102 poll => 'r', async => 1,
69 cb => \&Linux::AIO::poll_cb ); 103 cb => \&Linux::AIO::poll_cb);
70 104
105=item Linux::AIO::poll_wait
106
107Wait till the result filehandle becomes ready for reading (simply does a
108select on the filehandle. This is useful if you want to synchronously wait
109for some requests to finish).
110
111See C<nreqs> for an example.
71 112
72=item Linux::AIO::nreqs 113=item Linux::AIO::nreqs
73 114
74Returns the number of requests currently outstanding. 115Returns the number of requests currently outstanding.
75 116
117Example: wait till there are no outstanding requests anymore:
118
119 Linux::AIO::poll_wait while Linux::AIO::nreqs;
120
76=item aio_open $pathname, $flags, $mode, $callback 121=item aio_open $pathname, $flags, $mode, $callback
77 122
78Asynchronously open or create a file and call the callback with the 123Asynchronously open or create a file and call the callback with the
79filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this 124filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this
80might change in the future). 125might change in the future).
81 126
127The C<$mode> argument is a bitmask. See the C<Fcntl> module for a
128list. They are the same as used in C<sysopen>.
129
130Example:
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
82=item aio_close $fh, $callback 143=item aio_close $fh, $callback
83 144
84Asynchronously close a file and call the callback with the result code. 145Asynchronously close a file and call the callback with the result code.
85 146
86=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback 147=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback
87 148
88=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback 149=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback
89 150
90Reads or writes C<length> bytes from the specified C<fh> and C<offset> 151Reads or writes C<length> bytes from the specified C<fh> and C<offset>
91into the scalar given by C<data> and offset C<dataoffset> and calls the 152into the scalar given by C<data> and offset C<dataoffset> and calls the
92callback without the actual number of bytes read (or C<undef> on error). 153callback without the actual number of bytes read (or -1 on error, just
154like the syscall).
155
156Example: Read 15 bytes at offset 7 into scalar C<$buffer>, strating at
157offset 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
166Asynchronously reads the specified byte range into the page cache, using
167the C<readahead> syscall.
168
169readahead() populates the page cache with data from a file so that
170subsequent reads from that file will not block on disk I/O. The C<$offset>
171argument specifies the starting point from which data is to be read and
172C<$length> specifies the number of bytes to be read. I/O is performed in
173whole pages, so that offset is effectively rounded down to a page boundary
174and 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
176file. The current file offset of the file is left unchanged.
93 177
94=item aio_stat $fh_or_path, $callback 178=item aio_stat $fh_or_path, $callback
95 179
96=item aio_lstat $fh, $callback 180=item aio_lstat $fh, $callback
97 181
101 185
102Currently, the stats are always 64-bit-stats, i.e. instead of returning an 186Currently, the stats are always 64-bit-stats, i.e. instead of returning an
103error when stat'ing a large file, the results will be silently truncated 187error when stat'ing a large file, the results will be silently truncated
104unless perl itself is compiled with large file support. 188unless perl itself is compiled with large file support.
105 189
190Example: 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
197=item aio_unlink $pathname, $callback
198
199Asynchronously unlink (delete) a file and call the callback with the
200result code.
201
202=item aio_fsync $fh, $callback
203
204Asynchronously call fsync on the given filehandle and call the callback
205with the fsync result code.
206
207=item aio_fdatasync $fh, $callback
208
209Asynchronously call fdatasync on the given filehandle and call the
210callback with the fdatasync result code.
211
106=cut 212=cut
107 213
108min_parallel 1; 214min_parallel 1;
109 215
110END { 216END {
126 232
127L<Coro>. 233L<Coro>.
128 234
129=head1 AUTHOR 235=head1 AUTHOR
130 236
131 Marc Lehmann <pcg@goof.com> 237 Marc Lehmann <schmorp@schmorp.de>
132 http://www.goof.com/pcg/marc/ 238 http://home.schmorp.de/
133 239
134=cut 240=cut
135 241

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines