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.455 by root, Sat Sep 27 08:17:40 2008 UTC vs.
Revision 1.461 by root, Fri Dec 19 22:47:29 2008 UTC

32use Opcode; 32use Opcode;
33use Safe; 33use Safe;
34use Safe::Hole; 34use Safe::Hole;
35use Storable (); 35use Storable ();
36 36
37use Guard ();
37use Coro (); 38use Coro ();
38use Coro::State; 39use Coro::State;
39use Coro::Handle; 40use Coro::Handle;
40use Coro::EV; 41use Coro::EV;
41use Coro::AnyEvent; 42use Coro::AnyEvent;
42use Coro::Timer; 43use Coro::Timer;
43use Coro::Signal; 44use Coro::Signal;
44use Coro::Semaphore; 45use Coro::Semaphore;
46use Coro::SemaphoreSet;
45use Coro::AnyEvent; 47use Coro::AnyEvent;
46use Coro::AIO; 48use Coro::AIO;
47use Coro::BDB 1.6; 49use Coro::BDB 1.6;
48use Coro::Storable; 50use Coro::Storable;
49use Coro::Util (); 51use Coro::Util ();
130our $JITTER; # average jitter 132our $JITTER; # average jitter
131our $TICK_START; # for load detecting purposes 133our $TICK_START; # for load detecting purposes
132 134
133our @POST_INIT; 135our @POST_INIT;
134 136
137our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow)
138
135binmode STDOUT; 139binmode STDOUT;
136binmode STDERR; 140binmode STDERR;
137 141
138# read virtual server time, if available 142# read virtual server time, if available
139unless ($RUNTIME || !-e $RUNTIMEFILE) { 143unless ($RUNTIME || !-e $RUNTIMEFILE) {
328Wait until the given lock is available. See cf::lock_acquire. 332Wait until the given lock is available. See cf::lock_acquire.
329 333
330=item my $lock = cf::lock_acquire $string 334=item my $lock = cf::lock_acquire $string
331 335
332Wait until the given lock is available and then acquires it and returns 336Wait until the given lock is available and then acquires it and returns
333a Coro::guard object. If the guard object gets destroyed (goes out of scope, 337a L<Guard> object. If the guard object gets destroyed (goes out of scope,
334for example when the coroutine gets canceled), the lock is automatically 338for example when the coroutine gets canceled), the lock is automatically
335returned. 339returned.
336 340
337Locks are *not* recursive, locking from the same coro twice results in a 341Locks are *not* recursive, locking from the same coro twice results in a
338deadlocked coro. 342deadlocked coro.
344 348
345Return true if the lock is currently active, i.e. somebody has locked it. 349Return true if the lock is currently active, i.e. somebody has locked it.
346 350
347=cut 351=cut
348 352
349our %LOCK; 353our $LOCKS = new Coro::SemaphoreSet;
350our %LOCKER;#d#
351 354
352sub lock_wait($) { 355sub lock_wait($) {
353 my ($key) = @_; 356 $LOCKS->wait ($_[0]);
354
355 if ($LOCKER{$key} == $Coro::current) {#d#
356 Carp::cluck "lock_wait($key) for already-acquired lock";#d#
357 return;#d#
358 }#d#
359
360 # wait for lock, if any
361 while ($LOCK{$key}) {
362 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
363 push @{ $LOCK{$key} }, $Coro::current;
364 Coro::schedule;
365 }
366} 357}
367 358
368sub lock_acquire($) { 359sub lock_acquire($) {
369 my ($key) = @_; 360 $LOCKS->guard ($_[0])
370
371 # wait, to be sure we are not locked
372 lock_wait $key;
373
374 $LOCK{$key} = [];
375 $LOCKER{$key} = $Coro::current;#d#
376
377 Coro::guard {
378 delete $LOCKER{$key};#d#
379 # wake up all waiters, to be on the safe side
380 $_->ready for @{ delete $LOCK{$key} };
381 }
382} 361}
383 362
384sub lock_active($) { 363sub lock_active($) {
385 my ($key) = @_; 364 $LOCKS->count ($_[0]) < 1
386
387 ! ! $LOCK{$key}
388} 365}
389 366
390sub freeze_mainloop { 367sub freeze_mainloop {
391 tick_inhibit_inc; 368 tick_inhibit_inc;
392 369
393 Coro::guard \&tick_inhibit_dec; 370 &Guard::guard (\&tick_inhibit_dec);
394} 371}
395 372
396=item cf::periodic $interval, $cb 373=item cf::periodic $interval, $cb
397 374
398Like EV::periodic, but randomly selects a starting point so that the actions 375Like EV::periodic, but randomly selects a starting point so that the actions
1187 } else { 1164 } else {
1188 aio_unlink "$filename.pst"; 1165 aio_unlink "$filename.pst";
1189 } 1166 }
1190 1167
1191 aio_rename "$filename~", $filename; 1168 aio_rename "$filename~", $filename;
1169
1170 $filename =~ s%/[^/]+$%%;
1171 aio_pathsync $filename if $cf::USE_FSYNC;
1192 } else { 1172 } else {
1193 warn "FATAL: $filename~: $!\n"; 1173 warn "unable to save objects: $filename~: $!\n";
1194 } 1174 }
1195 } else { 1175 } else {
1196 aio_unlink $filename; 1176 aio_unlink $filename;
1197 aio_unlink "$filename.pst"; 1177 aio_unlink "$filename.pst";
1198 } 1178 }
1289 my ($name, $cb) = @_; 1269 my ($name, $cb) = @_;
1290 1270
1291 $EXTICMD{$name} = $cb; 1271 $EXTICMD{$name} = $cb;
1292} 1272}
1293 1273
1274use File::Glob ();
1275
1294cf::player->attach ( 1276cf::player->attach (
1295 on_command => sub { 1277 on_command => sub {
1296 my ($pl, $name, $params) = @_; 1278 my ($pl, $name, $params) = @_;
1297 1279
1298 my $cb = $COMMAND{$name} 1280 my $cb = $COMMAND{$name}
1329 } 1311 }
1330 1312
1331 cf::override; 1313 cf::override;
1332 }, 1314 },
1333); 1315);
1316
1317# "readahead" all extensions
1318sub cache_extensions {
1319 my $grp = IO::AIO::aio_group;
1320
1321 add $grp IO::AIO::aio_readdir $LIBDIR, sub {
1322 for (grep /\.ext$/, @{$_[0]}) {
1323 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1324 }
1325 };
1326
1327 $grp
1328}
1334 1329
1335sub load_extensions { 1330sub load_extensions {
1336 cf::sync_job { 1331 cf::sync_job {
1337 my %todo; 1332 my %todo;
1338 1333
1967sub find { 1962sub find {
1968 my ($path, $origin) = @_; 1963 my ($path, $origin) = @_;
1969 1964
1970 $path = normalise $path, $origin && $origin->path; 1965 $path = normalise $path, $origin && $origin->path;
1971 1966
1972 cf::lock_wait "map_data:$path";#d#remove 1967 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
1973 cf::lock_wait "map_find:$path"; 1968 my $guard2 = cf::lock_acquire "map_find:$path";
1974 1969
1975 $cf::MAP{$path} || do { 1970 $cf::MAP{$path} || do {
1976 my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it
1977 my $guard2 = cf::lock_acquire "map_find:$path";
1978
1979 my $map = new_from_path cf::map $path 1971 my $map = new_from_path cf::map $path
1980 or return; 1972 or return;
1981 1973
1982 $map->{last_save} = $cf::RUNTIME; 1974 $map->{last_save} = $cf::RUNTIME;
1983 1975
3409 3401
3410 cf::init_experience; 3402 cf::init_experience;
3411 cf::init_anim; 3403 cf::init_anim;
3412 cf::init_attackmess; 3404 cf::init_attackmess;
3413 cf::init_dynamic; 3405 cf::init_dynamic;
3414 cf::init_block;
3415 3406
3416 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3407 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3417 3408
3418 # we must not ever block the main coroutine 3409 # we must not ever block the main coroutine
3419 local $Coro::idle = sub { 3410 local $Coro::idle = sub {
3555} 3546}
3556 3547
3557sub emergency_save() { 3548sub emergency_save() {
3558 my $freeze_guard = cf::freeze_mainloop; 3549 my $freeze_guard = cf::freeze_mainloop;
3559 3550
3560 warn "enter emergency perl save\n"; 3551 warn "emergency_perl_save: enter\n";
3561 3552
3562 cf::sync_job { 3553 cf::sync_job {
3554 # this is a trade-off: we want to be very quick here, so
3555 # save all maps without fsync, and later call a global sync
3556 # (which in turn might be very very slow)
3557 local $USE_FSYNC = 0;
3558
3563 # use a peculiar iteration method to avoid tripping on perl 3559 # use a peculiar iteration method to avoid tripping on perl
3564 # refcount bugs in for. also avoids problems with players 3560 # refcount bugs in for. also avoids problems with players
3565 # and maps saved/destroyed asynchronously. 3561 # and maps saved/destroyed asynchronously.
3566 warn "begin emergency player save\n"; 3562 warn "emergency_perl_save: begin player save\n";
3567 for my $login (keys %cf::PLAYER) { 3563 for my $login (keys %cf::PLAYER) {
3568 my $pl = $cf::PLAYER{$login} or next; 3564 my $pl = $cf::PLAYER{$login} or next;
3569 $pl->valid or next; 3565 $pl->valid or next;
3570 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3566 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3571 $pl->save; 3567 $pl->save;
3572 } 3568 }
3573 warn "end emergency player save\n"; 3569 warn "emergency_perl_save: end player save\n";
3574 3570
3575 warn "begin emergency map save\n"; 3571 warn "emergency_perl_save: begin map save\n";
3576 for my $path (keys %cf::MAP) { 3572 for my $path (keys %cf::MAP) {
3577 my $map = $cf::MAP{$path} or next; 3573 my $map = $cf::MAP{$path} or next;
3578 $map->valid or next; 3574 $map->valid or next;
3579 $map->save; 3575 $map->save;
3580 } 3576 }
3581 warn "end emergency map save\n"; 3577 warn "emergency_perl_save: end map save\n";
3582 3578
3583 warn "begin emergency database checkpoint\n"; 3579 warn "emergency_perl_save: begin database checkpoint\n";
3584 BDB::db_env_txn_checkpoint $DB_ENV; 3580 BDB::db_env_txn_checkpoint $DB_ENV;
3585 warn "end emergency database checkpoint\n"; 3581 warn "emergency_perl_save: end database checkpoint\n";
3586 3582
3587 warn "begin write uuid\n"; 3583 warn "emergency_perl_save: begin write uuid\n";
3588 write_uuid_sync 1; 3584 write_uuid_sync 1;
3589 warn "end write uuid\n"; 3585 warn "emergency_perl_save: end write uuid\n";
3590 }; 3586 };
3591 3587
3588 warn "emergency_perl_save: starting sync()\n";
3589 IO::AIO::aio_sync sub {
3590 warn "emergency_perl_save: finished sync()\n";
3591 };
3592
3592 warn "leave emergency perl save\n"; 3593 warn "emergency_perl_save: leave\n";
3593} 3594}
3594 3595
3595sub post_cleanup { 3596sub post_cleanup {
3596 my ($make_core) = @_; 3597 my ($make_core) = @_;
3597 3598
3634 warn "can only reload from main coroutine"; 3635 warn "can only reload from main coroutine";
3635 return; 3636 return;
3636 } 3637 }
3637 3638
3638 return if $RELOAD++; 3639 return if $RELOAD++;
3640
3641 my $t1 = EV::time;
3639 3642
3640 while ($RELOAD) { 3643 while ($RELOAD) {
3641 warn "reloading..."; 3644 warn "reloading...";
3642 3645
3643 warn "entering sync_job"; 3646 warn "entering sync_job";
3722 cf::reload_config; 3725 cf::reload_config;
3723 3726
3724 warn "loading extensions"; 3727 warn "loading extensions";
3725 cf::load_extensions; 3728 cf::load_extensions;
3726 3729
3730 if ($REATTACH_ON_RELOAD) {
3727 warn "reattaching attachments to objects/players"; 3731 warn "reattaching attachments to objects/players";
3728 _global_reattach; # objects, sockets 3732 _global_reattach; # objects, sockets
3729 warn "reattaching attachments to maps"; 3733 warn "reattaching attachments to maps";
3730 reattach $_ for values %MAP; 3734 reattach $_ for values %MAP;
3731 warn "reattaching attachments to players"; 3735 warn "reattaching attachments to players";
3732 reattach $_ for values %PLAYER; 3736 reattach $_ for values %PLAYER;
3737 }
3733 3738
3734 warn "running post_load"; 3739 warn "running post_init jobs";
3735 (pop @POST_INIT)->(1) while @POST_INIT; 3740 (pop @POST_INIT)->(1) while @POST_INIT;
3736 3741
3737 warn "leaving sync_job"; 3742 warn "leaving sync_job";
3738 3743
3739 1 3744 1
3743 }; 3748 };
3744 3749
3745 warn "reloaded"; 3750 warn "reloaded";
3746 --$RELOAD; 3751 --$RELOAD;
3747 } 3752 }
3753
3754 $t1 = EV::time - $t1;
3755 warn "reload completed in ${t1}s\n";
3748}; 3756};
3749 3757
3750our $RELOAD_WATCHER; # used only during reload 3758our $RELOAD_WATCHER; # used only during reload
3751 3759
3752sub reload_perl() { 3760sub reload_perl() {
3753 # doing reload synchronously and two reloads happen back-to-back, 3761 # doing reload synchronously and two reloads happen back-to-back,
3754 # coro crashes during coro_state_free->destroy here. 3762 # coro crashes during coro_state_free->destroy here.
3755 3763
3764 $RELOAD_WATCHER ||= cf::async {
3765 Coro::AIO::aio_wait cache_extensions;
3766
3756 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { 3767 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub {
3757 do_reload_perl; 3768 do_reload_perl;
3758 undef $RELOAD_WATCHER; 3769 undef $RELOAD_WATCHER;
3770 };
3759 }; 3771 };
3760} 3772}
3761 3773
3762register_command "reload" => sub { 3774register_command "reload" => sub {
3763 my ($who, $arg) = @_; 3775 my ($who, $arg) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines