ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.362 by root, Mon Sep 10 12:44:06 2007 UTC vs.
Revision 1.370 by root, Thu Sep 13 10:59:41 2007 UTC

80our $NEXT_TICK; 80our $NEXT_TICK;
81our $NOW; 81our $NOW;
82our $USE_FSYNC = 1; # use fsync to write maps - default off 82our $USE_FSYNC = 1; # use fsync to write maps - default off
83 83
84our $BDB_POLL_WATCHER; 84our $BDB_POLL_WATCHER;
85our $BDB_CHECKPOINT_WATCHER;
86our $BDB_TRICKLE_WATCHER;
85our $DB_ENV; 87our $DB_ENV;
86 88
87our %CFG; 89our %CFG;
88 90
89our $UPTIME; $UPTIME ||= time; 91our $UPTIME; $UPTIME ||= time;
507=item cf::db_put $family => $key => $value 509=item cf::db_put $family => $key => $value
508 510
509Stores the given C<$value> in the family. It can currently store binary 511Stores the given C<$value> in the family. It can currently store binary
510data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). 512data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary).
511 513
514=item $db = cf::db_table "name"
515
516Create and/or open a new database table. The string must not be "db" and must be unique
517within each server.
518
512=cut 519=cut
520
521sub 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
514our $DB; 538our $DB;
515 539
516sub db_init { 540sub 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
533sub db_get($$) { 546sub 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 # see if we actually can go there
2469 if (@obs = grep !$self->blocked ($_->map, $_->x, $_->y), $ob, $ob->tail) {
2470 $ob = $obs[rand @obs];
2471 } else {
2472 $self->message ("Wow, it's pretty crowded in there.", cf::NDI_UNIQUE | cf::NDI_RED);
2473 }
2474 # else put us there anyways for now #d#
2475
2476 ($path, $x, $y) = ($ob->map, $ob->x, $ob->y);
2477 } else {
2478 ($path, $x, $y) = (undef, undef, undef);
2479 }
2480 }
2481
2450 my $map = eval { 2482 my $map = eval {
2451 my $map = cf::map::find $path; 2483 my $map = defined $path ? cf::map::find $path : undef;
2452 2484
2453 if ($map) { 2485 if ($map) {
2454 $map = $map->customise_for ($self); 2486 $map = $map->customise_for ($self);
2455 $map = $check->($map) if $check && $map; 2487 $map = $check->($map) if $check && $map;
2456 } else { 2488 } else {
2457 $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); 2489 $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED);
2458 } 2490 }
2459 2491
2460 $map 2492 $map
2461 }; 2493 };
2462 2494
3432 my $signal = new Coro::Signal; 3464 my $signal = new Coro::Signal;
3433 push @WAIT_FOR_TICK_BEGIN, $signal; 3465 push @WAIT_FOR_TICK_BEGIN, $signal;
3434 $signal->wait; 3466 $signal->wait;
3435} 3467}
3436 3468
3437 my $min = 1e6;#d#
3438 my $avg = 10;
3439$TICK_WATCHER = Event->timer ( 3469$TICK_WATCHER = Event->timer (
3440 reentrant => 0, 3470 reentrant => 0,
3441 parked => 1, 3471 parked => 1,
3442 prio => 0, 3472 prio => 0,
3443 at => $NEXT_TICK || $TICK, 3473 at => $NEXT_TICK || $TICK,
3451 3481
3452 $NOW = $tick_start = Event::time; 3482 $NOW = $tick_start = Event::time;
3453 3483
3454 cf::server_tick; # one server iteration 3484 cf::server_tick; # one server iteration
3455 3485
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; 3486 $RUNTIME += $TICK;
3480 $NEXT_TICK += $TICK; 3487 $NEXT_TICK += $TICK;
3481 3488
3482 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3489 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3483 $NEXT_RUNTIME_WRITE = $NOW + 10; 3490 $NEXT_RUNTIME_WRITE = $NOW + 10;
3485 write_runtime 3492 write_runtime
3486 or warn "ERROR: unable to write runtime file: $!"; 3493 or warn "ERROR: unable to write runtime file: $!";
3487 }; 3494 };
3488 } 3495 }
3489 3496
3490# my $AFTER = Event::time;
3491# warn $AFTER - $NOW;#d#
3492
3493 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 3497 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
3494 $sig->send; 3498 $sig->send;
3495 } 3499 }
3496 while (my $sig = shift @WAIT_FOR_TICK) { 3500 while (my $sig = shift @WAIT_FOR_TICK) {
3497 $sig->send; 3501 $sig->send;
3507 3511
3508 $LOAD = ($NOW - $tick_start) / $TICK; 3512 $LOAD = ($NOW - $tick_start) / $TICK;
3509 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; 3513 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25;
3510 3514
3511 _post_tick; 3515 _post_tick;
3512
3513
3514 }, 3516 },
3515); 3517);
3516 3518
3517{ 3519{
3520 BDB::min_parallel 8;
3518 BDB::max_poll_time $TICK * 0.1; 3521 BDB::max_poll_time $TICK * 0.1;
3519 $BDB_POLL_WATCHER = Event->io ( 3522 $BDB_POLL_WATCHER = Event->io (
3520 reentrant => 0, 3523 reentrant => 0,
3521 fd => BDB::poll_fileno, 3524 fd => BDB::poll_fileno,
3522 poll => 'r', 3525 poll => 'r',
3523 prio => 0, 3526 prio => 0,
3524 data => WF_AUTOCANCEL, 3527 data => WF_AUTOCANCEL,
3525 cb => \&BDB::poll_cb, 3528 cb => \&BDB::poll_cb,
3526 ); 3529 );
3527 BDB::min_parallel 8;
3528 3530
3529 BDB::set_sync_prepare { 3531 BDB::set_sync_prepare {
3530 my $status; 3532 my $status;
3531 my $current = $Coro::current; 3533 my $current = $Coro::current;
3532 ( 3534 (
3553 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 3555 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
3554 0666; 3556 0666;
3555 3557
3556 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 3558 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
3557 3559
3558 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); 3560 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC
3561 | BDB::LOG_AUTOREMOVE, 1);
3559 $DB_ENV->set_lk_detect; 3562 $DB_ENV->set_lk_detect;
3560 }; 3563 };
3561 3564
3562 cf::cleanup "db_env_open(db): $@" if $@; 3565 cf::cleanup "db_env_open(db): $@" if $@;
3563 }; 3566 };
3564 } 3567 }
3568
3569 $BDB_CHECKPOINT_WATCHER = Event->timer (
3570 after => 11,
3571 interval => 60,
3572 hard => 1,
3573 prio => 0,
3574 data => WF_AUTOCANCEL,
3575 cb => sub {
3576 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
3577 },
3578 );
3579 $BDB_TRICKLE_WATCHER = Event->timer (
3580 after => 5,
3581 interval => 10,
3582 hard => 1,
3583 prio => 0,
3584 data => WF_AUTOCANCEL,
3585 cb => sub {
3586 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
3587 },
3588 );
3565} 3589}
3566 3590
3567{ 3591{
3568 IO::AIO::min_parallel 8; 3592 IO::AIO::min_parallel 8;
3569 3593

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines