ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/map-prefetch.ext
(Generate patch)

Comparing deliantra/maps/perl/map-prefetch.ext (file contents):
Revision 1.5 by root, Tue Jun 27 18:22:14 2006 UTC vs.
Revision 1.9 by root, Wed Aug 2 11:17:08 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines