… | |
… | |
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_CHECKPOINT_WATCHER; |
|
|
86 | our $BDB_TRICKLE_WATCHER; |
85 | our $DB_ENV; |
87 | our $DB_ENV; |
86 | |
88 | |
87 | our %CFG; |
89 | our %CFG; |
88 | |
90 | |
89 | our $UPTIME; $UPTIME ||= time; |
91 | our $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'; |
… | |
… | |
285 | Return true if the lock is currently active, i.e. somebody has locked it. |
286 | Return true if the lock is currently active, i.e. somebody has locked it. |
286 | |
287 | |
287 | =cut |
288 | =cut |
288 | |
289 | |
289 | our %LOCK; |
290 | our %LOCK; |
|
|
291 | our %LOCKER;#d# |
290 | |
292 | |
291 | sub lock_wait($) { |
293 | sub lock_wait($) { |
292 | my ($key) = @_; |
294 | my ($key) = @_; |
|
|
295 | |
|
|
296 | if ($LOCKER{$key} == $Coro::current) {#d# |
|
|
297 | Carp::cluck "lock_wait($key) for already-acquired lock";#d# |
|
|
298 | return;#d# |
|
|
299 | }#d# |
293 | |
300 | |
294 | # wait for lock, if any |
301 | # wait for lock, if any |
295 | while ($LOCK{$key}) { |
302 | while ($LOCK{$key}) { |
296 | push @{ $LOCK{$key} }, $Coro::current; |
303 | push @{ $LOCK{$key} }, $Coro::current; |
297 | Coro::schedule; |
304 | Coro::schedule; |
… | |
… | |
303 | |
310 | |
304 | # wait, to be sure we are not locked |
311 | # wait, to be sure we are not locked |
305 | lock_wait $key; |
312 | lock_wait $key; |
306 | |
313 | |
307 | $LOCK{$key} = []; |
314 | $LOCK{$key} = []; |
|
|
315 | $LOCKER{$key} = $Coro::current;#d# |
308 | |
316 | |
309 | Coro::guard { |
317 | Coro::guard { |
|
|
318 | delete $LOCKER{$key};#d# |
310 | # wake up all waiters, to be on the safe side |
319 | # wake up all waiters, to be on the safe side |
311 | $_->ready for @{ delete $LOCK{$key} }; |
320 | $_->ready for @{ delete $LOCK{$key} }; |
312 | } |
321 | } |
313 | } |
322 | } |
314 | |
323 | |
… | |
… | |
500 | =item cf::db_put $family => $key => $value |
509 | =item cf::db_put $family => $key => $value |
501 | |
510 | |
502 | Stores the given C<$value> in the family. It can currently store binary |
511 | Stores the given C<$value> in the family. It can currently store binary |
503 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
512 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
504 | |
513 | |
|
|
514 | =item $db = cf::db_table "name" |
|
|
515 | |
|
|
516 | Create and/or open a new database table. The string must not be "db" and must be unique |
|
|
517 | within each server. |
|
|
518 | |
505 | =cut |
519 | =cut |
|
|
520 | |
|
|
521 | sub 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 | } |
506 | |
537 | |
507 | our $DB; |
538 | our $DB; |
508 | |
539 | |
509 | sub db_init { |
540 | sub db_init { |
510 | unless ($DB) { |
|
|
511 | $DB = BDB::db_create $DB_ENV; |
|
|
512 | |
|
|
513 | cf::sync_job { |
541 | cf::sync_job { |
514 | eval { |
542 | $DB ||= db_table "db"; |
515 | $DB->set_flags (BDB::CHKSUM); |
|
|
516 | |
|
|
517 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
518 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
519 | cf::cleanup "db_open(db): $!" if $!; |
|
|
520 | }; |
|
|
521 | cf::cleanup "db_open(db): $@" if $@; |
|
|
522 | }; |
|
|
523 | } |
543 | }; |
524 | } |
544 | } |
525 | |
545 | |
526 | sub db_get($$) { |
546 | sub db_get($$) { |
527 | my $key = "$_[0]/$_[1]"; |
547 | my $key = "$_[0]/$_[1]"; |
528 | |
548 | |
… | |
… | |
1009 | |
1029 | |
1010 | my $guard = cf::lock_acquire "io"; |
1030 | my $guard = cf::lock_acquire "io"; |
1011 | |
1031 | |
1012 | sync_job { |
1032 | sync_job { |
1013 | if (length $$rdata) { |
1033 | if (length $$rdata) { |
|
|
1034 | utf8::decode (my $decname = $filename); |
1014 | warn sprintf "saving %s (%d,%d)\n", |
1035 | warn sprintf "saving %s (%d,%d)\n", |
1015 | $filename, length $$rdata, scalar @$objs; |
1036 | $decname, length $$rdata, scalar @$objs; |
1016 | |
1037 | |
1017 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1038 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1018 | chmod SAVE_MODE, $fh; |
1039 | chmod SAVE_MODE, $fh; |
1019 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1040 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1020 | aio_fsync $fh if $cf::USE_FSYNC; |
1041 | aio_fsync $fh if $cf::USE_FSYNC; |
… | |
… | |
1070 | |
1091 | |
1071 | undef $guard; |
1092 | undef $guard; |
1072 | $av = eval { (Storable::thaw $av)->{objs} }; |
1093 | $av = eval { (Storable::thaw $av)->{objs} }; |
1073 | } |
1094 | } |
1074 | |
1095 | |
|
|
1096 | utf8::decode (my $decname = $filename); |
1075 | warn sprintf "loading %s (%d)\n", |
1097 | warn sprintf "loading %s (%d,%d)\n", |
1076 | $filename, length $data, scalar @{$av || []}; |
1098 | $decname, length $data, scalar @{$av || []}; |
1077 | |
1099 | |
1078 | ($data, $av) |
1100 | ($data, $av) |
1079 | } |
1101 | } |
1080 | |
1102 | |
1081 | =head2 COMMAND CALLBACKS |
1103 | =head2 COMMAND CALLBACKS |
… | |
… | |
1268 | use Coro::AIO; |
1290 | use Coro::AIO; |
1269 | |
1291 | |
1270 | =head3 cf::player |
1292 | =head3 cf::player |
1271 | |
1293 | |
1272 | =over 4 |
1294 | =over 4 |
|
|
1295 | |
|
|
1296 | =item cf::player::num_playing |
|
|
1297 | |
|
|
1298 | Returns the official number of playing players, as per the Crossfire metaserver rules. |
|
|
1299 | |
|
|
1300 | =cut |
|
|
1301 | |
|
|
1302 | sub num_playing { |
|
|
1303 | scalar grep |
|
|
1304 | $_->ob->map |
|
|
1305 | && !$_->hidden |
|
|
1306 | && !$_->ob->flag (cf::FLAG_WIZ), |
|
|
1307 | cf::player::list |
|
|
1308 | } |
1273 | |
1309 | |
1274 | =item cf::player::find $login |
1310 | =item cf::player::find $login |
1275 | |
1311 | |
1276 | Returns the given player object, loading it if necessary (might block). |
1312 | Returns the given player object, loading it if necessary (might block). |
1277 | |
1313 | |
… | |
… | |
1841 | sub find { |
1877 | sub find { |
1842 | my ($path, $origin) = @_; |
1878 | my ($path, $origin) = @_; |
1843 | |
1879 | |
1844 | $path = normalise $path, $origin && $origin->path; |
1880 | $path = normalise $path, $origin && $origin->path; |
1845 | |
1881 | |
|
|
1882 | cf::lock_wait "map_data:$path";#d#remove |
1846 | cf::lock_wait "map_find:$path"; |
1883 | cf::lock_wait "map_find:$path"; |
1847 | |
1884 | |
1848 | $cf::MAP{$path} || do { |
1885 | $cf::MAP{$path} || do { |
1849 | my $guard = cf::lock_acquire "map_find:$path"; |
1886 | my $guard1 = cf::lock_acquire "map_find:$path"; |
|
|
1887 | my $guard2 = cf::lock_acquire "map_data:$path"; # just for the fun of it |
|
|
1888 | |
1850 | my $map = new_from_path cf::map $path |
1889 | my $map = new_from_path cf::map $path |
1851 | or return; |
1890 | or return; |
1852 | |
1891 | |
1853 | $map->{last_save} = $cf::RUNTIME; |
1892 | $map->{last_save} = $cf::RUNTIME; |
1854 | |
1893 | |
… | |
… | |
1856 | or return; |
1895 | or return; |
1857 | |
1896 | |
1858 | if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?) |
1897 | if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?) |
1859 | # doing this can freeze the server in a sync job, obviously |
1898 | # doing this can freeze the server in a sync job, obviously |
1860 | #$cf::WAIT_FOR_TICK->wait; |
1899 | #$cf::WAIT_FOR_TICK->wait; |
|
|
1900 | undef $guard1; |
|
|
1901 | undef $guard2; |
1861 | $map->reset; |
1902 | $map->reset; |
1862 | undef $guard; |
|
|
1863 | return find $path; |
1903 | return find $path; |
1864 | } |
1904 | } |
1865 | |
1905 | |
1866 | $cf::MAP{$path} = $map |
1906 | $cf::MAP{$path} = $map |
1867 | } |
1907 | } |
… | |
… | |
1876 | local $self->{deny_reset} = 1; # loading can take a long time |
1916 | local $self->{deny_reset} = 1; # loading can take a long time |
1877 | |
1917 | |
1878 | my $path = $self->{path}; |
1918 | my $path = $self->{path}; |
1879 | |
1919 | |
1880 | { |
1920 | { |
1881 | my $guard1 = cf::lock_acquire "map_data:$path"; |
1921 | my $guard = cf::lock_acquire "map_data:$path"; |
1882 | my $guard2 = cf::lock_acquire "map_load:$path"; |
|
|
1883 | |
1922 | |
|
|
1923 | return unless $self->valid; |
1884 | return if $self->in_memory != cf::MAP_SWAPPED; |
1924 | return unless $self->in_memory == cf::MAP_SWAPPED; |
1885 | |
1925 | |
1886 | $self->in_memory (cf::MAP_LOADING); |
1926 | $self->in_memory (cf::MAP_LOADING); |
1887 | |
1927 | |
1888 | $self->alloc; |
1928 | $self->alloc; |
1889 | |
1929 | |
… | |
… | |
2061 | |
2101 | |
2062 | return if $self->players; |
2102 | return if $self->players; |
2063 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2103 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
2064 | return if $self->{deny_save}; |
2104 | return if $self->{deny_save}; |
2065 | |
2105 | |
|
|
2106 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2107 | |
|
|
2108 | $self->deactivate; |
|
|
2109 | $_->clear_links_to ($self) for values %cf::MAP; |
2066 | $self->clear; |
2110 | $self->clear; |
2067 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2068 | } |
2111 | } |
2069 | |
2112 | |
2070 | sub reset_at { |
2113 | sub reset_at { |
2071 | my ($self) = @_; |
2114 | my ($self) = @_; |
2072 | |
2115 | |
… | |
… | |
2104 | if $uniq; |
2147 | if $uniq; |
2105 | } |
2148 | } |
2106 | |
2149 | |
2107 | delete $cf::MAP{$self->path}; |
2150 | delete $cf::MAP{$self->path}; |
2108 | |
2151 | |
|
|
2152 | $self->deactivate; |
|
|
2153 | $_->clear_links_to ($self) for values %cf::MAP; |
2109 | $self->clear; |
2154 | $self->clear; |
2110 | |
|
|
2111 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2112 | |
2155 | |
2113 | $self->unlink_save; |
2156 | $self->unlink_save; |
2114 | $self->destroy; |
2157 | $self->destroy; |
2115 | } |
2158 | } |
2116 | |
2159 | |
… | |
… | |
2415 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2458 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2416 | |
2459 | |
2417 | $self->enter_link; |
2460 | $self->enter_link; |
2418 | |
2461 | |
2419 | (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 | # todo: use ob_blocked to check all tiles of the destination "object" |
|
|
2470 | # for suitability. |
|
|
2471 | |
|
|
2472 | ($path, $x, $y) = ($ob->map, $ob->x, $ob->y); |
|
|
2473 | } |
|
|
2474 | } |
|
|
2475 | |
2420 | my $map = eval { |
2476 | my $map = eval { |
2421 | my $map = cf::map::find $path; |
2477 | my $map = cf::map::find $path; |
2422 | |
2478 | |
2423 | if ($map) { |
2479 | if ($map) { |
2424 | $map = $map->customise_for ($self); |
2480 | $map = $map->customise_for ($self); |
… | |
… | |
3402 | my $signal = new Coro::Signal; |
3458 | my $signal = new Coro::Signal; |
3403 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3459 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3404 | $signal->wait; |
3460 | $signal->wait; |
3405 | } |
3461 | } |
3406 | |
3462 | |
3407 | my $min = 1e6;#d# |
3463 | our $stat_fh; |
3408 | my $avg = 10; |
3464 | sysopen $stat_fh, "/tmp/cfstats", Fcntl::O_APPEND | Fcntl::O_CREAT | Fcntl::O_WRONLY, 0600;#d# |
|
|
3465 | |
3409 | $TICK_WATCHER = Event->timer ( |
3466 | $TICK_WATCHER = Event->timer ( |
3410 | reentrant => 0, |
3467 | reentrant => 0, |
3411 | parked => 1, |
3468 | parked => 1, |
3412 | prio => 0, |
3469 | prio => 0, |
3413 | at => $NEXT_TICK || $TICK, |
3470 | at => $NEXT_TICK || $TICK, |
… | |
… | |
3417 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3474 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3418 | unless ++$bug_warning > 10; |
3475 | unless ++$bug_warning > 10; |
3419 | return; |
3476 | return; |
3420 | } |
3477 | } |
3421 | |
3478 | |
|
|
3479 | my @pl = cf::player::list; my $stats = sprintf "%.2f %d %d %d", $RUNTIME, (scalar @pl), cf::object::actives_size, cf::object::objects_size; #d# |
|
|
3480 | |
3422 | $NOW = $tick_start = Event::time; |
3481 | $NOW = $tick_start = Event::time; |
3423 | |
3482 | |
3424 | cf::server_tick; # one server iteration |
3483 | cf::server_tick; # one server iteration |
3425 | |
|
|
3426 | 0 && sync_job {#d# |
|
|
3427 | for(1..10) { |
|
|
3428 | my $t = Event::time; |
|
|
3429 | my $map = my $map = new_from_path cf::map "/tmp/x.map" |
|
|
3430 | or die; |
|
|
3431 | |
|
|
3432 | $map->width (50); |
|
|
3433 | $map->height (50); |
|
|
3434 | $map->alloc; |
|
|
3435 | $map->_load_objects ("/tmp/x.map", 1); #TODO: does not work |
|
|
3436 | my $t = Event::time - $t; |
|
|
3437 | |
|
|
3438 | #next unless $t < 0.0013;#d# |
|
|
3439 | if ($t < $min) { |
|
|
3440 | $min = $t; |
|
|
3441 | } |
|
|
3442 | $avg = $avg * 0.99 + $t * 0.01; |
|
|
3443 | } |
|
|
3444 | warn "XXXXXXXXXXXXXXXXXX min $min avg $avg\n";#d# |
|
|
3445 | exit 0; |
|
|
3446 | # 2007-05-22 02:33:04.569 min 0.00112509727478027 avg 0.0012259249572477 |
|
|
3447 | }; |
|
|
3448 | |
3484 | |
3449 | $RUNTIME += $TICK; |
3485 | $RUNTIME += $TICK; |
3450 | $NEXT_TICK += $TICK; |
3486 | $NEXT_TICK += $TICK; |
3451 | |
3487 | |
3452 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3488 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
… | |
… | |
3478 | $LOAD = ($NOW - $tick_start) / $TICK; |
3514 | $LOAD = ($NOW - $tick_start) / $TICK; |
3479 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3515 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3480 | |
3516 | |
3481 | _post_tick; |
3517 | _post_tick; |
3482 | |
3518 | |
3483 | |
3519 | # gather some statistics#d# |
|
|
3520 | $stats .= sprintf " %d\n", 10000 * ($NOW - $tick_start);#d# |
|
|
3521 | IO::AIO::aio_write $stat_fh, undef, undef, $stats, 0;#d# |
3484 | }, |
3522 | }, |
3485 | ); |
3523 | ); |
3486 | |
3524 | |
3487 | { |
3525 | { |
|
|
3526 | BDB::min_parallel 8; |
3488 | BDB::max_poll_time $TICK * 0.1; |
3527 | BDB::max_poll_time $TICK * 0.1; |
3489 | $BDB_POLL_WATCHER = Event->io ( |
3528 | $BDB_POLL_WATCHER = Event->io ( |
3490 | reentrant => 0, |
3529 | reentrant => 0, |
3491 | fd => BDB::poll_fileno, |
3530 | fd => BDB::poll_fileno, |
3492 | poll => 'r', |
3531 | poll => 'r', |
3493 | prio => 0, |
3532 | prio => 0, |
3494 | data => WF_AUTOCANCEL, |
3533 | data => WF_AUTOCANCEL, |
3495 | cb => \&BDB::poll_cb, |
3534 | cb => \&BDB::poll_cb, |
3496 | ); |
3535 | ); |
3497 | BDB::min_parallel 8; |
|
|
3498 | |
3536 | |
3499 | BDB::set_sync_prepare { |
3537 | BDB::set_sync_prepare { |
3500 | my $status; |
3538 | my $status; |
3501 | my $current = $Coro::current; |
3539 | my $current = $Coro::current; |
3502 | ( |
3540 | ( |
… | |
… | |
3523 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3561 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3524 | 0666; |
3562 | 0666; |
3525 | |
3563 | |
3526 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3564 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3527 | |
3565 | |
3528 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
3566 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
|
|
3567 | | BDB::LOG_AUTOREMOVE, 1); |
3529 | $DB_ENV->set_lk_detect; |
3568 | $DB_ENV->set_lk_detect; |
3530 | }; |
3569 | }; |
3531 | |
3570 | |
3532 | cf::cleanup "db_env_open(db): $@" if $@; |
3571 | cf::cleanup "db_env_open(db): $@" if $@; |
3533 | }; |
3572 | }; |
3534 | } |
3573 | } |
|
|
3574 | |
|
|
3575 | $BDB_CHECKPOINT_WATCHER = Event->timer ( |
|
|
3576 | after => 11, |
|
|
3577 | interval => 60, |
|
|
3578 | hard => 1, |
|
|
3579 | prio => 0, |
|
|
3580 | data => WF_AUTOCANCEL, |
|
|
3581 | cb => sub { |
|
|
3582 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
3583 | }, |
|
|
3584 | ); |
|
|
3585 | $BDB_TRICKLE_WATCHER = Event->timer ( |
|
|
3586 | after => 5, |
|
|
3587 | interval => 10, |
|
|
3588 | hard => 1, |
|
|
3589 | prio => 0, |
|
|
3590 | data => WF_AUTOCANCEL, |
|
|
3591 | cb => sub { |
|
|
3592 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
|
|
3593 | }, |
|
|
3594 | ); |
3535 | } |
3595 | } |
3536 | |
3596 | |
3537 | { |
3597 | { |
3538 | IO::AIO::min_parallel 8; |
3598 | IO::AIO::min_parallel 8; |
3539 | |
3599 | |