diff --git a/src/main/java/org/dynmap/MapManager.java b/src/main/java/org/dynmap/MapManager.java index 754ab86b..cb5eaf70 100644 --- a/src/main/java/org/dynmap/MapManager.java +++ b/src/main/java/org/dynmap/MapManager.java @@ -18,9 +18,10 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.command.CommandSender; @@ -28,7 +29,6 @@ import org.dynmap.DynmapPlugin.CompassMode; import org.dynmap.DynmapWorld.AutoGenerateOption; import org.dynmap.debug.Debug; import org.dynmap.hdmap.HDMapManager; -import org.dynmap.utils.LegacyMapChunkCache; import org.dynmap.utils.MapChunkCache; import org.dynmap.utils.NewMapChunkCache; import org.dynmap.utils.SnapshotCache; @@ -58,7 +58,16 @@ public class MapManager { private Object loadlock = new Object(); private int chunks_in_cur_tick = 0; private long cur_tick; - + + /* Chunk load performance numbers */ + AtomicInteger chunk_caches_created = new AtomicInteger(0); + AtomicInteger chunk_caches_attempted = new AtomicInteger(0); + AtomicLong total_chunk_cache_loadtime_ns = new AtomicLong(0); + AtomicInteger chunks_read = new AtomicInteger(0);; + AtomicInteger chunks_attempted = new AtomicInteger(0); + AtomicLong total_loadtime_ns = new AtomicLong(0L); + AtomicLong total_exceptions = new AtomicLong(0L); + /* Tile hash manager */ public TileHashManager hashman; /* lock for our data structures */ @@ -182,6 +191,8 @@ public class MapManager { String rendertype; boolean cancelled; String mapname; + AtomicLong total_render_ns = new AtomicLong(0L); + AtomicInteger rendercalls = new AtomicInteger(0); /* Full world, all maps render */ FullWorldRenderState(DynmapWorld dworld, Location l, CommandSender sender, String mapname) { @@ -342,17 +353,23 @@ public class MapManager { if(renderQueue.isEmpty()) { if(map_index >= 0) { /* Finished a map? */ double msecpertile = (double)timeaccum / (double)((rendercnt>0)?rendercnt:1)/(double)activemapcnt; + double rendtime = total_render_ns.doubleValue() * 0.000001 / rendercalls.get(); if(activemapcnt > 1) sendMessage(rendertype + " of maps [" + activemaps + "] of '" + - world.world.getName() + "' completed - " + rendercnt + " tiles rendered each (" + String.format("%.2f", msecpertile) + " msec/map-tile)."); + world.world.getName() + "' completed - " + rendercnt + " tiles rendered each (" + String.format("%.2f", msecpertile) + " msec/map-tile, " + + rendtime +" msec per render)"); else sendMessage(rendertype + " of map '" + activemaps + "' of '" + - world.world.getName() + "' completed - " + rendercnt + " tiles rendered (" + String.format("%.2f", msecpertile) + " msec/tile)."); + world.world.getName() + "' completed - " + rendercnt + " tiles rendered (" + String.format("%.2f", msecpertile) + " msec/tile. " + + rendtime +" msec per render)"); + } found.clear(); rendered.clear(); rendercnt = 0; timeaccum = 0; + total_render_ns.set(0); + rendercalls.set(0); /* Advance to next unrendered map */ while(map_index < world.maps.size()) { map_index++; /* Move to next one */ @@ -507,12 +524,21 @@ public class MapManager { if(!good) requiredChunks = Collections.emptyList(); } /* Fetch chunk cache from server thread */ + long clt0 = System.nanoTime(); MapChunkCache cache = createMapChunkCache(world, requiredChunks, tile.isBlockTypeDataNeeded(), tile.isHightestBlockYDataNeeded(), tile.isBiomeDataNeeded(), tile.isRawBiomeDataNeeded()); + total_chunk_cache_loadtime_ns.addAndGet(System.nanoTime() - clt0); + chunk_caches_attempted.incrementAndGet(); if(cache == null) { return false; /* Cancelled/aborted */ } + /* Update stats */ + chunk_caches_created.incrementAndGet(); + chunks_read.addAndGet(cache.getChunksLoaded()); + chunks_attempted.addAndGet(cache.getChunkLoadsAttempted()); + total_loadtime_ns.addAndGet(cache.getTotalRuntimeNanos()); + total_exceptions.addAndGet(cache.getExceptionCount()); if(tile0 != null) { /* Single tile? */ if(cache.isEmpty() == false) tile.render(cache, null); @@ -522,7 +548,10 @@ public class MapManager { tileQueue.remove(tile); /* Switch to not checking if rendered tile is blank - breaks us on skylands, where tiles can be nominally blank - just work off chunk cache empty */ if (cache.isEmpty() == false) { + long rt0 = System.nanoTime(); tile.render(cache, mapname); + total_render_ns.addAndGet(System.nanoTime()-rt0); + rendercalls.incrementAndGet(); synchronized(lock) { // found.setFlag(tile.tileOrdinalX(),tile.tileOrdinalY(),false); rendered.setFlag(tile.tileOrdinalX(), tile.tileOrdinalY(), true); @@ -541,13 +570,16 @@ public class MapManager { rendercnt++; timeaccum += System.currentTimeMillis() - tstart; if((rendercnt % progressinterval) == 0) { + double rendtime = total_render_ns.doubleValue() * 0.000001 / rendercalls.get(); double msecpertile = (double)timeaccum / (double)rendercnt / (double)activemapcnt; if(activemapcnt > 1) sendMessage(rendertype + " of maps [" + activemaps + "] of '" + - w.getName() + "' in progress - " + rendercnt + " tiles rendered each (" + String.format("%.2f", msecpertile) + " msec/map-tile)."); + w.getName() + "' in progress - " + rendercnt + " tiles rendered each (" + String.format("%.2f", msecpertile) + " msec/map-tile, " + + rendtime +" msec per render)"); else sendMessage(rendertype + " of map '" + activemaps + "' of '" + - w.getName() + "' in progress - " + rendercnt + " tiles rendered (" + String.format("%.2f", msecpertile) + " msec/tile)."); + w.getName() + "' in progress - " + rendercnt + " tiles rendered (" + String.format("%.2f", msecpertile) + " msec/tile. " + + rendtime +" msec per render)"); } } } @@ -1066,15 +1098,7 @@ public class MapManager { */ public MapChunkCache createMapChunkCache(DynmapWorld w, List chunks, boolean blockdata, boolean highesty, boolean biome, boolean rawbiome) { - MapChunkCache c = null; - try { - if(!use_legacy) - c = new NewMapChunkCache(); - } catch (NoClassDefFoundError ncdfe) { - use_legacy = true; - } - if(c == null) - c = new LegacyMapChunkCache(); + MapChunkCache c = new NewMapChunkCache(); if(w.visibility_limits != null) { for(MapChunkCache.VisibilityLimit limit: w.visibility_limits) { c.setVisibleRange(limit); @@ -1173,12 +1197,26 @@ public class MapManager { } sender.sendMessage(" TOTALS: processed=" + tot.loggedcnt + ", rendered=" + tot.renderedcnt + ", updated=" + tot.updatedcnt + ", transparent=" + tot.transparentcnt); - sender.sendMessage(" Cache hit rate: " + sscache.getHitRate() + "%"); sender.sendMessage(" Triggered update queue size: " + tileQueue.size()); String act = ""; for(String wn : active_renders.keySet()) act += wn + " "; sender.sendMessage(" Active render jobs: " + act); + /* Chunk load stats */ + sender.sendMessage("Chunk Loading Statistics:"); + sender.sendMessage(" Cache hit rate: " + sscache.getHitRate() + "%"); + int setcnt = chunk_caches_attempted.get(); + sender.sendMessage(" Chunk sets: created=" + chunk_caches_created.get() + ", attempted=" + chunk_caches_attempted.get()); + int readcnt = chunks_read.get(); + sender.sendMessage(" Chunk: loaded=" + readcnt + ", attempted=" + chunks_attempted.get()); + double ns = total_loadtime_ns.doubleValue() * 0.000001; /* Convert to milliseconds */ + double chunkloadns = total_chunk_cache_loadtime_ns.doubleValue() * 0.000001; + if(readcnt == 0) readcnt = 1; + if(setcnt == 0) setcnt = 1; + sender.sendMessage(" Chunk load times: " + ns + " msec (" + (ns / readcnt) + " msec/chunk)"); + sender.sendMessage(" Chunk set load times: " + chunkloadns + " msec (" + (chunkloadns / setcnt) + " msec/set)"); + sender.sendMessage(" Chunk set delay times: " + (chunkloadns-ns) + " msec (" + ((chunkloadns-ns) / setcnt) + " msec/set)"); + sender.sendMessage(" Chunk set exceptions: " + total_exceptions.get()); } /** * Print trigger statistics command @@ -1213,6 +1251,13 @@ public class MapManager { ts.callswithtiles = 0; ts.tilesqueued = 0; } + chunk_caches_created.set(0); + chunk_caches_attempted.set(0); + chunks_read.set(0); + chunks_attempted.set(0); + total_loadtime_ns.set(0); + total_chunk_cache_loadtime_ns.set(0); + total_exceptions.set(0); } sscache.resetStats(); sender.sendMessage("Tile Render Statistics reset"); diff --git a/src/main/java/org/dynmap/utils/CraftChunkSnapshot.java b/src/main/java/org/dynmap/utils/CraftChunkSnapshot.java deleted file mode 100644 index 2f31ef8b..00000000 --- a/src/main/java/org/dynmap/utils/CraftChunkSnapshot.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.dynmap.utils; - - -/** - * Represents a static, thread-safe snapshot of chunk of blocks - * Purpose is to allow clean, efficient copy of a chunk data to be made, and then handed off for processing in another thread (e.g. map rendering) - */ -public class CraftChunkSnapshot implements LegacyChunkSnapshot { - private final int x, z; - private final byte[] buf; /* Flat buffer in uncompressed chunk file format */ - private final byte[] hmap; /* Highest Y map */ - private static final int BLOCKDATA_OFF = 32768; - private static final int BLOCKLIGHT_OFF = BLOCKDATA_OFF + 16384; - private static final int SKYLIGHT_OFF = BLOCKLIGHT_OFF + 16384; - - /** - * Constructor - */ - CraftChunkSnapshot(int x, int z, byte[] buf, byte[] hmap) { - this.x = x; - this.z = z; - this.buf = buf; - this.hmap = hmap; - } - - /** - * Gets the X-coordinate of this chunk - * - * @return X-coordinate - */ - public int getX() { - return x; - } - - /** - * Gets the Z-coordinate of this chunk - * - * @return Z-coordinate - */ - public int getZ() { - return z; - } - - /** - * Get block type for block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-255 - */ - public int getBlockTypeId(int x, int y, int z) { - return buf[x << 11 | z << 7 | y] & 255; - } - - /** - * Get block data for block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-15 - */ - public int getBlockData(int x, int y, int z) { - int off = ((x << 10) | (z << 6) | (y >> 1)) + BLOCKDATA_OFF; - - return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF); - } - - /** - * Get sky light level for block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-15 - */ - public int getBlockSkyLight(int x, int y, int z) { - int off = ((x << 10) | (z << 6) | (y >> 1)) + SKYLIGHT_OFF; - - return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF); - } - - /** - * Get light level emitted by block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-15 - */ - public int getBlockEmittedLight(int x, int y, int z) { - int off = ((x << 10) | (z << 6) | (y >> 1)) + BLOCKLIGHT_OFF; - - return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF); - } - - public int getHighestBlockYAt(int x, int z) { - return hmap[z << 4 | x] & 255; - } -} diff --git a/src/main/java/org/dynmap/utils/LegacyChunkSnapshot.java b/src/main/java/org/dynmap/utils/LegacyChunkSnapshot.java deleted file mode 100644 index ca3edc20..00000000 --- a/src/main/java/org/dynmap/utils/LegacyChunkSnapshot.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.dynmap.utils; - -/** - * Represents a static, thread-safe snapshot of chunk of blocks - * Purpose is to allow clean, efficient copy of a chunk data to be made, and then handed off for processing in another thread (e.g. map rendering) - */ -public interface LegacyChunkSnapshot { - /** - * Get block type for block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-255 - */ - public int getBlockTypeId(int x, int y, int z); - /** - * Get block data for block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-15 - */ - public int getBlockData(int x, int y, int z); - /** - * Get sky light level for block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-15 - */ - public int getBlockSkyLight(int x, int y, int z); - /** - * Get light level emitted by block at corresponding coordinate in the chunk - * - * @param x 0-15 - * @param y 0-127 - * @param z 0-15 - * @return 0-15 - */ - public int getBlockEmittedLight(int x, int y, int z); - - public int getHighestBlockYAt(int x, int z); -} diff --git a/src/main/java/org/dynmap/utils/LegacyMapChunkCache.java b/src/main/java/org/dynmap/utils/LegacyMapChunkCache.java deleted file mode 100644 index 57ed9cc1..00000000 --- a/src/main/java/org/dynmap/utils/LegacyMapChunkCache.java +++ /dev/null @@ -1,555 +0,0 @@ -package org.dynmap.utils; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.ListIterator; -import java.lang.reflect.Field; - -import org.bukkit.ChunkSnapshot; -import org.bukkit.World; -import org.bukkit.Chunk; -import org.bukkit.block.Biome; -import org.bukkit.entity.Entity; -import org.dynmap.DynmapChunk; -import org.dynmap.DynmapPlugin; -import org.dynmap.DynmapWorld; -import org.dynmap.Log; -import org.dynmap.utils.MapIterator.BlockStep; - -import java.util.List; - -/** - * Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread - */ -public class LegacyMapChunkCache implements MapChunkCache { - private static Method getchunkdata = null; - private static Method gethandle = null; - private static Method poppreservedchunk = null; - private static Field heightmap = null; - private static boolean initialized = false; - - private World w; - private List chunks; - private ListIterator iterator; - private DynmapWorld.AutoGenerateOption generateopt; - private boolean do_generate = false; - private boolean do_save = false; - private boolean isempty = true; - - private int x_min, x_max, z_min, z_max; - private int x_dim; - private HiddenChunkStyle hidestyle = HiddenChunkStyle.FILL_AIR; - private List visible_limits = null; - - private LegacyChunkSnapshot[] snaparray; /* Index = (x-x_min) + ((z-z_min)*x_dim) */ - - private static final BlockStep unstep[] = { BlockStep.X_MINUS, BlockStep.Y_MINUS, BlockStep.Z_MINUS, - BlockStep.X_PLUS, BlockStep.Y_PLUS, BlockStep.Z_PLUS }; - - /** - * Iterator for traversing map chunk cache (base is for non-snapshot) - */ - public class OurMapIterator implements MapIterator { - private int x, y, z; - private LegacyChunkSnapshot snap; - private BlockStep laststep; - private int typeid; - - OurMapIterator(int x0, int y0, int z0) { - initialize(x0, y0, z0); - } - public final void initialize(int x0, int y0, int z0) { - this.x = x0; - this.y = y0; - this.z = z0; - try { - snap = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - } catch (ArrayIndexOutOfBoundsException aioobx) { - snap = EMPTY; - } - laststep = BlockStep.Y_MINUS; - typeid = -1; - } - public final int getBlockTypeID() { - if(typeid < 0) - typeid = snap.getBlockTypeId(x & 0xF, y, z & 0xF); - return typeid; - } - public final int getBlockData() { - return snap.getBlockData(x & 0xF, y, z & 0xF); - } - public final int getHighestBlockYAt() { - return snap.getHighestBlockYAt(x & 0xF, z & 0xF); - } - public final int getBlockSkyLight() { - return snap.getBlockSkyLight(x & 0xF, y, z & 0xF); - } - public final int getBlockEmittedLight() { - return snap.getBlockEmittedLight(x & 0xF, y, z & 0xF); - } - public Biome getBiome() { - return null; - } - public double getRawBiomeTemperature() { - return 0.0; - } - public double getRawBiomeRainfall() { - return 0.0; - } - /** - * Step current position in given direction - */ - public final void stepPosition(BlockStep step) { - switch(step) { - case X_PLUS: - x++; - if((x & 0xF) == 0) { /* Next chunk? */ - try { - snap = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - } catch (ArrayIndexOutOfBoundsException aioobx) { - snap = EMPTY; - } - } - break; - case X_MINUS: - x--; - if((x & 0xF) == 15) { /* Next chunk? */ - try { - snap = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - } catch (ArrayIndexOutOfBoundsException aioobx) { - snap = EMPTY; - } - } - break; - case Y_PLUS: - y++; - break; - case Y_MINUS: - y--; - break; - case Z_PLUS: - z++; - if((z & 0xF) == 0) { /* Next chunk? */ - try { - snap = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - } catch (ArrayIndexOutOfBoundsException aioobx) { - snap = EMPTY; - } - } - break; - case Z_MINUS: - z--; - if((z & 0xF) == 15) { /* Next chunk? */ - try { - snap = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - } catch (ArrayIndexOutOfBoundsException aioobx) { - snap = EMPTY; - } - } - break; - } - laststep = step; - typeid = -1; - } - /** - * Unstep current position to previous position - */ - public BlockStep unstepPosition() { - BlockStep ls = laststep; - stepPosition(unstep[ls.ordinal()]); - return ls; - } - /** - * Unstep current position in oppisite director of given step - */ - public void unstepPosition(BlockStep s) { - stepPosition(unstep[s.ordinal()]); - } - public final void setY(int y) { - if(y > this.y) - laststep = BlockStep.Y_PLUS; - else - laststep = BlockStep.Y_PLUS; - this.y = y; - typeid = -1; - } - public final int getX() { - return x; - } - public final int getY() { - return y; - } - public final int getZ() { - return z; - } - public final int getBlockTypeIDAt(BlockStep s) { - if(s == BlockStep.Y_MINUS) { - if(y > 0) - return snap.getBlockTypeId(x & 0xF, y-1, z & 0xF); - } - else if(s == BlockStep.Y_PLUS) { - if(y < 127) - return snap.getBlockTypeId(x & 0xF, y+1, z & 0xF); - } - else { - BlockStep ls = laststep; - stepPosition(s); - int tid = snap.getBlockTypeId(x & 0xF, y, z & 0xF); - unstepPosition(); - laststep = ls; - return tid; - } - return 0; - } - public BlockStep getLastStep() { - return laststep; - } - } - - /** - * Chunk cache for representing unloaded chunk (or air chunk) - */ - private static class EmptyChunk implements LegacyChunkSnapshot { - public final int getBlockTypeId(int x, int y, int z) { - return 0; - } - public final int getBlockData(int x, int y, int z) { - return 0; - } - public final int getBlockSkyLight(int x, int y, int z) { - return 15; - } - public final int getBlockEmittedLight(int x, int y, int z) { - return 0; - } - public final int getHighestBlockYAt(int x, int z) { - return 0; - } - } - - /** - * Chunk cache for representing hidden chunk as stone - */ - private static class PlainChunk implements LegacyChunkSnapshot { - private int fillid; - PlainChunk(int fillid) { this.fillid = fillid; } - - public final int getBlockTypeId(int x, int y, int z) { - if(y < 64) - return fillid; - return 0; - } - public final int getBlockData(int x, int y, int z) { - return 0; - } - public final int getBlockSkyLight(int x, int y, int z) { - if(y < 64) - return 0; - return 15; - } - public final int getBlockEmittedLight(int x, int y, int z) { - return 0; - } - public final int getHighestBlockYAt(int x, int z) { - return 64; - } - } - - private static final EmptyChunk EMPTY = new EmptyChunk(); - private static final PlainChunk STONE = new PlainChunk(1); - private static final PlainChunk OCEAN = new PlainChunk(9); - - - /** - * Construct empty cache - */ - public LegacyMapChunkCache() { - } - /** - * Set chunks to load, and world to load from - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public void setChunks(World w, List chunks) { - this.w = w; - this.chunks = chunks; - - /* Compute range */ - if(chunks.size() == 0) { - this.x_min = 0; - this.x_max = 0; - this.z_min = 0; - this.z_max = 0; - x_dim = 1; - } - else { - x_min = x_max = chunks.get(0).x; - z_min = z_max = chunks.get(0).z; - for(DynmapChunk c : chunks) { - if(c.x > x_max) - x_max = c.x; - if(c.x < x_min) - x_min = c.x; - if(c.z > z_max) - z_max = c.z; - if(c.z < z_min) - z_min = c.z; - } - x_dim = x_max - x_min + 1; - } - - if(!initialized) { - try { - Class c = Class.forName("net.minecraft.server.Chunk"); - getchunkdata = c.getDeclaredMethod("a", new Class[] { byte[].class, int.class, - int.class, int.class, int.class, int.class, int.class, int.class }); - heightmap = c.getDeclaredField("h"); - c = Class.forName("org.bukkit.craftbukkit.CraftChunk"); - gethandle = c.getDeclaredMethod("getHandle", new Class[0]); - } catch (ClassNotFoundException cnfx) { - } catch (NoSuchMethodException nsmx) { - } catch (NoSuchFieldException nsfx) { - } - /* Get CraftWorld.popPreservedChunk(x,z) - reduces memory bloat from map traversals (optional) */ - try { - Class c = Class.forName("org.bukkit.craftbukkit.CraftWorld"); - poppreservedchunk = c.getDeclaredMethod("popPreservedChunk", new Class[] { int.class, int.class }); - } catch (ClassNotFoundException cnfx) { - } catch (NoSuchMethodException nsmx) { - } - initialized = true; - if(gethandle == null) { - Log.severe("ERROR: Chunk snapshot support not found - rendering not functiona!l"); - return; - } - } - snaparray = new LegacyChunkSnapshot[x_dim * (z_max-z_min+1)]; - } - - public int loadChunks(int max_to_load) { - int cnt = 0; - if(iterator == null) - iterator = chunks.listIterator(); - - DynmapPlugin.setIgnoreChunkLoads(true); - // Load the required chunks. - while((cnt < max_to_load) && iterator.hasNext()) { - DynmapChunk chunk = iterator.next(); - if(gethandle != null) { - boolean vis = true; - if(visible_limits != null) { - vis = false; - for(VisibilityLimit limit : visible_limits) { - if((chunk.x >= limit.x0) && (chunk.x <= limit.x1) && (chunk.z >= limit.z0) && (chunk.z <= limit.z1)) { - vis = true; - break; - } - } - } - boolean wasLoaded = w.isChunkLoaded(chunk.x, chunk.z); - boolean didload = w.loadChunk(chunk.x, chunk.z, false); - boolean didgenerate = false; - /* If we didn't load, and we're supposed to generate, do it */ - if((!didload) && do_generate && vis) - didgenerate = didload = w.loadChunk(chunk.x, chunk.z, true); - /* If it did load, make cache of it */ - if(didload) { - LegacyChunkSnapshot ss = null; - if(!vis) { - if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN) - ss = STONE; - else if(hidestyle == HiddenChunkStyle.FILL_OCEAN) - ss = OCEAN; - else - ss = EMPTY; - } - else { - Chunk c = w.getChunkAt(chunk.x, chunk.z); - try { - Object cc = gethandle.invoke(c); - byte[] buf = new byte[32768 + 16384 + 16384 + 16384]; /* Get big enough buffer for whole chunk */ - getchunkdata.invoke(cc, buf, 0, 0, 0, 16, 128, 16, 0); - byte[] h = (byte[])heightmap.get(cc); - byte[] hmap = new byte[256]; - System.arraycopy(h, 0, hmap, 0, 256); - ss = new CraftChunkSnapshot(chunk.x, chunk.z, buf, hmap); - } catch (Exception x) { - } - } - snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss; - } - if ((!wasLoaded) && didload) { - /* It looks like bukkit "leaks" entities - they don't get removed from the world-level table - * when chunks are unloaded but not saved - removing them seems to do the trick */ - if(!didgenerate) { - Chunk cc = w.getChunkAt(chunk.x, chunk.z); - if(cc != null) { - for(Entity e: cc.getEntities()) - e.remove(); - } - } - /* Since we only remember ones we loaded, and we're synchronous, no player has - * moved, so it must be safe (also prevent chunk leak, which appears to happen - * because isChunkInUse defined "in use" as being within 256 blocks of a player, - * while the actual in-use chunk area for a player where the chunks are managed - * by the MC base server is 21x21 (or about a 160 block radius). - * Also, if we did generate it, need to save it */ - w.unloadChunk(chunk.x, chunk.z, didgenerate && do_save, false); - /* And pop preserved chunk - this is a bad leak in Bukkit for map traversals like us */ - try { - if(poppreservedchunk != null) - poppreservedchunk.invoke(w, chunk.x, chunk.z); - } catch (Exception x) { - Log.severe("Cannot pop preserved chunk - " + x.toString()); - } - } - } - cnt++; - } - DynmapPlugin.setIgnoreChunkLoads(false); - - /* If done, finish table */ - if(iterator.hasNext() == false) { - isempty = true; - /* Fill missing chunks with empty dummy chunk */ - for(int i = 0; i < snaparray.length; i++) { - if(snaparray[i] == null) - snaparray[i] = EMPTY; - else if(snaparray[i] != EMPTY) - isempty = false; - } - } - return cnt; - } - /** - * Test if done loading - */ - public boolean isDoneLoading() { - if(iterator != null) - return !iterator.hasNext(); - return false; - } - /** - * Test if all empty blocks - */ - public boolean isEmpty() { - return isempty; - } - /** - * Unload chunks - */ - public void unloadChunks() { - if(snaparray != null) { - for(int i = 0; i < snaparray.length; i++) { - snaparray[i] = null; - } - snaparray = null; - } - } - /** - * Get block ID at coordinates - */ - public int getBlockTypeID(int x, int y, int z) { - LegacyChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - return ss.getBlockTypeId(x & 0xF, y, z & 0xF); - } - /** - * Get block data at coordiates - */ - public byte getBlockData(int x, int y, int z) { - LegacyChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - return (byte)ss.getBlockData(x & 0xF, y, z & 0xF); - } - /* Get highest block Y - * - */ - public int getHighestBlockYAt(int x, int z) { - LegacyChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - return ss.getHighestBlockYAt(x & 0xF, z & 0xF); - } - /* Get sky light level - */ - public int getBlockSkyLight(int x, int y, int z) { - LegacyChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - return ss.getBlockSkyLight(x & 0xF, y, z & 0xF); - } - /* Get emitted light level - */ - public int getBlockEmittedLight(int x, int y, int z) { - LegacyChunkSnapshot ss = snaparray[((x>>4) - x_min) + ((z>>4) - z_min) * x_dim]; - return ss.getBlockEmittedLight(x & 0xF, y, z & 0xF); - } - public Biome getBiome(int x, int z) { - return null; - } - public double getRawBiomeTemperature(int x, int z) { - return 0.0; - } - public double getRawBiomeRainfall(int x, int z) { - return 0.0; - } - - /** - * Get cache iterator - */ - public MapIterator getIterator(int x, int y, int z) { - return new OurMapIterator(x, y, z); - } - /** - * Set hidden chunk style (default is FILL_AIR) - */ - public void setHiddenFillStyle(HiddenChunkStyle style) { - this.hidestyle = style; - } - /** - * Add visible area limit - can be called more than once - * Needs to be set before chunks are loaded - * Coordinates are block coordinates - */ - public void setVisibleRange(VisibilityLimit lim) { - VisibilityLimit limit = new VisibilityLimit(); - if(lim.x0 > lim.x1) { - limit.x0 = (lim.x1 >> 4); limit.x1 = ((lim.x0+15) >> 4); - } - else { - limit.x0 = (lim.x0 >> 4); limit.x1 = ((lim.x1+15) >> 4); - } - if(lim.z0 > lim.z1) { - limit.z0 = (lim.z1 >> 4); limit.z1 = ((lim.z0+15) >> 4); - } - else { - limit.z0 = (lim.z0 >> 4); limit.z1 = ((lim.z1+15) >> 4); - } - if(visible_limits == null) - visible_limits = new ArrayList(); - visible_limits.add(limit); - } - /** - * Add hidden area limit - can be called more than once - * Needs to be set before chunks are loaded - * Coordinates are block coordinates - */ - public void setHiddenRange(VisibilityLimit lim) { - Log.severe("LegacyMapChunkCache does not support hidden areas"); - } - /** - * Set autogenerate - must be done after at least one visible range has been set - */ - public void setAutoGenerateVisbileRanges(DynmapWorld.AutoGenerateOption generateopt) { - if((generateopt != DynmapWorld.AutoGenerateOption.NONE) && ((visible_limits == null) || (visible_limits.size() == 0))) { - Log.severe("Cannot setAutoGenerateVisibleRanges() without visible ranges defined"); - return; - } - this.generateopt = generateopt; - this.do_generate = (generateopt != DynmapWorld.AutoGenerateOption.NONE); - this.do_save = (generateopt == DynmapWorld.AutoGenerateOption.PERMANENT); - } - @Override - public boolean setChunkDataTypes(boolean blockdata, boolean biome, boolean highestblocky, boolean rawbiome) { - if(biome || rawbiome) /* Legacy doesn't support these */ - return false; - return true; - } - public World getWorld() { - return w; - } -} diff --git a/src/main/java/org/dynmap/utils/MapChunkCache.java b/src/main/java/org/dynmap/utils/MapChunkCache.java index c7500408..4ee10cfe 100644 --- a/src/main/java/org/dynmap/utils/MapChunkCache.java +++ b/src/main/java/org/dynmap/utils/MapChunkCache.java @@ -106,4 +106,21 @@ public interface MapChunkCache { * Get world */ public World getWorld(); + /** + * Get total chunks loaded + * @return + */ + public int getChunksLoaded(); + /** + * Get total chunk loads attempted + * @return + */ + public int getChunkLoadsAttempted(); + /** + * Get total run time processing chunks + * @return + */ + public long getTotalRuntimeNanos(); + + public long getExceptionCount(); } diff --git a/src/main/java/org/dynmap/utils/NewMapChunkCache.java b/src/main/java/org/dynmap/utils/NewMapChunkCache.java index 271ebd88..d9b88ee4 100644 --- a/src/main/java/org/dynmap/utils/NewMapChunkCache.java +++ b/src/main/java/org/dynmap/utils/NewMapChunkCache.java @@ -51,6 +51,12 @@ public class NewMapChunkCache implements MapChunkCache { private boolean isempty = true; private ChunkSnapshot[] snaparray; /* Index = (x-x_min) + ((z-z_min)*x_dim) */ + private int chunks_read; /* Number of chunks actually loaded */ + private int chunks_attempted; /* Number of chunks attempted to load */ + private long total_loadtime; /* Total time loading chunks, in nanoseconds */ + + private long exceptions; + private static final BlockStep unstep[] = { BlockStep.X_MINUS, BlockStep.Y_MINUS, BlockStep.Z_MINUS, BlockStep.X_PLUS, BlockStep.Y_PLUS, BlockStep.Z_PLUS }; @@ -78,6 +84,7 @@ public class NewMapChunkCache implements MapChunkCache { snap = snaparray[chunkindex]; } catch (ArrayIndexOutOfBoundsException aioobx) { snap = EMPTY; + exceptions++; } laststep = BlockStep.Y_MINUS; typeid = blkdata = -1; @@ -125,6 +132,7 @@ public class NewMapChunkCache implements MapChunkCache { snap = snaparray[chunkindex]; } catch (ArrayIndexOutOfBoundsException aioobx) { snap = EMPTY; + exceptions++; } } break; @@ -141,6 +149,7 @@ public class NewMapChunkCache implements MapChunkCache { snap = snaparray[chunkindex]; } catch (ArrayIndexOutOfBoundsException aioobx) { snap = EMPTY; + exceptions++; } } break; @@ -154,6 +163,7 @@ public class NewMapChunkCache implements MapChunkCache { snap = snaparray[chunkindex]; } catch (ArrayIndexOutOfBoundsException aioobx) { snap = EMPTY; + exceptions++; } } break; @@ -170,6 +180,7 @@ public class NewMapChunkCache implements MapChunkCache { snap = snaparray[chunkindex]; } catch (ArrayIndexOutOfBoundsException aioobx) { snap = EMPTY; + exceptions++; } } break; @@ -387,15 +398,6 @@ public class NewMapChunkCache implements MapChunkCache { public void setChunks(World w, List chunks) { this.w = w; this.chunks = chunks; - if(poppreservedchunk == null) { - /* Get CraftWorld.popPreservedChunk(x,z) - reduces memory bloat from map traversals (optional) */ - try { - Class c = Class.forName("org.bukkit.craftbukkit.CraftWorld"); - poppreservedchunk = c.getDeclaredMethod("popPreservedChunk", new Class[] { int.class, int.class }); - } catch (ClassNotFoundException cnfx) { - } catch (NoSuchMethodException nsmx) { - } - } /* Compute range */ if(chunks.size() == 0) { this.x_min = 0; @@ -424,6 +426,7 @@ public class NewMapChunkCache implements MapChunkCache { } public int loadChunks(int max_to_load) { + long t0 = System.nanoTime(); int cnt = 0; if(iterator == null) iterator = chunks.listIterator(); @@ -465,6 +468,7 @@ public class NewMapChunkCache implements MapChunkCache { snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss; continue; } + chunks_attempted++; boolean wasLoaded = w.isChunkLoaded(chunk.x, chunk.z); boolean didload = w.loadChunk(chunk.x, chunk.z, false); boolean didgenerate = false; @@ -498,6 +502,7 @@ public class NewMapChunkCache implements MapChunkCache { snaparray[(chunk.x-x_min) + (chunk.z - z_min)*x_dim] = ss; } if ((!wasLoaded) && didload) { + chunks_read++; /* It looks like bukkit "leaks" entities - they don't get removed from the world-level table * when chunks are unloaded but not saved - removing them seems to do the trick */ if(!(didgenerate && do_save)) { @@ -551,6 +556,8 @@ public class NewMapChunkCache implements MapChunkCache { isempty = false; } } + total_loadtime += System.nanoTime() - t0; + return cnt; } /** @@ -744,5 +751,20 @@ public class NewMapChunkCache implements MapChunkCache { public World getWorld() { return w; } - + @Override + public int getChunksLoaded() { + return chunks_read; + } + @Override + public int getChunkLoadsAttempted() { + return chunks_attempted; + } + @Override + public long getTotalRuntimeNanos() { + return total_loadtime; + } + @Override + public long getExceptionCount() { + return exceptions; + } }