3 # This script is used to delete tiles that are extremely cheap to rerender.
4 # If a tile can be regenerated in one second, we really don't need to keep it
5 # on disk and regularily update it. This can happen on the fly when it is
8 # Where are the tile directories?
9 $tiledirprefix = '/mnt/tiles/tiles/';
10 # Map for map-ID in DB to tile subdir.
11 %tiledirmap = ( 1 => 'osm',
15 # What is the maximum rendertime to consider a tile useless?
16 $maxuselesstime = 0.5;
18 # How old do files need to be before we delete them? in seconds
19 $minuselessage = 48 * 60 * 60;
21 # Where is the osm stats database?
22 $statsdb = 'osmstats';
24 # -----------------------------------------------------------------------------
27 use POSIX qw(strftime mktime);
32 sub calcpathfromcomponents($$) {
34 my $i; my $x = $_[0]; my $y = $_[1];
35 for ($i = 4; $i >= 0; $i--) {
36 $res[$i] = sprintf("%d", (($x & 0x0f) << 4) + ($y & 0x0f));
40 return $res[0] . "/" . $res[1] . "/" . $res[2] . "/" . $res[3] . "/" . $res[4];
43 unless ($dbh = DBI->connect("dbi:Pg:dbname=$statsdb","","")) {
44 print(STDERR "Failed to open database. Please try again later.\n"); exit(1);
46 # select x, y, z, avg(rendertime) as avgrt, max(rendertime) as maxrt from renderrequests where mapid=1 group by x, y, z
47 # select distinct on (x, y, z) x, y, z, rendertime from renderrequests where mapid=1 order by x,y,z,ts desc;
48 foreach $mapid (sort(keys(%tiledirmap))) {
50 my $sth = $dbh->prepare("select distinct on (x, y, z) x, y, z, rendertime"
51 . " from renderrequests where mapid=? order by x, y, z, ts desc");
52 unless ($sth->execute($mapid)) {
53 print(STDERR "Sorry, database query blew up.!\n");
56 while (($x, $y, $z, $rendertime) = $sth->fetchrow_array()) {
58 if ($rendertime > $maxuselesstime) {
59 #print("$tiledirmap{$mapid} $z/$x/$y rendered in $rendertime seconds, leaving it alone\n");
62 my $tilepath = $tiledirprefix . $tiledirmap{$mapid} . '/';
63 $tilepath .= $z . '/' . calcpathfromcomponents($x, $y) . '.meta';
64 unless (-e $tilepath) {
65 #print("...file does not exist, nothing to delete.\n");
68 @statres = lstat($tilepath);
69 unless (@statres > 12) {
70 # This usually happens if we have a symlink to a file for which we have
71 # no permission to read.
72 #print("...stat failed\n");
75 if (S_ISLNK($statres[2])) { next; } # A symlink? We no like.
76 if (($statres[9] + $minuselessage) >= time()) {
77 #print("...not old enough, skipped\n");
80 print("$tiledirmap{$mapid} $z/$x/$y rendered in $rendertime seconds, deleting $tilepath\n");
85 print("Deleted $ndeleted tiles in $tiledirmap{$mapid}\n");