ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/README
Revision: 1.3
Committed: Sun Jul 10 20:57:36 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-0_2
Changes since 1.2: +16 -3 lines
Log Message:
*** empty log message ***

File Contents

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