ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.563 by root, Sat Apr 23 04:56:51 2011 UTC vs.
Revision 1.575 by root, Sat Aug 13 20:20:30 2011 UTC

131our @EXTRA_MODULES = qw(pod match mapscript incloader); 131our @EXTRA_MODULES = qw(pod match mapscript incloader);
132 132
133our %CFG; 133our %CFG;
134 134
135our $UPTIME; $UPTIME ||= time; 135our $UPTIME; $UPTIME ||= time;
136our $RUNTIME; 136our $RUNTIME = 0;
137our $SERVER_TICK = 0;
137our $NOW; 138our $NOW;
138 139
139our (%PLAYER, %PLAYER_LOADING); # all users 140our (%PLAYER, %PLAYER_LOADING); # all users
140our (%MAP, %MAP_LOADING ); # all maps 141our (%MAP, %MAP_LOADING ); # all maps
141our $LINK_MAP; # the special {link} map, which is always available 142our $LINK_MAP; # the special {link} map, which is always available
150 151
151our @POST_INIT; 152our @POST_INIT;
152 153
153our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) 154our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow)
154our $REALLY_UNLOOP; # never set to true, please :) 155our $REALLY_UNLOOP; # never set to true, please :)
156
157our $WAIT_FOR_TICK = new Coro::Signal;
158our @WAIT_FOR_TICK_BEGIN;
155 159
156binmode STDOUT; 160binmode STDOUT;
157binmode STDERR; 161binmode STDERR;
158 162
159# read virtual server time, if available 163# read virtual server time, if available
193 197
194=over 4 198=over 4
195 199
196=item $cf::UPTIME 200=item $cf::UPTIME
197 201
198The timestamp of the server start (so not actually an uptime). 202The timestamp of the server start (so not actually an "uptime").
203
204=item $cf::SERVER_TICK
205
206An unsigned integer that starts at zero when the server is started and is
207incremented on every tick.
208
209=item $cf::NOW
210
211The (real) time of the last (current) server tick - updated before and
212after tick processing, so this is useful only as a rough "what time is it
213now" estimate.
214
215=item $cf::TICK
216
217The interval between each server tick, in seconds.
199 218
200=item $cf::RUNTIME 219=item $cf::RUNTIME
201 220
202The time this server has run, starts at 0 and is increased by $cf::TICK on 221The time this server has run, starts at 0 and is increased by $cf::TICK on
203every server tick. 222every server tick.
209Various directories - "/etc", read-only install directory, perl-library 228Various directories - "/etc", read-only install directory, perl-library
210directory, pod-directory, read-only maps directory, "/var", "/var/tmp", 229directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
211unique-items directory, player file directory, random maps directory and 230unique-items directory, player file directory, random maps directory and
212database environment. 231database environment.
213 232
214=item $cf::NOW
215
216The time of the last (current) server tick.
217
218=item $cf::TICK
219
220The interval between server ticks, in seconds.
221
222=item $cf::LOADAVG 233=item $cf::LOADAVG
223 234
224The current CPU load on the server (alpha-smoothed), as a value between 0 235The current CPU load on the server (alpha-smoothed), as a value between 0
225(none) and 1 (overloaded), indicating how much time is spent on processing 236(none) and 1 (overloaded), indicating how much time is spent on processing
226objects per tick. Healthy values are < 0.5. 237objects per tick. Healthy values are < 0.5.
237=item cf::wait_for_tick, cf::wait_for_tick_begin 248=item cf::wait_for_tick, cf::wait_for_tick_begin
238 249
239These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only 250These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
240returns directly I<after> the tick processing (and consequently, can only wake one thread 251returns directly I<after> the tick processing (and consequently, can only wake one thread
241per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 252per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
253
254Note that cf::Wait_for_tick will immediately return when the server is not
255ticking, making it suitable for small pauses in threads that need to run
256when the server is paused. If that is not applicable (i.e. you I<really>
257want to wait, use C<$cf::WAIT_FOR_TICK>).
258
259=item $cf::WAIT_FOR_TICK
260
261Note that C<cf::wait_for_tick> is probably the correct thing to use. This
262variable contains a L<Coro::Signal> that is broadcats after every server
263tick. Calling C<< ->wait >> on it will suspend the caller until after the
264next server tick.
242 265
243=cut 266=cut
244 267
245sub wait_for_tick(); 268sub wait_for_tick();
246sub wait_for_tick_begin(); 269sub wait_for_tick_begin();
545 } 568 }
546 } 569 }
547 570
548 if (@SLOT_QUEUE) { 571 if (@SLOT_QUEUE) {
549 # we do not use wait_for_tick() as it returns immediately when tick is inactive 572 # we do not use wait_for_tick() as it returns immediately when tick is inactive
550 push @cf::WAIT_FOR_TICK, $signal; 573 $WAIT_FOR_TICK->wait;
551 $signal->wait;
552 } else { 574 } else {
553 $busy = 0; 575 $busy = 0;
554 Coro::schedule; 576 Coro::schedule;
555 } 577 }
556 } 578 }
2069 $self->{load_path} = $path; 2091 $self->{load_path} = $path;
2070 2092
2071 1 2093 1
2072} 2094}
2073 2095
2096# used to laod the header of an original map
2074sub load_header_orig { 2097sub load_header_orig {
2075 my ($self) = @_; 2098 my ($self) = @_;
2076 2099
2077 $self->load_header_from ($self->load_path) 2100 $self->load_header_from ($self->load_path)
2078} 2101}
2079 2102
2103# used to laod the header of an instantiated map
2080sub load_header_temp { 2104sub load_header_temp {
2081 my ($self) = @_; 2105 my ($self) = @_;
2082 2106
2083 $self->load_header_from ($self->save_path) 2107 $self->load_header_from ($self->save_path)
2084} 2108}
2085 2109
2110# called after loading the header from an instantiated map
2086sub prepare_temp { 2111sub prepare_temp {
2087 my ($self) = @_; 2112 my ($self) = @_;
2088 2113
2089 $self->last_access ((delete $self->{last_access}) 2114 $self->last_access ((delete $self->{last_access})
2090 || $cf::RUNTIME); #d# 2115 || $cf::RUNTIME); #d#
2091 # safety 2116 # safety
2092 $self->{instantiate_time} = $cf::RUNTIME 2117 $self->{instantiate_time} = $cf::RUNTIME
2093 if $self->{instantiate_time} > $cf::RUNTIME; 2118 if $self->{instantiate_time} > $cf::RUNTIME;
2094} 2119}
2095 2120
2121# called after loading the header from an original map
2096sub prepare_orig { 2122sub prepare_orig {
2097 my ($self) = @_; 2123 my ($self) = @_;
2098 2124
2099 $self->{load_original} = 1; 2125 $self->{load_original} = 1;
2100 $self->{instantiate_time} = $cf::RUNTIME; 2126 $self->{instantiate_time} = $cf::RUNTIME;
2126sub find { 2152sub find {
2127 my ($path, $origin) = @_; 2153 my ($path, $origin) = @_;
2128 2154
2129 cf::cede_to_tick; 2155 cf::cede_to_tick;
2130 2156
2131 $path = normalise $path, $origin && $origin->path; 2157 $path = normalise $path, $origin;
2132 2158
2133 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2159 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2134 my $guard2 = cf::lock_acquire "map_find:$path"; 2160 my $guard2 = cf::lock_acquire "map_find:$path";
2135 2161
2136 $cf::MAP{$path} || do { 2162 $cf::MAP{$path} || do {
2167 2193
2168 { 2194 {
2169 my $guard = cf::lock_acquire "map_data:$path"; 2195 my $guard = cf::lock_acquire "map_data:$path";
2170 2196
2171 return unless $self->valid; 2197 return unless $self->valid;
2172 return unless $self->in_memory == cf::MAP_SWAPPED; 2198 return unless $self->state == cf::MAP_SWAPPED;
2173
2174 $self->in_memory (cf::MAP_LOADING);
2175 2199
2176 $self->alloc; 2200 $self->alloc;
2177 2201
2178 $self->pre_load; 2202 $self->pre_load;
2179 cf::cede_to_tick; 2203 cf::cede_to_tick;
2197 } 2221 }
2198 } 2222 }
2199 } 2223 }
2200 2224
2201 $f->resolve_delayed_derefs; 2225 $f->resolve_delayed_derefs;
2226 } else {
2227 $self->post_load_original
2228 if delete $self->{load_original};
2202 } 2229 }
2230
2231 $self->state (cf::MAP_INACTIVE);
2203 2232
2204 cf::cede_to_tick; 2233 cf::cede_to_tick;
2205 # now do the right thing for maps 2234 # now do the right thing for maps
2206 $self->link_multipart_objects; 2235 $self->link_multipart_objects;
2207 $self->difficulty ($self->estimate_difficulty) 2236 $self->difficulty ($self->estimate_difficulty)
2211 unless ($self->{deny_activate}) { 2240 unless ($self->{deny_activate}) {
2212 $self->decay_objects; 2241 $self->decay_objects;
2213 $self->fix_auto_apply; 2242 $self->fix_auto_apply;
2214 $self->update_buttons; 2243 $self->update_buttons;
2215 cf::cede_to_tick; 2244 cf::cede_to_tick;
2216 $self->activate; 2245 #$self->activate; # no longer activate maps automatically
2217 } 2246 }
2218 2247
2219 $self->{last_save} = $cf::RUNTIME; 2248 $self->{last_save} = $cf::RUNTIME;
2220 $self->last_access ($cf::RUNTIME); 2249 $self->last_access ($cf::RUNTIME);
2221
2222 $self->in_memory (cf::MAP_ACTIVE);
2223 } 2250 }
2224 2251
2225 $self->post_load; 2252 $self->post_load;
2226 2253
2227 1 2254 1
2240# if $self->per_party; 2267# if $self->per_party;
2241 2268
2242 $self 2269 $self
2243} 2270}
2244 2271
2245# find and load all maps in the 3x3 area around a map
2246sub load_neighbours {
2247 my ($map) = @_;
2248
2249 my @neigh; # diagonal neighbours
2250
2251 for (0 .. 3) {
2252 my $neigh = $map->tile_path ($_)
2253 or next;
2254 $neigh = find $neigh, $map
2255 or next;
2256 $neigh->load;
2257
2258 # now find the diagonal neighbours
2259 push @neigh,
2260 [$neigh->tile_path (($_ + 3) % 4), $neigh],
2261 [$neigh->tile_path (($_ + 1) % 4), $neigh];
2262 }
2263
2264 for (grep defined $_->[0], @neigh) {
2265 my ($path, $origin) = @$_;
2266 my $neigh = find $path, $origin
2267 or next;
2268 $neigh->load;
2269 }
2270}
2271
2272sub find_sync { 2272sub find_sync {
2273 my ($path, $origin) = @_; 2273 my ($path, $origin) = @_;
2274 2274
2275 # it's a bug to call this from the main context
2275 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" 2276 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2276 if $Coro::current == $Coro::main; 2277 if $Coro::current == $Coro::main;
2277 2278
2278 find $path, $origin 2279 find $path, $origin
2279} 2280}
2280 2281
2281sub do_load_sync { 2282sub do_load_sync {
2282 my ($map) = @_; 2283 my ($map) = @_;
2283 2284
2285 # it's a bug to call this from the main context
2284 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" 2286 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2285 if $Coro::current == $Coro::main; 2287 if $Coro::current == $Coro::main;
2286 2288
2287 $map->load; 2289 $map->load;
2288} 2290}
2291our $MAP_PREFETCHER = undef; 2293our $MAP_PREFETCHER = undef;
2292 2294
2293sub find_async { 2295sub find_async {
2294 my ($path, $origin, $load) = @_; 2296 my ($path, $origin, $load) = @_;
2295 2297
2296 $path = normalise $path, $origin && $origin->{path}; 2298 $path = normalise $path, $origin;
2297 2299
2298 if (my $map = $cf::MAP{$path}) { 2300 if (my $map = $cf::MAP{$path}) {
2299 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; 2301 return $map if !$load || $map->linkable;
2300 } 2302 }
2301 2303
2302 $MAP_PREFETCH{$path} |= $load; 2304 $MAP_PREFETCH{$path} |= $load;
2303 2305
2304 $MAP_PREFETCHER ||= cf::async { 2306 $MAP_PREFETCHER ||= cf::async {
2363sub swap_out { 2365sub swap_out {
2364 my ($self) = @_; 2366 my ($self) = @_;
2365 2367
2366 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2368 my $lock = cf::lock_acquire "map_data:$self->{path}";
2367 2369
2368 return if $self->in_memory != cf::MAP_ACTIVE; 2370 return if !$self->linkable;
2369 return if $self->{deny_save}; 2371 return if $self->{deny_save};
2370 return if $self->players; 2372 return if $self->players;
2371 2373
2372 # first deactivate the map and "unlink" it from the core 2374 # first deactivate the map and "unlink" it from the core
2373 $self->deactivate; 2375 $self->deactivate;
2374 $_->clear_links_to ($self) for values %cf::MAP; 2376 $_->clear_links_to ($self) for values %cf::MAP;
2375 $self->in_memory (cf::MAP_SWAPPED); 2377 $self->state (cf::MAP_SWAPPED);
2376 2378
2377 # then atomically save 2379 # then atomically save
2378 $self->_save; 2380 $self->_save;
2379 2381
2380 # then free the map 2382 # then free the map
2406 2408
2407 return if $self->players; 2409 return if $self->players;
2408 2410
2409 cf::trace "resetting map ", $self->path, "\n"; 2411 cf::trace "resetting map ", $self->path, "\n";
2410 2412
2411 $self->in_memory (cf::MAP_SWAPPED); 2413 $self->state (cf::MAP_SWAPPED);
2412 2414
2413 # need to save uniques path 2415 # need to save uniques path
2414 unless ($self->{deny_save}) { 2416 unless ($self->{deny_save}) {
2415 my $uniq = $self->uniq_path; utf8::encode $uniq; 2417 my $uniq = $self->uniq_path; utf8::encode $uniq;
2416 2418
2746 # use -1 or undef as default coordinates, not 0, 0 2748 # use -1 or undef as default coordinates, not 0, 0
2747 ($x, $y) = ($map->enter_x, $map->enter_y) 2749 ($x, $y) = ($map->enter_x, $map->enter_y)
2748 if $x <= 0 && $y <= 0; 2750 if $x <= 0 && $y <= 0;
2749 2751
2750 $map->load; 2752 $map->load;
2751 $map->load_neighbours;
2752 2753
2753 return unless $self->contr->active; 2754 return unless $self->contr->active;
2754 2755
2755 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2756 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2756 if ($self->enter_map ($map, $x, $y)) { 2757 if ($self->enter_map ($map, $x, $y)) {
2812 ($path, $x, $y) = (undef, undef, undef); 2813 ($path, $x, $y) = (undef, undef, undef);
2813 } 2814 }
2814 } 2815 }
2815 2816
2816 my $map = eval { 2817 my $map = eval {
2817 my $map = defined $path ? cf::map::find $path : undef; 2818 my $map = defined $path ? cf::map::find $path, $self->map : undef;
2818 2819
2819 if ($map) { 2820 if ($map) {
2820 $map = $map->customise_for ($self); 2821 $map = $map->customise_for ($self);
2821 $map = $check->($map, $x, $y, $self) if $check && $map; 2822 $map = $check->($map, $x, $y, $self) if $check && $map;
2822 } else { 2823 } else {
2912 $Coro::current->{desc} = "enter_exit"; 2913 $Coro::current->{desc} = "enter_exit";
2913 2914
2914 unless (eval { 2915 unless (eval {
2915 $self->deactivate_recursive; # just to be sure 2916 $self->deactivate_recursive; # just to be sure
2916 2917
2917 # random map handling
2918 {
2919 my $guard = cf::lock_acquire "exit_prepare:$exit";
2920
2921 prepare_random_map $exit
2922 if $exit->slaying eq "/!";
2923 }
2924
2925 my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; 2918 my $map = cf::map::normalise $exit->slaying, $exit->map;
2926 my $x = $exit->stats->hp; 2919 my $x = $exit->stats->hp;
2927 my $y = $exit->stats->sp; 2920 my $y = $exit->stats->sp;
2921
2922 # special map handling
2923 my $slaying = $exit->slaying;
2924
2925 # special map handling
2926 if ($slaying eq "/!") {
2927 my $guard = cf::lock_acquire "exit_prepare:$exit";
2928
2929 prepare_random_map $exit
2930 if $exit->slaying eq "/!"; # need to re-check after getting the lock
2931
2932 $map = $exit->slaying;
2933
2934 } elsif ($slaying eq '!up') {
2935 $map = $exit->map->tile_path (cf::TILE_UP);
2936 $x = $exit->x;
2937 $y = $exit->y;
2938
2939 } elsif ($slaying eq '!down') {
2940 $map = $exit->map->tile_path (cf::TILE_DOWN);
2941 $x = $exit->x;
2942 $y = $exit->y;
2943 }
2928 2944
2929 $self->goto ($map, $x, $y); 2945 $self->goto ($map, $x, $y);
2930 2946
2931 # if exit is damned, update players death & WoR home-position 2947 # if exit is damned, update players death & WoR home-position
2932 $self->contr->savebed ($map, $x, $y) 2948 $self->contr->savebed ($map, $x, $y)
3712 }; 3728 };
3713 3729
3714 cf::object::thawer::errors_are_fatal 0; 3730 cf::object::thawer::errors_are_fatal 0;
3715 info "parse errors in files are no longer fatal from this point on.\n"; 3731 info "parse errors in files are no longer fatal from this point on.\n";
3716 3732
3717 my $free_main; $free_main = EV::idle sub { 3733 AE::postpone {
3718 undef $free_main;
3719 undef &main; # free gobs of memory :) 3734 undef &main; # free gobs of memory :)
3720 }; 3735 };
3721 3736
3722 goto &main_loop; 3737 goto &main_loop;
3723} 3738}
3880 3895
3881 cf::write_runtime_sync; # external watchdog should not bark 3896 cf::write_runtime_sync; # external watchdog should not bark
3882 3897
3883 trace "emergency_perl_save: flushing outstanding aio requests"; 3898 trace "emergency_perl_save: flushing outstanding aio requests";
3884 while (IO::AIO::nreqs || BDB::nreqs) { 3899 while (IO::AIO::nreqs || BDB::nreqs) {
3885 Coro::EV::timer_once 0.01; # let the sync_job do it's thing 3900 Coro::AnyEvent::sleep 0.01; # let the sync_job do it's thing
3886 } 3901 }
3887 3902
3888 cf::write_runtime_sync; # external watchdog should not bark 3903 cf::write_runtime_sync; # external watchdog should not bark
3889 }; 3904 };
3890 3905
4071 4086
4072############################################################################# 4087#############################################################################
4073 4088
4074my $bug_warning = 0; 4089my $bug_warning = 0;
4075 4090
4076our @WAIT_FOR_TICK;
4077our @WAIT_FOR_TICK_BEGIN;
4078
4079sub wait_for_tick() { 4091sub wait_for_tick() {
4080 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4092 return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main;
4081 4093
4082 my $signal = new Coro::Signal; 4094 $WAIT_FOR_TICK->wait;
4083 push @WAIT_FOR_TICK, $signal;
4084 $signal->wait;
4085} 4095}
4086 4096
4087sub wait_for_tick_begin() { 4097sub wait_for_tick_begin() {
4088 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4098 return Coro::AnyEvent::poll if tick_inhibit || $Coro::current == $Coro::main;
4089 4099
4090 my $signal = new Coro::Signal; 4100 my $signal = new Coro::Signal;
4091 push @WAIT_FOR_TICK_BEGIN, $signal; 4101 push @WAIT_FOR_TICK_BEGIN, $signal;
4092 $signal->wait; 4102 $signal->wait;
4093} 4103}
4097 Carp::cluck "major BUG: server tick called outside of main coro, skipping it" 4107 Carp::cluck "major BUG: server tick called outside of main coro, skipping it"
4098 unless ++$bug_warning > 10; 4108 unless ++$bug_warning > 10;
4099 return; 4109 return;
4100 } 4110 }
4101 4111
4102 cf::server_tick; # one server iteration 4112 cf::one_tick; # one server iteration
4103 4113
4104 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# 4114 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d#
4105 4115
4106 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4116 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4107 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4117 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4113 } 4123 }
4114 4124
4115 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4125 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4116 $sig->send; 4126 $sig->send;
4117 } 4127 }
4118 while (my $sig = shift @WAIT_FOR_TICK) { 4128 $WAIT_FOR_TICK->broadcast;
4119 $sig->send;
4120 }
4121 4129
4122 $LOAD = ($NOW - $TICK_START) / $TICK; 4130 $LOAD = ($NOW - $TICK_START) / $TICK;
4123 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; 4131 $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25;
4124 4132
4125 if (0) { 4133 if (0) {
4134{ 4142{
4135 # configure BDB 4143 # configure BDB
4136 4144
4137 BDB::min_parallel 16; 4145 BDB::min_parallel 16;
4138 BDB::max_poll_reqs $TICK * 0.1; 4146 BDB::max_poll_reqs $TICK * 0.1;
4139 $AnyEvent::BDB::WATCHER->priority (1); 4147 #$AnyEvent::BDB::WATCHER->priority (1);
4140 4148
4141 unless ($DB_ENV) { 4149 unless ($DB_ENV) {
4142 $DB_ENV = BDB::db_env_create; 4150 $DB_ENV = BDB::db_env_create;
4143 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); 4151 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
4144 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; 4152 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines