… | |
… | |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
110 | |
111 | our %RESOURCE; |
111 | our %RESOURCE; |
112 | |
112 | |
|
|
113 | our $OUTPUT_RATE_MIN = 4000; |
|
|
114 | our $OUTPUT_RATE_MAX = 100000; |
|
|
115 | |
113 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
116 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
114 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
117 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
115 | our $NEXT_TICK; |
118 | our $NEXT_TICK; |
116 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
119 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
117 | |
120 | |
… | |
… | |
396 | } |
399 | } |
397 | |
400 | |
398 | =item cf::periodic $interval, $cb |
401 | =item cf::periodic $interval, $cb |
399 | |
402 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
403 | Like EV::periodic, but randomly selects a starting point so that the actions |
401 | get spread over timer. |
404 | get spread over time. |
402 | |
405 | |
403 | =cut |
406 | =cut |
404 | |
407 | |
405 | sub periodic($$) { |
408 | sub periodic($$) { |
406 | my ($interval, $cb) = @_; |
409 | my ($interval, $cb) = @_; |
… | |
… | |
503 | |
506 | |
504 | sub sync_job(&) { |
507 | sub sync_job(&) { |
505 | my ($job) = @_; |
508 | my ($job) = @_; |
506 | |
509 | |
507 | if ($Coro::current == $Coro::main) { |
510 | if ($Coro::current == $Coro::main) { |
508 | my $time = EV::time; |
511 | my $time = AE::time; |
509 | |
512 | |
510 | # this is the main coro, too bad, we have to block |
513 | # this is the main coro, too bad, we have to block |
511 | # till the operation succeeds, freezing the server :/ |
514 | # till the operation succeeds, freezing the server :/ |
512 | |
515 | |
513 | LOG llevError, Carp::longmess "sync job";#d# |
516 | LOG llevError, Carp::longmess "sync job";#d# |
… | |
… | |
530 | } else { |
533 | } else { |
531 | EV::loop EV::LOOP_ONESHOT; |
534 | EV::loop EV::LOOP_ONESHOT; |
532 | } |
535 | } |
533 | } |
536 | } |
534 | |
537 | |
535 | my $time = EV::time - $time; |
538 | my $time = AE::time - $time; |
536 | |
539 | |
537 | $TICK_START += $time; # do not account sync jobs to server load |
540 | $TICK_START += $time; # do not account sync jobs to server load |
538 | |
541 | |
539 | wantarray ? @res : $res[0] |
542 | wantarray ? @res : $res[0] |
540 | } else { |
543 | } else { |
… | |
… | |
584 | reset_signals; |
587 | reset_signals; |
585 | &$cb |
588 | &$cb |
586 | }, @args; |
589 | }, @args; |
587 | |
590 | |
588 | wantarray ? @res : $res[-1] |
591 | wantarray ? @res : $res[-1] |
|
|
592 | } |
|
|
593 | |
|
|
594 | sub objinfo { |
|
|
595 | ( |
|
|
596 | "counter value" => cf::object::object_count, |
|
|
597 | "objects created" => cf::object::create_count, |
|
|
598 | "objects destroyed" => cf::object::destroy_count, |
|
|
599 | "freelist size" => cf::object::free_count, |
|
|
600 | "allocated objects" => cf::object::objects_size, |
|
|
601 | "active objects" => cf::object::actives_size, |
|
|
602 | ) |
589 | } |
603 | } |
590 | |
604 | |
591 | =item $coin = coin_from_name $name |
605 | =item $coin = coin_from_name $name |
592 | |
606 | |
593 | =cut |
607 | =cut |
… | |
… | |
3584 | |
3598 | |
3585 | # install some emergency cleanup handlers |
3599 | # install some emergency cleanup handlers |
3586 | BEGIN { |
3600 | BEGIN { |
3587 | our %SIGWATCHER = (); |
3601 | our %SIGWATCHER = (); |
3588 | for my $signal (qw(INT HUP TERM)) { |
3602 | for my $signal (qw(INT HUP TERM)) { |
3589 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3603 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3590 | cf::cleanup "SIG$signal"; |
3604 | cf::cleanup "SIG$signal"; |
3591 | }; |
3605 | }; |
3592 | } |
3606 | } |
3593 | } |
3607 | } |
3594 | |
3608 | |
3595 | sub write_runtime_sync { |
3609 | sub write_runtime_sync { |
3596 | my $t0 = EV::time; |
3610 | my $t0 = AE::time; |
3597 | |
3611 | |
3598 | # first touch the runtime file to show we are still running: |
3612 | # first touch the runtime file to show we are still running: |
3599 | # the fsync below can take a very very long time. |
3613 | # the fsync below can take a very very long time. |
3600 | |
3614 | |
3601 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3615 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
… | |
… | |
3623 | or return; |
3637 | or return; |
3624 | |
3638 | |
3625 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3639 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3626 | and return; |
3640 | and return; |
3627 | |
3641 | |
3628 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3642 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3629 | |
3643 | |
3630 | 1 |
3644 | 1 |
3631 | } |
3645 | } |
3632 | |
3646 | |
3633 | our $uuid_lock; |
3647 | our $uuid_lock; |
… | |
… | |
3771 | return; |
3785 | return; |
3772 | } |
3786 | } |
3773 | |
3787 | |
3774 | return if $RELOAD++; |
3788 | return if $RELOAD++; |
3775 | |
3789 | |
3776 | my $t1 = EV::time; |
3790 | my $t1 = AE::time; |
3777 | |
3791 | |
3778 | while ($RELOAD) { |
3792 | while ($RELOAD) { |
3779 | warn "reloading..."; |
3793 | warn "reloading..."; |
3780 | |
3794 | |
3781 | warn "entering sync_job"; |
3795 | warn "entering sync_job"; |
… | |
… | |
3884 | |
3898 | |
3885 | warn "reloaded"; |
3899 | warn "reloaded"; |
3886 | --$RELOAD; |
3900 | --$RELOAD; |
3887 | } |
3901 | } |
3888 | |
3902 | |
3889 | $t1 = EV::time - $t1; |
3903 | $t1 = AE::time - $t1; |
3890 | warn "reload completed in ${t1}s\n"; |
3904 | warn "reload completed in ${t1}s\n"; |
3891 | }; |
3905 | }; |
3892 | |
3906 | |
3893 | our $RELOAD_WATCHER; # used only during reload |
3907 | our $RELOAD_WATCHER; # used only during reload |
3894 | |
3908 | |
… | |
… | |
3897 | # coro crashes during coro_state_free->destroy here. |
3911 | # coro crashes during coro_state_free->destroy here. |
3898 | |
3912 | |
3899 | $RELOAD_WATCHER ||= cf::async { |
3913 | $RELOAD_WATCHER ||= cf::async { |
3900 | Coro::AIO::aio_wait cache_extensions; |
3914 | Coro::AIO::aio_wait cache_extensions; |
3901 | |
3915 | |
3902 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3916 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3903 | do_reload_perl; |
3917 | do_reload_perl; |
3904 | undef $RELOAD_WATCHER; |
3918 | undef $RELOAD_WATCHER; |
3905 | }; |
3919 | }; |
3906 | }; |
3920 | }; |
3907 | } |
3921 | } |
… | |
… | |
3948 | return; |
3962 | return; |
3949 | } |
3963 | } |
3950 | |
3964 | |
3951 | cf::server_tick; # one server iteration |
3965 | cf::server_tick; # one server iteration |
3952 | |
3966 | |
3953 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3967 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3954 | |
3968 | |
3955 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3969 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3956 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3970 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3957 | Coro::async_pool { |
3971 | Coro::async_pool { |
3958 | $Coro::current->{desc} = "runtime saver"; |
3972 | $Coro::current->{desc} = "runtime saver"; |