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.449 by root, Sat Sep 20 00:09:27 2008 UTC vs.
Revision 1.458 by root, Sat Dec 13 20:34:37 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;
81our %EXTCMD = (); 82our %EXTCMD = ();
82our %EXTICMD = (); 83our %EXTICMD = ();
83our %EXT_CORO = (); # coroutines bound to extensions 84our %EXT_CORO = (); # coroutines bound to extensions
84our %EXT_MAP = (); # pluggable maps 85our %EXT_MAP = (); # pluggable maps
85 86
86our $RELOAD; # number of reloads so far 87our $RELOAD; # number of reloads so far, non-zero while in reload
87our @EVENT; 88our @EVENT;
88 89
89our $CONFDIR = confdir; 90our $CONFDIR = confdir;
90our $DATADIR = datadir; 91our $DATADIR = datadir;
91our $LIBDIR = "$DATADIR/ext"; 92our $LIBDIR = "$DATADIR/ext";
128our $LOAD; # a number between 0 (idle) and 1 (too many objects) 129our $LOAD; # a number between 0 (idle) and 1 (too many objects)
129our $LOADAVG; # same thing, but with alpha-smoothing 130our $LOADAVG; # same thing, but with alpha-smoothing
130our $JITTER; # average jitter 131our $JITTER; # average jitter
131our $TICK_START; # for load detecting purposes 132our $TICK_START; # for load detecting purposes
132 133
134our @POST_INIT;
135
136our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow)
137
133binmode STDOUT; 138binmode STDOUT;
134binmode STDERR; 139binmode STDERR;
135 140
136# read virtual server time, if available 141# read virtual server time, if available
137unless ($RUNTIME || !-e $RUNTIMEFILE) { 142unless ($RUNTIME || !-e $RUNTIMEFILE) {
305our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 310our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
306 311
307sub encode_json($) { $json_coder->encode ($_[0]) } 312sub encode_json($) { $json_coder->encode ($_[0]) }
308sub decode_json($) { $json_coder->decode ($_[0]) } 313sub decode_json($) { $json_coder->decode ($_[0]) }
309 314
315=item cf::post_init { BLOCK }
316
317Execute the given codeblock, I<after> all extensions have been (re-)loaded,
318but I<before> the server starts ticking again.
319
320The cdoeblock will have a single boolean argument to indicate whether this
321is a reload or not.
322
323=cut
324
325sub post_init(&) {
326 push @POST_INIT, shift;
327}
328
310=item cf::lock_wait $string 329=item cf::lock_wait $string
311 330
312Wait until the given lock is available. See cf::lock_acquire. 331Wait until the given lock is available. See cf::lock_acquire.
313 332
314=item my $lock = cf::lock_acquire $string 333=item my $lock = cf::lock_acquire $string
315 334
316Wait until the given lock is available and then acquires it and returns 335Wait until the given lock is available and then acquires it and returns
317a Coro::guard object. If the guard object gets destroyed (goes out of scope, 336a L<Guard> object. If the guard object gets destroyed (goes out of scope,
318for example when the coroutine gets canceled), the lock is automatically 337for example when the coroutine gets canceled), the lock is automatically
319returned. 338returned.
320 339
321Locks are *not* recursive, locking from the same coro twice results in a 340Locks are *not* recursive, locking from the same coro twice results in a
322deadlocked coro. 341deadlocked coro.
341 return;#d# 360 return;#d#
342 }#d# 361 }#d#
343 362
344 # wait for lock, if any 363 # wait for lock, if any
345 while ($LOCK{$key}) { 364 while ($LOCK{$key}) {
365 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
346 push @{ $LOCK{$key} }, $Coro::current; 366 push @{ $LOCK{$key} }, $Coro::current;
347 Coro::schedule; 367 Coro::schedule;
348 } 368 }
349} 369}
350 370
355 lock_wait $key; 375 lock_wait $key;
356 376
357 $LOCK{$key} = []; 377 $LOCK{$key} = [];
358 $LOCKER{$key} = $Coro::current;#d# 378 $LOCKER{$key} = $Coro::current;#d#
359 379
360 Coro::guard { 380 Guard::guard {
361 delete $LOCKER{$key};#d# 381 delete $LOCKER{$key};#d#
362 # wake up all waiters, to be on the safe side 382 # wake up all waiters, to be on the safe side
363 $_->ready for @{ delete $LOCK{$key} }; 383 $_->ready for @{ delete $LOCK{$key} };
364 } 384 }
365} 385}
371} 391}
372 392
373sub freeze_mainloop { 393sub freeze_mainloop {
374 tick_inhibit_inc; 394 tick_inhibit_inc;
375 395
376 Coro::guard \&tick_inhibit_dec; 396 &Guard::guard (\&tick_inhibit_dec);
377} 397}
378 398
379=item cf::periodic $interval, $cb 399=item cf::periodic $interval, $cb
380 400
381Like EV::periodic, but randomly selects a starting point so that the actions 401Like EV::periodic, but randomly selects a starting point so that the actions
1170 } else { 1190 } else {
1171 aio_unlink "$filename.pst"; 1191 aio_unlink "$filename.pst";
1172 } 1192 }
1173 1193
1174 aio_rename "$filename~", $filename; 1194 aio_rename "$filename~", $filename;
1195
1196 $filename =~ s%/[^/]+$%%;
1197 aio_pathsync $filename if $cf::USE_FSYNC;
1175 } else { 1198 } else {
1176 warn "FATAL: $filename~: $!\n"; 1199 warn "FATAL: $filename~: $!\n";
1177 } 1200 }
1178 } else { 1201 } else {
1179 aio_unlink $filename; 1202 aio_unlink $filename;
1272 my ($name, $cb) = @_; 1295 my ($name, $cb) = @_;
1273 1296
1274 $EXTICMD{$name} = $cb; 1297 $EXTICMD{$name} = $cb;
1275} 1298}
1276 1299
1300use File::Glob ();
1301
1277cf::player->attach ( 1302cf::player->attach (
1278 on_command => sub { 1303 on_command => sub {
1279 my ($pl, $name, $params) = @_; 1304 my ($pl, $name, $params) = @_;
1280 1305
1281 my $cb = $COMMAND{$name} 1306 my $cb = $COMMAND{$name}
1312 } 1337 }
1313 1338
1314 cf::override; 1339 cf::override;
1315 }, 1340 },
1316); 1341);
1342
1343# "readahead" all extensions
1344sub cache_extensions {
1345 my $grp = IO::AIO::aio_group;
1346
1347 add $grp IO::AIO::aio_readdir $LIBDIR, sub {
1348 for (grep /\.ext$/, @{$_[0]}) {
1349 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1350 }
1351 };
1352
1353 $grp
1354}
1317 1355
1318sub load_extensions { 1356sub load_extensions {
1319 cf::sync_job { 1357 cf::sync_job {
1320 my %todo; 1358 my %todo;
1321 1359
1444 1482
1445sub exists($) { 1483sub exists($) {
1446 my ($login) = @_; 1484 my ($login) = @_;
1447 1485
1448 $cf::PLAYER{$login} 1486 $cf::PLAYER{$login}
1449 or cf::sync_job { !aio_stat path $login } 1487 or !aio_stat path $login
1450} 1488}
1451 1489
1452sub find($) { 1490sub find($) {
1453 return $cf::PLAYER{$_[0]} || do { 1491 return $cf::PLAYER{$_[0]} || do {
1454 my $login = $_[0]; 1492 my $login = $_[0];
2771 id => "infobox", 2809 id => "infobox",
2772 title => "Body Parts", 2810 title => "Body Parts",
2773 reply => undef, 2811 reply => undef,
2774 tooltip => "Shows which body parts you posess and are available", 2812 tooltip => "Shows which body parts you posess and are available",
2775 }, 2813 },
2814 "c/skills" => {
2815 id => "infobox",
2816 title => "Skills",
2817 reply => undef,
2818 tooltip => "Shows your experience per skill and item power",
2819 },
2776 "c/uptime" => { 2820 "c/uptime" => {
2777 id => "infobox", 2821 id => "infobox",
2778 title => "Uptime", 2822 title => "Uptime",
2779 reply => undef, 2823 reply => undef,
2780 tooltip => "How long the server has been running since last restart", 2824 tooltip => "How long the server has been running since last restart",
3334 reload_treasures; 3378 reload_treasures;
3335 3379
3336 warn "finished reloading resource files\n"; 3380 warn "finished reloading resource files\n";
3337} 3381}
3338 3382
3339sub init {
3340 my $guard = freeze_mainloop;
3341
3342 evthread_start IO::AIO::poll_fileno;
3343
3344 reload_resources;
3345}
3346
3347sub reload_config { 3383sub reload_config {
3348 open my $fh, "<:utf8", "$CONFDIR/config" 3384 open my $fh, "<:utf8", "$CONFDIR/config"
3349 or return; 3385 or return;
3350 3386
3351 local $/; 3387 local $/;
3383 seek $fh, 0, 0; 3419 seek $fh, 0, 0;
3384 print $fh $$; 3420 print $fh $$;
3385} 3421}
3386 3422
3387sub main { 3423sub main {
3388 atomic; 3424 cf::init_globals; # initialise logging
3425
3426 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3427 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3428 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3429 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3430
3431 cf::init_experience;
3432 cf::init_anim;
3433 cf::init_attackmess;
3434 cf::init_dynamic;
3435 cf::init_block;
3436
3437 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3389 3438
3390 # we must not ever block the main coroutine 3439 # we must not ever block the main coroutine
3391 local $Coro::idle = sub { 3440 local $Coro::idle = sub {
3392 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3441 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3393 (async { 3442 (async {
3394 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3443 $Coro::current->{desc} = "IDLE BUG HANDLER";
3395 EV::loop EV::LOOP_ONESHOT; 3444 EV::loop EV::LOOP_ONESHOT;
3396 })->prio (Coro::PRIO_MAX); 3445 })->prio (Coro::PRIO_MAX);
3397 }; 3446 };
3398 3447
3399 { 3448 evthread_start IO::AIO::poll_fileno;
3400 my $guard = freeze_mainloop; 3449
3450 cf::sync_job {
3451 reload_resources;
3401 reload_config; 3452 reload_config;
3402 db_init; 3453 db_init;
3454
3455 cf::load_settings;
3456 cf::load_materials;
3457 cf::init_uuid;
3458 cf::init_signals;
3459 cf::init_commands;
3460 cf::init_skills;
3461
3462 cf::init_beforeplay;
3463
3464 atomic;
3465
3403 load_extensions; 3466 load_extensions;
3404 3467
3405 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3406 }
3407
3408 utime time, time, $RUNTIMEFILE; 3468 utime time, time, $RUNTIMEFILE;
3409 3469
3410 # no (long-running) fork's whatsoever before this point(!) 3470 # no (long-running) fork's whatsoever before this point(!)
3411 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3471 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3472
3473 (pop @POST_INIT)->(0) while @POST_INIT;
3474 };
3412 3475
3413 EV::loop; 3476 EV::loop;
3414} 3477}
3415 3478
3416############################################################################# 3479#############################################################################
3474 my $uuid = "$LOCALDIR/uuid"; 3537 my $uuid = "$LOCALDIR/uuid";
3475 3538
3476 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3539 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3477 or return; 3540 or return;
3478 3541
3479 my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; 3542 my $value = uuid_seq uuid_cur;
3543
3544 unless ($value) {
3545 warn "cowardly refusing to write zero uuid value!\n";
3546 return;
3547 }
3548
3549 my $value = uuid_str $value + $uuid_skip;
3480 $uuid_skip = 0; 3550 $uuid_skip = 0;
3481 3551
3482 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3552 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3483 and return; 3553 and return;
3484 3554
3506} 3576}
3507 3577
3508sub emergency_save() { 3578sub emergency_save() {
3509 my $freeze_guard = cf::freeze_mainloop; 3579 my $freeze_guard = cf::freeze_mainloop;
3510 3580
3511 warn "enter emergency perl save\n"; 3581 warn "emergency_perl_save: enter\n";
3512 3582
3513 cf::sync_job { 3583 cf::sync_job {
3584 # this is a trade-off: we want to be very quick here, so
3585 # save all maps without fsync, and later call a global sync
3586 # (which in turn might be very very slow)
3587 local $USE_FSYNC = 0;
3588
3514 # use a peculiar iteration method to avoid tripping on perl 3589 # use a peculiar iteration method to avoid tripping on perl
3515 # refcount bugs in for. also avoids problems with players 3590 # refcount bugs in for. also avoids problems with players
3516 # and maps saved/destroyed asynchronously. 3591 # and maps saved/destroyed asynchronously.
3517 warn "begin emergency player save\n"; 3592 warn "emergency_perl_save: begin player save\n";
3518 for my $login (keys %cf::PLAYER) { 3593 for my $login (keys %cf::PLAYER) {
3519 my $pl = $cf::PLAYER{$login} or next; 3594 my $pl = $cf::PLAYER{$login} or next;
3520 $pl->valid or next; 3595 $pl->valid or next;
3521 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3596 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3522 $pl->save; 3597 $pl->save;
3523 } 3598 }
3524 warn "end emergency player save\n"; 3599 warn "emergency_perl_save: end player save\n";
3525 3600
3526 warn "begin emergency map save\n"; 3601 warn "emergency_perl_save: begin map save\n";
3527 for my $path (keys %cf::MAP) { 3602 for my $path (keys %cf::MAP) {
3528 my $map = $cf::MAP{$path} or next; 3603 my $map = $cf::MAP{$path} or next;
3529 $map->valid or next; 3604 $map->valid or next;
3530 $map->save; 3605 $map->save;
3531 } 3606 }
3532 warn "end emergency map save\n"; 3607 warn "emergency_perl_save: end map save\n";
3533 3608
3534 warn "begin emergency database checkpoint\n"; 3609 warn "emergency_perl_save: begin database checkpoint\n";
3535 BDB::db_env_txn_checkpoint $DB_ENV; 3610 BDB::db_env_txn_checkpoint $DB_ENV;
3536 warn "end emergency database checkpoint\n"; 3611 warn "emergency_perl_save: end database checkpoint\n";
3537 3612
3538 warn "begin write uuid\n"; 3613 warn "emergency_perl_save: begin write uuid\n";
3539 write_uuid_sync 1; 3614 write_uuid_sync 1;
3540 warn "end write uuid\n"; 3615 warn "emergency_perl_save: end write uuid\n";
3541 }; 3616 };
3542 3617
3618 warn "emergency_perl_save: starting sync()\n";
3619 IO::AIO::aio_sync sub {
3620 warn "emergency_perl_save: finished sync()\n";
3621 };
3622
3543 warn "leave emergency perl save\n"; 3623 warn "emergency_perl_save: leave\n";
3544} 3624}
3545 3625
3546sub post_cleanup { 3626sub post_cleanup {
3547 my ($make_core) = @_; 3627 my ($make_core) = @_;
3548 3628
3574 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3654 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3575 for my $name (keys %$leaf_symtab) { 3655 for my $name (keys %$leaf_symtab) {
3576 _gv_clear *{"$pkg$name"}; 3656 _gv_clear *{"$pkg$name"};
3577# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3657# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3578 } 3658 }
3579 warn "cleared package #$pkg\n";#d# 3659 warn "cleared package $pkg\n";#d#
3580} 3660}
3581
3582our $RELOAD; # how many times to reload
3583 3661
3584sub do_reload_perl() { 3662sub do_reload_perl() {
3585 # can/must only be called in main 3663 # can/must only be called in main
3586 if ($Coro::current != $Coro::main) { 3664 if ($Coro::current != $Coro::main) {
3587 warn "can only reload from main coroutine"; 3665 warn "can only reload from main coroutine";
3588 return; 3666 return;
3589 } 3667 }
3590 3668
3591 return if $RELOAD++; 3669 return if $RELOAD++;
3670
3671 my $t1 = EV::time;
3592 3672
3593 while ($RELOAD) { 3673 while ($RELOAD) {
3594 warn "reloading..."; 3674 warn "reloading...";
3595 3675
3596 warn "entering sync_job"; 3676 warn "entering sync_job";
3675 cf::reload_config; 3755 cf::reload_config;
3676 3756
3677 warn "loading extensions"; 3757 warn "loading extensions";
3678 cf::load_extensions; 3758 cf::load_extensions;
3679 3759
3760 if ($REATTACH_ON_RELOAD) {
3680 warn "reattaching attachments to objects/players"; 3761 warn "reattaching attachments to objects/players";
3681 _global_reattach; # objects, sockets 3762 _global_reattach; # objects, sockets
3682 warn "reattaching attachments to maps"; 3763 warn "reattaching attachments to maps";
3683 reattach $_ for values %MAP; 3764 reattach $_ for values %MAP;
3684 warn "reattaching attachments to players"; 3765 warn "reattaching attachments to players";
3685 reattach $_ for values %PLAYER; 3766 reattach $_ for values %PLAYER;
3767 }
3768
3769 warn "running post_init jobs";
3770 (pop @POST_INIT)->(1) while @POST_INIT;
3686 3771
3687 warn "leaving sync_job"; 3772 warn "leaving sync_job";
3688 3773
3689 1 3774 1
3690 } or do { 3775 } or do {
3693 }; 3778 };
3694 3779
3695 warn "reloaded"; 3780 warn "reloaded";
3696 --$RELOAD; 3781 --$RELOAD;
3697 } 3782 }
3783
3784 $t1 = EV::time - $t1;
3785 warn "reload completed in ${t1}s\n";
3698}; 3786};
3699 3787
3700our $RELOAD_WATCHER; # used only during reload 3788our $RELOAD_WATCHER; # used only during reload
3701 3789
3702sub reload_perl() { 3790sub reload_perl() {
3703 # doing reload synchronously and two reloads happen back-to-back, 3791 # doing reload synchronously and two reloads happen back-to-back,
3704 # coro crashes during coro_state_free->destroy here. 3792 # coro crashes during coro_state_free->destroy here.
3705 3793
3794 $RELOAD_WATCHER ||= cf::async {
3795 Coro::AIO::aio_wait cache_extensions;
3796
3706 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3797 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub {
3707 do_reload_perl; 3798 do_reload_perl;
3708 undef $RELOAD_WATCHER; 3799 undef $RELOAD_WATCHER;
3800 };
3709 }; 3801 };
3710} 3802}
3711 3803
3712register_command "reload" => sub { 3804register_command "reload" => sub {
3713 my ($who, $arg) = @_; 3805 my ($who, $arg) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines