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.28 by root, Sat Jul 9 04:11:30 2005 UTC vs.
Revision 1.32 by root, Sun Jul 10 15:00:38 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines