… | |
… | |
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_DEADLOCK_WATCHER; |
|
|
86 | our $BDB_CHECKPOINT_WATCHER; |
|
|
87 | our $BDB_TRICKLE_WATCHER; |
85 | our $DB_ENV; |
88 | our $DB_ENV; |
86 | |
89 | |
87 | our %CFG; |
90 | our %CFG; |
88 | |
91 | |
89 | our $UPTIME; $UPTIME ||= time; |
92 | our $UPTIME; $UPTIME ||= time; |
… | |
… | |
507 | =item cf::db_put $family => $key => $value |
510 | =item cf::db_put $family => $key => $value |
508 | |
511 | |
509 | Stores the given C<$value> in the family. It can currently store binary |
512 | 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). |
513 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
511 | |
514 | |
|
|
515 | =item $db = cf::db_table "name" |
|
|
516 | |
|
|
517 | Create and/or open a new database table. The string must not be "db" and must be unique |
|
|
518 | within each server. |
|
|
519 | |
512 | =cut |
520 | =cut |
|
|
521 | |
|
|
522 | sub db_table($) { |
|
|
523 | my ($name) = @_; |
|
|
524 | my $db = BDB::db_create $DB_ENV; |
|
|
525 | |
|
|
526 | eval { |
|
|
527 | $db->set_flags (BDB::CHKSUM); |
|
|
528 | |
|
|
529 | utf8::encode $name; |
|
|
530 | BDB::db_open $db, undef, $name, undef, BDB::BTREE, |
|
|
531 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
532 | cf::cleanup "db_open(db): $!" if $!; |
|
|
533 | }; |
|
|
534 | cf::cleanup "db_open(db): $@" if $@; |
|
|
535 | |
|
|
536 | $db |
|
|
537 | } |
513 | |
538 | |
514 | our $DB; |
539 | our $DB; |
515 | |
540 | |
516 | sub db_init { |
541 | sub db_init { |
517 | unless ($DB) { |
|
|
518 | $DB = BDB::db_create $DB_ENV; |
|
|
519 | |
|
|
520 | cf::sync_job { |
542 | cf::sync_job { |
521 | eval { |
543 | $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 | } |
544 | }; |
531 | } |
545 | } |
532 | |
546 | |
533 | sub db_get($$) { |
547 | sub db_get($$) { |
534 | my $key = "$_[0]/$_[1]"; |
548 | my $key = "$_[0]/$_[1]"; |
535 | |
549 | |
… | |
… | |
1011 | }, |
1025 | }, |
1012 | ); |
1026 | ); |
1013 | |
1027 | |
1014 | sub object_freezer_save { |
1028 | sub object_freezer_save { |
1015 | my ($filename, $rdata, $objs) = @_; |
1029 | my ($filename, $rdata, $objs) = @_; |
1016 | |
|
|
1017 | my $guard = cf::lock_acquire "io"; |
|
|
1018 | |
1030 | |
1019 | sync_job { |
1031 | sync_job { |
1020 | if (length $$rdata) { |
1032 | if (length $$rdata) { |
1021 | utf8::decode (my $decname = $filename); |
1033 | utf8::decode (my $decname = $filename); |
1022 | warn sprintf "saving %s (%d,%d)\n", |
1034 | warn sprintf "saving %s (%d,%d)\n", |
… | |
… | |
1048 | } else { |
1060 | } else { |
1049 | aio_unlink $filename; |
1061 | aio_unlink $filename; |
1050 | aio_unlink "$filename.pst"; |
1062 | aio_unlink "$filename.pst"; |
1051 | } |
1063 | } |
1052 | }; |
1064 | }; |
1053 | |
|
|
1054 | undef $guard; |
|
|
1055 | } |
1065 | } |
1056 | |
1066 | |
1057 | sub object_freezer_as_string { |
1067 | sub object_freezer_as_string { |
1058 | my ($rdata, $objs) = @_; |
1068 | my ($rdata, $objs) = @_; |
1059 | |
1069 | |
… | |
… | |
1064 | |
1074 | |
1065 | sub object_thawer_load { |
1075 | sub object_thawer_load { |
1066 | my ($filename) = @_; |
1076 | my ($filename) = @_; |
1067 | |
1077 | |
1068 | my ($data, $av); |
1078 | my ($data, $av); |
1069 | |
|
|
1070 | my $guard = cf::lock_acquire "io"; |
|
|
1071 | |
1079 | |
1072 | (aio_load $filename, $data) >= 0 |
1080 | (aio_load $filename, $data) >= 0 |
1073 | or return; |
1081 | or return; |
1074 | |
1082 | |
1075 | unless (aio_stat "$filename.pst") { |
1083 | unless (aio_stat "$filename.pst") { |
1076 | (aio_load "$filename.pst", $av) >= 0 |
1084 | (aio_load "$filename.pst", $av) >= 0 |
1077 | or return; |
1085 | or return; |
1078 | |
1086 | |
1079 | undef $guard; |
|
|
1080 | $av = eval { (Storable::thaw $av)->{objs} }; |
1087 | $av = eval { (Storable::thaw $av)->{objs} }; |
1081 | } |
1088 | } |
1082 | |
1089 | |
1083 | utf8::decode (my $decname = $filename); |
1090 | utf8::decode (my $decname = $filename); |
1084 | warn sprintf "loading %s (%d,%d)\n", |
1091 | warn sprintf "loading %s (%d,%d)\n", |
… | |
… | |
1339 | aio_unlink +(playerdir $login) . "/$login.pl"; |
1346 | aio_unlink +(playerdir $login) . "/$login.pl"; |
1340 | |
1347 | |
1341 | my $f = new_from_file cf::object::thawer path $login |
1348 | my $f = new_from_file cf::object::thawer path $login |
1342 | or return; |
1349 | or return; |
1343 | |
1350 | |
1344 | $f->next; |
|
|
1345 | my $pl = cf::player::load_pl $f |
1351 | my $pl = cf::player::load_pl $f |
1346 | or return; |
1352 | or return; |
1347 | local $cf::PLAYER_LOADING{$login} = $pl; |
1353 | local $cf::PLAYER_LOADING{$login} = $pl; |
1348 | $f->resolve_delayed_derefs; |
1354 | $f->resolve_delayed_derefs; |
1349 | $cf::PLAYER{$login} = $pl |
1355 | $cf::PLAYER{$login} = $pl |
… | |
… | |
2445 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2451 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2446 | |
2452 | |
2447 | $self->enter_link; |
2453 | $self->enter_link; |
2448 | |
2454 | |
2449 | (async { |
2455 | (async { |
|
|
2456 | # *tag paths override both path and x|y |
|
|
2457 | if ($path =~ /^\*(.*)$/) { |
|
|
2458 | if (my @obs = grep $_->map, ext::map_tags::find $1) { |
|
|
2459 | my $ob = $obs[rand @obs]; |
|
|
2460 | |
|
|
2461 | # see if we actually can go there |
|
|
2462 | if (@obs = grep !$self->blocked ($_->map, $_->x, $_->y), $ob, $ob->tail) { |
|
|
2463 | $ob = $obs[rand @obs]; |
|
|
2464 | } else { |
|
|
2465 | $self->message ("Wow, it's pretty crowded in there.", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
2466 | } |
|
|
2467 | # else put us there anyways for now #d# |
|
|
2468 | |
|
|
2469 | ($path, $x, $y) = ($ob->map, $ob->x, $ob->y); |
|
|
2470 | } else { |
|
|
2471 | ($path, $x, $y) = (undef, undef, undef); |
|
|
2472 | } |
|
|
2473 | } |
|
|
2474 | |
2450 | my $map = eval { |
2475 | my $map = eval { |
2451 | my $map = cf::map::find $path; |
2476 | my $map = defined $path ? cf::map::find $path : undef; |
2452 | |
2477 | |
2453 | if ($map) { |
2478 | if ($map) { |
2454 | $map = $map->customise_for ($self); |
2479 | $map = $map->customise_for ($self); |
2455 | $map = $check->($map) if $check && $map; |
2480 | $map = $check->($map) if $check && $map; |
2456 | } else { |
2481 | } else { |
2457 | $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
2482 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2458 | } |
2483 | } |
2459 | |
2484 | |
2460 | $map |
2485 | $map |
2461 | }; |
2486 | }; |
2462 | |
2487 | |
… | |
… | |
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# |
|
|
3438 | my $avg = 10; |
|
|
3439 | $TICK_WATCHER = Event->timer ( |
3462 | $TICK_WATCHER = Event->timer ( |
3440 | reentrant => 0, |
3463 | reentrant => 0, |
3441 | parked => 1, |
3464 | parked => 1, |
3442 | prio => 0, |
3465 | prio => 0, |
3443 | at => $NEXT_TICK || $TICK, |
3466 | at => $NEXT_TICK || $TICK, |
… | |
… | |
3451 | |
3474 | |
3452 | $NOW = $tick_start = Event::time; |
3475 | $NOW = $tick_start = Event::time; |
3453 | |
3476 | |
3454 | cf::server_tick; # one server iteration |
3477 | cf::server_tick; # one server iteration |
3455 | |
3478 | |
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 | |
|
|
3479 | $RUNTIME += $TICK; |
3479 | $RUNTIME += $TICK; |
3480 | $NEXT_TICK += $TICK; |
3480 | $NEXT_TICK += $TICK; |
3481 | |
3481 | |
3482 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3482 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3483 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3483 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
3485 | write_runtime |
3485 | write_runtime |
3486 | or warn "ERROR: unable to write runtime file: $!"; |
3486 | or warn "ERROR: unable to write runtime file: $!"; |
3487 | }; |
3487 | }; |
3488 | } |
3488 | } |
3489 | |
3489 | |
3490 | # my $AFTER = Event::time; |
|
|
3491 | # warn $AFTER - $NOW;#d# |
|
|
3492 | |
|
|
3493 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3490 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3494 | $sig->send; |
3491 | $sig->send; |
3495 | } |
3492 | } |
3496 | while (my $sig = shift @WAIT_FOR_TICK) { |
3493 | while (my $sig = shift @WAIT_FOR_TICK) { |
3497 | $sig->send; |
3494 | $sig->send; |
… | |
… | |
3507 | |
3504 | |
3508 | $LOAD = ($NOW - $tick_start) / $TICK; |
3505 | $LOAD = ($NOW - $tick_start) / $TICK; |
3509 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3506 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3510 | |
3507 | |
3511 | _post_tick; |
3508 | _post_tick; |
3512 | |
|
|
3513 | |
|
|
3514 | }, |
3509 | }, |
3515 | ); |
3510 | ); |
3516 | |
3511 | |
3517 | { |
3512 | { |
|
|
3513 | BDB::min_parallel 8; |
3518 | BDB::max_poll_time $TICK * 0.1; |
3514 | BDB::max_poll_time $TICK * 0.1; |
3519 | $BDB_POLL_WATCHER = Event->io ( |
3515 | $BDB_POLL_WATCHER = Event->io ( |
3520 | reentrant => 0, |
3516 | reentrant => 0, |
3521 | fd => BDB::poll_fileno, |
3517 | fd => BDB::poll_fileno, |
3522 | poll => 'r', |
3518 | poll => 'r', |
3523 | prio => 0, |
3519 | prio => 0, |
3524 | data => WF_AUTOCANCEL, |
3520 | data => WF_AUTOCANCEL, |
3525 | cb => \&BDB::poll_cb, |
3521 | cb => \&BDB::poll_cb, |
3526 | ); |
3522 | ); |
3527 | BDB::min_parallel 8; |
|
|
3528 | |
3523 | |
3529 | BDB::set_sync_prepare { |
3524 | BDB::set_sync_prepare { |
3530 | my $status; |
3525 | my $status; |
3531 | my $current = $Coro::current; |
3526 | my $current = $Coro::current; |
3532 | ( |
3527 | ( |
… | |
… | |
3541 | ) |
3536 | ) |
3542 | }; |
3537 | }; |
3543 | |
3538 | |
3544 | unless ($DB_ENV) { |
3539 | unless ($DB_ENV) { |
3545 | $DB_ENV = BDB::db_env_create; |
3540 | $DB_ENV = BDB::db_env_create; |
|
|
3541 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
|
|
3542 | | BDB::LOG_AUTOREMOVE, 1); |
|
|
3543 | $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); |
|
|
3544 | $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); |
3546 | |
3545 | |
3547 | cf::sync_job { |
3546 | cf::sync_job { |
3548 | eval { |
3547 | eval { |
3549 | BDB::db_env_open |
3548 | BDB::db_env_open |
3550 | $DB_ENV, |
3549 | $DB_ENV, |
… | |
… | |
3552 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
3551 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
3553 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3552 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3554 | 0666; |
3553 | 0666; |
3555 | |
3554 | |
3556 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3555 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3557 | |
|
|
3558 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
|
|
3559 | $DB_ENV->set_lk_detect; |
|
|
3560 | }; |
3556 | }; |
3561 | |
3557 | |
3562 | cf::cleanup "db_env_open(db): $@" if $@; |
3558 | cf::cleanup "db_env_open(db): $@" if $@; |
3563 | }; |
3559 | }; |
3564 | } |
3560 | } |
|
|
3561 | |
|
|
3562 | $BDB_DEADLOCK_WATCHER = Event->timer ( |
|
|
3563 | after => 3, |
|
|
3564 | interval => 1, |
|
|
3565 | hard => 1, |
|
|
3566 | prio => 0, |
|
|
3567 | data => WF_AUTOCANCEL, |
|
|
3568 | cb => sub { |
|
|
3569 | BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; |
|
|
3570 | }, |
|
|
3571 | ); |
|
|
3572 | $BDB_CHECKPOINT_WATCHER = Event->timer ( |
|
|
3573 | after => 11, |
|
|
3574 | interval => 60, |
|
|
3575 | hard => 1, |
|
|
3576 | prio => 0, |
|
|
3577 | data => WF_AUTOCANCEL, |
|
|
3578 | cb => sub { |
|
|
3579 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
3580 | }, |
|
|
3581 | ); |
|
|
3582 | $BDB_TRICKLE_WATCHER = Event->timer ( |
|
|
3583 | after => 5, |
|
|
3584 | interval => 10, |
|
|
3585 | hard => 1, |
|
|
3586 | prio => 0, |
|
|
3587 | data => WF_AUTOCANCEL, |
|
|
3588 | cb => sub { |
|
|
3589 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
|
|
3590 | }, |
|
|
3591 | ); |
3565 | } |
3592 | } |
3566 | |
3593 | |
3567 | { |
3594 | { |
3568 | IO::AIO::min_parallel 8; |
3595 | IO::AIO::min_parallel 8; |
3569 | |
3596 | |