… | |
… | |
7 | use Errno (); |
7 | use Errno (); |
8 | use Time::HiRes; |
8 | use Time::HiRes; |
9 | use Fcntl; |
9 | use Fcntl; |
10 | use IO::AIO; |
10 | use IO::AIO; |
11 | |
11 | |
|
|
12 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
13 | poll => 'r', |
|
|
14 | cb => \&IO::AIO::poll_cb); |
|
|
15 | |
12 | # find all potential exit paths, this is slow, so this info is cached |
16 | # find all potential exit paths, this is slow, so this info is cached |
13 | sub find_exits { |
17 | sub find_exits { |
14 | my ($map) = @_; |
18 | my ($map) = @_; |
15 | |
19 | |
16 | my %exit; |
20 | my %exit; |
17 | |
21 | |
|
|
22 | # normal exits |
18 | for my $x (0 .. $map->width - 1) { |
23 | for my $x (0 .. $map->width - 1) { |
19 | for my $y (0 .. $map->height - 1) { |
24 | for my $y (0 .. $map->height - 1) { |
20 | for (grep $_->type == 66, $map->at ($x, $y)) { |
25 | for (grep $_->type == 66, $map->at ($x, $y)) { |
21 | my $path = $_->slaying; |
26 | my $path = $_->slaying; |
22 | |
27 | |
23 | next if 3 > length $path; |
28 | next if 3 > length $path; |
24 | |
29 | |
|
|
30 | # TODO: improve unique exit detection etc. |
25 | $path = cf::maps_directory cf::path_combine_and_normalize $map->path, $path; |
31 | $exit{cf::maps_directory cf::path_combine_and_normalize $map->path, $path}++; |
26 | |
|
|
27 | $exit{$path}++; |
|
|
28 | } |
32 | } |
29 | } |
33 | } |
|
|
34 | } |
|
|
35 | |
|
|
36 | # tiled maps |
|
|
37 | for (0..3) { |
|
|
38 | my $path = $map->tile_path ($_) |
|
|
39 | or next; |
|
|
40 | |
|
|
41 | $exit{cf::maps_directory cf::path_combine_and_normalize $map->path, $path}++; |
30 | } |
42 | } |
31 | |
43 | |
32 | [keys %exit] |
44 | [keys %exit] |
33 | } |
45 | } |
34 | |
46 | |
|
|
47 | my $PREFETCHING; |
35 | my @PREFETCH; |
48 | my @PREFETCH; |
36 | my %MAP_TIMEOUT; |
49 | my %FILE_TIMEOUT; |
37 | |
50 | |
38 | sub prefetch; |
51 | sub _prefetch; |
|
|
52 | |
|
|
53 | my $empty_cb = sub { }; |
|
|
54 | |
|
|
55 | sub load_file { |
|
|
56 | my ($path, $cb) = @_; |
|
|
57 | |
|
|
58 | my $NOW = Time::HiRes::time; |
|
|
59 | |
|
|
60 | aio_open $path, O_RDONLY, 0, sub { |
|
|
61 | my $fh = shift |
|
|
62 | or return $cb->(), _prefetch; |
|
|
63 | |
|
|
64 | aio_readahead $fh, 0, -s $fh, sub { |
|
|
65 | my $time = Time::HiRes::time - $NOW; |
|
|
66 | warn "LONG PREFETCH $path $time\n" if $time > 0.3; |
|
|
67 | |
|
|
68 | $cb->(), _prefetch; |
|
|
69 | }; |
|
|
70 | }; |
|
|
71 | } |
|
|
72 | |
|
|
73 | sub prefetch($$;$) { |
|
|
74 | my ($type, $path, $cb) = @_; |
|
|
75 | |
|
|
76 | push @PREFETCH, [$type, $path, $cb || $empty_cb]; |
|
|
77 | _prefetch unless $PREFETCHING; |
|
|
78 | } |
|
|
79 | |
39 | sub prefetch { |
80 | sub _prefetch { |
40 | while (my $path = shift @PREFETCH) { |
81 | $PREFETCHING = 1; |
|
|
82 | |
|
|
83 | while (@PREFETCH) { |
|
|
84 | my ($type, $path, $cb) = @{ shift @PREFETCH }; |
|
|
85 | |
41 | my $NOW = Time::HiRes::time; |
86 | my $NOW = Time::HiRes::time; |
|
|
87 | $cb->(), next if $FILE_TIMEOUT{$path} > $NOW; |
|
|
88 | $FILE_TIMEOUT{$path} = $NOW + 60 + rand 60; |
42 | |
89 | |
43 | next if $MAP_TIMEOUT{$path} > $NOW; |
90 | if ($type eq "map") { |
|
|
91 | if (my $map = cf::map::has_been_loaded $path) { |
|
|
92 | $cb->(), next if $map->in_memory == cf::MAP_IN_MEMORY; |
44 | |
93 | |
45 | $MAP_TIMEOUT{$path} = $NOW + 60 + rand 60; |
94 | prefetch file => $map->tmppath |
46 | |
|
|
47 | if (my $map = cf::map::has_been_loaded $path) { |
|
|
48 | next if $map->in_memory == cf::MAP_IN_MEMORY; |
|
|
49 | |
|
|
50 | $path = $map->tmppath if $map->in_memory == cf::MAP_SWAPPED; |
95 | if $map->in_memory == cf::MAP_SWAPPED; |
|
|
96 | } |
51 | } |
97 | } |
52 | |
98 | |
53 | aio_open $path, O_RDONLY, 0, sub { |
99 | load_file $path, $cb; |
54 | my $fh = shift |
|
|
55 | or return; |
100 | return; |
56 | aio_readahead $fh, 0, -s $fh, sub { |
101 | } |
57 | my $time = Time::HiRes::time - $NOW; |
|
|
58 | warn "LONG PREFETCH $path $time\n" if $time > 0.3; |
|
|
59 | |
102 | |
60 | prefetch; |
103 | $PREFETCHING = 0; |
61 | }; |
|
|
62 | }; |
|
|
63 | |
|
|
64 | last; |
|
|
65 | } |
|
|
66 | } |
104 | } |
67 | |
105 | |
68 | my %MAP_EXITS; |
106 | my %MAP_EXITS; |
69 | |
107 | |
|
|
108 | sub prefetch_map { |
|
|
109 | my ($map) = @_; |
|
|
110 | |
|
|
111 | my $exit = $MAP_EXITS{$map->path} ||= find_exits $map; |
|
|
112 | prefetch map => $_ for @$exit; |
|
|
113 | } |
|
|
114 | |
70 | sub on_mapenter { |
115 | sub on_mapenter { |
71 | my ($ob) = @_; |
116 | my ($ob) = @_; |
72 | |
117 | |
73 | my $exit = $MAP_EXITS{$ob->map->path} ||= find_exits $ob->map; |
118 | prefetch_map $ob->map; |
74 | |
|
|
75 | push @PREFETCH, @$exit; |
|
|
76 | prefetch; |
|
|
77 | } |
119 | } |
78 | |
120 | |
79 | sub on_clock { |
121 | # prefetch a few players/second |
80 | # boy how I hate polling |
122 | { |
81 | IO::AIO::poll_cb; |
123 | my @players; |
|
|
124 | |
|
|
125 | Event->timer (interval => 0.2, cb => sub { |
|
|
126 | @players = map $_->ob->name, cf::player::list unless @players; |
|
|
127 | my $player = cf::player::find pop @players |
|
|
128 | or return; |
|
|
129 | |
|
|
130 | prefetch_map $player->ob->map; |
|
|
131 | prefetch map => +($player->get_savebed)[0]; |
|
|
132 | }); |
82 | } |
133 | } |
83 | |
134 | |
|
|
135 | # prefetch all .pl files every few minutes (thats only a "few" megabytes) |
|
|
136 | Event->timer (after => 1, interval => 600, cb => sub { |
|
|
137 | my $playerdir = cf::localdir . "/" . cf::playerdir; |
84 | |
138 | |
|
|
139 | aio_readdir $playerdir, sub { |
|
|
140 | my ($players) = @_; |
|
|
141 | my $prefetch; $prefetch = sub { |
|
|
142 | my $player = pop @$players |
|
|
143 | or return; |
|
|
144 | |
|
|
145 | load_file "$playerdir/$player/$player.pl", $prefetch; |
|
|
146 | }; |
|
|
147 | |
|
|
148 | $prefetch->(); $prefetch->(); |
|
|
149 | }; |
|
|
150 | }); |
|
|
151 | |
|
|
152 | |
|
|
153 | |