1 |
=head1 NAME |
2 |
|
3 |
BDB - Asynchronous Berkeley DB access |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use BDB; |
8 |
|
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 |
package BDB; |
61 |
|
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 |
db_env_create db_env_open db_env_close |
72 |
db_create db_open db_close db_compact db_sync db_put db_get db_pget |
73 |
db_txn_commit db_txn_abort |
74 |
); |
75 |
our @EXPORT = (@BDB_REQ, qw(dbreq_pri dbreq_nice)); |
76 |
our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
77 |
min_parallel max_parallel max_idle |
78 |
nreqs nready npending nthreads |
79 |
max_poll_time max_poll_reqs); |
80 |
|
81 |
require XSLoader; |
82 |
XSLoader::load ("BDB", $VERSION); |
83 |
} |
84 |
|
85 |
=head2 SUPPORT FUNCTIONS |
86 |
|
87 |
=head3 EVENT PROCESSING AND EVENT LOOP INTEGRATION |
88 |
|
89 |
=over 4 |
90 |
|
91 |
=item $fileno = BDB::poll_fileno |
92 |
|
93 |
Return the I<request result pipe file descriptor>. This filehandle must be |
94 |
polled for reading by some mechanism outside this module (e.g. Event or |
95 |
select, see below or the SYNOPSIS). If the pipe becomes readable you have |
96 |
to call C<poll_cb> to check the results. |
97 |
|
98 |
See C<poll_cb> for an example. |
99 |
|
100 |
=item BDB::poll_cb |
101 |
|
102 |
Process some outstanding events on the result pipe. You have to call this |
103 |
regularly. Returns the number of events processed. Returns immediately |
104 |
when no events are outstanding. The amount of events processed depends on |
105 |
the settings of C<BDB::max_poll_req> and C<BDB::max_poll_time>. |
106 |
|
107 |
If not all requests were processed for whatever reason, the filehandle |
108 |
will still be ready when C<poll_cb> returns. |
109 |
|
110 |
Example: Install an Event watcher that automatically calls |
111 |
BDB::poll_cb with high priority: |
112 |
|
113 |
Event->io (fd => BDB::poll_fileno, |
114 |
poll => 'r', async => 1, |
115 |
cb => \&BDB::poll_cb); |
116 |
|
117 |
=item BDB::max_poll_reqs $nreqs |
118 |
|
119 |
=item BDB::max_poll_time $seconds |
120 |
|
121 |
These set the maximum number of requests (default C<0>, meaning infinity) |
122 |
that are being processed by C<BDB::poll_cb> in one call, respectively |
123 |
the maximum amount of time (default C<0>, meaning infinity) spent in |
124 |
C<BDB::poll_cb> to process requests (more correctly the mininum amount |
125 |
of time C<poll_cb> is allowed to use). |
126 |
|
127 |
Setting C<max_poll_time> to a non-zero value creates an overhead of one |
128 |
syscall per request processed, which is not normally a problem unless your |
129 |
callbacks are really really fast or your OS is really really slow (I am |
130 |
not mentioning Solaris here). Using C<max_poll_reqs> incurs no overhead. |
131 |
|
132 |
Setting these is useful if you want to ensure some level of |
133 |
interactiveness when perl is not fast enough to process all requests in |
134 |
time. |
135 |
|
136 |
For interactive programs, values such as C<0.01> to C<0.1> should be fine. |
137 |
|
138 |
Example: Install an Event watcher that automatically calls |
139 |
BDB::poll_cb with low priority, to ensure that other parts of the |
140 |
program get the CPU sometimes even under high AIO load. |
141 |
|
142 |
# try not to spend much more than 0.1s in poll_cb |
143 |
BDB::max_poll_time 0.1; |
144 |
|
145 |
# use a low priority so other tasks have priority |
146 |
Event->io (fd => BDB::poll_fileno, |
147 |
poll => 'r', nice => 1, |
148 |
cb => &BDB::poll_cb); |
149 |
|
150 |
=item BDB::poll_wait |
151 |
|
152 |
If there are any outstanding requests and none of them in the result |
153 |
phase, wait till the result filehandle becomes ready for reading (simply |
154 |
does a C<select> on the filehandle. This is useful if you want to |
155 |
synchronously wait for some requests to finish). |
156 |
|
157 |
See C<nreqs> for an example. |
158 |
|
159 |
=item BDB::poll |
160 |
|
161 |
Waits until some requests have been handled. |
162 |
|
163 |
Returns the number of requests processed, but is otherwise strictly |
164 |
equivalent to: |
165 |
|
166 |
BDB::poll_wait, BDB::poll_cb |
167 |
|
168 |
=item BDB::flush |
169 |
|
170 |
Wait till all outstanding AIO requests have been handled. |
171 |
|
172 |
Strictly equivalent to: |
173 |
|
174 |
BDB::poll_wait, BDB::poll_cb |
175 |
while BDB::nreqs; |
176 |
|
177 |
=head3 CONTROLLING THE NUMBER OF THREADS |
178 |
|
179 |
=item BDB::min_parallel $nthreads |
180 |
|
181 |
Set the minimum number of AIO threads to C<$nthreads>. The current |
182 |
default is C<8>, which means eight asynchronous operations can execute |
183 |
concurrently at any one time (the number of outstanding requests, |
184 |
however, is unlimited). |
185 |
|
186 |
BDB starts threads only on demand, when an AIO request is queued and |
187 |
no free thread exists. Please note that queueing up a hundred requests can |
188 |
create demand for a hundred threads, even if it turns out that everything |
189 |
is in the cache and could have been processed faster by a single thread. |
190 |
|
191 |
It is recommended to keep the number of threads relatively low, as some |
192 |
Linux kernel versions will scale negatively with the number of threads |
193 |
(higher parallelity => MUCH higher latency). With current Linux 2.6 |
194 |
versions, 4-32 threads should be fine. |
195 |
|
196 |
Under most circumstances you don't need to call this function, as the |
197 |
module selects a default that is suitable for low to moderate load. |
198 |
|
199 |
=item BDB::max_parallel $nthreads |
200 |
|
201 |
Sets the maximum number of AIO threads to C<$nthreads>. If more than the |
202 |
specified number of threads are currently running, this function kills |
203 |
them. This function blocks until the limit is reached. |
204 |
|
205 |
While C<$nthreads> are zero, aio requests get queued but not executed |
206 |
until the number of threads has been increased again. |
207 |
|
208 |
This module automatically runs C<max_parallel 0> at program end, to ensure |
209 |
that all threads are killed and that there are no outstanding requests. |
210 |
|
211 |
Under normal circumstances you don't need to call this function. |
212 |
|
213 |
=item BDB::max_idle $nthreads |
214 |
|
215 |
Limit the number of threads (default: 4) that are allowed to idle (i.e., |
216 |
threads that did not get a request to process within 10 seconds). That |
217 |
means if a thread becomes idle while C<$nthreads> other threads are also |
218 |
idle, it will free its resources and exit. |
219 |
|
220 |
This is useful when you allow a large number of threads (e.g. 100 or 1000) |
221 |
to allow for extremely high load situations, but want to free resources |
222 |
under normal circumstances (1000 threads can easily consume 30MB of RAM). |
223 |
|
224 |
The default is probably ok in most situations, especially if thread |
225 |
creation is fast. If thread creation is very slow on your system you might |
226 |
want to use larger values. |
227 |
|
228 |
=item $oldmaxreqs = BDB::max_outstanding $maxreqs |
229 |
|
230 |
This is a very bad function to use in interactive programs because it |
231 |
blocks, and a bad way to reduce concurrency because it is inexact: Better |
232 |
use an C<aio_group> together with a feed callback. |
233 |
|
234 |
Sets the maximum number of outstanding requests to C<$nreqs>. If you |
235 |
to queue up more than this number of requests, the next call to the |
236 |
C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
237 |
function will block until the limit is no longer exceeded. |
238 |
|
239 |
The default value is very large, so there is no practical limit on the |
240 |
number of outstanding requests. |
241 |
|
242 |
You can still queue as many requests as you want. Therefore, |
243 |
C<max_oustsanding> is mainly useful in simple scripts (with low values) or |
244 |
as a stop gap to shield against fatal memory overflow (with large values). |
245 |
|
246 |
=item BDB::set_sync_prepare $cb |
247 |
|
248 |
Sets a callback that is called whenever a request is created without an |
249 |
explicit callback. It has to return two code references. The first is used |
250 |
as the request callback, and the second is called to wait until the first |
251 |
callback has been called. The default implementation works like this: |
252 |
|
253 |
sub { |
254 |
my $status; |
255 |
( |
256 |
sub { $status = $! }, |
257 |
sub { BDB::poll while !defined $status; $! = $status }, |
258 |
) |
259 |
} |
260 |
|
261 |
=back |
262 |
|
263 |
=head3 STATISTICAL INFORMATION |
264 |
|
265 |
=over 4 |
266 |
|
267 |
=item BDB::nreqs |
268 |
|
269 |
Returns the number of requests currently in the ready, execute or pending |
270 |
states (i.e. for which their callback has not been invoked yet). |
271 |
|
272 |
Example: wait till there are no outstanding requests anymore: |
273 |
|
274 |
BDB::poll_wait, BDB::poll_cb |
275 |
while BDB::nreqs; |
276 |
|
277 |
=item BDB::nready |
278 |
|
279 |
Returns the number of requests currently in the ready state (not yet |
280 |
executed). |
281 |
|
282 |
=item BDB::npending |
283 |
|
284 |
Returns the number of requests currently in the pending state (executed, |
285 |
but not yet processed by poll_cb). |
286 |
|
287 |
=back |
288 |
|
289 |
=cut |
290 |
|
291 |
set_sync_prepare { |
292 |
my $status; |
293 |
( |
294 |
sub { |
295 |
$status = $!; |
296 |
}, |
297 |
sub { |
298 |
BDB::poll while !defined $status; |
299 |
$! = $status; |
300 |
}, |
301 |
) |
302 |
}; |
303 |
|
304 |
min_parallel 8; |
305 |
|
306 |
END { flush } |
307 |
|
308 |
1; |
309 |
|
310 |
=head2 FORK BEHAVIOUR |
311 |
|
312 |
This module should do "the right thing" when the process using it forks: |
313 |
|
314 |
Before the fork, IO::AIO enters a quiescent state where no requests |
315 |
can be added in other threads and no results will be processed. After |
316 |
the fork the parent simply leaves the quiescent state and continues |
317 |
request/result processing, while the child frees the request/result queue |
318 |
(so that the requests started before the fork will only be handled in the |
319 |
parent). Threads will be started on demand until the limit set in the |
320 |
parent process has been reached again. |
321 |
|
322 |
In short: the parent will, after a short pause, continue as if fork had |
323 |
not been called, while the child will act as if IO::AIO has not been used |
324 |
yet. |
325 |
|
326 |
=head2 MEMORY USAGE |
327 |
|
328 |
Per-request usage: |
329 |
|
330 |
Each aio request uses - depending on your architecture - around 100-200 |
331 |
bytes of memory. In addition, stat requests need a stat buffer (possibly |
332 |
a few hundred bytes), readdir requires a result buffer and so on. Perl |
333 |
scalars and other data passed into aio requests will also be locked and |
334 |
will consume memory till the request has entered the done state. |
335 |
|
336 |
This is now awfully much, so queuing lots of requests is not usually a |
337 |
problem. |
338 |
|
339 |
Per-thread usage: |
340 |
|
341 |
In the execution phase, some aio requests require more memory for |
342 |
temporary buffers, and each thread requires a stack and other data |
343 |
structures (usually around 16k-128k, depending on the OS). |
344 |
|
345 |
=head1 KNOWN BUGS |
346 |
|
347 |
Known bugs will be fixed in the next release. |
348 |
|
349 |
=head1 SEE ALSO |
350 |
|
351 |
L<Coro::AIO>. |
352 |
|
353 |
=head1 AUTHOR |
354 |
|
355 |
Marc Lehmann <schmorp@schmorp.de> |
356 |
http://home.schmorp.de/ |
357 |
|
358 |
=cut |
359 |
|