… | |
… | |
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 |
… | |
… | |
1533 | $cf::PLAYER{$login} = $pl |
1547 | $cf::PLAYER{$login} = $pl |
1534 | } |
1548 | } |
1535 | } |
1549 | } |
1536 | } |
1550 | } |
1537 | |
1551 | |
|
|
1552 | cf::player->attach ( |
|
|
1553 | on_load => sub { |
|
|
1554 | my ($pl, $path) = @_; |
|
|
1555 | |
|
|
1556 | # restore slots saved in save, below |
|
|
1557 | my $slots = delete $pl->{_slots}; |
|
|
1558 | |
|
|
1559 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1560 | $pl->combat_ob ($slots->[1]); |
|
|
1561 | $pl->ranged_ob ($slots->[2]); |
|
|
1562 | }, |
|
|
1563 | ); |
|
|
1564 | |
1538 | sub save($) { |
1565 | sub save($) { |
1539 | my ($pl) = @_; |
1566 | my ($pl) = @_; |
1540 | |
1567 | |
1541 | return if $pl->{deny_save}; |
1568 | return if $pl->{deny_save}; |
1542 | |
1569 | |
… | |
… | |
1547 | |
1574 | |
1548 | aio_mkdir playerdir $pl, 0770; |
1575 | aio_mkdir playerdir $pl, 0770; |
1549 | $pl->{last_save} = $cf::RUNTIME; |
1576 | $pl->{last_save} = $cf::RUNTIME; |
1550 | |
1577 | |
1551 | cf::get_slot 0.01; |
1578 | cf::get_slot 0.01; |
|
|
1579 | |
|
|
1580 | # save slots, to be restored later |
|
|
1581 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1552 | |
1582 | |
1553 | $pl->save_pl ($path); |
1583 | $pl->save_pl ($path); |
1554 | cf::cede_to_tick; |
1584 | cf::cede_to_tick; |
1555 | } |
1585 | } |
1556 | |
1586 | |
… | |
… | |
2182 | $MAP_PREFETCHER->prio (6); |
2212 | $MAP_PREFETCHER->prio (6); |
2183 | |
2213 | |
2184 | () |
2214 | () |
2185 | } |
2215 | } |
2186 | |
2216 | |
|
|
2217 | # common code, used by both ->save and ->swapout |
2187 | sub save { |
2218 | sub _save { |
2188 | my ($self) = @_; |
2219 | my ($self) = @_; |
2189 | |
|
|
2190 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2191 | |
2220 | |
2192 | $self->{last_save} = $cf::RUNTIME; |
2221 | $self->{last_save} = $cf::RUNTIME; |
2193 | |
2222 | |
2194 | return unless $self->dirty; |
2223 | return unless $self->dirty; |
2195 | |
2224 | |
… | |
… | |
2215 | } else { |
2244 | } else { |
2216 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2245 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2217 | } |
2246 | } |
2218 | } |
2247 | } |
2219 | |
2248 | |
|
|
2249 | sub save { |
|
|
2250 | my ($self) = @_; |
|
|
2251 | |
|
|
2252 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2253 | |
|
|
2254 | $self->_save; |
|
|
2255 | } |
|
|
2256 | |
2220 | sub swap_out { |
2257 | sub swap_out { |
2221 | my ($self) = @_; |
2258 | my ($self) = @_; |
2222 | |
2259 | |
2223 | # save first because save cedes |
|
|
2224 | $self->save; |
|
|
2225 | |
|
|
2226 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2260 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2227 | |
2261 | |
2228 | return if $self->players; |
|
|
2229 | return if $self->in_memory != cf::MAP_ACTIVE; |
2262 | return if $self->in_memory != cf::MAP_ACTIVE; |
2230 | return if $self->{deny_save}; |
2263 | return if $self->{deny_save}; |
|
|
2264 | return if $self->players; |
2231 | |
2265 | |
2232 | $self->in_memory (cf::MAP_SWAPPED); |
2266 | # first deactivate the map and "unlink" it from the core |
2233 | |
|
|
2234 | $self->deactivate; |
2267 | $self->deactivate; |
2235 | $_->clear_links_to ($self) for values %cf::MAP; |
2268 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2269 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2270 | |
|
|
2271 | # then atomically save |
|
|
2272 | $self->_save; |
|
|
2273 | |
|
|
2274 | # then free the map |
2236 | $self->clear; |
2275 | $self->clear; |
2237 | } |
2276 | } |
2238 | |
2277 | |
2239 | sub reset_at { |
2278 | sub reset_at { |
2240 | my ($self) = @_; |
2279 | my ($self) = @_; |
… | |
… | |
3513 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3552 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3514 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3553 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3515 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3554 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3516 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3555 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3517 | |
3556 | |
3518 | cf::init_experience; |
|
|
3519 | cf::init_anim; |
|
|
3520 | cf::init_attackmess; |
|
|
3521 | cf::init_dynamic; |
|
|
3522 | |
|
|
3523 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3557 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3524 | |
3558 | |
3525 | # we must not ever block the main coroutine |
3559 | # we must not ever block the main coroutine |
3526 | local $Coro::idle = sub { |
3560 | local $Coro::idle = sub { |
3527 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3561 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
… | |
… | |
3532 | }; |
3566 | }; |
3533 | |
3567 | |
3534 | evthread_start IO::AIO::poll_fileno; |
3568 | evthread_start IO::AIO::poll_fileno; |
3535 | |
3569 | |
3536 | cf::sync_job { |
3570 | cf::sync_job { |
|
|
3571 | cf::init_experience; |
|
|
3572 | cf::init_anim; |
|
|
3573 | cf::init_attackmess; |
|
|
3574 | cf::init_dynamic; |
|
|
3575 | |
3537 | cf::load_settings; |
3576 | cf::load_settings; |
3538 | cf::load_materials; |
3577 | cf::load_materials; |
3539 | |
3578 | |
3540 | reload_resources; |
3579 | reload_resources; |
3541 | reload_config; |
3580 | reload_config; |
… | |
… | |
3557 | use POSIX (); |
3596 | use POSIX (); |
3558 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3597 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3559 | |
3598 | |
3560 | (pop @POST_INIT)->(0) while @POST_INIT; |
3599 | (pop @POST_INIT)->(0) while @POST_INIT; |
3561 | }; |
3600 | }; |
|
|
3601 | |
|
|
3602 | cf::object::thawer::errors_are_fatal 0; |
|
|
3603 | warn "parse errors in files are no longer fatal from this point on.\n"; |
3562 | |
3604 | |
3563 | main_loop; |
3605 | main_loop; |
3564 | } |
3606 | } |
3565 | |
3607 | |
3566 | ############################################################################# |
3608 | ############################################################################# |
… | |
… | |
3568 | |
3610 | |
3569 | # install some emergency cleanup handlers |
3611 | # install some emergency cleanup handlers |
3570 | BEGIN { |
3612 | BEGIN { |
3571 | our %SIGWATCHER = (); |
3613 | our %SIGWATCHER = (); |
3572 | for my $signal (qw(INT HUP TERM)) { |
3614 | for my $signal (qw(INT HUP TERM)) { |
3573 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3615 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3574 | cf::cleanup "SIG$signal"; |
3616 | cf::cleanup "SIG$signal"; |
3575 | }; |
3617 | }; |
3576 | } |
3618 | } |
3577 | } |
3619 | } |
3578 | |
3620 | |
3579 | sub write_runtime_sync { |
3621 | sub write_runtime_sync { |
3580 | my $t0 = EV::time; |
3622 | my $t0 = AE::time; |
3581 | |
3623 | |
3582 | # first touch the runtime file to show we are still running: |
3624 | # first touch the runtime file to show we are still running: |
3583 | # the fsync below can take a very very long time. |
3625 | # the fsync below can take a very very long time. |
3584 | |
3626 | |
3585 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3627 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
… | |
… | |
3607 | or return; |
3649 | or return; |
3608 | |
3650 | |
3609 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3651 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3610 | and return; |
3652 | and return; |
3611 | |
3653 | |
3612 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3654 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3613 | |
3655 | |
3614 | 1 |
3656 | 1 |
3615 | } |
3657 | } |
3616 | |
3658 | |
3617 | our $uuid_lock; |
3659 | our $uuid_lock; |
… | |
… | |
3755 | return; |
3797 | return; |
3756 | } |
3798 | } |
3757 | |
3799 | |
3758 | return if $RELOAD++; |
3800 | return if $RELOAD++; |
3759 | |
3801 | |
3760 | my $t1 = EV::time; |
3802 | my $t1 = AE::time; |
3761 | |
3803 | |
3762 | while ($RELOAD) { |
3804 | while ($RELOAD) { |
3763 | warn "reloading..."; |
3805 | warn "reloading..."; |
3764 | |
3806 | |
3765 | warn "entering sync_job"; |
3807 | warn "entering sync_job"; |
… | |
… | |
3868 | |
3910 | |
3869 | warn "reloaded"; |
3911 | warn "reloaded"; |
3870 | --$RELOAD; |
3912 | --$RELOAD; |
3871 | } |
3913 | } |
3872 | |
3914 | |
3873 | $t1 = EV::time - $t1; |
3915 | $t1 = AE::time - $t1; |
3874 | warn "reload completed in ${t1}s\n"; |
3916 | warn "reload completed in ${t1}s\n"; |
3875 | }; |
3917 | }; |
3876 | |
3918 | |
3877 | our $RELOAD_WATCHER; # used only during reload |
3919 | our $RELOAD_WATCHER; # used only during reload |
3878 | |
3920 | |
… | |
… | |
3881 | # coro crashes during coro_state_free->destroy here. |
3923 | # coro crashes during coro_state_free->destroy here. |
3882 | |
3924 | |
3883 | $RELOAD_WATCHER ||= cf::async { |
3925 | $RELOAD_WATCHER ||= cf::async { |
3884 | Coro::AIO::aio_wait cache_extensions; |
3926 | Coro::AIO::aio_wait cache_extensions; |
3885 | |
3927 | |
3886 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3928 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3887 | do_reload_perl; |
3929 | do_reload_perl; |
3888 | undef $RELOAD_WATCHER; |
3930 | undef $RELOAD_WATCHER; |
3889 | }; |
3931 | }; |
3890 | }; |
3932 | }; |
3891 | } |
3933 | } |
… | |
… | |
3932 | return; |
3974 | return; |
3933 | } |
3975 | } |
3934 | |
3976 | |
3935 | cf::server_tick; # one server iteration |
3977 | cf::server_tick; # one server iteration |
3936 | |
3978 | |
3937 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3979 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3938 | |
3980 | |
3939 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3981 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3940 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3982 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3941 | Coro::async_pool { |
3983 | Coro::async_pool { |
3942 | $Coro::current->{desc} = "runtime saver"; |
3984 | $Coro::current->{desc} = "runtime saver"; |