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.361 by root, Sun Sep 9 12:52:48 2007 UTC vs.
Revision 1.368 by root, Wed Sep 12 11:18:25 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
2444 my $gen = $self->{_goto_generation} = ++$GOTOGEN; 2458 my $gen = $self->{_goto_generation} = ++$GOTOGEN;
2445 2459
2446 $self->enter_link; 2460 $self->enter_link;
2447 2461
2448 (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 }
2472 # else put him there anyways for now #d#
2473
2474 ($path, $x, $y) = ($ob->map, $ob->x, $ob->y);
2475 }
2476 }
2477
2449 my $map = eval { 2478 my $map = eval {
2450 my $map = cf::map::find $path; 2479 my $map = cf::map::find $path;
2451 2480
2452 if ($map) { 2481 if ($map) {
2453 $map = $map->customise_for ($self); 2482 $map = $map->customise_for ($self);
3431 my $signal = new Coro::Signal; 3460 my $signal = new Coro::Signal;
3432 push @WAIT_FOR_TICK_BEGIN, $signal; 3461 push @WAIT_FOR_TICK_BEGIN, $signal;
3433 $signal->wait; 3462 $signal->wait;
3434} 3463}
3435 3464
3436 my $min = 1e6;#d# 3465our $stat_fh;
3437 my $avg = 10; 3466sysopen $stat_fh, "/tmp/cfstats", Fcntl::O_APPEND | Fcntl::O_CREAT | Fcntl::O_WRONLY, 0600;#d#
3467
3438$TICK_WATCHER = Event->timer ( 3468$TICK_WATCHER = Event->timer (
3439 reentrant => 0, 3469 reentrant => 0,
3440 parked => 1, 3470 parked => 1,
3441 prio => 0, 3471 prio => 0,
3442 at => $NEXT_TICK || $TICK, 3472 at => $NEXT_TICK || $TICK,
3446 Carp::cluck "major BUG: server tick called outside of main coro, skipping it" 3476 Carp::cluck "major BUG: server tick called outside of main coro, skipping it"
3447 unless ++$bug_warning > 10; 3477 unless ++$bug_warning > 10;
3448 return; 3478 return;
3449 } 3479 }
3450 3480
3481 my @pl = cf::player::list; my $stats = sprintf "%.2f %d %d %d", $RUNTIME, (scalar @pl), cf::object::actives_size, cf::object::objects_size; #d#
3482
3451 $NOW = $tick_start = Event::time; 3483 $NOW = $tick_start = Event::time;
3452 3484
3453 cf::server_tick; # one server iteration 3485 cf::server_tick; # one server iteration
3454
3455 0 && sync_job {#d#
3456 for(1..10) {
3457 my $t = Event::time;
3458 my $map = my $map = new_from_path cf::map "/tmp/x.map"
3459 or die;
3460
3461 $map->width (50);
3462 $map->height (50);
3463 $map->alloc;
3464 $map->_load_objects ("/tmp/x.map", 1); #TODO: does not work
3465 my $t = Event::time - $t;
3466
3467 #next unless $t < 0.0013;#d#
3468 if ($t < $min) {
3469 $min = $t;
3470 }
3471 $avg = $avg * 0.99 + $t * 0.01;
3472 }
3473 warn "XXXXXXXXXXXXXXXXXX min $min avg $avg\n";#d#
3474 exit 0;
3475 # 2007-05-22 02:33:04.569 min 0.00112509727478027 avg 0.0012259249572477
3476 };
3477 3486
3478 $RUNTIME += $TICK; 3487 $RUNTIME += $TICK;
3479 $NEXT_TICK += $TICK; 3488 $NEXT_TICK += $TICK;
3480 3489
3481 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3490 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3507 $LOAD = ($NOW - $tick_start) / $TICK; 3516 $LOAD = ($NOW - $tick_start) / $TICK;
3508 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; 3517 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25;
3509 3518
3510 _post_tick; 3519 _post_tick;
3511 3520
3512 3521 # gather some statistics#d#
3522 $stats .= sprintf " %d\n", 10000 * ($NOW - $tick_start);#d#
3523 IO::AIO::aio_write $stat_fh, undef, undef, $stats, 0;#d#
3513 }, 3524 },
3514); 3525);
3515 3526
3516{ 3527{
3528 BDB::min_parallel 8;
3517 BDB::max_poll_time $TICK * 0.1; 3529 BDB::max_poll_time $TICK * 0.1;
3518 $BDB_POLL_WATCHER = Event->io ( 3530 $BDB_POLL_WATCHER = Event->io (
3519 reentrant => 0, 3531 reentrant => 0,
3520 fd => BDB::poll_fileno, 3532 fd => BDB::poll_fileno,
3521 poll => 'r', 3533 poll => 'r',
3522 prio => 0, 3534 prio => 0,
3523 data => WF_AUTOCANCEL, 3535 data => WF_AUTOCANCEL,
3524 cb => \&BDB::poll_cb, 3536 cb => \&BDB::poll_cb,
3525 ); 3537 );
3526 BDB::min_parallel 8;
3527 3538
3528 BDB::set_sync_prepare { 3539 BDB::set_sync_prepare {
3529 my $status; 3540 my $status;
3530 my $current = $Coro::current; 3541 my $current = $Coro::current;
3531 ( 3542 (
3552 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 3563 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
3553 0666; 3564 0666;
3554 3565
3555 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 3566 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
3556 3567
3557 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); 3568 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC
3569 | BDB::LOG_AUTOREMOVE, 1);
3558 $DB_ENV->set_lk_detect; 3570 $DB_ENV->set_lk_detect;
3559 }; 3571 };
3560 3572
3561 cf::cleanup "db_env_open(db): $@" if $@; 3573 cf::cleanup "db_env_open(db): $@" if $@;
3562 }; 3574 };
3563 } 3575 }
3576
3577 $BDB_CHECKPOINT_WATCHER = Event->timer (
3578 after => 11,
3579 interval => 60,
3580 hard => 1,
3581 prio => 0,
3582 data => WF_AUTOCANCEL,
3583 cb => sub {
3584 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
3585 },
3586 );
3587 $BDB_TRICKLE_WATCHER = Event->timer (
3588 after => 5,
3589 interval => 10,
3590 hard => 1,
3591 prio => 0,
3592 data => WF_AUTOCANCEL,
3593 cb => sub {
3594 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
3595 },
3596 );
3564} 3597}
3565 3598
3566{ 3599{
3567 IO::AIO::min_parallel 8; 3600 IO::AIO::min_parallel 8;
3568 3601

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines