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.532 by root, Thu Apr 29 07:32:34 2010 UTC vs.
Revision 1.540 by root, Tue May 4 23:32:29 2010 UTC

20# The authors can be reached via e-mail to <support@deliantra.net> 20# The authors can be reached via e-mail to <support@deliantra.net>
21# 21#
22 22
23package cf; 23package cf;
24 24
25use 5.10.0; 25use common::sense;
26use utf8;
27use strict qw(vars subs);
28 26
29use Symbol; 27use Symbol;
30use List::Util; 28use List::Util;
31use Socket; 29use Socket;
32use EV; 30use EV;
78# strictly for debugging 76# strictly for debugging
79$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; 77$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" };
80 78
81sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 79sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
82 80
81our @ORIG_INC;
82
83our %COMMAND = (); 83our %COMMAND = ();
84our %COMMAND_TIME = (); 84our %COMMAND_TIME = ();
85 85
86our @EXTS = (); # list of extension package names 86our @EXTS = (); # list of extension package names
87our %EXTCMD = (); 87our %EXTCMD = ();
518=cut 518=cut
519 519
520sub sync_job(&) { 520sub sync_job(&) {
521 my ($job) = @_; 521 my ($job) = @_;
522 522
523 if ($Coro::current == $Coro::main) { 523 if (in_main) {
524 my $time = AE::time; 524 my $time = AE::time;
525 525
526 # this is the main coro, too bad, we have to block 526 # this is the main coro, too bad, we have to block
527 # till the operation succeeds, freezing the server :/ 527 # till the operation succeeds, freezing the server :/
528 528
529 LOG llevError, Carp::longmess "sync job";#d# 529 #LOG llevError, Carp::longmess "sync job";#d#
530 530
531 my $freeze_guard = freeze_mainloop; 531 my $freeze_guard = freeze_mainloop;
532 532
533 my $busy = 1; 533 my $busy = 1;
534 my @res; 534 my @res;
657within each server. 657within each server.
658 658
659=cut 659=cut
660 660
661sub db_table($) { 661sub db_table($) {
662 cf::error "db_get called from main context"
663 if $Coro::current == $Coro::main;
664
662 my ($name) = @_; 665 my ($name) = @_;
663 my $db = BDB::db_create $DB_ENV; 666 my $db = BDB::db_create $DB_ENV;
664 667
665 eval { 668 eval {
666 $db->set_flags (BDB::CHKSUM); 669 $db->set_flags (BDB::CHKSUM);
676} 679}
677 680
678our $DB; 681our $DB;
679 682
680sub db_init { 683sub db_init {
681 cf::sync_job {
682 $DB ||= db_table "db"; 684 $DB ||= db_table "db";
683 };
684} 685}
685 686
686sub db_get($$) { 687sub db_get($$) {
687 my $key = "$_[0]/$_[1]"; 688 my $key = "$_[0]/$_[1]";
688 689
689 cf::sync_job { 690 cf::error "db_get called from main context"
691 if $Coro::current == $Coro::main;
692
690 BDB::db_get $DB, undef, $key, my $data; 693 BDB::db_get $DB, undef, $key, my $data;
691 694
692 $! ? () 695 $! ? ()
693 : $data 696 : $data
694 }
695} 697}
696 698
697sub db_put($$$) { 699sub db_put($$$) {
698 BDB::dbreq_pri 4; 700 BDB::dbreq_pri 4;
699 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; 701 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { };
1421 1423
1422 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } 1424 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 }
1423 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; 1425 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m;
1424 1426
1425 $ext{source} = 1427 $ext{source} =
1426 "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" 1428 "package $pkg; use common::sense;\n"
1427 . "#line 1 \"$path\"\n{\n" 1429 . "#line 1 \"$path\"\n{\n"
1428 . $source 1430 . $source
1429 . "\n};\n1"; 1431 . "\n};\n1";
1430 1432
1431 $todo{$base} = \%ext; 1433 $todo{$base} = \%ext;
1449 1451
1450 my $active = eval $v->{source}; 1452 my $active = eval $v->{source};
1451 1453
1452 if (length $@) { 1454 if (length $@) {
1453 error "$v->{path}: $@\n"; 1455 error "$v->{path}: $@\n";
1456 undef $@; # work around perl 5.10.0 utf-8 caching bug
1454 1457
1455 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1458 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1456 if exists $v->{meta}{mandatory}; 1459 if exists $v->{meta}{mandatory};
1457 1460
1458 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1461 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1846 1849
1847sub register { 1850sub register {
1848 my (undef, $regex, $prio) = @_; 1851 my (undef, $regex, $prio) = @_;
1849 my $pkg = caller; 1852 my $pkg = caller;
1850 1853
1851 no strict;
1852 push @{"$pkg\::ISA"}, __PACKAGE__; 1854 push @{"$pkg\::ISA"}, __PACKAGE__;
1853 1855
1854 $EXT_MAP{$pkg} = [$prio, qr<$regex>]; 1856 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1855} 1857}
1856 1858
2190} 2192}
2191 2193
2192sub find_sync { 2194sub find_sync {
2193 my ($path, $origin) = @_; 2195 my ($path, $origin) = @_;
2194 2196
2195 cf::sync_job { find $path, $origin } 2197 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2198 if $Coro::current == $Coro::main;
2199
2200 find $path, $origin
2196} 2201}
2197 2202
2198sub do_load_sync { 2203sub do_load_sync {
2199 my ($map) = @_; 2204 my ($map) = @_;
2200 2205
2201 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2206 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2202 if $Coro::current == $Coro::main; 2207 if $Coro::current == $Coro::main;
2203 2208
2204 cf::sync_job { $map->load }; 2209 $map->load;
2205} 2210}
2206 2211
2207our %MAP_PREFETCH; 2212our %MAP_PREFETCH;
2208our $MAP_PREFETCHER = undef; 2213our $MAP_PREFETCHER = undef;
2209 2214
3268 decrease split destroy change_exp value msg lore send_msg)], 3273 decrease split destroy change_exp value msg lore send_msg)],
3269 ["cf::object::player" => qw(player)], 3274 ["cf::object::player" => qw(player)],
3270 ["cf::player" => qw(peaceful send_msg)], 3275 ["cf::player" => qw(peaceful send_msg)],
3271 ["cf::map" => qw(trigger)], 3276 ["cf::map" => qw(trigger)],
3272) { 3277) {
3273 no strict 'refs';
3274 my ($pkg, @funs) = @$_; 3278 my ($pkg, @funs) = @$_;
3275 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3279 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3276 for @funs; 3280 for @funs;
3277} 3281}
3278 3282
3354=cut 3358=cut
3355 3359
3356############################################################################# 3360#############################################################################
3357# the server's init and main functions 3361# the server's init and main functions
3358 3362
3363# async inc loader. yay.
3364sub inc_loader {
3365 my $mod = $_[1];
3366
3367 if (in_main && !tick_inhibit) {
3368 Carp::cluck "ERROR: attempted synchronous perl module load ($mod)";
3369 } else {
3370 debug "loading perl module $mod\n";
3371 }
3372
3373 # 1. find real file
3374 for my $dir (@ORIG_INC) {
3375 warn "$dir/$mod\n";#d#
3376 ref $dir and next;
3377 0 <= Coro::AIO::aio_load "$dir/$mod", my $data
3378 or next;
3379
3380 $data = "#line 1 $dir/$mod\n$data";
3381
3382 open my $fh, "<", \$data or die;
3383
3384 return $fh;
3385 }
3386
3387 ()
3388}
3389
3390sub init_inc {
3391 # save original @INC
3392 @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC;
3393
3394 # make sure we can do scalar-opens
3395 open my $dummy, "<", \my $dummy2;
3396
3397 # execute some stuff so perl load's some of the core modules
3398 /Ü/ =~ /ü/i;
3399 eval { &Storable::nstore_fd };
3400
3401 @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc.
3402
3403 debug "module loading will be asynchronous from this point on.";
3404}
3405
3359sub load_facedata($) { 3406sub load_facedata($) {
3360 my ($path) = @_; 3407 my ($path) = @_;
3361 3408
3362 # HACK to clear player env face cache, we need some signal framework 3409 # HACK to clear player env face cache, we need some signal framework
3363 # for this (global event?) 3410 # for this (global event?)
3586 3633
3587sub main { 3634sub main {
3588 cf::init_globals; # initialise logging 3635 cf::init_globals; # initialise logging
3589 3636
3590 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3637 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3591 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3638 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3592 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3639 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3593 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3640 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3594 3641
3595 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3642 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3596 3643
3604 }; 3651 };
3605 3652
3606 evthread_start IO::AIO::poll_fileno; 3653 evthread_start IO::AIO::poll_fileno;
3607 3654
3608 cf::sync_job { 3655 cf::sync_job {
3656 init_inc;
3657
3609 cf::init_experience; 3658 cf::init_experience;
3610 cf::init_anim; 3659 cf::init_anim;
3611 cf::init_attackmess; 3660 cf::init_attackmess;
3612 cf::init_dynamic; 3661 cf::init_dynamic;
3613 3662
3638 }; 3687 };
3639 3688
3640 cf::object::thawer::errors_are_fatal 0; 3689 cf::object::thawer::errors_are_fatal 0;
3641 info "parse errors in files are no longer fatal from this point on.\n"; 3690 info "parse errors in files are no longer fatal from this point on.\n";
3642 3691
3692 my $free_main; $free_main = EV::idle sub {
3693 undef $free_main;
3694 undef &main; # free gobs of memory :)
3695 };
3696
3643 main_loop; 3697 goto &main_loop;
3644} 3698}
3645 3699
3646############################################################################# 3700#############################################################################
3647# initialisation and cleanup 3701# initialisation and cleanup
3648 3702
3747sub emergency_save() { 3801sub emergency_save() {
3748 my $freeze_guard = cf::freeze_mainloop; 3802 my $freeze_guard = cf::freeze_mainloop;
3749 3803
3750 info "emergency_perl_save: enter\n"; 3804 info "emergency_perl_save: enter\n";
3751 3805
3806 # this is a trade-off: we want to be very quick here, so
3807 # save all maps without fsync, and later call a global sync
3808 # (which in turn might be very very slow)
3809 local $USE_FSYNC = 0;
3810
3752 cf::sync_job { 3811 cf::sync_job {
3753 # this is a trade-off: we want to be very quick here, so 3812 cf::write_runtime_sync; # external watchdog should not bark
3754 # save all maps without fsync, and later call a global sync
3755 # (which in turn might be very very slow)
3756 local $USE_FSYNC = 0;
3757 3813
3758 # use a peculiar iteration method to avoid tripping on perl 3814 # use a peculiar iteration method to avoid tripping on perl
3759 # refcount bugs in for. also avoids problems with players 3815 # refcount bugs in for. also avoids problems with players
3760 # and maps saved/destroyed asynchronously. 3816 # and maps saved/destroyed asynchronously.
3761 info "emergency_perl_save: begin player save\n"; 3817 info "emergency_perl_save: begin player save\n";
3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3821 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3766 $pl->save; 3822 $pl->save;
3767 } 3823 }
3768 info "emergency_perl_save: end player save\n"; 3824 info "emergency_perl_save: end player save\n";
3769 3825
3826 cf::write_runtime_sync; # external watchdog should not bark
3827
3770 info "emergency_perl_save: begin map save\n"; 3828 info "emergency_perl_save: begin map save\n";
3771 for my $path (keys %cf::MAP) { 3829 for my $path (keys %cf::MAP) {
3772 my $map = $cf::MAP{$path} or next; 3830 my $map = $cf::MAP{$path} or next;
3773 $map->valid or next; 3831 $map->valid or next;
3774 $map->save; 3832 $map->save;
3775 } 3833 }
3776 info "emergency_perl_save: end map save\n"; 3834 info "emergency_perl_save: end map save\n";
3777 3835
3836 cf::write_runtime_sync; # external watchdog should not bark
3837
3778 info "emergency_perl_save: begin database checkpoint\n"; 3838 info "emergency_perl_save: begin database checkpoint\n";
3779 BDB::db_env_txn_checkpoint $DB_ENV; 3839 BDB::db_env_txn_checkpoint $DB_ENV;
3780 info "emergency_perl_save: end database checkpoint\n"; 3840 info "emergency_perl_save: end database checkpoint\n";
3781 3841
3782 info "emergency_perl_save: begin write uuid\n"; 3842 info "emergency_perl_save: begin write uuid\n";
3783 write_uuid_sync 1; 3843 write_uuid_sync 1;
3784 info "emergency_perl_save: end write uuid\n"; 3844 info "emergency_perl_save: end write uuid\n";
3785 };
3786 3845
3846 cf::write_runtime_sync; # external watchdog should not bark
3847
3848 trace "emergency_perl_save: syncing database to disk";
3849 BDB::db_env_txn_checkpoint $DB_ENV;
3850
3787 info "emergency_perl_save: starting sync()\n"; 3851 info "emergency_perl_save: starting sync\n";
3788 IO::AIO::aio_sync sub { 3852 IO::AIO::aio_sync sub {
3789 info "emergency_perl_save: finished sync()\n"; 3853 info "emergency_perl_save: finished sync\n";
3854 };
3855
3856 cf::write_runtime_sync; # external watchdog should not bark
3857
3858 trace "emergency_perl_save: flushing outstanding aio requests";
3859 while (IO::AIO::nreqs || BDB::nreqs) {
3860 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3861 }
3862
3863 cf::write_runtime_sync; # external watchdog should not bark
3790 }; 3864 };
3791 3865
3792 info "emergency_perl_save: leave\n"; 3866 info "emergency_perl_save: leave\n";
3793} 3867}
3794 3868
3795sub post_cleanup { 3869sub post_cleanup {
3796 my ($make_core) = @_; 3870 my ($make_core) = @_;
3871
3872 IO::AIO::flush;
3797 3873
3798 error Carp::longmess "post_cleanup backtrace" 3874 error Carp::longmess "post_cleanup backtrace"
3799 if $make_core; 3875 if $make_core;
3800 3876
3801 my $fh = pidfile; 3877 my $fh = pidfile;
3842 info "reloading..."; 3918 info "reloading...";
3843 3919
3844 trace "entering sync_job"; 3920 trace "entering sync_job";
3845 3921
3846 cf::sync_job { 3922 cf::sync_job {
3847 cf::write_runtime_sync; # external watchdog should not bark
3848 cf::emergency_save; 3923 cf::emergency_save;
3849 cf::write_runtime_sync; # external watchdog should not bark
3850
3851 trace "syncing database to disk";
3852 BDB::db_env_txn_checkpoint $DB_ENV;
3853
3854 # if anything goes wrong in here, we should simply crash as we already saved
3855
3856 trace "flushing outstanding aio requests";
3857 while (IO::AIO::nreqs || BDB::nreqs) {
3858 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3859 }
3860 3924
3861 trace "cancelling all extension coros"; 3925 trace "cancelling all extension coros";
3862 $_->cancel for values %EXT_CORO; 3926 $_->cancel for values %EXT_CORO;
3863 %EXT_CORO = (); 3927 %EXT_CORO = ();
3864 3928
3979 reload_perl; 4043 reload_perl;
3980 }; 4044 };
3981 } 4045 }
3982}; 4046};
3983 4047
3984unshift @INC, $LIBDIR; 4048#############################################################################
3985 4049
3986my $bug_warning = 0; 4050my $bug_warning = 0;
3987 4051
3988our @WAIT_FOR_TICK; 4052our @WAIT_FOR_TICK;
3989our @WAIT_FOR_TICK_BEGIN; 4053our @WAIT_FOR_TICK_BEGIN;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines