1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.2 |
BDB - Asynchronous Berkeley DB access |
4 |
root |
1.1 |
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
root |
1.2 |
use BDB; |
8 |
root |
1.1 |
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
=head2 EXAMPLE |
12 |
|
|
|
13 |
|
|
=head1 REQUEST ANATOMY AND LIFETIME |
14 |
|
|
|
15 |
|
|
Every request method creates a request. which is a C data structure not |
16 |
|
|
directly visible to Perl. |
17 |
|
|
|
18 |
|
|
During their existance, bdb requests travel through the following states, |
19 |
|
|
in order: |
20 |
|
|
|
21 |
|
|
=over 4 |
22 |
|
|
|
23 |
|
|
=item ready |
24 |
|
|
|
25 |
|
|
Immediately after a request is created it is put into the ready state, |
26 |
|
|
waiting for a thread to execute it. |
27 |
|
|
|
28 |
|
|
=item execute |
29 |
|
|
|
30 |
|
|
A thread has accepted the request for processing and is currently |
31 |
|
|
executing it (e.g. blocking in read). |
32 |
|
|
|
33 |
|
|
=item pending |
34 |
|
|
|
35 |
|
|
The request has been executed and is waiting for result processing. |
36 |
|
|
|
37 |
|
|
While request submission and execution is fully asynchronous, result |
38 |
|
|
processing is not and relies on the perl interpreter calling C<poll_cb> |
39 |
|
|
(or another function with the same effect). |
40 |
|
|
|
41 |
|
|
=item result |
42 |
|
|
|
43 |
|
|
The request results are processed synchronously by C<poll_cb>. |
44 |
|
|
|
45 |
|
|
The C<poll_cb> function will process all outstanding aio requests by |
46 |
|
|
calling their callbacks, freeing memory associated with them and managing |
47 |
|
|
any groups they are contained in. |
48 |
|
|
|
49 |
|
|
=item done |
50 |
|
|
|
51 |
|
|
Request has reached the end of its lifetime and holds no resources anymore |
52 |
|
|
(except possibly for the Perl object, but its connection to the actual |
53 |
|
|
aio request is severed and calling its methods will either do nothing or |
54 |
|
|
result in a runtime error). |
55 |
|
|
|
56 |
|
|
=back |
57 |
|
|
|
58 |
|
|
=cut |
59 |
|
|
|
60 |
root |
1.2 |
package BDB; |
61 |
root |
1.1 |
|
62 |
|
|
no warnings; |
63 |
|
|
use strict 'vars'; |
64 |
|
|
|
65 |
|
|
use base 'Exporter'; |
66 |
|
|
|
67 |
|
|
BEGIN { |
68 |
|
|
our $VERSION = '0.1'; |
69 |
|
|
|
70 |
|
|
our @BDB_REQ = qw(); |
71 |
|
|
our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
72 |
|
|
min_parallel max_parallel max_idle |
73 |
|
|
nreqs nready npending nthreads |
74 |
|
|
max_poll_time max_poll_reqs); |
75 |
|
|
|
76 |
|
|
require XSLoader; |
77 |
root |
1.2 |
XSLoader::load ("BDB", $VERSION); |
78 |
root |
1.1 |
} |
79 |
|
|
|
80 |
|
|
=head2 SUPPORT FUNCTIONS |
81 |
|
|
|
82 |
|
|
=head3 EVENT PROCESSING AND EVENT LOOP INTEGRATION |
83 |
|
|
|
84 |
|
|
=over 4 |
85 |
|
|
|
86 |
root |
1.2 |
=item $fileno = BDB::poll_fileno |
87 |
root |
1.1 |
|
88 |
|
|
Return the I<request result pipe file descriptor>. This filehandle must be |
89 |
|
|
polled for reading by some mechanism outside this module (e.g. Event or |
90 |
|
|
select, see below or the SYNOPSIS). If the pipe becomes readable you have |
91 |
|
|
to call C<poll_cb> to check the results. |
92 |
|
|
|
93 |
|
|
See C<poll_cb> for an example. |
94 |
|
|
|
95 |
root |
1.2 |
=item BDB::poll_cb |
96 |
root |
1.1 |
|
97 |
|
|
Process some outstanding events on the result pipe. You have to call this |
98 |
|
|
regularly. Returns the number of events processed. Returns immediately |
99 |
|
|
when no events are outstanding. The amount of events processed depends on |
100 |
root |
1.2 |
the settings of C<BDB::max_poll_req> and C<BDB::max_poll_time>. |
101 |
root |
1.1 |
|
102 |
|
|
If not all requests were processed for whatever reason, the filehandle |
103 |
|
|
will still be ready when C<poll_cb> returns. |
104 |
|
|
|
105 |
|
|
Example: Install an Event watcher that automatically calls |
106 |
root |
1.2 |
BDB::poll_cb with high priority: |
107 |
root |
1.1 |
|
108 |
root |
1.2 |
Event->io (fd => BDB::poll_fileno, |
109 |
root |
1.1 |
poll => 'r', async => 1, |
110 |
root |
1.2 |
cb => \&BDB::poll_cb); |
111 |
root |
1.1 |
|
112 |
root |
1.2 |
=item BDB::max_poll_reqs $nreqs |
113 |
root |
1.1 |
|
114 |
root |
1.2 |
=item BDB::max_poll_time $seconds |
115 |
root |
1.1 |
|
116 |
|
|
These set the maximum number of requests (default C<0>, meaning infinity) |
117 |
root |
1.2 |
that are being processed by C<BDB::poll_cb> in one call, respectively |
118 |
root |
1.1 |
the maximum amount of time (default C<0>, meaning infinity) spent in |
119 |
root |
1.2 |
C<BDB::poll_cb> to process requests (more correctly the mininum amount |
120 |
root |
1.1 |
of time C<poll_cb> is allowed to use). |
121 |
|
|
|
122 |
|
|
Setting C<max_poll_time> to a non-zero value creates an overhead of one |
123 |
|
|
syscall per request processed, which is not normally a problem unless your |
124 |
|
|
callbacks are really really fast or your OS is really really slow (I am |
125 |
|
|
not mentioning Solaris here). Using C<max_poll_reqs> incurs no overhead. |
126 |
|
|
|
127 |
|
|
Setting these is useful if you want to ensure some level of |
128 |
|
|
interactiveness when perl is not fast enough to process all requests in |
129 |
|
|
time. |
130 |
|
|
|
131 |
|
|
For interactive programs, values such as C<0.01> to C<0.1> should be fine. |
132 |
|
|
|
133 |
|
|
Example: Install an Event watcher that automatically calls |
134 |
root |
1.2 |
BDB::poll_cb with low priority, to ensure that other parts of the |
135 |
root |
1.1 |
program get the CPU sometimes even under high AIO load. |
136 |
|
|
|
137 |
|
|
# try not to spend much more than 0.1s in poll_cb |
138 |
root |
1.2 |
BDB::max_poll_time 0.1; |
139 |
root |
1.1 |
|
140 |
|
|
# use a low priority so other tasks have priority |
141 |
root |
1.2 |
Event->io (fd => BDB::poll_fileno, |
142 |
root |
1.1 |
poll => 'r', nice => 1, |
143 |
root |
1.2 |
cb => &BDB::poll_cb); |
144 |
root |
1.1 |
|
145 |
root |
1.2 |
=item BDB::poll_wait |
146 |
root |
1.1 |
|
147 |
|
|
If there are any outstanding requests and none of them in the result |
148 |
|
|
phase, wait till the result filehandle becomes ready for reading (simply |
149 |
|
|
does a C<select> on the filehandle. This is useful if you want to |
150 |
|
|
synchronously wait for some requests to finish). |
151 |
|
|
|
152 |
|
|
See C<nreqs> for an example. |
153 |
|
|
|
154 |
root |
1.2 |
=item BDB::poll |
155 |
root |
1.1 |
|
156 |
|
|
Waits until some requests have been handled. |
157 |
|
|
|
158 |
|
|
Returns the number of requests processed, but is otherwise strictly |
159 |
|
|
equivalent to: |
160 |
|
|
|
161 |
root |
1.2 |
BDB::poll_wait, BDB::poll_cb |
162 |
root |
1.1 |
|
163 |
root |
1.2 |
=item BDB::flush |
164 |
root |
1.1 |
|
165 |
|
|
Wait till all outstanding AIO requests have been handled. |
166 |
|
|
|
167 |
|
|
Strictly equivalent to: |
168 |
|
|
|
169 |
root |
1.2 |
BDB::poll_wait, BDB::poll_cb |
170 |
|
|
while BDB::nreqs; |
171 |
root |
1.1 |
|
172 |
|
|
=head3 CONTROLLING THE NUMBER OF THREADS |
173 |
|
|
|
174 |
root |
1.2 |
=item BDB::min_parallel $nthreads |
175 |
root |
1.1 |
|
176 |
|
|
Set the minimum number of AIO threads to C<$nthreads>. The current |
177 |
|
|
default is C<8>, which means eight asynchronous operations can execute |
178 |
|
|
concurrently at any one time (the number of outstanding requests, |
179 |
|
|
however, is unlimited). |
180 |
|
|
|
181 |
root |
1.2 |
BDB starts threads only on demand, when an AIO request is queued and |
182 |
root |
1.1 |
no free thread exists. Please note that queueing up a hundred requests can |
183 |
|
|
create demand for a hundred threads, even if it turns out that everything |
184 |
|
|
is in the cache and could have been processed faster by a single thread. |
185 |
|
|
|
186 |
|
|
It is recommended to keep the number of threads relatively low, as some |
187 |
|
|
Linux kernel versions will scale negatively with the number of threads |
188 |
|
|
(higher parallelity => MUCH higher latency). With current Linux 2.6 |
189 |
|
|
versions, 4-32 threads should be fine. |
190 |
|
|
|
191 |
|
|
Under most circumstances you don't need to call this function, as the |
192 |
|
|
module selects a default that is suitable for low to moderate load. |
193 |
|
|
|
194 |
root |
1.2 |
=item BDB::max_parallel $nthreads |
195 |
root |
1.1 |
|
196 |
|
|
Sets the maximum number of AIO threads to C<$nthreads>. If more than the |
197 |
|
|
specified number of threads are currently running, this function kills |
198 |
|
|
them. This function blocks until the limit is reached. |
199 |
|
|
|
200 |
|
|
While C<$nthreads> are zero, aio requests get queued but not executed |
201 |
|
|
until the number of threads has been increased again. |
202 |
|
|
|
203 |
|
|
This module automatically runs C<max_parallel 0> at program end, to ensure |
204 |
|
|
that all threads are killed and that there are no outstanding requests. |
205 |
|
|
|
206 |
|
|
Under normal circumstances you don't need to call this function. |
207 |
|
|
|
208 |
root |
1.2 |
=item BDB::max_idle $nthreads |
209 |
root |
1.1 |
|
210 |
|
|
Limit the number of threads (default: 4) that are allowed to idle (i.e., |
211 |
|
|
threads that did not get a request to process within 10 seconds). That |
212 |
|
|
means if a thread becomes idle while C<$nthreads> other threads are also |
213 |
|
|
idle, it will free its resources and exit. |
214 |
|
|
|
215 |
|
|
This is useful when you allow a large number of threads (e.g. 100 or 1000) |
216 |
|
|
to allow for extremely high load situations, but want to free resources |
217 |
|
|
under normal circumstances (1000 threads can easily consume 30MB of RAM). |
218 |
|
|
|
219 |
|
|
The default is probably ok in most situations, especially if thread |
220 |
|
|
creation is fast. If thread creation is very slow on your system you might |
221 |
|
|
want to use larger values. |
222 |
|
|
|
223 |
root |
1.2 |
=item $oldmaxreqs = BDB::max_outstanding $maxreqs |
224 |
root |
1.1 |
|
225 |
|
|
This is a very bad function to use in interactive programs because it |
226 |
|
|
blocks, and a bad way to reduce concurrency because it is inexact: Better |
227 |
|
|
use an C<aio_group> together with a feed callback. |
228 |
|
|
|
229 |
|
|
Sets the maximum number of outstanding requests to C<$nreqs>. If you |
230 |
|
|
to queue up more than this number of requests, the next call to the |
231 |
|
|
C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
232 |
|
|
function will block until the limit is no longer exceeded. |
233 |
|
|
|
234 |
|
|
The default value is very large, so there is no practical limit on the |
235 |
|
|
number of outstanding requests. |
236 |
|
|
|
237 |
|
|
You can still queue as many requests as you want. Therefore, |
238 |
|
|
C<max_oustsanding> is mainly useful in simple scripts (with low values) or |
239 |
|
|
as a stop gap to shield against fatal memory overflow (with large values). |
240 |
|
|
|
241 |
|
|
=head3 STATISTICAL INFORMATION |
242 |
|
|
|
243 |
root |
1.2 |
=item BDB::nreqs |
244 |
root |
1.1 |
|
245 |
|
|
Returns the number of requests currently in the ready, execute or pending |
246 |
|
|
states (i.e. for which their callback has not been invoked yet). |
247 |
|
|
|
248 |
|
|
Example: wait till there are no outstanding requests anymore: |
249 |
|
|
|
250 |
root |
1.2 |
BDB::poll_wait, BDB::poll_cb |
251 |
|
|
while BDB::nreqs; |
252 |
root |
1.1 |
|
253 |
root |
1.2 |
=item BDB::nready |
254 |
root |
1.1 |
|
255 |
|
|
Returns the number of requests currently in the ready state (not yet |
256 |
|
|
executed). |
257 |
|
|
|
258 |
root |
1.2 |
=item BDB::npending |
259 |
root |
1.1 |
|
260 |
|
|
Returns the number of requests currently in the pending state (executed, |
261 |
|
|
but not yet processed by poll_cb). |
262 |
|
|
|
263 |
|
|
=back |
264 |
|
|
|
265 |
|
|
=cut |
266 |
|
|
|
267 |
|
|
min_parallel 8; |
268 |
|
|
|
269 |
|
|
END { flush } |
270 |
|
|
|
271 |
|
|
1; |
272 |
|
|
|
273 |
|
|
=head2 FORK BEHAVIOUR |
274 |
|
|
|
275 |
|
|
This module should do "the right thing" when the process using it forks: |
276 |
|
|
|
277 |
|
|
Before the fork, IO::AIO enters a quiescent state where no requests |
278 |
|
|
can be added in other threads and no results will be processed. After |
279 |
|
|
the fork the parent simply leaves the quiescent state and continues |
280 |
|
|
request/result processing, while the child frees the request/result queue |
281 |
|
|
(so that the requests started before the fork will only be handled in the |
282 |
|
|
parent). Threads will be started on demand until the limit set in the |
283 |
|
|
parent process has been reached again. |
284 |
|
|
|
285 |
|
|
In short: the parent will, after a short pause, continue as if fork had |
286 |
|
|
not been called, while the child will act as if IO::AIO has not been used |
287 |
|
|
yet. |
288 |
|
|
|
289 |
|
|
=head2 MEMORY USAGE |
290 |
|
|
|
291 |
|
|
Per-request usage: |
292 |
|
|
|
293 |
|
|
Each aio request uses - depending on your architecture - around 100-200 |
294 |
|
|
bytes of memory. In addition, stat requests need a stat buffer (possibly |
295 |
|
|
a few hundred bytes), readdir requires a result buffer and so on. Perl |
296 |
|
|
scalars and other data passed into aio requests will also be locked and |
297 |
|
|
will consume memory till the request has entered the done state. |
298 |
|
|
|
299 |
|
|
This is now awfully much, so queuing lots of requests is not usually a |
300 |
|
|
problem. |
301 |
|
|
|
302 |
|
|
Per-thread usage: |
303 |
|
|
|
304 |
|
|
In the execution phase, some aio requests require more memory for |
305 |
|
|
temporary buffers, and each thread requires a stack and other data |
306 |
|
|
structures (usually around 16k-128k, depending on the OS). |
307 |
|
|
|
308 |
|
|
=head1 KNOWN BUGS |
309 |
|
|
|
310 |
|
|
Known bugs will be fixed in the next release. |
311 |
|
|
|
312 |
|
|
=head1 SEE ALSO |
313 |
|
|
|
314 |
|
|
L<Coro::AIO>. |
315 |
|
|
|
316 |
|
|
=head1 AUTHOR |
317 |
|
|
|
318 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
319 |
|
|
http://home.schmorp.de/ |
320 |
|
|
|
321 |
|
|
=cut |
322 |
|
|
|