… | |
… | |
6 | |
6 | |
7 | use Linux::AIO; |
7 | use Linux::AIO; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
|
|
11 | This module implements asynchronous i/o using the means available to linux |
|
|
12 | - clone. It does not hook into the POSIX aio_* functions because linux |
|
|
13 | does not yet support these in the kernel (and even if, it would only allow |
|
|
14 | aio_read and write, not open and stat). |
|
|
15 | |
|
|
16 | Instead, in this module a number of (non-posix) threads are started that |
|
|
17 | execute your read/writes and signal their completion. You don't need |
|
|
18 | thread support in your libc or perl, and the threads created by this |
|
|
19 | module will not be visible to the pthreads library. |
|
|
20 | |
|
|
21 | NOTICE: the threads created by this module will automatically be killed |
|
|
22 | when the thread calling min_parallel exits. Make sure you only ever call |
|
|
23 | min_parallel from the same thread that loaded this module. |
|
|
24 | |
|
|
25 | Although the module will work with threads, it is not reentrant, so use |
|
|
26 | appropriate locking yourself. |
|
|
27 | |
11 | =over 4 |
28 | =over 4 |
12 | |
29 | |
13 | =cut |
30 | =cut |
14 | |
31 | |
15 | package Linux::AIO; |
32 | package Linux::AIO; |
16 | |
33 | |
|
|
34 | use base 'Exporter'; |
|
|
35 | |
17 | BEGIN { |
36 | BEGIN { |
18 | $VERSION = 0.001; |
37 | $VERSION = 1.41; |
|
|
38 | |
|
|
39 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink); |
|
|
40 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
19 | |
41 | |
20 | require XSLoader; |
42 | require XSLoader; |
21 | XSLoader::load Linux::AIO, $VERSION; |
43 | XSLoader::load Linux::AIO, $VERSION; |
22 | } |
44 | } |
23 | |
45 | |
24 | use base 'Exporter'; |
46 | =item Linux::AIO::min_parallel $nthreads |
25 | |
47 | |
26 | =item Linux::AIO::min_parallel($nthreads) |
48 | Set the minimum number of AIO threads to C<$nthreads>. The default is |
|
|
49 | C<1>, which means a single asynchronous operation can be done at one time |
|
|
50 | (the number of outstanding operations, however, is unlimited). |
27 | |
51 | |
28 | Set the minimum number of AIO threads to $nthreads. |
52 | It is recommended to keep the number of threads low, as many linux |
|
|
53 | kernel versions will scale negatively with the number of threads (higher |
|
|
54 | parallelity => MUCH higher latency). |
|
|
55 | |
|
|
56 | =item $fileno = Linux::AIO::poll_fileno |
|
|
57 | |
|
|
58 | Return the I<request result pipe filehandle>. This filehandle must be |
|
|
59 | polled for reading by some mechanism outside this module (e.g. Event |
|
|
60 | or select, see below). If the pipe becomes readable you have to call |
|
|
61 | C<poll_cb> to check the results. |
|
|
62 | |
|
|
63 | =item Linux::AIO::poll_cb |
|
|
64 | |
|
|
65 | Process all outstanding events on the result pipe. You have to call this |
|
|
66 | regularly. Returns the number of events processed. Returns immediately |
|
|
67 | when no events are outstanding. |
|
|
68 | |
|
|
69 | You can use Event to multiplex, e.g.: |
|
|
70 | |
|
|
71 | Event->io (fd => Linux::AIO::poll_fileno, |
|
|
72 | poll => 'r', async => 1, |
|
|
73 | cb => \&Linux::AIO::poll_cb ); |
|
|
74 | |
|
|
75 | |
|
|
76 | =item Linux::AIO::nreqs |
|
|
77 | |
|
|
78 | Returns the number of requests currently outstanding. |
|
|
79 | |
|
|
80 | =item aio_open $pathname, $flags, $mode, $callback |
|
|
81 | |
|
|
82 | Asynchronously open or create a file and call the callback with the |
|
|
83 | filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
|
|
84 | might change in the future). |
|
|
85 | |
|
|
86 | =item aio_close $fh, $callback |
|
|
87 | |
|
|
88 | Asynchronously close a file and call the callback with the result code. |
|
|
89 | |
|
|
90 | =item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
|
|
91 | |
|
|
92 | =item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
|
|
93 | |
|
|
94 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
|
|
95 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
|
|
96 | callback without the actual number of bytes read (or C<undef> on error). |
|
|
97 | |
|
|
98 | =item aio_stat $fh_or_path, $callback |
|
|
99 | |
|
|
100 | =item aio_lstat $fh, $callback |
|
|
101 | |
|
|
102 | Works like perl's C<stat> or C<lstat> in void context. The callback will |
|
|
103 | be called after the stat and the results will be available using C<stat _> |
|
|
104 | or C<-s _> etc... |
|
|
105 | |
|
|
106 | Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
|
|
107 | error when stat'ing a large file, the results will be silently truncated |
|
|
108 | unless perl itself is compiled with large file support. |
|
|
109 | |
|
|
110 | =item aio_unlink $pathname, $callback |
|
|
111 | |
|
|
112 | Asynchronously unlink a file. |
29 | |
113 | |
30 | =cut |
114 | =cut |
31 | |
115 | |
32 | =item Linux::AIO::read($fh,$offset,$length, $data,$dataoffset,$callback) |
116 | min_parallel 1; |
33 | Linux::AIO::write($fh,$offset,$length, $data,$dataoffset,$callback) |
|
|
34 | |
117 | |
35 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
118 | END { |
36 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
119 | max_parallel 0; |
37 | callback without the actual number of bytes read (or undef on error). |
120 | } |
38 | |
|
|
39 | =cut |
|
|
40 | |
121 | |
41 | 1; |
122 | 1; |
42 | |
123 | |
43 | =back |
124 | =back |
44 | |
125 | |
45 | =head1 BUGS |
126 | =head1 BUGS |
46 | |
127 | |
47 | This module has not yet been extensively tested. Watch out! |
128 | This module has been extensively tested in a large and very busy webserver |
|
|
129 | for many years now. |
48 | |
130 | |
49 | This module does not use the aio_* posix functions because a) linux does |
131 | - aio_open gives a fd, but all other functions expect a perl filehandle. |
50 | not have aio, b) the existing aio_* functions use pthreads, which are too |
|
|
51 | buggy for my usage. |
|
|
52 | |
132 | |
53 | =head1 SEE ALSO |
133 | =head1 SEE ALSO |
54 | |
134 | |
55 | L<Coro>. |
135 | L<Coro>. |
56 | |
136 | |
57 | =head1 AUTHOR |
137 | =head1 AUTHOR |
58 | |
138 | |
59 | Marc Lehmann <pcg@goof.com> |
139 | Marc Lehmann <schmorp@schmorp.de> |
60 | http://www.goof.com/pcg/marc/ |
140 | http://home.schmorp.de/ |
61 | |
141 | |
62 | =cut |
142 | =cut |
63 | |
143 | |