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.360 by root, Sun Sep 9 06:25:46 2007 UTC vs.
Revision 1.371 by root, Thu Sep 13 16:16:01 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_DEADLOCK_WATCHER;
86our $BDB_CHECKPOINT_WATCHER;
87our $BDB_TRICKLE_WATCHER;
85our $DB_ENV; 88our $DB_ENV;
86 89
87our %CFG; 90our %CFG;
88 91
89our $UPTIME; $UPTIME ||= time; 92our $UPTIME; $UPTIME ||= time;
187 $msg .= "\n" 190 $msg .= "\n"
188 unless $msg =~ /\n$/; 191 unless $msg =~ /\n$/;
189 192
190 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 193 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
191 194
192 utf8::encode $msg;
193 LOG llevError, $msg; 195 LOG llevError, $msg;
194 }; 196 };
195} 197}
196 198
197@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 199@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
508=item cf::db_put $family => $key => $value 510=item cf::db_put $family => $key => $value
509 511
510Stores the given C<$value> in the family. It can currently store binary 512Stores the given C<$value> in the family. It can currently store binary
511data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). 513data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary).
512 514
515=item $db = cf::db_table "name"
516
517Create and/or open a new database table. The string must not be "db" and must be unique
518within each server.
519
513=cut 520=cut
521
522sub 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}
514 538
515our $DB; 539our $DB;
516 540
517sub db_init { 541sub db_init {
518 unless ($DB) {
519 $DB = BDB::db_create $DB_ENV;
520
521 cf::sync_job { 542 cf::sync_job {
522 eval { 543 $DB ||= db_table "db";
523 $DB->set_flags (BDB::CHKSUM);
524
525 BDB::db_open $DB, undef, "db", undef, BDB::BTREE,
526 BDB::CREATE | BDB::AUTO_COMMIT, 0666;
527 cf::cleanup "db_open(db): $!" if $!;
528 };
529 cf::cleanup "db_open(db): $@" if $@;
530 };
531 } 544 };
532} 545}
533 546
534sub db_get($$) { 547sub db_get($$) {
535 my $key = "$_[0]/$_[1]"; 548 my $key = "$_[0]/$_[1]";
536 549
1017 1030
1018 my $guard = cf::lock_acquire "io"; 1031 my $guard = cf::lock_acquire "io";
1019 1032
1020 sync_job { 1033 sync_job {
1021 if (length $$rdata) { 1034 if (length $$rdata) {
1035 utf8::decode (my $decname = $filename);
1022 warn sprintf "saving %s (%d,%d)\n", 1036 warn sprintf "saving %s (%d,%d)\n",
1023 $filename, length $$rdata, scalar @$objs; 1037 $decname, length $$rdata, scalar @$objs;
1024 1038
1025 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1039 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1026 chmod SAVE_MODE, $fh; 1040 chmod SAVE_MODE, $fh;
1027 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1041 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1028 aio_fsync $fh if $cf::USE_FSYNC; 1042 aio_fsync $fh if $cf::USE_FSYNC;
1078 1092
1079 undef $guard; 1093 undef $guard;
1080 $av = eval { (Storable::thaw $av)->{objs} }; 1094 $av = eval { (Storable::thaw $av)->{objs} };
1081 } 1095 }
1082 1096
1097 utf8::decode (my $decname = $filename);
1083 warn sprintf "loading %s (%d)\n", 1098 warn sprintf "loading %s (%d,%d)\n",
1084 $filename, length $data, scalar @{$av || []}; 1099 $decname, length $data, scalar @{$av || []};
1085 1100
1086 ($data, $av) 1101 ($data, $av)
1087} 1102}
1088 1103
1089=head2 COMMAND CALLBACKS 1104=head2 COMMAND CALLBACKS
1276use Coro::AIO; 1291use Coro::AIO;
1277 1292
1278=head3 cf::player 1293=head3 cf::player
1279 1294
1280=over 4 1295=over 4
1296
1297=item cf::player::num_playing
1298
1299Returns the official number of playing players, as per the Crossfire metaserver rules.
1300
1301=cut
1302
1303sub num_playing {
1304 scalar grep
1305 $_->ob->map
1306 && !$_->hidden
1307 && !$_->ob->flag (cf::FLAG_WIZ),
1308 cf::player::list
1309}
1281 1310
1282=item cf::player::find $login 1311=item cf::player::find $login
1283 1312
1284Returns the given player object, loading it if necessary (might block). 1313Returns the given player object, loading it if necessary (might block).
1285 1314
2430 my $gen = $self->{_goto_generation} = ++$GOTOGEN; 2459 my $gen = $self->{_goto_generation} = ++$GOTOGEN;
2431 2460
2432 $self->enter_link; 2461 $self->enter_link;
2433 2462
2434 (async { 2463 (async {
2464 # *tag paths override both path and x|y
2465 if ($path =~ /^\*(.*)$/) {
2466 if (my @obs = grep $_->map, ext::map_tags::find $1) {
2467 my $ob = $obs[rand @obs];
2468
2469 # see if we actually can go there
2470 if (@obs = grep !$self->blocked ($_->map, $_->x, $_->y), $ob, $ob->tail) {
2471 $ob = $obs[rand @obs];
2472 } else {
2473 $self->message ("Wow, it's pretty crowded in there.", cf::NDI_UNIQUE | cf::NDI_RED);
2474 }
2475 # else put us there anyways for now #d#
2476
2477 ($path, $x, $y) = ($ob->map, $ob->x, $ob->y);
2478 } else {
2479 ($path, $x, $y) = (undef, undef, undef);
2480 }
2481 }
2482
2435 my $map = eval { 2483 my $map = eval {
2436 my $map = cf::map::find $path; 2484 my $map = defined $path ? cf::map::find $path : undef;
2437 2485
2438 if ($map) { 2486 if ($map) {
2439 $map = $map->customise_for ($self); 2487 $map = $map->customise_for ($self);
2440 $map = $check->($map) if $check && $map; 2488 $map = $check->($map) if $check && $map;
2441 } else { 2489 } else {
2442 $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); 2490 $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED);
2443 } 2491 }
2444 2492
2445 $map 2493 $map
2446 }; 2494 };
2447 2495
3417 my $signal = new Coro::Signal; 3465 my $signal = new Coro::Signal;
3418 push @WAIT_FOR_TICK_BEGIN, $signal; 3466 push @WAIT_FOR_TICK_BEGIN, $signal;
3419 $signal->wait; 3467 $signal->wait;
3420} 3468}
3421 3469
3422 my $min = 1e6;#d#
3423 my $avg = 10;
3424$TICK_WATCHER = Event->timer ( 3470$TICK_WATCHER = Event->timer (
3425 reentrant => 0, 3471 reentrant => 0,
3426 parked => 1, 3472 parked => 1,
3427 prio => 0, 3473 prio => 0,
3428 at => $NEXT_TICK || $TICK, 3474 at => $NEXT_TICK || $TICK,
3436 3482
3437 $NOW = $tick_start = Event::time; 3483 $NOW = $tick_start = Event::time;
3438 3484
3439 cf::server_tick; # one server iteration 3485 cf::server_tick; # one server iteration
3440 3486
3441 0 && sync_job {#d#
3442 for(1..10) {
3443 my $t = Event::time;
3444 my $map = my $map = new_from_path cf::map "/tmp/x.map"
3445 or die;
3446
3447 $map->width (50);
3448 $map->height (50);
3449 $map->alloc;
3450 $map->_load_objects ("/tmp/x.map", 1); #TODO: does not work
3451 my $t = Event::time - $t;
3452
3453 #next unless $t < 0.0013;#d#
3454 if ($t < $min) {
3455 $min = $t;
3456 }
3457 $avg = $avg * 0.99 + $t * 0.01;
3458 }
3459 warn "XXXXXXXXXXXXXXXXXX min $min avg $avg\n";#d#
3460 exit 0;
3461 # 2007-05-22 02:33:04.569 min 0.00112509727478027 avg 0.0012259249572477
3462 };
3463
3464 $RUNTIME += $TICK; 3487 $RUNTIME += $TICK;
3465 $NEXT_TICK += $TICK; 3488 $NEXT_TICK += $TICK;
3466 3489
3467 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3490 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3468 $NEXT_RUNTIME_WRITE = $NOW + 10; 3491 $NEXT_RUNTIME_WRITE = $NOW + 10;
3470 write_runtime 3493 write_runtime
3471 or warn "ERROR: unable to write runtime file: $!"; 3494 or warn "ERROR: unable to write runtime file: $!";
3472 }; 3495 };
3473 } 3496 }
3474 3497
3475# my $AFTER = Event::time;
3476# warn $AFTER - $NOW;#d#
3477
3478 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 3498 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
3479 $sig->send; 3499 $sig->send;
3480 } 3500 }
3481 while (my $sig = shift @WAIT_FOR_TICK) { 3501 while (my $sig = shift @WAIT_FOR_TICK) {
3482 $sig->send; 3502 $sig->send;
3492 3512
3493 $LOAD = ($NOW - $tick_start) / $TICK; 3513 $LOAD = ($NOW - $tick_start) / $TICK;
3494 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; 3514 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25;
3495 3515
3496 _post_tick; 3516 _post_tick;
3497
3498
3499 }, 3517 },
3500); 3518);
3501 3519
3502{ 3520{
3521 BDB::min_parallel 8;
3503 BDB::max_poll_time $TICK * 0.1; 3522 BDB::max_poll_time $TICK * 0.1;
3504 $BDB_POLL_WATCHER = Event->io ( 3523 $BDB_POLL_WATCHER = Event->io (
3505 reentrant => 0, 3524 reentrant => 0,
3506 fd => BDB::poll_fileno, 3525 fd => BDB::poll_fileno,
3507 poll => 'r', 3526 poll => 'r',
3508 prio => 0, 3527 prio => 0,
3509 data => WF_AUTOCANCEL, 3528 data => WF_AUTOCANCEL,
3510 cb => \&BDB::poll_cb, 3529 cb => \&BDB::poll_cb,
3511 ); 3530 );
3512 BDB::min_parallel 8;
3513 3531
3514 BDB::set_sync_prepare { 3532 BDB::set_sync_prepare {
3515 my $status; 3533 my $status;
3516 my $current = $Coro::current; 3534 my $current = $Coro::current;
3517 ( 3535 (
3526 ) 3544 )
3527 }; 3545 };
3528 3546
3529 unless ($DB_ENV) { 3547 unless ($DB_ENV) {
3530 $DB_ENV = BDB::db_env_create; 3548 $DB_ENV = BDB::db_env_create;
3549 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC
3550 | BDB::LOG_AUTOREMOVE, 1);
3551 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT);
3552 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT);
3531 3553
3532 cf::sync_job { 3554 cf::sync_job {
3533 eval { 3555 eval {
3534 BDB::db_env_open 3556 BDB::db_env_open
3535 $DB_ENV, 3557 $DB_ENV,
3537 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN 3559 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN
3538 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 3560 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
3539 0666; 3561 0666;
3540 3562
3541 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 3563 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
3542
3543 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1);
3544 $DB_ENV->set_lk_detect;
3545 }; 3564 };
3546 3565
3547 cf::cleanup "db_env_open(db): $@" if $@; 3566 cf::cleanup "db_env_open(db): $@" if $@;
3548 }; 3567 };
3549 } 3568 }
3569
3570 $BDB_DEADLOCK_WATCHER = Event->timer (
3571 after => 3,
3572 interval => 1,
3573 hard => 1,
3574 prio => 0,
3575 data => WF_AUTOCANCEL,
3576 cb => sub {
3577 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { };
3578 },
3579 );
3580 $BDB_CHECKPOINT_WATCHER = Event->timer (
3581 after => 11,
3582 interval => 60,
3583 hard => 1,
3584 prio => 0,
3585 data => WF_AUTOCANCEL,
3586 cb => sub {
3587 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
3588 },
3589 );
3590 $BDB_TRICKLE_WATCHER = Event->timer (
3591 after => 5,
3592 interval => 10,
3593 hard => 1,
3594 prio => 0,
3595 data => WF_AUTOCANCEL,
3596 cb => sub {
3597 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
3598 },
3599 );
3550} 3600}
3551 3601
3552{ 3602{
3553 IO::AIO::min_parallel 8; 3603 IO::AIO::min_parallel 8;
3554 3604

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines