ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/map-prefetch.ext
Revision: 1.1
Committed: Mon Jun 26 15:41:12 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Log Message:
Add preliminary map-prefetch plug-in that asynchronously prefetches map
files connected to a specific map form disk every time a player enters it.

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
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
5     # into the server, but into the OS cache only).
6    
7     use Errno ();
8     use Time::HiRes;
9     use Fcntl;
10     use IO::AIO;
11    
12     sub find_exits {
13     my ($map) = @_;
14    
15     my %exit;
16    
17     for my $x (0 .. $map->width - 1) {
18     for my $y (0 .. $map->height - 1) {
19     for (grep $_->type == 66, $map->at ($x, $y)) {
20     my $path = $_->slaying;
21    
22     next if 3 > length $path;
23    
24     $path = cf::maps_directory cf::path_combine_and_normalize $map->path, $path;
25    
26     $exit{$path}++;
27     }
28     }
29     }
30    
31     [keys %exit]
32     }
33    
34     my %MAP_EXITS;
35     my %MAP_TIMEOUT;
36    
37     sub on_mapenter {
38     my ($ob) = @_;
39    
40     my $exit = $MAP_EXITS{$ob->map->path} ||= find_exits $ob->map;
41    
42     my $NOW = Time::HiRes::time;
43    
44     for my $path (@$exit) {
45     next if $MAP_TIMEOUT{$path} > $NOW;
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;
51     }
52    
53     $MAP_TIMEOUT{$path} = $NOW + 60 + rand 60;
54    
55     aio_open $path, O_RDONLY, 0, sub {
56     my ($fh) = @_
57     or return;
58     aio_readahead $fh, 0, -s $fh, sub {
59     my $stop = Time::HiRes::time;
60     warn "$path ", $stop-$NOW;
61     };
62     };
63     }
64     }
65    
66     sub on_clock {
67     # boy how I hate polling
68     IO::AIO::poll_cb;
69     }
70    
71