… | |
… | |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
110 | |
111 | our %RESOURCE; |
111 | our %RESOURCE; |
112 | |
112 | |
|
|
113 | our $OUTPUT_RATE_MIN = 4000; |
|
|
114 | our $OUTPUT_RATE_MAX = 100000; |
|
|
115 | |
113 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
116 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
114 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
117 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
115 | our $NEXT_TICK; |
118 | our $NEXT_TICK; |
116 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
119 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
117 | |
120 | |
… | |
… | |
396 | } |
399 | } |
397 | |
400 | |
398 | =item cf::periodic $interval, $cb |
401 | =item cf::periodic $interval, $cb |
399 | |
402 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
403 | Like EV::periodic, but randomly selects a starting point so that the actions |
401 | get spread over timer. |
404 | get spread over time. |
402 | |
405 | |
403 | =cut |
406 | =cut |
404 | |
407 | |
405 | sub periodic($$) { |
408 | sub periodic($$) { |
406 | my ($interval, $cb) = @_; |
409 | my ($interval, $cb) = @_; |
… | |
… | |
503 | |
506 | |
504 | sub sync_job(&) { |
507 | sub sync_job(&) { |
505 | my ($job) = @_; |
508 | my ($job) = @_; |
506 | |
509 | |
507 | if ($Coro::current == $Coro::main) { |
510 | if ($Coro::current == $Coro::main) { |
508 | my $time = EV::time; |
511 | my $time = AE::time; |
509 | |
512 | |
510 | # this is the main coro, too bad, we have to block |
513 | # this is the main coro, too bad, we have to block |
511 | # till the operation succeeds, freezing the server :/ |
514 | # till the operation succeeds, freezing the server :/ |
512 | |
515 | |
513 | LOG llevError, Carp::longmess "sync job";#d# |
516 | LOG llevError, Carp::longmess "sync job";#d# |
… | |
… | |
530 | } else { |
533 | } else { |
531 | EV::loop EV::LOOP_ONESHOT; |
534 | EV::loop EV::LOOP_ONESHOT; |
532 | } |
535 | } |
533 | } |
536 | } |
534 | |
537 | |
535 | my $time = EV::time - $time; |
538 | my $time = AE::time - $time; |
536 | |
539 | |
537 | $TICK_START += $time; # do not account sync jobs to server load |
540 | $TICK_START += $time; # do not account sync jobs to server load |
538 | |
541 | |
539 | wantarray ? @res : $res[0] |
542 | wantarray ? @res : $res[0] |
540 | } else { |
543 | } else { |
… | |
… | |
584 | reset_signals; |
587 | reset_signals; |
585 | &$cb |
588 | &$cb |
586 | }, @args; |
589 | }, @args; |
587 | |
590 | |
588 | wantarray ? @res : $res[-1] |
591 | wantarray ? @res : $res[-1] |
|
|
592 | } |
|
|
593 | |
|
|
594 | sub objinfo { |
|
|
595 | ( |
|
|
596 | "counter value" => cf::object::object_count, |
|
|
597 | "objects created" => cf::object::create_count, |
|
|
598 | "objects destroyed" => cf::object::destroy_count, |
|
|
599 | "freelist size" => cf::object::free_count, |
|
|
600 | "allocated objects" => cf::object::objects_size, |
|
|
601 | "active objects" => cf::object::actives_size, |
|
|
602 | ) |
589 | } |
603 | } |
590 | |
604 | |
591 | =item $coin = coin_from_name $name |
605 | =item $coin = coin_from_name $name |
592 | |
606 | |
593 | =cut |
607 | =cut |
… | |
… | |
1422 | if (length $@) { |
1436 | if (length $@) { |
1423 | warn "$v->{path}: $@\n"; |
1437 | warn "$v->{path}: $@\n"; |
1424 | |
1438 | |
1425 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1439 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1426 | if exists $v->{meta}{mandatory}; |
1440 | if exists $v->{meta}{mandatory}; |
|
|
1441 | |
|
|
1442 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
|
|
1443 | delete $todo{$k}; |
1427 | } else { |
1444 | } else { |
1428 | $done{$k} = delete $todo{$k}; |
1445 | $done{$k} = delete $todo{$k}; |
1429 | push @EXTS, $v->{pkg}; |
1446 | push @EXTS, $v->{pkg}; |
1430 | $progress = 1; |
1447 | $progress = 1; |
1431 | |
1448 | |
… | |
… | |
2198 | $MAP_PREFETCHER->prio (6); |
2215 | $MAP_PREFETCHER->prio (6); |
2199 | |
2216 | |
2200 | () |
2217 | () |
2201 | } |
2218 | } |
2202 | |
2219 | |
|
|
2220 | # common code, used by both ->save and ->swapout |
2203 | sub save { |
2221 | sub _save { |
2204 | my ($self) = @_; |
2222 | my ($self) = @_; |
2205 | |
|
|
2206 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2207 | |
2223 | |
2208 | $self->{last_save} = $cf::RUNTIME; |
2224 | $self->{last_save} = $cf::RUNTIME; |
2209 | |
2225 | |
2210 | return unless $self->dirty; |
2226 | return unless $self->dirty; |
2211 | |
2227 | |
… | |
… | |
2231 | } else { |
2247 | } else { |
2232 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2248 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2233 | } |
2249 | } |
2234 | } |
2250 | } |
2235 | |
2251 | |
|
|
2252 | sub save { |
|
|
2253 | my ($self) = @_; |
|
|
2254 | |
|
|
2255 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2256 | |
|
|
2257 | $self->_save; |
|
|
2258 | } |
|
|
2259 | |
2236 | sub swap_out { |
2260 | sub swap_out { |
2237 | my ($self) = @_; |
2261 | my ($self) = @_; |
2238 | |
2262 | |
2239 | # save first because save cedes |
|
|
2240 | $self->save; |
|
|
2241 | |
|
|
2242 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2263 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2243 | |
2264 | |
2244 | return if $self->players; |
|
|
2245 | return if $self->in_memory != cf::MAP_ACTIVE; |
2265 | return if $self->in_memory != cf::MAP_ACTIVE; |
2246 | return if $self->{deny_save}; |
2266 | return if $self->{deny_save}; |
|
|
2267 | return if $self->players; |
2247 | |
2268 | |
2248 | $self->in_memory (cf::MAP_SWAPPED); |
2269 | # first deactivate the map and "unlink" it from the core |
2249 | |
|
|
2250 | $self->deactivate; |
2270 | $self->deactivate; |
2251 | $_->clear_links_to ($self) for values %cf::MAP; |
2271 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2272 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2273 | |
|
|
2274 | # then atomically save |
|
|
2275 | $self->_save; |
|
|
2276 | |
|
|
2277 | # then free the map |
2252 | $self->clear; |
2278 | $self->clear; |
2253 | } |
2279 | } |
2254 | |
2280 | |
2255 | sub reset_at { |
2281 | sub reset_at { |
2256 | my ($self) = @_; |
2282 | my ($self) = @_; |
… | |
… | |
2585 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2611 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2586 | |
2612 | |
2587 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2613 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2588 | if $self->map && $self->map->{path} ne "{link}"; |
2614 | if $self->map && $self->map->{path} ne "{link}"; |
2589 | |
2615 | |
2590 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
2616 | $self->enter_map ($LINK_MAP || link_map, 3, 3); |
2591 | } |
2617 | } |
2592 | |
2618 | |
2593 | sub cf::object::player::leave_link { |
2619 | sub cf::object::player::leave_link { |
2594 | my ($self, $map, $x, $y) = @_; |
2620 | my ($self, $map, $x, $y) = @_; |
2595 | |
2621 | |
… | |
… | |
3529 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3555 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3530 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3556 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3531 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3557 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3532 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3558 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3533 | |
3559 | |
3534 | cf::init_experience; |
|
|
3535 | cf::init_anim; |
|
|
3536 | cf::init_attackmess; |
|
|
3537 | cf::init_dynamic; |
|
|
3538 | |
|
|
3539 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3560 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3540 | |
3561 | |
3541 | # we must not ever block the main coroutine |
3562 | # we must not ever block the main coroutine |
3542 | local $Coro::idle = sub { |
3563 | local $Coro::idle = sub { |
3543 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3564 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
… | |
… | |
3548 | }; |
3569 | }; |
3549 | |
3570 | |
3550 | evthread_start IO::AIO::poll_fileno; |
3571 | evthread_start IO::AIO::poll_fileno; |
3551 | |
3572 | |
3552 | cf::sync_job { |
3573 | cf::sync_job { |
|
|
3574 | cf::init_experience; |
|
|
3575 | cf::init_anim; |
|
|
3576 | cf::init_attackmess; |
|
|
3577 | cf::init_dynamic; |
|
|
3578 | |
3553 | cf::load_settings; |
3579 | cf::load_settings; |
3554 | cf::load_materials; |
3580 | cf::load_materials; |
3555 | |
3581 | |
3556 | reload_resources; |
3582 | reload_resources; |
3557 | reload_config; |
3583 | reload_config; |
… | |
… | |
3573 | use POSIX (); |
3599 | use POSIX (); |
3574 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3600 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3575 | |
3601 | |
3576 | (pop @POST_INIT)->(0) while @POST_INIT; |
3602 | (pop @POST_INIT)->(0) while @POST_INIT; |
3577 | }; |
3603 | }; |
|
|
3604 | |
|
|
3605 | cf::object::thawer::errors_are_fatal 0; |
|
|
3606 | warn "parse errors in files are no longer fatal from this point on.\n"; |
3578 | |
3607 | |
3579 | main_loop; |
3608 | main_loop; |
3580 | } |
3609 | } |
3581 | |
3610 | |
3582 | ############################################################################# |
3611 | ############################################################################# |
… | |
… | |
3584 | |
3613 | |
3585 | # install some emergency cleanup handlers |
3614 | # install some emergency cleanup handlers |
3586 | BEGIN { |
3615 | BEGIN { |
3587 | our %SIGWATCHER = (); |
3616 | our %SIGWATCHER = (); |
3588 | for my $signal (qw(INT HUP TERM)) { |
3617 | for my $signal (qw(INT HUP TERM)) { |
3589 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3618 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3590 | cf::cleanup "SIG$signal"; |
3619 | cf::cleanup "SIG$signal"; |
3591 | }; |
3620 | }; |
3592 | } |
3621 | } |
3593 | } |
3622 | } |
3594 | |
3623 | |
3595 | sub write_runtime_sync { |
3624 | sub write_runtime_sync { |
3596 | my $t0 = EV::time; |
3625 | my $t0 = AE::time; |
3597 | |
3626 | |
3598 | # first touch the runtime file to show we are still running: |
3627 | # first touch the runtime file to show we are still running: |
3599 | # the fsync below can take a very very long time. |
3628 | # the fsync below can take a very very long time. |
3600 | |
3629 | |
3601 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3630 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
… | |
… | |
3623 | or return; |
3652 | or return; |
3624 | |
3653 | |
3625 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3654 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3626 | and return; |
3655 | and return; |
3627 | |
3656 | |
3628 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3657 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3629 | |
3658 | |
3630 | 1 |
3659 | 1 |
3631 | } |
3660 | } |
3632 | |
3661 | |
3633 | our $uuid_lock; |
3662 | our $uuid_lock; |
… | |
… | |
3759 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3788 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3760 | for my $name (keys %$leaf_symtab) { |
3789 | for my $name (keys %$leaf_symtab) { |
3761 | _gv_clear *{"$pkg$name"}; |
3790 | _gv_clear *{"$pkg$name"}; |
3762 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3791 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3763 | } |
3792 | } |
3764 | warn "cleared package $pkg\n";#d# |
|
|
3765 | } |
3793 | } |
3766 | |
3794 | |
3767 | sub do_reload_perl() { |
3795 | sub do_reload_perl() { |
3768 | # can/must only be called in main |
3796 | # can/must only be called in main |
3769 | if ($Coro::current != $Coro::main) { |
3797 | if ($Coro::current != $Coro::main) { |
… | |
… | |
3771 | return; |
3799 | return; |
3772 | } |
3800 | } |
3773 | |
3801 | |
3774 | return if $RELOAD++; |
3802 | return if $RELOAD++; |
3775 | |
3803 | |
3776 | my $t1 = EV::time; |
3804 | my $t1 = AE::time; |
3777 | |
3805 | |
3778 | while ($RELOAD) { |
3806 | while ($RELOAD) { |
3779 | warn "reloading..."; |
3807 | warn "reloading..."; |
3780 | |
3808 | |
3781 | warn "entering sync_job"; |
3809 | warn "entering sync_job"; |
… | |
… | |
3884 | |
3912 | |
3885 | warn "reloaded"; |
3913 | warn "reloaded"; |
3886 | --$RELOAD; |
3914 | --$RELOAD; |
3887 | } |
3915 | } |
3888 | |
3916 | |
3889 | $t1 = EV::time - $t1; |
3917 | $t1 = AE::time - $t1; |
3890 | warn "reload completed in ${t1}s\n"; |
3918 | warn "reload completed in ${t1}s\n"; |
3891 | }; |
3919 | }; |
3892 | |
3920 | |
3893 | our $RELOAD_WATCHER; # used only during reload |
3921 | our $RELOAD_WATCHER; # used only during reload |
3894 | |
3922 | |
… | |
… | |
3897 | # coro crashes during coro_state_free->destroy here. |
3925 | # coro crashes during coro_state_free->destroy here. |
3898 | |
3926 | |
3899 | $RELOAD_WATCHER ||= cf::async { |
3927 | $RELOAD_WATCHER ||= cf::async { |
3900 | Coro::AIO::aio_wait cache_extensions; |
3928 | Coro::AIO::aio_wait cache_extensions; |
3901 | |
3929 | |
3902 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3930 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3903 | do_reload_perl; |
3931 | do_reload_perl; |
3904 | undef $RELOAD_WATCHER; |
3932 | undef $RELOAD_WATCHER; |
3905 | }; |
3933 | }; |
3906 | }; |
3934 | }; |
3907 | } |
3935 | } |
… | |
… | |
3948 | return; |
3976 | return; |
3949 | } |
3977 | } |
3950 | |
3978 | |
3951 | cf::server_tick; # one server iteration |
3979 | cf::server_tick; # one server iteration |
3952 | |
3980 | |
3953 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3981 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3954 | |
3982 | |
3955 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3983 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3956 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3984 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3957 | Coro::async_pool { |
3985 | Coro::async_pool { |
3958 | $Coro::current->{desc} = "runtime saver"; |
3986 | $Coro::current->{desc} = "runtime saver"; |