Figured out what was wrong. The $env and $db vars need to be child-process-local or the locking doesn't work right. Here's the updated, working script code:
#!/usr/bin/env perl # vim:set ts=4 sw=4 ai: use strict; use warnings; use Carp; use POSIX ":sys_wait_h"; use Data::Dumper; use lib '/junk/lib'; use BerkeleyDB; use BerkeleyDB::Hash; use Time::HiRes qw(gettimeofday tv_interval); use constant RUNS => 1000000; use constant DBPATH => '/junk/test.db'; use constant NUM_CHILDREN => 10; $|++; # print backtrace info sub bt { croak "DIED ($.)\n"; } # generate random integer sub rand_num { 4278190080 + int(rand(1048576)); } # read an entry from a BDB sub read_db { my ($db, $key) = @_; my $val; $db->db_get($key, $val); return (defined($val)) ? eval($val) : {}; } # increment an entry in a BDB, inserting if not there sub write_db { my ($db, $key, $stuff) = @_; my $val = read_db($db, $key); $val->{timestamp} = time; $val->{events}++; $val->{stuff}++ if ($stuff); my $str = Dumper(\$val) or croak "failed to serialize row data: $! +\n"; return $db->db_put($key, $str); } # fork off a worker process that will mutate the BDB independently of # the others sub spawn_child { my $pid = fork; croak "Could not fork: '$!'\n" unless (defined($pid)); return unless ($pid == 0); my $env = new BerkeleyDB::Env( -Cachesize => 4 * 1024 * 1024, -Flags => (DB_CREATE() | DB_INIT_CDB() | DB_INIT_MPOOL())) or croak "Failed to create DB env: '$!' ($BerkeleyDB::Error)\n +"; my $db = new BerkeleyDB::Hash( -Filename => DBPATH, -Env => $env, -Flags => DB_CREATE()) or croak "Failed to open DBPATH: '$!' ($BerkeleyDB::Error)\n"; # child process starts here my ($start, $finish, $key, $ip, $rv, $val, $reads, $writes, $delet +es); # perform a number of operations using BDB transactions $start = [ gettimeofday ]; for (1..RUNS) { $key = int(rand(101)); $ip = rand_num(); if ($key <= 49) { # do random update/insert $rv = write_db($db, $ip, ($key % 2 == 0)); croak "db_put failed: $rv ($! / $BerkeleyDB::Error)\n" unless ($rv == 0); $reads++; $writes++; } elsif ($key <= 98) { # do random read read_db($db, $ip); $reads++; } else { # do random delete $db->db_del($key); $deletes++; } } $finish = [ gettimeofday ]; # print summary info for timing my $sum = ($reads || 0) + ($writes || 0) + ($deletes || 0); my $elapsed = tv_interval($start, $finish); printf "$$: %d reads, %d writes, %d deletes (%d total) in %0.2f se +conds: %0.2f ops/sec\n", ($reads || 0), ($writes || 0), ($deletes || 0), $sum, $elapsed, $sum / $elapsed; exit 0; } # install signal handlers $SIG{__WARN__} = \&bt; # spawn concurrent DB mutators and just wait for them to finish for (1..NUM_CHILDREN) { spawn_child(); } for (1..NUM_CHILDREN) { my $pid = wait; }

In reply to Re: BerkeleyDB blowing up on multiple, concurrent processes by m0r0n
in thread BerkeleyDB blowing up on multiple, concurrent processes by m0r0n

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.