ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.pm
Revision: 1.5
Committed: Tue Feb 6 01:09:04 2007 UTC (17 years, 3 months ago) by root
Branch: MAIN
Changes since 1.4: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

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