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.359 by root, Sat Sep 8 18:15:55 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;
187 $msg .= "\n" 189 $msg .= "\n"
188 unless $msg =~ /\n$/; 190 unless $msg =~ /\n$/;
189 191
190 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 192 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
191 193
192 utf8::encode $msg;
193 LOG llevError, $msg; 194 LOG llevError, $msg;
194 }; 195 };
195} 196}
196 197
197@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 198@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
508=item cf::db_put $family => $key => $value 509=item cf::db_put $family => $key => $value
509 510
510Stores 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
511data 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).
512 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
513=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}
514 537
515our $DB; 538our $DB;
516 539
517sub db_init { 540sub db_init {
518 unless ($DB) {
519 $DB = BDB::db_create $DB_ENV;
520
521 cf::sync_job { 541 cf::sync_job {
522 eval { 542 $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 } 543 };
532} 544}
533 545
534sub db_get($$) { 546sub db_get($$) {
535 my $key = "$_[0]/$_[1]"; 547 my $key = "$_[0]/$_[1]";
536 548
1017 1029
1018 my $guard = cf::lock_acquire "io"; 1030 my $guard = cf::lock_acquire "io";
1019 1031
1020 sync_job { 1032 sync_job {
1021 if (length $$rdata) { 1033 if (length $$rdata) {
1034 utf8::decode (my $decname = $filename);
1022 warn sprintf "saving %s (%d,%d)\n", 1035 warn sprintf "saving %s (%d,%d)\n",
1023 $filename, length $$rdata, scalar @$objs; 1036 $decname, length $$rdata, scalar @$objs;
1024 1037
1025 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1038 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1026 chmod SAVE_MODE, $fh; 1039 chmod SAVE_MODE, $fh;
1027 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1040 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1028 aio_fsync $fh if $cf::USE_FSYNC; 1041 aio_fsync $fh if $cf::USE_FSYNC;
1078 1091
1079 undef $guard; 1092 undef $guard;
1080 $av = eval { (Storable::thaw $av)->{objs} }; 1093 $av = eval { (Storable::thaw $av)->{objs} };
1081 } 1094 }
1082 1095
1096 utf8::decode (my $decname = $filename);
1083 warn sprintf "loading %s (%d)\n", 1097 warn sprintf "loading %s (%d,%d)\n",
1084 $filename, length $data, scalar @{$av || []}; 1098 $decname, length $data, scalar @{$av || []};
1085 1099
1086 ($data, $av) 1100 ($data, $av)
1087} 1101}
1088 1102
1089=head2 COMMAND CALLBACKS 1103=head2 COMMAND CALLBACKS
1276use Coro::AIO; 1290use Coro::AIO;
1277 1291
1278=head3 cf::player 1292=head3 cf::player
1279 1293
1280=over 4 1294=over 4
1295
1296=item cf::player::num_playing
1297
1298Returns the official number of playing players, as per the Crossfire metaserver rules.
1299
1300=cut
1301
1302sub num_playing {
1303 scalar grep
1304 $_->ob->map
1305 && !$_->hidden
1306 && !$_->ob->flag (cf::FLAG_WIZ),
1307 cf::player::list
1308}
1281 1309
1282=item cf::player::find $login 1310=item cf::player::find $login
1283 1311
1284Returns the given player object, loading it if necessary (might block). 1312Returns the given player object, loading it if necessary (might block).
1285 1313
1891 1919
1892 { 1920 {
1893 my $guard = cf::lock_acquire "map_data:$path"; 1921 my $guard = cf::lock_acquire "map_data:$path";
1894 1922
1895 return unless $self->valid; 1923 return unless $self->valid;
1896 return if $self->in_memory != cf::MAP_SWAPPED; 1924 return unless $self->in_memory == cf::MAP_SWAPPED;
1897 1925
1898 $self->in_memory (cf::MAP_LOADING); 1926 $self->in_memory (cf::MAP_LOADING);
1899 1927
1900 $self->alloc; 1928 $self->alloc;
1901 1929
2430 my $gen = $self->{_goto_generation} = ++$GOTOGEN; 2458 my $gen = $self->{_goto_generation} = ++$GOTOGEN;
2431 2459
2432 $self->enter_link; 2460 $self->enter_link;
2433 2461
2434 (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
2435 my $map = eval { 2482 my $map = eval {
2436 my $map = cf::map::find $path; 2483 my $map = defined $path ? cf::map::find $path : undef;
2437 2484
2438 if ($map) { 2485 if ($map) {
2439 $map = $map->customise_for ($self); 2486 $map = $map->customise_for ($self);
2440 $map = $check->($map) if $check && $map; 2487 $map = $check->($map) if $check && $map;
2441 } else { 2488 } else {
2442 $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);
2443 } 2490 }
2444 2491
2445 $map 2492 $map
2446 }; 2493 };
2447 2494
3417 my $signal = new Coro::Signal; 3464 my $signal = new Coro::Signal;
3418 push @WAIT_FOR_TICK_BEGIN, $signal; 3465 push @WAIT_FOR_TICK_BEGIN, $signal;
3419 $signal->wait; 3466 $signal->wait;
3420} 3467}
3421 3468
3422 my $min = 1e6;#d#
3423 my $avg = 10;
3424$TICK_WATCHER = Event->timer ( 3469$TICK_WATCHER = Event->timer (
3425 reentrant => 0, 3470 reentrant => 0,
3426 parked => 1, 3471 parked => 1,
3427 prio => 0, 3472 prio => 0,
3428 at => $NEXT_TICK || $TICK, 3473 at => $NEXT_TICK || $TICK,
3436 3481
3437 $NOW = $tick_start = Event::time; 3482 $NOW = $tick_start = Event::time;
3438 3483
3439 cf::server_tick; # one server iteration 3484 cf::server_tick; # one server iteration
3440 3485
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; 3486 $RUNTIME += $TICK;
3465 $NEXT_TICK += $TICK; 3487 $NEXT_TICK += $TICK;
3466 3488
3467 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3489 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3468 $NEXT_RUNTIME_WRITE = $NOW + 10; 3490 $NEXT_RUNTIME_WRITE = $NOW + 10;
3470 write_runtime 3492 write_runtime
3471 or warn "ERROR: unable to write runtime file: $!"; 3493 or warn "ERROR: unable to write runtime file: $!";
3472 }; 3494 };
3473 } 3495 }
3474 3496
3475# my $AFTER = Event::time;
3476# warn $AFTER - $NOW;#d#
3477
3478 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 3497 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
3479 $sig->send; 3498 $sig->send;
3480 } 3499 }
3481 while (my $sig = shift @WAIT_FOR_TICK) { 3500 while (my $sig = shift @WAIT_FOR_TICK) {
3482 $sig->send; 3501 $sig->send;
3492 3511
3493 $LOAD = ($NOW - $tick_start) / $TICK; 3512 $LOAD = ($NOW - $tick_start) / $TICK;
3494 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; 3513 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25;
3495 3514
3496 _post_tick; 3515 _post_tick;
3497
3498
3499 }, 3516 },
3500); 3517);
3501 3518
3502{ 3519{
3520 BDB::min_parallel 8;
3503 BDB::max_poll_time $TICK * 0.1; 3521 BDB::max_poll_time $TICK * 0.1;
3504 $BDB_POLL_WATCHER = Event->io ( 3522 $BDB_POLL_WATCHER = Event->io (
3505 reentrant => 0, 3523 reentrant => 0,
3506 fd => BDB::poll_fileno, 3524 fd => BDB::poll_fileno,
3507 poll => 'r', 3525 poll => 'r',
3508 prio => 0, 3526 prio => 0,
3509 data => WF_AUTOCANCEL, 3527 data => WF_AUTOCANCEL,
3510 cb => \&BDB::poll_cb, 3528 cb => \&BDB::poll_cb,
3511 ); 3529 );
3512 BDB::min_parallel 8;
3513 3530
3514 BDB::set_sync_prepare { 3531 BDB::set_sync_prepare {
3515 my $status; 3532 my $status;
3516 my $current = $Coro::current; 3533 my $current = $Coro::current;
3517 ( 3534 (
3538 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 3555 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
3539 0666; 3556 0666;
3540 3557
3541 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 3558 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
3542 3559
3543 $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);
3544 $DB_ENV->set_lk_detect; 3562 $DB_ENV->set_lk_detect;
3545 }; 3563 };
3546 3564
3547 cf::cleanup "db_env_open(db): $@" if $@; 3565 cf::cleanup "db_env_open(db): $@" if $@;
3548 }; 3566 };
3549 } 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 );
3550} 3589}
3551 3590
3552{ 3591{
3553 IO::AIO::min_parallel 8; 3592 IO::AIO::min_parallel 8;
3554 3593

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines