1 | #! perl |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # this extension swaps out maps and resets them, in essence managing |
3 | # this extension swaps out maps and resets them, in essence managing |
4 | # the reset/swap policy of the server. |
4 | # the reset/swap policy of the server. |
|
|
5 | # it also contains the map prefetching logic |
5 | |
6 | |
6 | our $DEACTIVATE_TIMEOUT = 20; # number of seconds after which maps get deactivated to save cpu |
7 | #our $DEACTIVATE_TIMEOUT = 20; # number of seconds after which maps get deactivated to save cpu |
7 | our $SWAP_TIMEOUT = 35; # number of seconds after which maps inactive get swapped out |
8 | our $SWAP_TIMEOUT = $cf::CFG{swap_timeout} || 300; # number of seconds after which inactive maps get swapped out |
8 | our $SCHEDULE_INTERVAL = 2; # time the map scheduler sleeps between runs |
9 | our $SCHEDULE_INTERVAL = $cf::CFG{schedule_interval} || 5; # time the map scheduler sleeps between runs |
9 | our $SAVE_TIMEOUT = 20; # save maps every n seconds |
10 | our $SAVE_TIMEOUT = $cf::CFG{save_timeout} || 30; # save maps every n seconds |
10 | our $SAVE_INTERVAL = 0.1; # save at max. one map every $SAVE_INTERVAL |
11 | our $SWAP_LOAD1 = $cf::CFG{swap_load1} || .1; # start aggressively swapping at this load |
|
|
12 | our $SWAP_LOAD2 = $cf::CFG{swap_load2} || .6; # swap as fast as possible at this load |
11 | |
13 | |
12 | cf::coro { |
14 | sub reload { |
|
|
15 | local $Coro::current->{desc} = "startup map scanner"; |
13 | $Coro::current->prio (Coro::PRIO_MIN); |
16 | $Coro::current->prio (Coro::PRIO_MIN); |
|
|
17 | |
14 | # load the header of swapped-out maps. |
18 | # load the header of swapped-out maps. |
15 | # this is not a correctness issue, it simply saves diskspace |
19 | # this is not a correctness issue, it simply saves diskspace |
16 | # because old files will get cleaned up on reset time |
20 | # because old files will get cleaned up on reset time |
17 | Coro::Timer::sleep 5; |
|
|
18 | |
21 | |
19 | my $files = Coro::AIO::aio_readdir cf::localdir . "/" . cf::tmpdir; |
22 | for my $path (@{ cf::map::tmp_maps or [] }, @{ cf::map::random_maps or [] }) { |
20 | |
|
|
21 | for my $map (@$files) { |
|
|
22 | utf8::decode $map; |
|
|
23 | next if $map =~ /\.(?:pst|meta)$/; |
|
|
24 | $map =~ s/∕/\//g; |
|
|
25 | cf::map::find $map; |
23 | cf::map::find $path; |
26 | Coro::Timer::sleep 0.05; |
|
|
27 | } |
24 | } |
28 | |
25 | |
29 | #TODO: should also preload random maps... |
26 | # now hunt for resettable per-player maps |
30 | }; |
27 | for my $login (@{ cf::player::list_logins or [] }) { |
|
|
28 | for my $path (@{ cf::player::maps $login or [] }) { |
|
|
29 | $path =~ /^~[^\/]+(\/.*)$/ |
|
|
30 | or next; # doh |
31 | |
31 | |
32 | our $SCHEDULER = cf::coro { |
32 | my $base = cf::map::find $1; |
33 | while () { |
|
|
34 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
|
|
35 | |
33 | |
36 | # this weird form of iteration over values is used because |
34 | # skip maps without base maps on the assumption |
37 | # the hash changes underneath us frequently, and for |
35 | # that those are old, unresettable maps |
38 | # keeps a direct reference to the value without (in 5.8 perls) |
36 | next unless $base; |
39 | # keeping a reference, so this is prone to crashes or worse. |
|
|
40 | my @maps = keys %cf::MAP; |
|
|
41 | for (@maps) { |
|
|
42 | my $map = $cf::MAP{$_} |
|
|
43 | or next; |
|
|
44 | $map->valid or next; |
|
|
45 | |
37 | |
|
|
38 | # skip unresettable maps, for speed |
|
|
39 | next if $base->{deny_reset}; |
|
|
40 | |
|
|
41 | my $map = cf::map::find $path; |
|
|
42 | |
|
|
43 | if ($map->{deny_reset}) { |
|
|
44 | warn "found noreset map with resettable base map, resetting: $path\n"; |
|
|
45 | delete $map->{deny_reset}; |
|
|
46 | } |
|
|
47 | } |
|
|
48 | } |
|
|
49 | } |
|
|
50 | |
|
|
51 | cf::post_init { |
|
|
52 | cf::async { reload }; |
|
|
53 | |
|
|
54 | our $SCHEDULER = cf::async_ext { |
|
|
55 | $Coro::current->{desc} = "map scheduler"; |
|
|
56 | $Coro::current->prio (Coro::PRIO_MAX); |
|
|
57 | |
|
|
58 | while () { |
|
|
59 | if ($cf::LOADAVG > $SWAP_LOAD2) { |
|
|
60 | cf::wait_for_tick; |
|
|
61 | } else { |
|
|
62 | Coro::AnyEvent::sleep $SCHEDULE_INTERVAL; |
|
|
63 | } |
|
|
64 | |
|
|
65 | # this weird form of iteration over values is used because |
|
|
66 | # the hash changes underneath us frequently, and 'for' |
|
|
67 | # keeps a direct reference to the value without (in 5.8 perls) |
|
|
68 | # keeping a reference, so this is prone to crashes or worse. |
|
|
69 | my @maps = keys %cf::MAP; |
|
|
70 | for (@maps) { |
|
|
71 | my $map = $cf::MAP{$_} |
|
|
72 | or next; |
|
|
73 | $map->valid or next; |
|
|
74 | |
46 | eval { |
75 | eval { |
47 | # not yet, because maps might become visible to players nearby |
76 | # not yet, because maps might become visible to players nearby |
48 | # we need to remove the map from %cf::MAP and all tiled map links |
77 | # we need to remove the map from %cf::MAP and all tiled map links |
49 | # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { |
78 | # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { |
50 | # $map->deactivate; |
79 | # $map->deactivate; |
51 | # delete $map->{active}; |
80 | # delete $map->{active}; |
52 | # } |
81 | # } |
53 | if ($map->should_reset) { |
82 | if ($map->should_reset) { |
54 | $map->reset; |
83 | $map->reset; |
55 | } elsif ($map->in_memory == cf::MAP_IN_MEMORY) { |
84 | } elsif ($map->linkable) { |
|
|
85 | my $max_idle = cf::clamp +(cf::lerp $cf::LOADAVG, $SWAP_LOAD1, $SWAP_LOAD2, $SWAP_TIMEOUT, $cf::TICK * 1.5), |
|
|
86 | $cf::TICK * 1.5, $SWAP_TIMEOUT; |
|
|
87 | |
56 | if ($map->last_access + $SWAP_TIMEOUT <= $cf::RUNTIME && !$map->players) { |
88 | if ($map->last_access + $max_idle <= $cf::RUNTIME && !$map->players) { |
57 | $map->swap_out; |
89 | $map->swap_out; |
58 | Coro::Timer::sleep $SAVE_INTERVAL; |
|
|
59 | } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
90 | } elsif ($map->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
60 | $map->save; |
91 | $map->save; |
61 | Coro::Timer::sleep $SAVE_INTERVAL; |
92 | $map->{last_save} -= cf::rndm; # randomise map save times a bit |
|
|
93 | } |
62 | } |
94 | } |
63 | } |
95 | }; |
|
|
96 | warn $@ if $@; |
|
|
97 | cf::cede_to_tick; |
64 | }; |
98 | }; |
65 | warn $@ if $@; |
|
|
66 | Coro::cede; |
|
|
67 | }; |
99 | } |
68 | } |
100 | }; |
|
|
101 | |
|
|
102 | $SCHEDULER->prio (-2); |
69 | }; |
103 | }; |
70 | |
104 | |
71 | $SCHEDULER->prio (-2); |
105 | # purely for debugging, will load ALL maps |
|
|
106 | sub loadall { |
|
|
107 | my $maps = cf::map::static_maps; |
72 | |
108 | |
73 | # map load prefetch |
109 | for my $path (@$maps) { |
74 | cf::map->attach ( |
110 | my $map = cf::map::find $path |
75 | on_enter => sub { |
111 | or warn "ERROR: unable to find map $path\n"; |
76 | my ($map, $pl) = @_; |
112 | $map->load; |
|
|
113 | } |
|
|
114 | } |
77 | |
115 | |
78 | # floodfill surrounging maps, asynchronously |
|
|
79 | }, |
|
|
80 | ); |
|
|
81 | |
|
|