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.3 by root, Mon Jun 26 15:43:24 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;
49my @PREFETCH;
50my %FILE_TIMEOUT;
51
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
81sub _prefetch {
82 $PREFETCHING = 1;
83
84 while (@PREFETCH) {
85 my ($type, $path, $cb) = @{ shift @PREFETCH };
86
87 my $NOW = Time::HiRes::time;
88 $cb->(), next if $FILE_TIMEOUT{$path} > $NOW;
89 $FILE_TIMEOUT{$path} = $NOW + 60 + rand 60;
90
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;
94
95 prefetch file => $map->tmpname
96 if $map->in_memory == cf::MAP_SWAPPED;
97 }
98 }
99
100 load_file $path, $cb;
101 return;
102 }
103
104 $PREFETCHING = 0;
105}
106
35my %MAP_EXITS; 107my %MAP_EXITS;
36my %MAP_TIMEOUT; 108
109sub prefetch_map($) {
110 my ($map) = @_;
111
112 my $exit = $MAP_EXITS{$map->path} ||= find_exits $map;
113 prefetch map => $_ for @$exit;
114}
37 115
38sub on_mapenter { 116sub on_mapenter {
39 my ($ob) = @_; 117 my ($ob) = @_;
40 118
41 my $exit = $MAP_EXITS{$ob->map->path} ||= find_exits $ob->map; 119 prefetch_map $ob->map;
120}
42 121
43 my $NOW = Time::HiRes::time; 122# prefetch a few players/second
123{
124 my @players;
44 125
45 for my $path (@$exit) { 126 Event->timer (interval => 0.2, cb => sub {
46 next if $MAP_TIMEOUT{$path} > $NOW; 127 @players = map $_->ob->name, cf::player::list unless @players;
128 my $player = cf::player::find pop @players
129 or return;
47 130
48 if (my $map = cf::map::has_been_loaded $path) { 131 if (my $map = $player->ob->map) {
49 next if $map->in_memory == cf::MAP_IN_MEMORY; 132 prefetch_map $map;
50
51 $path = $map->tmppath if $map->in_memory == cf::MAP_SWAPPED;
52 } 133 }
53 134
54 $MAP_TIMEOUT{$path} = $NOW + 60 + rand 60; 135 prefetch map => +($player->get_savebed)[0];
55
56 aio_open $path, O_RDONLY, 0, sub {
57 my ($fh) = @_
58 or return;
59 aio_readahead $fh, 0, -s $fh, sub {
60 my $time = Time::HiRes::time - $NOW;
61 warn "LONG PREFETCH $path $time\n" if $time > 0.15;
62 };
63 };
64 } 136 });
65} 137}
66 138
67sub on_clock { 139# prefetch all .pl files every few minutes (thats only a "few" megabytes)
68 # boy how I hate polling 140Event->timer (after => 1, interval => 600, cb => sub {
69 IO::AIO::poll_cb; 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});
70} 155}
71 156
72 157
158

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines