ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/README
Revision: 1.4
Committed: Sun Jul 10 21:04:46 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
Changes since 1.3: +72 -73 lines
Log Message:
*** empty log message ***

File Contents

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