ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/README
Revision: 1.2
Committed: Sun Jul 10 18:16:49 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-0_1
Changes since 1.1: +26 -21 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     It is recommended to keep the number of threads low, as some linux
47     kernel versions will scale negatively with the number of threads
48     (higher parallelity => MUCH higher latency).
49    
50     Under normal circumstances you don't need to call this function, as
51     this module automatically starts a single async thread.
52    
53     IO::AIO::max_parallel $nthreads
54     Sets the maximum number of AIO threads to $nthreads. If more than
55     the specified number of threads are currently running, kill them.
56     This function blocks until the limit is reached.
57    
58     This module automatically runs "max_parallel 0" at program end, to
59     ensure that all threads are killed and that there are no outstanding
60     requests.
61    
62     Under normal circumstances you don't need to call this function.
63    
64     $fileno = IO::AIO::poll_fileno
65     Return the *request result pipe filehandle*. This filehandle must be
66     polled for reading by some mechanism outside this module (e.g. Event
67     or select, see below). If the pipe becomes readable you have to call
68     "poll_cb" to check the results.
69    
70     See "poll_cb" for an example.
71    
72     IO::AIO::poll_cb
73     Process all outstanding events on the result pipe. You have to call
74     this regularly. Returns the number of events processed. Returns
75     immediately when no events are outstanding.
76    
77     You can use Event to multiplex, e.g.:
78    
79     Event->io (fd => IO::AIO::poll_fileno,
80     poll => 'r', async => 1,
81     cb => \&IO::AIO::poll_cb);
82    
83     IO::AIO::poll_wait
84     Wait till the result filehandle becomes ready for reading (simply
85     does a select on the filehandle. This is useful if you want to
86     synchronously wait for some requests to finish).
87    
88     See "nreqs" for an example.
89    
90     IO::AIO::nreqs
91     Returns the number of requests currently outstanding.
92    
93     Example: wait till there are no outstanding requests anymore:
94    
95     IO::AIO::poll_wait, IO::AIO::poll_cb
96     while IO::AIO::nreqs;
97    
98     aio_open $pathname, $flags, $mode, $callback
99 root 1.2 Asynchronously open or create a file and call the callback with a
100     newly created filehandle for the file.
101    
102     The pathname passed to "aio_open" must be absolute. See API NOTES,
103     above, for an explanation.
104 root 1.1
105     The $mode argument is a bitmask. See the "Fcntl" module for a list.
106     They are the same as used in "sysopen".
107    
108     Example:
109    
110     aio_open "/etc/passwd", O_RDONLY, 0, sub {
111 root 1.2 if ($_[0]) {
112     print "open successful, fh is $_[0]\n";
113 root 1.1 ...
114     } else {
115     die "open failed: $!\n";
116     }
117     };
118    
119     aio_close $fh, $callback
120     Asynchronously close a file and call the callback with the result
121 root 1.2 code. *WARNING:* although accepted, you should not pass in a perl
122     filehandle here, as perl will likely close the file descriptor
123     itself when the filehandle is destroyed. Normally, you can safely
124     call perls "close" or just let filehandles go out of scope.
125 root 1.1
126     aio_read $fh,$offset,$length, $data,$dataoffset,$callback
127     aio_write $fh,$offset,$length, $data,$dataoffset,$callback
128     Reads or writes "length" bytes from the specified "fh" and "offset"
129     into the scalar given by "data" and offset "dataoffset" and calls
130     the callback without the actual number of bytes read (or -1 on
131     error, just like the syscall).
132    
133     Example: Read 15 bytes at offset 7 into scalar $buffer, strating at
134     offset 0 within the scalar:
135    
136     aio_read $fh, 7, 15, $buffer, 0, sub {
137     $_[0] >= 0 or die "read error: $!";
138     print "read <$buffer>\n";
139     };
140    
141     aio_readahead $fh,$offset,$length, $callback
142     Asynchronously reads the specified byte range into the page cache,
143 root 1.2 using the "readahead" syscall. If that syscall doesn't exist the
144     status will be -1 and $! is set to ENOSYS.
145 root 1.1
146     readahead() populates the page cache with data from a file so that
147     subsequent reads from that file will not block on disk I/O. The
148     $offset argument specifies the starting point from which data is to
149     be read and $length specifies the number of bytes to be read. I/O is
150     performed in whole pages, so that offset is effectively rounded down
151     to a page boundary and bytes are read up to the next page boundary
152     greater than or equal to (off-set+length). aio_readahead() does not
153     read beyond the end of the file. The current file offset of the file
154     is left unchanged.
155    
156     aio_stat $fh_or_path, $callback
157     aio_lstat $fh, $callback
158     Works like perl's "stat" or "lstat" in void context. The callback
159     will be called after the stat and the results will be available
160     using "stat _" or "-s _" etc...
161    
162     The pathname passed to "aio_stat" must be absolute. See API NOTES,
163     above, for an explanation.
164    
165     Currently, the stats are always 64-bit-stats, i.e. instead of
166     returning an error when stat'ing a large file, the results will be
167     silently truncated unless perl itself is compiled with large file
168     support.
169    
170     Example: Print the length of /etc/passwd:
171    
172     aio_stat "/etc/passwd", sub {
173     $_[0] and die "stat failed: $!";
174     print "size is ", -s _, "\n";
175     };
176    
177     aio_unlink $pathname, $callback
178     Asynchronously unlink (delete) a file and call the callback with the
179     result code.
180    
181     aio_fsync $fh, $callback
182     Asynchronously call fsync on the given filehandle and call the
183     callback with the fsync result code.
184    
185     aio_fdatasync $fh, $callback
186     Asynchronously call fdatasync on the given filehandle and call the
187     callback with the fdatasync result code.
188    
189     BUGS
190 root 1.2 - could be optimized to use more semaphores instead of filehandles.
191 root 1.1
192     SEE ALSO
193 root 1.2 Coro, Linux::AIO.
194 root 1.1
195     AUTHOR
196     Marc Lehmann <schmorp@schmorp.de>
197     http://home.schmorp.de/
198