--- deliantra/server/lib/cf.pm 2010/04/29 08:13:51 1.534 +++ deliantra/server/lib/cf.pm 2010/07/03 01:49:18 1.556 @@ -22,9 +22,7 @@ package cf; -use 5.10.0; -use utf8; -use strict qw(vars subs); +use common::sense; use Symbol; use List::Util; @@ -80,6 +78,8 @@ sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload +our @ORIG_INC; + our %COMMAND = (); our %COMMAND_TIME = (); @@ -126,7 +126,7 @@ our $BDB_TRICKLE_WATCHER; our $DB_ENV; -our @EXTRA_MODULES = qw(pod match mapscript); +our @EXTRA_MODULES = qw(pod match mapscript incloader); our %CFG; @@ -235,9 +235,14 @@ =item cf::wait_for_tick, cf::wait_for_tick_begin These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only -returns directly I the tick processing (and consequently, can only wake one process +returns directly I the tick processing (and consequently, can only wake one thread per tick), while cf::wait_for_tick wakes up all waiters after tick processing. +=cut + +sub wait_for_tick(); +sub wait_for_tick_begin(); + =item @cf::INVOKE_RESULTS This array contains the results of the last C call. When @@ -354,12 +359,24 @@ sub encode_json($) { $json_coder->encode ($_[0]) } sub decode_json($) { $json_coder->decode ($_[0]) } +=item $ref = cf::yaml_load $scalar + +Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). + +=cut + +sub fork_call(&@); + +sub yaml_load($) { + fork_call { YAML::XS::Load $_[0] } @_ +} + =item cf::post_init { BLOCK } Execute the given codeblock, I all extensions have been (re-)loaded, but I the server starts ticking again. -The cdoeblock will have a single boolean argument to indicate whether this +The codeblock will have a single boolean argument to indicate whether this is a reload or not. =cut @@ -368,6 +385,23 @@ push @POST_INIT, shift; } +sub _post_init { + trace "running post_init jobs"; + + # run them in parallel... + + my @join; + + while () { + push @join, map &Coro::async ($_, 0), @POST_INIT; + @POST_INIT = (); + + @join or last; + + (pop @join)->join; + } +} + =item cf::lock_wait $string Wait until the given lock is available. See cf::lock_acquire. @@ -580,7 +614,7 @@ $coro } -=item fork_call { }, $args +=item fork_call { }, @args Executes the given code block with the given arguments in a seperate process, returning the results. Everything must be serialisable with @@ -589,6 +623,10 @@ =cut +sub post_fork { + reset_signals; +} + sub fork_call(&@) { my ($cb, @args) = @_; @@ -597,9 +635,9 @@ # TODO: investigate and fix (likely this will be rather laborious) my @res = Coro::Util::fork_eval { - reset_signals; + cf::post_fork; &$cb - }, @args; + } @args; wantarray ? @res : $res[-1] } @@ -1425,7 +1463,7 @@ if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; $ext{source} = - "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" + "package $pkg; use common::sense;\n" . "#line 1 \"$path\"\n{\n" . $source . "\n};\n1"; @@ -1643,10 +1681,13 @@ $pl->{deny_save} = 1; $pl->password ("*"); # this should lock out the player until we have nuked the dir - $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; + $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; $pl->deactivate; + my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; - $pl->invoke (cf::EVENT_PLAYER_QUIT); + $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; + ext::highscore::check ($pl->ob); + $pl->ns->destroy if $pl->ns; my $path = playerdir $pl; @@ -1822,20 +1863,7 @@ my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM - # mit "rum" bekleckern, nicht - $self->_create_random_map ( - $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, - $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, - $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, - $rmp->{exit_on_final_map}, - $rmp->{xsize}, $rmp->{ysize}, - $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, - $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, - $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, - $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, - $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, - (cf::region::find $rmp->{region}), $rmp->{custom} - ) + $self->_create_random_map ($rmp); } =item cf::map->register ($regex, $prio) @@ -1850,7 +1878,6 @@ my (undef, $regex, $prio) = @_; my $pkg = caller; - no strict; push @{"$pkg\::ISA"}, __PACKAGE__; $EXT_MAP{$pkg} = [$prio, qr<$regex>]; @@ -1872,7 +1899,7 @@ sub normalise { my ($path, $base) = @_; - $path = "$path"; # make sure its a string + $path = "$path"; # make sure it's a string $path =~ s/\.map$//; @@ -1897,7 +1924,6 @@ } for ($path) { - redo if s{//}{/}; redo if s{/\.?/}{/}; redo if s{/[^/]+/\.\./}{/}; } @@ -1921,7 +1947,7 @@ } } - Carp::cluck "unable to resolve path '$path' (base '$base')."; + Carp::cluck "unable to resolve path '$path' (base '$base')"; () } @@ -2054,6 +2080,8 @@ sub find { my ($path, $origin) = @_; + cf::cede_to_tick; + $path = normalise $path, $origin && $origin->path; my $guard1 = cf::lock_acquire "map_data:$path";#d#remove @@ -2147,6 +2175,8 @@ } $self->post_load; + + 1 } # customize the map for a given player, i.e. @@ -3275,7 +3305,6 @@ ["cf::player" => qw(peaceful send_msg)], ["cf::map" => qw(trigger)], ) { - no strict 'refs'; my ($pkg, @funs) = @$_; *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) for @funs; @@ -3372,8 +3401,7 @@ trace "loading facedata from $path\n"; - my $facedata; - 0 < aio_load $path, $facedata + 0 < aio_load $path, my $facedata or die "$path: $!"; $facedata = Coro::Storable::thaw $facedata; @@ -3527,6 +3555,8 @@ sub reload_resources { trace "reloading resource files...\n"; + reload_exp_table; + reload_materials; reload_facedata; reload_sound; reload_archetypes; @@ -3539,11 +3569,11 @@ sub reload_config { trace "reloading config file...\n"; - open my $fh, "<:utf8", "$CONFDIR/config" - or return; + 0 < aio_load "$CONFDIR/config", my $config + or die "$CONFDIR/config: $!"; - local $/; - *CFG = YAML::XS::Load scalar <$fh>; + utf8::decode $config; + *CFG = yaml_load $config; $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; @@ -3557,8 +3587,6 @@ }; warn $@ if $@; } - - trace "finished reloading resource files\n"; } sub pidfile() { @@ -3593,7 +3621,7 @@ cf::init_globals; # initialise logging LOG llevInfo, "Welcome to Deliantra, v" . VERSION; - LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; + LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; @@ -3611,13 +3639,13 @@ evthread_start IO::AIO::poll_fileno; cf::sync_job { - cf::init_experience; + cf::incloader::init (); + cf::init_anim; cf::init_attackmess; cf::init_dynamic; cf::load_settings; - cf::load_materials; reload_resources; reload_config; @@ -3639,13 +3667,18 @@ use POSIX (); POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; - (pop @POST_INIT)->(0) while @POST_INIT; + cf::_post_init 0; }; cf::object::thawer::errors_are_fatal 0; info "parse errors in files are no longer fatal from this point on.\n"; - main_loop; + my $free_main; $free_main = EV::idle sub { + undef $free_main; + undef &main; # free gobs of memory :) + }; + + goto &main_loop; } ############################################################################# @@ -3760,6 +3793,8 @@ local $USE_FSYNC = 0; cf::sync_job { + cf::write_runtime_sync; # external watchdog should not bark + # use a peculiar iteration method to avoid tripping on perl # refcount bugs in for. also avoids problems with players # and maps saved/destroyed asynchronously. @@ -3772,6 +3807,8 @@ } info "emergency_perl_save: end player save\n"; + cf::write_runtime_sync; # external watchdog should not bark + info "emergency_perl_save: begin map save\n"; for my $path (keys %cf::MAP) { my $map = $cf::MAP{$path} or next; @@ -3780,6 +3817,8 @@ } info "emergency_perl_save: end map save\n"; + cf::write_runtime_sync; # external watchdog should not bark + info "emergency_perl_save: begin database checkpoint\n"; BDB::db_env_txn_checkpoint $DB_ENV; info "emergency_perl_save: end database checkpoint\n"; @@ -3787,11 +3826,25 @@ info "emergency_perl_save: begin write uuid\n"; write_uuid_sync 1; info "emergency_perl_save: end write uuid\n"; - }; - info "emergency_perl_save: starting sync()\n"; - IO::AIO::aio_sync sub { - info "emergency_perl_save: finished sync()\n"; + cf::write_runtime_sync; # external watchdog should not bark + + trace "emergency_perl_save: syncing database to disk"; + BDB::db_env_txn_checkpoint $DB_ENV; + + info "emergency_perl_save: starting sync\n"; + IO::AIO::aio_sync sub { + info "emergency_perl_save: finished sync\n"; + }; + + cf::write_runtime_sync; # external watchdog should not bark + + trace "emergency_perl_save: flushing outstanding aio requests"; + while (IO::AIO::nreqs || BDB::nreqs) { + Coro::EV::timer_once 0.01; # let the sync_job do it's thing + } + + cf::write_runtime_sync; # external watchdog should not bark }; info "emergency_perl_save: leave\n"; @@ -3800,6 +3853,8 @@ sub post_cleanup { my ($make_core) = @_; + IO::AIO::flush; + error Carp::longmess "post_cleanup backtrace" if $make_core; @@ -3834,7 +3889,7 @@ sub do_reload_perl() { # can/must only be called in main - if (in_main) { + unless (in_main) { error "can only reload from main coroutine"; return; } @@ -3844,37 +3899,26 @@ my $t1 = AE::time; while ($RELOAD) { - info "reloading..."; + cf::get_slot 0.1, -1, "reload_perl"; + info "perl_reload: reloading..."; - trace "entering sync_job"; + trace "perl_reload: entering sync_job"; cf::sync_job { - cf::write_runtime_sync; # external watchdog should not bark - cf::emergency_save; - cf::write_runtime_sync; # external watchdog should not bark - - trace "syncing database to disk"; - BDB::db_env_txn_checkpoint $DB_ENV; - - # if anything goes wrong in here, we should simply crash as we already saved - - trace "flushing outstanding aio requests"; - while (IO::AIO::nreqs || BDB::nreqs) { - Coro::EV::timer_once 0.01; # let the sync_job do it's thing - } + #cf::emergency_save; - trace "cancelling all extension coros"; + trace "perl_reload: cancelling all extension coros"; $_->cancel for values %EXT_CORO; %EXT_CORO = (); - trace "removing commands"; + trace "perl_reload: removing commands"; %COMMAND = (); - trace "removing ext/exti commands"; + trace "perl_reload: removing ext/exti commands"; %EXTCMD = (); %EXTICMD = (); - trace "unloading/nuking all extensions"; + trace "perl_reload: unloading/nuking all extensions"; for my $pkg (@EXTS) { trace "... unloading $pkg"; @@ -3889,7 +3933,7 @@ clear_package $pkg; } - trace "unloading all perl modules loaded from $LIBDIR"; + trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; while (my ($k, $v) = each %INC) { next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; @@ -3906,11 +3950,11 @@ clear_package $k; } - trace "getting rid of safe::, as good as possible"; + trace "perl_reload: getting rid of safe::, as good as possible"; clear_package "safe::$_" for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); - trace "unloading cf.pm \"a bit\""; + trace "perl_reload: unloading cf.pm \"a bit\""; delete $INC{"cf.pm"}; delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; @@ -3918,44 +3962,42 @@ # and global variables created in xs #clear_package __PACKAGE__; - info "unload completed, starting to reload now"; + info "perl_reload: unload completed, starting to reload now"; - trace "reloading cf.pm"; + trace "perl_reload: reloading cf.pm"; require cf; cf::_connect_to_perl_1; - trace "loading config and database again"; + trace "perl_reload: loading config and database again"; cf::reload_config; - trace "loading extensions"; + trace "perl_reload: loading extensions"; cf::load_extensions; if ($REATTACH_ON_RELOAD) { - trace "reattaching attachments to objects/players"; + trace "perl_reload: reattaching attachments to objects/players"; _global_reattach; # objects, sockets - trace "reattaching attachments to maps"; + trace "perl_reload: reattaching attachments to maps"; reattach $_ for values %MAP; - trace "reattaching attachments to players"; + trace "perl_reload: reattaching attachments to players"; reattach $_ for values %PLAYER; } - trace "running post_init jobs"; - (pop @POST_INIT)->(1) while @POST_INIT; + cf::_post_init 1; - trace "leaving sync_job"; + trace "perl_reload: leaving sync_job"; 1 } or do { error $@; - cf::cleanup "error while reloading, exiting."; + cf::cleanup "perl_reload: error, exiting."; }; - info "reloaded"; --$RELOAD; } $t1 = AE::time - $t1; - info "reload completed in ${t1}s\n"; + info "perl_reload: completed in ${t1}s\n"; }; our $RELOAD_WATCHER; # used only during reload @@ -3986,14 +4028,14 @@ } }; -unshift @INC, $LIBDIR; +############################################################################# my $bug_warning = 0; our @WAIT_FOR_TICK; our @WAIT_FOR_TICK_BEGIN; -sub wait_for_tick { +sub wait_for_tick() { return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; my $signal = new Coro::Signal; @@ -4001,7 +4043,7 @@ $signal->wait; } -sub wait_for_tick_begin { +sub wait_for_tick_begin() { return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; my $signal = new Coro::Signal; @@ -4098,15 +4140,20 @@ undef $AnyEvent::AIO::WATCHER; } -my $_log_backtrace; +our $_log_backtrace; +our $_log_backtrace_last; sub _log_backtrace { my ($msg, @addr) = @_; - $msg =~ s/\n//; + $msg =~ s/\n$//; + if ($_log_backtrace_last eq $msg) { + LOG llevInfo, "[ABT] $msg\n"; + LOG llevInfo, "[ABT] [duplicate, suppressed]\n"; # limit the # of concurrent backtraces - if ($_log_backtrace < 2) { + } elsif ($_log_backtrace < 2) { + $_log_backtrace_last = $msg; ++$_log_backtrace; my $perl_bt = Carp::longmess $msg; async { @@ -4136,7 +4183,7 @@ }; } else { LOG llevInfo, "[ABT] $msg\n"; - LOG llevInfo, "[ABT] [suppressed]\n"; + LOG llevInfo, "[ABT] [overload, suppressed]\n"; } }