--- deliantra/server/ext/map-scheduler.ext 2007/01/09 21:32:42 1.20 +++ deliantra/server/ext/map-scheduler.ext 2010/05/14 22:56:47 1.55 @@ -1,102 +1,115 @@ -#! perl +#! perl # mandatory # this extension swaps out maps and resets them, in essence managing # the reset/swap policy of the server. # it also contains the map prefetching logic -our $DEACTIVATE_TIMEOUT = 20; # number of seconds after which maps get deactivated to save cpu -our $SWAP_TIMEOUT = 35; # number of seconds after which maps inactive get swapped out -our $SCHEDULE_INTERVAL = 2; # time the map scheduler sleeps between runs -our $SAVE_TIMEOUT = 20; # save maps every n seconds +#our $DEACTIVATE_TIMEOUT = 20; # number of seconds after which maps get deactivated to save cpu +our $SWAP_TIMEOUT = $cf::CFG{swap_timeout} || 300; # number of seconds after which inactive maps get swapped out +our $SCHEDULE_INTERVAL = $cf::CFG{schedule_interval} || 5; # time the map scheduler sleeps between runs +our $SAVE_TIMEOUT = $cf::CFG{save_timeout} || 30; # save maps every n seconds +our $SWAP_LOAD1 = $cf::CFG{swap_load1} || .1; # start aggressively swapping at this load +our $SWAP_LOAD2 = $cf::CFG{swap_load2} || .6; # swap as fast as possible at this load -cf::async_ext { +sub reload { + local $Coro::current->{desc} = "startup map scanner"; $Coro::current->prio (Coro::PRIO_MIN); + # load the header of swapped-out maps. # this is not a correctness issue, it simply saves diskspace # because old files will get cleaned up on reset time - Coro::Timer::sleep 1; - my $files = Coro::AIO::aio_readdir cf::localdir . "/" . cf::tmpdir; + for my $path (@{ cf::map::tmp_maps or [] }, @{ cf::map::random_maps or [] }) { + cf::map::find $path; + } - for my $map (@$files) { - utf8::decode $map; - next if $map =~ /\.(?:pst|meta)$/; - $map =~ s/∕/\//g; - cf::map::find $map; - Coro::Timer::sleep 0.3; + # now hunt for resettable per-player maps + for my $login (@{ cf::player::list_logins or [] }) { + for my $path (@{ cf::player::maps $login or [] }) { + $path =~ /^~[^\/]+(\/.*)$/ + or next; # doh + + my $base = cf::map::find $1; + + # skip maps without base maps on the assumption + # that those are old, unresettable maps + next unless $base; + + # skip unresettable maps, for speed + next if $base->{deny_reset}; + + my $map = cf::map::find $path; + + if ($map->{deny_reset}) { + warn "found noreset map with resettable base map, resetting: $path\n"; + delete $map->{deny_reset}; + } + } } +} - #TODO: should also preload random maps... -}; +cf::post_init { + cf::async { reload }; + + our $SCHEDULER = cf::async_ext { + $Coro::current->{desc} = "map scheduler"; + $Coro::current->prio (Coro::PRIO_MAX); + + while () { + if ($cf::LOADAVG > $SWAP_LOAD2) { + cf::wait_for_tick; + } else { + Coro::EV::timer_once $SCHEDULE_INTERVAL; + } -our $SCHEDULER = cf::async_ext { - while () { - Coro::Timer::sleep $SCHEDULE_INTERVAL; - - # this weird form of iteration over values is used because - # the hash changes underneath us frequently, and for - # keeps a direct reference to the value without (in 5.8 perls) - # keeping a reference, so this is prone to crashes or worse. - my @maps = keys %cf::MAP; - for (@maps) { - my $map = $cf::MAP{$_} - or next; - $map->valid or next; - - eval { - # not yet, because maps might become visible to players nearby - # we need to remove the map from %cf::MAP and all tiled map links + # this weird form of iteration over values is used because + # the hash changes underneath us frequently, and 'for' + # keeps a direct reference to the value without (in 5.8 perls) + # keeping a reference, so this is prone to crashes or worse. + my @maps = keys %cf::MAP; + for (@maps) { + my $map = $cf::MAP{$_} + or next; + $map->valid or next; + + eval { + # not yet, because maps might become visible to players nearby + # we need to remove the map from %cf::MAP and all tiled map links # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { # $map->deactivate; # delete $map->{active}; # } - if ($map->should_reset) { - $map->reset; - } elsif ($map->in_memory == cf::MAP_IN_MEMORY) { - if ($map->last_access + $SWAP_TIMEOUT <= $cf::RUNTIME && !$map->players) { - $cf::WAIT_FOR_TICK_ONE->wait; - $map->swap_out; - } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { - $cf::WAIT_FOR_TICK_ONE->wait; - $map->save; + if ($map->should_reset) { + $map->reset; + } elsif ($map->in_memory == cf::MAP_ACTIVE) { + my $max_idle = cf::clamp +(cf::lerp $cf::LOADAVG, $SWAP_LOAD1, $SWAP_LOAD2, $SWAP_TIMEOUT, $cf::TICK * 1.5), + $cf::TICK * 1.5, $SWAP_TIMEOUT; + + if ($map->last_access + $max_idle <= $cf::RUNTIME && !$map->players) { + $map->swap_out; + } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { + $map->save; + $map->{last_save} -= cf::rndm; # randomise map save times a bit + } } - } + }; + warn $@ if $@; + cf::cede_to_tick; }; - warn $@ if $@; - Coro::cede; - }; - } -}; - -$SCHEDULER->prio (-2); + } + }; -# map load prefetch for tiled maps, possibly exits, too -cf::map->attach ( - on_enter => sub { - my ($map, $pl) = @_; - - (cf::async { - my @diag; # diagonal neighbours - - for (0 .. 3) { - my $neigh = $map->tile_path ($_) - or next; - $neigh = cf::map::find $neigh, $map - or next; - $neigh->load; - - push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], - [$neigh->tile_path (($_ + 1) % 4), $neigh]; - } + $SCHEDULER->prio (-2); +}; - for (@diag) { - my $neigh = cf::map::find @$_ - or next; - $neigh->load; - } - })->prio (2); - # higher prio because prefetching is very important - # as it prefetches maps that can make the server block - }, -); +# purely for debugging, will load ALL maps +sub loadall { + my $maps = cf::map::static_maps; + + for my $path (@$maps) { + my $map = cf::map::find $path + or warn "ERROR: unable to find map $path\n"; + $map->load; + } +}