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