Skip to content

Commit

Permalink
Refactor EntityList and EntityTickProfiler, fix NaN/Infinite values b…
Browse files Browse the repository at this point in the history
…eing formatted incorrectly, fix ticks being set too high when profiling multiple worlds.

Signed-off-by: Ross Allan <rallanpcl@gmail.com>
  • Loading branch information
LunNova committed May 29, 2013
1 parent e77fdf3 commit 6a4708a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
9 changes: 7 additions & 2 deletions src/common/me/nallar/tickprofiler/minecraft/TickProfiler.java
Expand Up @@ -162,6 +162,7 @@ private static class ProfilingScheduledTickHandler implements IScheduledTickHand
private static final EnumSet<TickType> TICKS = EnumSet.of(TickType.SERVER);
private final int profilingInterval;
private final File profilingFile;
private int counter = 0;

public ProfilingScheduledTickHandler(final int profilingInterval, final File profilingFile) {
this.profilingInterval = profilingInterval;
Expand All @@ -170,12 +171,16 @@ public ProfilingScheduledTickHandler(final int profilingInterval, final File pro

@Override
public int nextTickSpacing() {
return profilingInterval * 60 * 20;
return 1;
}

@Override
public void tickStart(final EnumSet<TickType> type, final Object... tickData) {
final EntityTickProfiler entityTickProfiler = EntityList.ENTITY_TICK_PROFILER;
final EntityTickProfiler entityTickProfiler = EntityTickProfiler.ENTITY_TICK_PROFILER;
entityTickProfiler.tick();
if (counter++ % profilingInterval * 60 * 20 != 0) {
return;
}
entityTickProfiler.startProfiling(new Runnable() {
@Override
public void run() {
Expand Down
Expand Up @@ -6,7 +6,6 @@

import me.nallar.tickprofiler.Log;
import me.nallar.tickprofiler.minecraft.TickProfiler;
import me.nallar.tickprofiler.minecraft.entitylist.EntityList;
import me.nallar.tickprofiler.minecraft.profiling.EntityTickProfiler;
import me.nallar.tickprofiler.util.TableFormatter;
import net.minecraft.command.ICommandSender;
Expand Down Expand Up @@ -66,7 +65,7 @@ public void processCommand(final ICommandSender commandSender, List<String> argu
worlds.add(world);
}
final int time = time_;
final EntityTickProfiler entityTickProfiler = EntityList.ENTITY_TICK_PROFILER;
final EntityTickProfiler entityTickProfiler = EntityTickProfiler.ENTITY_TICK_PROFILER;
if (!entityTickProfiler.startProfiling(new Runnable() {
@Override
public void run() {
Expand Down
Expand Up @@ -12,15 +12,12 @@
import me.nallar.tickprofiler.minecraft.commands.ProfileCommand;
import me.nallar.tickprofiler.minecraft.profiling.EntityTickProfiler;
import me.nallar.tickprofiler.util.contextaccess.ContextAccess;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;

/*
* Used to override World.loadedTile/EntityList.
* */
public abstract class EntityList<T> extends ArrayList<T> {
public static final EntityTickProfiler ENTITY_TICK_PROFILER = new EntityTickProfiler();
public static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private static final ContextAccess contextAccess = ContextAccess.$;
protected final ArrayList<T> innerList;
protected final World world;
Expand Down Expand Up @@ -53,18 +50,6 @@ public void unhook() throws IllegalAccessException {

public abstract void tick();

public static synchronized boolean startProfiling(ProfileCommand.ProfilingState profilingState_) {
if (profilingState != ProfileCommand.ProfilingState.NONE) {
return false;
}
profilingState = profilingState_;
return true;
}

public static synchronized void endProfiling() {
profilingState = ProfileCommand.ProfilingState.NONE;
}

@Override
public void trimToSize() {
innerList.trimToSize();
Expand All @@ -77,7 +62,7 @@ public void ensureCapacity(final int minCapacity) {

@Override
public int size() {
if (profilingState == ProfileCommand.ProfilingState.NONE || !World.class.isAssignableFrom(contextAccess.getContext(1)) || !World.class.isAssignableFrom(contextAccess.getContext(2))) {
if (EntityTickProfiler.profilingState == ProfileCommand.ProfilingState.NONE || !World.class.isAssignableFrom(contextAccess.getContext(1)) || !World.class.isAssignableFrom(contextAccess.getContext(2))) {
return innerList.size();
}
tick();
Expand Down Expand Up @@ -186,7 +171,7 @@ public ListIterator<T> listIterator() {

@Override
public Iterator<T> iterator() {
if (profilingState == ProfileCommand.ProfilingState.NONE || !World.class.isAssignableFrom(contextAccess.getContext(1)) || !World.class.isAssignableFrom(contextAccess.getContext(2))) {
if (EntityTickProfiler.profilingState == ProfileCommand.ProfilingState.NONE || !World.class.isAssignableFrom(contextAccess.getContext(1)) || !World.class.isAssignableFrom(contextAccess.getContext(2))) {
return innerList.iterator();
}
tick();
Expand Down
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;

import me.nallar.tickprofiler.minecraft.profiling.EntityTickProfiler;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;

Expand All @@ -12,7 +13,7 @@ public LoadedEntityList(World world, Field overriddenField) {

@Override
public void tick() {
ENTITY_TICK_PROFILER.tick();
ENTITY_TICK_PROFILER.runEntities(world, innerList);
EntityTickProfiler.ENTITY_TICK_PROFILER.tick();
EntityTickProfiler.ENTITY_TICK_PROFILER.runEntities(world, innerList);
}
}
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;

import me.nallar.tickprofiler.minecraft.profiling.EntityTickProfiler;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

Expand All @@ -12,6 +13,6 @@ public LoadedTileEntityList(World world, Field overriddenField) {

@Override
public void tick() {
ENTITY_TICK_PROFILER.runTileEntities(world, innerList);
EntityTickProfiler.ENTITY_TICK_PROFILER.runTileEntities(world, innerList);
}
}
Expand Up @@ -14,7 +14,6 @@

import me.nallar.tickprofiler.minecraft.TickProfiler;
import me.nallar.tickprofiler.minecraft.commands.ProfileCommand;
import me.nallar.tickprofiler.minecraft.entitylist.EntityList;
import me.nallar.tickprofiler.util.MappingUtil;
import me.nallar.tickprofiler.util.TableFormatter;
import net.minecraft.crash.CrashReport;
Expand All @@ -28,11 +27,28 @@
import org.cliffc.high_scale_lib.NonBlockingHashMap;

public class EntityTickProfiler {
public static final EntityTickProfiler ENTITY_TICK_PROFILER = new EntityTickProfiler();
public static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private int ticks;
private final AtomicLong totalTime = new AtomicLong();
private volatile int chunkX;
private volatile int chunkZ;

private EntityTickProfiler() {
}

public static synchronized boolean startProfiling(ProfileCommand.ProfilingState profilingState_) {
if (profilingState != ProfileCommand.ProfilingState.NONE) {
return false;
}
profilingState = profilingState_;
return true;
}

public static synchronized void endProfiling() {
profilingState = ProfileCommand.ProfilingState.NONE;
}

public void setLocation(final int x, final int z) {
chunkX = x;
chunkZ = z;
Expand All @@ -43,8 +59,8 @@ public boolean startProfiling(final Runnable runnable, ProfileCommand.ProfilingS
throw new IllegalArgumentException("time must be > 0");
}
final Collection<World> worlds = new ArrayList<World>(worlds_);
synchronized (EntityList.class) {
if (!EntityList.startProfiling(state)) {
synchronized (EntityTickProfiler.class) {
if (!startProfiling(state)) {
return false;
}
for (World world_ : worlds) {
Expand All @@ -60,8 +76,8 @@ public void run() {
} catch (InterruptedException ignored) {
}

synchronized (EntityList.class) {
EntityList.endProfiling();
synchronized (EntityTickProfiler.class) {
endProfiling();
runnable.run();
clear();
for (World world_ : worlds) {
Expand All @@ -79,7 +95,7 @@ public void run() {
public void runEntities(World world, ArrayList<Entity> toTick) {
long end = System.nanoTime();
long start;
boolean isGlobal = EntityList.profilingState == ProfileCommand.ProfilingState.GLOBAL;
boolean isGlobal = profilingState == ProfileCommand.ProfilingState.GLOBAL;
for (int i = 0; i < toTick.size(); i++) {
Entity entity = toTick.get(i);

Expand Down Expand Up @@ -133,7 +149,7 @@ public void runTileEntities(World world, ArrayList<TileEntity> toTick) {
Iterator<TileEntity> iterator = toTick.iterator();
long end = System.nanoTime();
long start;
boolean isGlobal = EntityList.profilingState == ProfileCommand.ProfilingState.GLOBAL;
boolean isGlobal = profilingState == ProfileCommand.ProfilingState.GLOBAL;
while (iterator.hasNext()) {
start = end;
TileEntity tileEntity = iterator.next();
Expand Down Expand Up @@ -192,7 +208,9 @@ public void clear() {
}

public void tick() {
ticks++;
if (profilingState != ProfileCommand.ProfilingState.NONE) {
ticks++;
}
}

public TableFormatter writeData(TableFormatter tf) {
Expand Down
3 changes: 3 additions & 0 deletions src/common/me/nallar/tickprofiler/util/TableFormatter.java
Expand Up @@ -105,6 +105,9 @@ private int getMaxLengths(double[] rowLengths, int rowIndex, int rowCount, Itera
* http://stackoverflow.com/a/10554128/250076
*/
public static String formatDoubleWithPrecision(double val, int precision) {
if (Double.isInfinite(val) || Double.isNaN(val)) {
return Double.toString(val);
}
StringBuilder sb = new StringBuilder();
if (val < 0) {
sb.append('-');
Expand Down

0 comments on commit 6a4708a

Please sign in to comment.