Shift all rendering over to async thread pool, minimize server thread
load
This commit is contained in:
parent
d2c947653d
commit
9b12ebc025
7 changed files with 138 additions and 259 deletions
|
|
@ -8,24 +8,24 @@ import java.util.HashSet;
|
|||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Chunk;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.dynmap.debug.Debug;
|
||||
|
||||
public class MapManager {
|
||||
public AsynchronousQueue<MapTile> tileQueue;
|
||||
public AsynchronousQueue<ImageWriter> writeQueue;
|
||||
|
||||
public List<DynmapWorld> worlds = new ArrayList<DynmapWorld>();
|
||||
public Map<String, DynmapWorld> worldsLookup = new HashMap<String, DynmapWorld>();
|
||||
private BukkitScheduler scheduler;
|
||||
private DynmapPlugin plug_in;
|
||||
private double timeslice_interval = 0.0;
|
||||
/* Which timesliced renders are active */
|
||||
private long timeslice_int = 0; /* In milliseconds */
|
||||
/* Which fullrenders are active */
|
||||
private HashMap<String, FullWorldRenderState> active_renders = new HashMap<String, FullWorldRenderState>();
|
||||
|
||||
/* lock for our data structures */
|
||||
|
|
@ -33,9 +33,9 @@ public class MapManager {
|
|||
|
||||
public static MapManager mapman; /* Our singleton */
|
||||
|
||||
private static class ImageWriter {
|
||||
Runnable run;
|
||||
}
|
||||
/* Thread pool for processing renders */
|
||||
private ScheduledThreadPoolExecutor renderpool;
|
||||
private static final int POOL_SIZE = 3;
|
||||
|
||||
public DynmapWorld getWorld(String name) {
|
||||
DynmapWorld world = worldsLookup.get(name);
|
||||
|
|
@ -46,6 +46,7 @@ public class MapManager {
|
|||
return worlds;
|
||||
}
|
||||
|
||||
/* This always runs on render pool threads - no bukkit calls from here */
|
||||
private class FullWorldRenderState implements Runnable {
|
||||
DynmapWorld world; /* Which world are we rendering */
|
||||
Location loc;
|
||||
|
|
@ -74,7 +75,8 @@ public class MapManager {
|
|||
|
||||
public void run() {
|
||||
MapTile tile;
|
||||
|
||||
long tstart = System.currentTimeMillis();
|
||||
|
||||
if(tile0 == null) { /* Not single tile render */
|
||||
/* If render queue is empty, start next map */
|
||||
if(renderQueue.isEmpty()) {
|
||||
|
|
@ -87,8 +89,10 @@ public class MapManager {
|
|||
rendercnt = 0;
|
||||
map_index++; /* Next map */
|
||||
if(map_index >= world.maps.size()) { /* Last one done? */
|
||||
Log.info("Full render finished.");
|
||||
active_renders.remove(world.world.getName());
|
||||
Log.info("Full render of '" + world.world.getName() + "' finished.");
|
||||
synchronized(lock) {
|
||||
active_renders.remove(world.world.getName());
|
||||
}
|
||||
return;
|
||||
}
|
||||
map = world.maps.get(map_index);
|
||||
|
|
@ -116,9 +120,13 @@ public class MapManager {
|
|||
else { /* Else, single tile render */
|
||||
tile = tile0;
|
||||
}
|
||||
DynmapChunk[] requiredChunks = tile.getMap().getRequiredChunks(tile);
|
||||
MapChunkCache cache = new MapChunkCache(world.world, requiredChunks);
|
||||
World w = world.world;
|
||||
/* Fetch chunk cache from server thread */
|
||||
DynmapChunk[] requiredChunks = tile.getMap().getRequiredChunks(tile);
|
||||
MapChunkCache cache = createMapChunkCache(w, requiredChunks);
|
||||
if(cache == null) {
|
||||
return; /* Cancelled/aborted */
|
||||
}
|
||||
if(tile0 != null) { /* Single tile? */
|
||||
render(cache, tile); /* Just render */
|
||||
}
|
||||
|
|
@ -143,8 +151,13 @@ public class MapManager {
|
|||
/* And unload what we loaded */
|
||||
cache.unloadChunks();
|
||||
if(tile0 == null) { /* fullrender */
|
||||
/* Schedule the next tile to be worked */
|
||||
scheduler.scheduleSyncDelayedTask(plug_in, this, (int)(timeslice_interval*20));
|
||||
long tend = System.currentTimeMillis();
|
||||
if(timeslice_int > (tend-tstart)) { /* We were fast enough */
|
||||
renderpool.schedule(this, timeslice_int - (tend-tstart), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
else { /* Schedule to run ASAP */
|
||||
renderpool.execute(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -156,25 +169,16 @@ public class MapManager {
|
|||
this.tileQueue = new AsynchronousQueue<MapTile>(new Handler<MapTile>() {
|
||||
@Override
|
||||
public void handle(MapTile t) {
|
||||
scheduler.scheduleSyncDelayedTask(plug_in,
|
||||
new FullWorldRenderState(t), 1);
|
||||
renderpool.execute(new FullWorldRenderState(t));
|
||||
}
|
||||
}, (int) (configuration.getDouble("renderinterval", 0.5) * 1000));
|
||||
|
||||
this.writeQueue = new AsynchronousQueue<ImageWriter>(
|
||||
new Handler<ImageWriter>() {
|
||||
@Override
|
||||
public void handle(ImageWriter w) {
|
||||
w.run.run();
|
||||
}
|
||||
}, 10);
|
||||
|
||||
timeslice_interval = configuration.getDouble("timesliceinterval", 0.5);
|
||||
/* On dedicated thread, so default to no delays */
|
||||
timeslice_int = (long)(configuration.getDouble("timesliceinterval", 0.0) * 1000);
|
||||
|
||||
scheduler = plugin.getServer().getScheduler();
|
||||
|
||||
tileQueue.start();
|
||||
writeQueue.start();
|
||||
|
||||
for (World world : plug_in.getServer().getWorlds()) {
|
||||
activateWorld(world);
|
||||
|
|
@ -188,16 +192,19 @@ public class MapManager {
|
|||
return;
|
||||
}
|
||||
String wname = l.getWorld().getName();
|
||||
FullWorldRenderState rndr = active_renders.get(wname);
|
||||
if(rndr != null) {
|
||||
Log.info("Full world render of world '" + wname + "' already active.");
|
||||
return;
|
||||
FullWorldRenderState rndr;
|
||||
synchronized(lock) {
|
||||
rndr = active_renders.get(wname);
|
||||
if(rndr != null) {
|
||||
Log.info("Full world render of world '" + wname + "' already active.");
|
||||
return;
|
||||
}
|
||||
rndr = new FullWorldRenderState(world,l); /* Make new activation record */
|
||||
active_renders.put(wname, rndr); /* Add to active table */
|
||||
}
|
||||
rndr = new FullWorldRenderState(world,l); /* Make new activation record */
|
||||
active_renders.put(wname, rndr); /* Add to active table */
|
||||
/* Schedule first tile to be worked */
|
||||
scheduler.scheduleSyncDelayedTask(plug_in, rndr, (int)(timeslice_interval*20));
|
||||
Log.info("Full render starting on world '" + wname + "' (timesliced)...");
|
||||
renderpool.execute(rndr);
|
||||
Log.info("Full render starting on world '" + wname + "'...");
|
||||
}
|
||||
|
||||
public void activateWorld(World w) {
|
||||
|
|
@ -282,12 +289,15 @@ public class MapManager {
|
|||
|
||||
public void startRendering() {
|
||||
tileQueue.start();
|
||||
writeQueue.start();
|
||||
renderpool = new ScheduledThreadPoolExecutor(POOL_SIZE);
|
||||
}
|
||||
|
||||
public void stopRendering() {
|
||||
if(renderpool != null) {
|
||||
renderpool.shutdown();
|
||||
renderpool = null;
|
||||
}
|
||||
tileQueue.stop();
|
||||
writeQueue.stop();
|
||||
}
|
||||
|
||||
public boolean render(MapChunkCache cache, MapTile tile) {
|
||||
|
|
@ -333,13 +343,21 @@ public class MapManager {
|
|||
return world.updates.getUpdatedObjects(since);
|
||||
}
|
||||
|
||||
public void enqueueImageWrite(Runnable run) {
|
||||
ImageWriter handler = new ImageWriter();
|
||||
handler.run = run;
|
||||
writeQueue.push(handler);
|
||||
}
|
||||
|
||||
public boolean doSyncRender() {
|
||||
return true;
|
||||
/**
|
||||
* Render processor helper - used by code running on render threads to request chunk snapshot cache from server/sync thread
|
||||
*/
|
||||
public MapChunkCache createMapChunkCache(final World w, final DynmapChunk[] chunks) {
|
||||
Callable<MapChunkCache> job = new Callable<MapChunkCache>() {
|
||||
public MapChunkCache call() {
|
||||
return new MapChunkCache(w, chunks);
|
||||
}
|
||||
};
|
||||
Future<MapChunkCache> rslt = scheduler.callSyncMethod(plug_in, job);
|
||||
try {
|
||||
return rslt.get();
|
||||
} catch (Exception x) {
|
||||
Log.info("createMapChunk - " + x);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue