1 | NAME |
1 | NAME |
2 | BDB - Asynchronous Berkeley DB access |
2 | BDB - Asynchronous Berkeley DB access |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use BDB; |
5 | use BDB; |
|
|
6 | |
|
|
7 | my $env = db_env_create; |
|
|
8 | |
|
|
9 | mkdir "bdtest", 0700; |
|
|
10 | db_env_open |
|
|
11 | $env, |
|
|
12 | "bdtest", |
|
|
13 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL |
|
|
14 | | BDB::INIT_TXN | BDB::RECOVER | BDB::USE_ENVIRON | BDB::CREATE, |
|
|
15 | 0600; |
|
|
16 | |
|
|
17 | $env->set_flags (BDB::AUTO_COMMIT | BDB::TXN_NOSYNC, 1); |
|
|
18 | |
|
|
19 | my $db = db_create $env; |
|
|
20 | db_open $db, undef, "table", undef, BDB::BTREE, BDB::AUTO_COMMIT | BDB::CREATE |
|
|
21 | | BDB::READ_UNCOMMITTED, 0600; |
|
|
22 | db_put $db, undef, "key", "data", 0, sub { |
|
|
23 | db_del $db, undef, "key"; |
|
|
24 | }; |
|
|
25 | db_sync $db; |
|
|
26 | |
|
|
27 | # automatic result processing with AnyEvent: |
|
|
28 | our $FH; open $FH, "<&=" . BDB::poll_fileno; |
|
|
29 | our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => \&BDB::poll_cb); |
|
|
30 | |
|
|
31 | # automatic result processing with EV: |
|
|
32 | my $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
|
|
33 | |
|
|
34 | # with Glib: |
|
|
35 | add_watch Glib::IO BDB::poll_fileno, |
|
|
36 | in => sub { BDB::poll_cb; 1 }; |
|
|
37 | |
|
|
38 | # or simply flush manually |
|
|
39 | BDB::flush; |
6 | |
40 | |
7 | DESCRIPTION |
41 | DESCRIPTION |
8 | See the BerkeleyDB documentation |
42 | See the BerkeleyDB documentation |
9 | (<http://www.oracle.com/technology/documentation/berkeley-db/db/index.ht |
43 | (<http://www.oracle.com/technology/documentation/berkeley-db/db/index.ht |
10 | ml>). The BDB API is very similar to the C API (the translation has been |
44 | ml>). The BDB API is very similar to the C API (the translation has been |
… | |
… | |
193 | $int = $env->set_lk_max_lockers (U32 max) |
227 | $int = $env->set_lk_max_lockers (U32 max) |
194 | $int = $env->set_lk_max_locks (U32 max) |
228 | $int = $env->set_lk_max_locks (U32 max) |
195 | $int = $env->set_lk_max_objects (U32 max) |
229 | $int = $env->set_lk_max_objects (U32 max) |
196 | $int = $env->set_lg_bsize (U32 max) |
230 | $int = $env->set_lg_bsize (U32 max) |
197 | $int = $env->set_lg_max (U32 max) |
231 | $int = $env->set_lg_max (U32 max) |
|
|
232 | $int = $env->mutex_set_increment (U32 increment) |
|
|
233 | $int = $env->mutex_set_tas_spins (U32 tas_spins) |
|
|
234 | $int = $env->mutex_set_max (U32 max) |
|
|
235 | $int = $env->mutex_set_align (U32 align) |
198 | |
236 | |
199 | $txn = $env->txn_begin (DB_TXN_ornull *parent = 0, U32 flags = 0) |
237 | $txn = $env->txn_begin (DB_TXN_ornull *parent = 0, U32 flags = 0) |
200 | flags: READ_COMMITTED READ_UNCOMMITTED TXN_NOSYNC TXN_NOWAIT TXN_SNAPSHOT TXN_SYNC TXN_WAIT TXN_WRITE_NOSYNC |
238 | flags: READ_COMMITTED READ_UNCOMMITTED TXN_NOSYNC TXN_NOWAIT TXN_SNAPSHOT TXN_SYNC TXN_WAIT TXN_WRITE_NOSYNC |
201 | |
239 | |
202 | Example: |
240 | Example: |
… | |
… | |
367 | interactiveness when perl is not fast enough to process all requests |
405 | interactiveness when perl is not fast enough to process all requests |
368 | in time. |
406 | in time. |
369 | |
407 | |
370 | For interactive programs, values such as 0.01 to 0.1 should be fine. |
408 | For interactive programs, values such as 0.01 to 0.1 should be fine. |
371 | |
409 | |
372 | Example: Install an Event watcher that automatically calls |
410 | Example: Install an EV watcher that automatically calls BDB::poll_cb |
373 | BDB::poll_cb with low priority, to ensure that other parts of the |
411 | with low priority, to ensure that other parts of the program get the |
374 | program get the CPU sometimes even under high AIO load. |
412 | CPU sometimes even under high load. |
375 | |
413 | |
376 | # try not to spend much more than 0.1s in poll_cb |
414 | # try not to spend much more than 0.1s in poll_cb |
377 | BDB::max_poll_time 0.1; |
415 | BDB::max_poll_time 0.1; |
378 | |
416 | |
379 | # use a low priority so other tasks have priority |
417 | my $bdb_poll = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb); |
380 | Event->io (fd => BDB::poll_fileno, |
|
|
381 | poll => 'r', nice => 1, |
|
|
382 | cb => &BDB::poll_cb); |
|
|
383 | |
418 | |
384 | BDB::poll_wait |
419 | BDB::poll_wait |
385 | If there are any outstanding requests and none of them in the result |
420 | If there are any outstanding requests and none of them in the result |
386 | phase, wait till the result filehandle becomes ready for reading |
421 | phase, wait till the result filehandle becomes ready for reading |
387 | (simply does a "select" on the filehandle. This is useful if you |
422 | (simply does a "select" on the filehandle. This is useful if you |
… | |
… | |
396 | equivalent to: |
431 | equivalent to: |
397 | |
432 | |
398 | BDB::poll_wait, BDB::poll_cb |
433 | BDB::poll_wait, BDB::poll_cb |
399 | |
434 | |
400 | BDB::flush |
435 | BDB::flush |
401 | Wait till all outstanding AIO requests have been handled. |
436 | Wait till all outstanding BDB requests have been handled. |
402 | |
437 | |
403 | Strictly equivalent to: |
438 | Strictly equivalent to: |
404 | |
439 | |
405 | BDB::poll_wait, BDB::poll_cb |
440 | BDB::poll_wait, BDB::poll_cb |
406 | while BDB::nreqs; |
441 | while BDB::nreqs; |
407 | |
442 | |
408 | CONTROLLING THE NUMBER OF THREADS |
443 | CONTROLLING THE NUMBER OF THREADS |
409 | BDB::min_parallel $nthreads |
444 | BDB::min_parallel $nthreads |
410 | Set the minimum number of AIO threads to $nthreads. The current |
445 | Set the minimum number of BDB threads to $nthreads. The current |
411 | default is 8, which means eight asynchronous operations can execute |
446 | default is 8, which means eight asynchronous operations can execute |
412 | concurrently at any one time (the number of outstanding requests, |
447 | concurrently at any one time (the number of outstanding requests, |
413 | however, is unlimited). |
448 | however, is unlimited). |
414 | |
449 | |
415 | BDB starts threads only on demand, when an AIO request is queued and |
450 | BDB starts threads only on demand, when an BDB request is queued and |
416 | no free thread exists. Please note that queueing up a hundred |
451 | no free thread exists. Please note that queueing up a hundred |
417 | requests can create demand for a hundred threads, even if it turns |
452 | requests can create demand for a hundred threads, even if it turns |
418 | out that everything is in the cache and could have been processed |
453 | out that everything is in the cache and could have been processed |
419 | faster by a single thread. |
454 | faster by a single thread. |
420 | |
455 | |
… | |
… | |
426 | Under most circumstances you don't need to call this function, as |
461 | Under most circumstances you don't need to call this function, as |
427 | the module selects a default that is suitable for low to moderate |
462 | the module selects a default that is suitable for low to moderate |
428 | load. |
463 | load. |
429 | |
464 | |
430 | BDB::max_parallel $nthreads |
465 | BDB::max_parallel $nthreads |
431 | Sets the maximum number of AIO threads to $nthreads. If more than |
466 | Sets the maximum number of BDB threads to $nthreads. If more than |
432 | the specified number of threads are currently running, this function |
467 | the specified number of threads are currently running, this function |
433 | kills them. This function blocks until the limit is reached. |
468 | kills them. This function blocks until the limit is reached. |
434 | |
469 | |
435 | While $nthreads are zero, aio requests get queued but not executed |
470 | While $nthreads are zero, aio requests get queued but not executed |
436 | until the number of threads has been increased again. |
471 | until the number of threads has been increased again. |
… | |
… | |
509 | (executed, but not yet processed by poll_cb). |
544 | (executed, but not yet processed by poll_cb). |
510 | |
545 | |
511 | FORK BEHAVIOUR |
546 | FORK BEHAVIOUR |
512 | This module should do "the right thing" when the process using it forks: |
547 | This module should do "the right thing" when the process using it forks: |
513 | |
548 | |
514 | Before the fork, IO::AIO enters a quiescent state where no requests can |
549 | Before the fork, BDB enters a quiescent state where no requests can be |
515 | be added in other threads and no results will be processed. After the |
550 | added in other threads and no results will be processed. After the fork |
516 | fork the parent simply leaves the quiescent state and continues |
551 | the parent simply leaves the quiescent state and continues |
517 | request/result processing, while the child frees the request/result |
552 | request/result processing, while the child frees the request/result |
518 | queue (so that the requests started before the fork will only be handled |
553 | queue (so that the requests started before the fork will only be handled |
519 | in the parent). Threads will be started on demand until the limit set in |
554 | in the parent). Threads will be started on demand until the limit set in |
520 | the parent process has been reached again. |
555 | the parent process has been reached again. |
521 | |
556 | |
522 | In short: the parent will, after a short pause, continue as if fork had |
557 | In short: the parent will, after a short pause, continue as if fork had |
523 | not been called, while the child will act as if IO::AIO has not been |
558 | not been called, while the child will act as if BDB has not been used |
524 | used yet. |
559 | yet. |
525 | |
560 | |
526 | MEMORY USAGE |
561 | MEMORY USAGE |
527 | Per-request usage: |
562 | Per-request usage: |
528 | |
563 | |
529 | Each aio request uses - depending on your architecture - around 100-200 |
564 | Each aio request uses - depending on your architecture - around 100-200 |
… | |
… | |
548 | with an operating system error or DB_LOCK_NOTGRANTED, the internal |
583 | with an operating system error or DB_LOCK_NOTGRANTED, the internal |
549 | TXN_DEADLOCK flag will be set on the transaction. See C<db_txn_finish>, |
584 | TXN_DEADLOCK flag will be set on the transaction. See C<db_txn_finish>, |
550 | above. |
585 | above. |
551 | |
586 | |
552 | SEE ALSO |
587 | SEE ALSO |
553 | Coro::AIO. |
588 | Coro::BDB, IO::AIO. |
554 | |
589 | |
555 | AUTHOR |
590 | AUTHOR |
556 | Marc Lehmann <schmorp@schmorp.de> |
591 | Marc Lehmann <schmorp@schmorp.de> |
557 | http://home.schmorp.de/ |
592 | http://home.schmorp.de/ |
558 | |
593 | |