… | |
… | |
80 | our $NEXT_TICK; |
80 | our $NEXT_TICK; |
81 | our $NOW; |
81 | our $NOW; |
82 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
82 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
83 | |
83 | |
84 | our $BDB_POLL_WATCHER; |
84 | our $BDB_POLL_WATCHER; |
|
|
85 | our $BDB_CHECKPOINT_WATCHER; |
|
|
86 | our $BDB_TRICKLE_WATCHER; |
85 | our $DB_ENV; |
87 | our $DB_ENV; |
86 | |
88 | |
87 | our %CFG; |
89 | our %CFG; |
88 | |
90 | |
89 | our $UPTIME; $UPTIME ||= time; |
91 | our $UPTIME; $UPTIME ||= time; |
… | |
… | |
507 | =item cf::db_put $family => $key => $value |
509 | =item cf::db_put $family => $key => $value |
508 | |
510 | |
509 | Stores the given C<$value> in the family. It can currently store binary |
511 | Stores the given C<$value> in the family. It can currently store binary |
510 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
512 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
511 | |
513 | |
|
|
514 | =item $db = cf::db_table "name" |
|
|
515 | |
|
|
516 | Create and/or open a new database table. The string must not be "db" and must be unique |
|
|
517 | within each server. |
|
|
518 | |
512 | =cut |
519 | =cut |
|
|
520 | |
|
|
521 | sub db_table($) { |
|
|
522 | my ($name) = @_; |
|
|
523 | my $db = BDB::db_create $DB_ENV; |
|
|
524 | |
|
|
525 | eval { |
|
|
526 | $db->set_flags (BDB::CHKSUM); |
|
|
527 | |
|
|
528 | utf8::encode $name; |
|
|
529 | BDB::db_open $db, undef, $name, undef, BDB::BTREE, |
|
|
530 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
531 | cf::cleanup "db_open(db): $!" if $!; |
|
|
532 | }; |
|
|
533 | cf::cleanup "db_open(db): $@" if $@; |
|
|
534 | |
|
|
535 | $db |
|
|
536 | } |
513 | |
537 | |
514 | our $DB; |
538 | our $DB; |
515 | |
539 | |
516 | sub db_init { |
540 | sub db_init { |
517 | unless ($DB) { |
|
|
518 | $DB = BDB::db_create $DB_ENV; |
|
|
519 | |
|
|
520 | cf::sync_job { |
541 | cf::sync_job { |
521 | eval { |
542 | $DB ||= db_table "db"; |
522 | $DB->set_flags (BDB::CHKSUM); |
|
|
523 | |
|
|
524 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
525 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
526 | cf::cleanup "db_open(db): $!" if $!; |
|
|
527 | }; |
|
|
528 | cf::cleanup "db_open(db): $@" if $@; |
|
|
529 | }; |
|
|
530 | } |
543 | }; |
531 | } |
544 | } |
532 | |
545 | |
533 | sub db_get($$) { |
546 | sub db_get($$) { |
534 | my $key = "$_[0]/$_[1]"; |
547 | my $key = "$_[0]/$_[1]"; |
535 | |
548 | |
… | |
… | |
2445 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2458 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2446 | |
2459 | |
2447 | $self->enter_link; |
2460 | $self->enter_link; |
2448 | |
2461 | |
2449 | (async { |
2462 | (async { |
|
|
2463 | # *tag paths override both path and x|y |
|
|
2464 | if ($path =~ /^\*(.*)$/) { |
|
|
2465 | if (my @obs = grep $_->map, ext::map_tags::find $1) { |
|
|
2466 | my $ob = $obs[rand @obs]; |
|
|
2467 | |
|
|
2468 | # todo: use ob_blocked to check all tiles of the destination "object" |
|
|
2469 | # for suitability. |
|
|
2470 | |
|
|
2471 | ($path, $x, $y) = ($ob->map, $ob->x, $ob->y); |
|
|
2472 | } |
|
|
2473 | } |
|
|
2474 | |
2450 | my $map = eval { |
2475 | my $map = eval { |
2451 | my $map = cf::map::find $path; |
2476 | my $map = cf::map::find $path; |
2452 | |
2477 | |
2453 | if ($map) { |
2478 | if ($map) { |
2454 | $map = $map->customise_for ($self); |
2479 | $map = $map->customise_for ($self); |
… | |
… | |
3432 | my $signal = new Coro::Signal; |
3457 | my $signal = new Coro::Signal; |
3433 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3458 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3434 | $signal->wait; |
3459 | $signal->wait; |
3435 | } |
3460 | } |
3436 | |
3461 | |
3437 | my $min = 1e6;#d# |
3462 | our $stat_fh; |
3438 | my $avg = 10; |
3463 | sysopen $stat_fh, "/tmp/cfstats", Fcntl::O_APPEND | Fcntl::O_CREAT | Fcntl::O_WRONLY, 0600;#d# |
|
|
3464 | |
3439 | $TICK_WATCHER = Event->timer ( |
3465 | $TICK_WATCHER = Event->timer ( |
3440 | reentrant => 0, |
3466 | reentrant => 0, |
3441 | parked => 1, |
3467 | parked => 1, |
3442 | prio => 0, |
3468 | prio => 0, |
3443 | at => $NEXT_TICK || $TICK, |
3469 | at => $NEXT_TICK || $TICK, |
… | |
… | |
3447 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3473 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3448 | unless ++$bug_warning > 10; |
3474 | unless ++$bug_warning > 10; |
3449 | return; |
3475 | return; |
3450 | } |
3476 | } |
3451 | |
3477 | |
|
|
3478 | my @pl = cf::player::list; my $stats = sprintf "%.2f %d %d %d", $RUNTIME, (scalar @pl), cf::object::actives_size, cf::object::objects_size; #d# |
|
|
3479 | |
3452 | $NOW = $tick_start = Event::time; |
3480 | $NOW = $tick_start = Event::time; |
3453 | |
3481 | |
3454 | cf::server_tick; # one server iteration |
3482 | cf::server_tick; # one server iteration |
3455 | |
|
|
3456 | 0 && sync_job {#d# |
|
|
3457 | for(1..10) { |
|
|
3458 | my $t = Event::time; |
|
|
3459 | my $map = my $map = new_from_path cf::map "/tmp/x.map" |
|
|
3460 | or die; |
|
|
3461 | |
|
|
3462 | $map->width (50); |
|
|
3463 | $map->height (50); |
|
|
3464 | $map->alloc; |
|
|
3465 | $map->_load_objects ("/tmp/x.map", 1); #TODO: does not work |
|
|
3466 | my $t = Event::time - $t; |
|
|
3467 | |
|
|
3468 | #next unless $t < 0.0013;#d# |
|
|
3469 | if ($t < $min) { |
|
|
3470 | $min = $t; |
|
|
3471 | } |
|
|
3472 | $avg = $avg * 0.99 + $t * 0.01; |
|
|
3473 | } |
|
|
3474 | warn "XXXXXXXXXXXXXXXXXX min $min avg $avg\n";#d# |
|
|
3475 | exit 0; |
|
|
3476 | # 2007-05-22 02:33:04.569 min 0.00112509727478027 avg 0.0012259249572477 |
|
|
3477 | }; |
|
|
3478 | |
3483 | |
3479 | $RUNTIME += $TICK; |
3484 | $RUNTIME += $TICK; |
3480 | $NEXT_TICK += $TICK; |
3485 | $NEXT_TICK += $TICK; |
3481 | |
3486 | |
3482 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3487 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
… | |
… | |
3508 | $LOAD = ($NOW - $tick_start) / $TICK; |
3513 | $LOAD = ($NOW - $tick_start) / $TICK; |
3509 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3514 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3510 | |
3515 | |
3511 | _post_tick; |
3516 | _post_tick; |
3512 | |
3517 | |
3513 | |
3518 | # gather some statistics#d# |
|
|
3519 | $stats .= sprintf " %d\n", 10000 * ($NOW - $tick_start);#d# |
|
|
3520 | IO::AIO::aio_write $stat_fh, undef, undef, $stats, 0;#d# |
3514 | }, |
3521 | }, |
3515 | ); |
3522 | ); |
3516 | |
3523 | |
3517 | { |
3524 | { |
|
|
3525 | BDB::min_parallel 8; |
3518 | BDB::max_poll_time $TICK * 0.1; |
3526 | BDB::max_poll_time $TICK * 0.1; |
3519 | $BDB_POLL_WATCHER = Event->io ( |
3527 | $BDB_POLL_WATCHER = Event->io ( |
3520 | reentrant => 0, |
3528 | reentrant => 0, |
3521 | fd => BDB::poll_fileno, |
3529 | fd => BDB::poll_fileno, |
3522 | poll => 'r', |
3530 | poll => 'r', |
3523 | prio => 0, |
3531 | prio => 0, |
3524 | data => WF_AUTOCANCEL, |
3532 | data => WF_AUTOCANCEL, |
3525 | cb => \&BDB::poll_cb, |
3533 | cb => \&BDB::poll_cb, |
3526 | ); |
3534 | ); |
3527 | BDB::min_parallel 8; |
|
|
3528 | |
3535 | |
3529 | BDB::set_sync_prepare { |
3536 | BDB::set_sync_prepare { |
3530 | my $status; |
3537 | my $status; |
3531 | my $current = $Coro::current; |
3538 | my $current = $Coro::current; |
3532 | ( |
3539 | ( |
… | |
… | |
3553 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3560 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3554 | 0666; |
3561 | 0666; |
3555 | |
3562 | |
3556 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3563 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3557 | |
3564 | |
3558 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
3565 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
|
|
3566 | | BDB::LOG_AUTOREMOVE, 1); |
3559 | $DB_ENV->set_lk_detect; |
3567 | $DB_ENV->set_lk_detect; |
3560 | }; |
3568 | }; |
3561 | |
3569 | |
3562 | cf::cleanup "db_env_open(db): $@" if $@; |
3570 | cf::cleanup "db_env_open(db): $@" if $@; |
3563 | }; |
3571 | }; |
3564 | } |
3572 | } |
|
|
3573 | |
|
|
3574 | $BDB_CHECKPOINT_WATCHER = Event->timer ( |
|
|
3575 | after => 11, |
|
|
3576 | interval => 60, |
|
|
3577 | hard => 1, |
|
|
3578 | prio => 0, |
|
|
3579 | data => WF_AUTOCANCEL, |
|
|
3580 | cb => sub { |
|
|
3581 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
3582 | }, |
|
|
3583 | ); |
|
|
3584 | $BDB_TRICKLE_WATCHER = Event->timer ( |
|
|
3585 | after => 5, |
|
|
3586 | interval => 10, |
|
|
3587 | hard => 1, |
|
|
3588 | prio => 0, |
|
|
3589 | data => WF_AUTOCANCEL, |
|
|
3590 | cb => sub { |
|
|
3591 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
|
|
3592 | }, |
|
|
3593 | ); |
3565 | } |
3594 | } |
3566 | |
3595 | |
3567 | { |
3596 | { |
3568 | IO::AIO::min_parallel 8; |
3597 | IO::AIO::min_parallel 8; |
3569 | |
3598 | |