Skip to content

Commit

Permalink
General cleanup and refactoring: remove unused methods and fields, un…
Browse files Browse the repository at this point in the history
…used return types
  • Loading branch information
LunNova committed May 18, 2017
1 parent 9a2a6fc commit d24e30d
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 136 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ buildscript {
maven {
name = "nallar"
url = "https://repo.nallar.me/"
}
}
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 10, 'seconds'
resolutionStrategy.cacheChangingModulesFor 1, 'hours'
resolutionStrategy.cacheDynamicVersionsFor 1, 'hours'
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT'
Expand All @@ -38,15 +39,15 @@ def calculateVersion() {
return ver + "-dev"
}


version = calculateVersion()

repositories {
maven { url = 'https://repo.nallar.me/' }
}

configurations.all {
resolutionStrategy.cacheChangingModulesFor 10, 'seconds'
resolutionStrategy.cacheChangingModulesFor 1, 'hours'
resolutionStrategy.cacheDynamicVersionsFor 1, 'hours'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void sendChat(ICommandSender commandSender, String message) {
}
}

protected boolean requireOp() {
boolean requireOp() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void processCommand(final ICommandSender commandSender, List<String> argu
process(commandSender, arguments);
}

private boolean process(final ICommandSender commandSender, List<String> arguments) {
private void process(final ICommandSender commandSender, List<String> arguments) {
World world = null;
int time_;
Integer x = null;
Expand All @@ -56,17 +56,21 @@ private boolean process(final ICommandSender commandSender, List<String> argumen

switch (type) {
case PACKETS:
return PacketProfiler.profile(commandSender, time_);
PacketProfiler.profile(commandSender, time_);
return;
case UTILISATION:
return UtilisationProfiler.profile(commandSender, time_);
UtilisationProfiler.profile(commandSender, time_);
return;
case LOCK_CONTENTION:
int resolution = 240;
if (arguments.size() > 2) {
resolution = Integer.valueOf(arguments.get(2));
}
return ContentionProfiler.profile(commandSender, time_, resolution);
ContentionProfiler.profile(commandSender, time_, resolution);
return;
case LAG_SPIKE_DETECTOR:
return LagSpikeProfiler.profile(commandSender, time_);
LagSpikeProfiler.profile(commandSender, time_);
return;
}

if (arguments.size() > 2) {
Expand All @@ -84,7 +88,7 @@ private boolean process(final ICommandSender commandSender, List<String> argumen
}
} catch (UsageException e) {
sendChat(commandSender, getCommandUsage(commandSender));
return true;
return;
}

final List<World> worlds = new ArrayList<>();
Expand All @@ -97,14 +101,13 @@ private boolean process(final ICommandSender commandSender, List<String> argumen
final EntityTickProfiler entityTickProfiler = EntityTickProfiler.INSTANCE;
if (!entityTickProfiler.startProfiling(() -> sendChat(commandSender, entityTickProfiler.writeStringData(new TableFormatter(commandSender)).toString()), type, time, worlds)) {
sendChat(commandSender, "Someone else is currently profiling.");
return false;
return;
}
if (type == ProfilingState.CHUNK_ENTITIES) {
entityTickProfiler.setLocation(x, z);
}
sendChat(commandSender, "Profiling for " + time + " seconds in " + (world == null ? "all worlds " : Log.name(world))
+ (type == ProfilingState.CHUNK_ENTITIES ? " at " + x + ',' + z : ""));
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ public class ContentionProfiler {
private long ticks;
private long[] threads;

public ContentionProfiler(int seconds, int resolution) {
private ContentionProfiler(int seconds, int resolution) {
this.seconds = seconds;
this.resolution = resolution;
}

public static boolean profile(final ICommandSender commandSender, int seconds, int resolution) {
public static void profile(final ICommandSender commandSender, int seconds, int resolution) {
Command.sendChat(commandSender, "Performing lock contention profiling for " + seconds + " seconds.");
final ContentionProfiler contentionProfiler = new ContentionProfiler(seconds, resolution);
contentionProfiler.run(() -> {
TableFormatter tf = new TableFormatter(commandSender);
contentionProfiler.dump(tf, commandSender instanceof MinecraftServer ? 15 : 6);
Command.sendChat(commandSender, tf.toString());
});
return true;
}

private static String name(StackTraceElement stack) {
Expand All @@ -45,15 +44,15 @@ private static String name(StackTraceElement stack) {
return className.substring(className.lastIndexOf('.') + 1) + '.' + stack.getMethodName();
}

public void run(final Runnable completed) {
private void run(final Runnable completed) {
final int ticks = seconds * 1000 / resolution;
new Thread(() -> {
profile(ticks);
completed.run();
}, "Contention Profiler").start();
}

public void dump(final TableFormatter tf, int entries) {
private void dump(final TableFormatter tf, int entries) {
float ticks = this.ticks;
tf
.heading("Monitor")
Expand Down Expand Up @@ -165,7 +164,7 @@ public IntegerHolder get(Object k) {
}

private static class IntegerHolder implements Comparable<IntegerHolder> {
public int value;
int value;

IntegerHolder() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.world.World;
import net.minecraft.world.WorldProvider;
import org.codehaus.jackson.map.ObjectMapper;
import org.minimallycorrect.modpatcher.api.UsedByPatch;
import org.minimallycorrect.tickprofiler.Log;
import org.minimallycorrect.tickprofiler.minecraft.TickProfiler;
import org.minimallycorrect.tickprofiler.minecraft.commands.ProfileCommand;
Expand All @@ -20,15 +21,12 @@

public class EntityTickProfiler {
public static final EntityTickProfiler INSTANCE = new EntityTickProfiler();
public static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private final HashMap<Class<?>, AtomicInteger> invocationCount = new HashMap<>();
private final HashMap<Class<?>, AtomicLong> time = new HashMap<>();
private final HashMap<Object, AtomicLong> singleTime = new HashMap<>();
private final HashMap<Object, AtomicLong> singleInvocationCount = new HashMap<>();
private final AtomicLong totalTime = new AtomicLong();
private int lastCX = Integer.MIN_VALUE;
private int lastCZ = Integer.MIN_VALUE;
private boolean cachedActive = false;
private int ticks;
private volatile int chunkX;
private volatile int chunkZ;
Expand All @@ -37,15 +35,15 @@ public class EntityTickProfiler {
private EntityTickProfiler() {
}

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

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

Expand Down Expand Up @@ -125,6 +123,7 @@ public boolean startProfiling(final Runnable runnable, ProfileCommand.ProfilingS
return true;
}

@UsedByPatch("entityhook.xml")
public void profileEntity(World w, Entity entity) {
final boolean profile = profilingState == ProfileCommand.ProfilingState.ENTITIES || (entity.chunkCoordX == chunkX && entity.chunkCoordZ == chunkZ);

Expand All @@ -138,6 +137,7 @@ public void profileEntity(World w, Entity entity) {
record(entity, System.nanoTime() - start);
}

@UsedByPatch("entityhook.xml")
public void profileTickable(ITickable tickable) {
final boolean profile = profilingState == ProfileCommand.ProfilingState.ENTITIES || shouldProfilePos(((TileEntity) tickable).getPos());

Expand All @@ -155,7 +155,7 @@ private boolean shouldProfilePos(BlockPos pos) {
return pos.getX() >> 4 == chunkX && pos.getZ() >> 4 == chunkZ;
}

public void record(Object o, long time) {
private void record(Object o, long time) {
if (time < 0) {
time = 0;
}
Expand All @@ -167,7 +167,7 @@ public void record(Object o, long time) {
totalTime.addAndGet(time);
}

public void clear() {
private void clear() {
invocationCount.clear();
synchronized (time) {
time.clear();
Expand Down Expand Up @@ -214,7 +214,7 @@ public TableFormatter writeStringData(TableFormatter tf, int elements) {
return writeData(tf, elements);
}

public TableFormatter writeData(TableFormatter tf, int elements) {
private TableFormatter writeData(TableFormatter tf, int elements) {
Map<Class<?>, Long> time = new HashMap<>();
synchronized (this.time) {
for (Map.Entry<Class<?>, AtomicLong> entry : this.time.entrySet()) {
Expand Down Expand Up @@ -319,23 +319,15 @@ public ComparableLongHolder get(Object key_) {
private AtomicLong getSingleInvocationCount(Object o) {
AtomicLong t = singleInvocationCount.get(o);
if (t == null) {
t = singleInvocationCount.get(o);
if (t == null) {
t = new AtomicLong();
singleInvocationCount.put(o, t);
}
t = singleInvocationCount.computeIfAbsent(o, k -> new AtomicLong());
}
return t;
}

private AtomicInteger getInvocationCount(Class<?> clazz) {
AtomicInteger i = invocationCount.get(clazz);
if (i == null) {
i = invocationCount.get(clazz);
if (i == null) {
i = new AtomicInteger();
invocationCount.put(clazz, i);
}
i = invocationCount.computeIfAbsent(clazz, k -> new AtomicInteger());
}
return i;
}
Expand Down Expand Up @@ -364,9 +356,9 @@ private <T> AtomicLong getTime(T clazz, HashMap<T, AtomicLong> time) {
}

private static final class ChunkCoords {
public final int chunkXPos;
public final int chunkZPos;
public final int dimension;
final int chunkXPos;
final int chunkZPos;
final int dimension;

ChunkCoords(final int chunkXPos, final int chunkZPos, final int dimension) {
this.chunkXPos = chunkXPos;
Expand All @@ -386,7 +378,7 @@ public int hashCode() {
}

private class ComparableLongHolder implements Comparable<ComparableLongHolder> {
public long value;
long value;

ComparableLongHolder() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ private LagSpikeProfiler(ICommandSender commandSender, int time_) {
stopTime = System.nanoTime() + TimeUnit.SECONDS.toNanos(time_);
}

public static boolean profile(ICommandSender commandSender, int time_) {
public static void profile(ICommandSender commandSender, int time_) {
synchronized (LagSpikeProfiler.class) {
if (inProgress) {
Command.sendChat(commandSender, "Lag spike profiling is already in progress");
return false;
return;
}
inProgress = true;
}
Command.sendChat(commandSender, "Started lag spike detection for " + time_ + " seconds.");
new LagSpikeProfiler(commandSender, time_).start();
return true;
}

public static void tick(long nanoTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.command.ICommandSender;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketBuffer;
import org.minimallycorrect.modpatcher.api.UsedByPatch;
import org.minimallycorrect.tickprofiler.minecraft.commands.Command;
import org.minimallycorrect.tickprofiler.util.TableFormatter;

Expand All @@ -17,29 +18,25 @@ public class PacketProfiler {
private static final Map<String, AtomicInteger> count = new ConcurrentHashMap<>();
private static boolean profiling = false;

public static synchronized boolean profile(final ICommandSender commandSender, final int time) {
public static synchronized void profile(final ICommandSender commandSender, final int time) {
if (profiling) {
Command.sendChat(commandSender, "Someone else is already profiling packets.");
return false;
return;
}
profiling = true;
Command.sendChat(commandSender, "Profiling packets for " + time + " seconds.");
new Thread() {
@Override
public void run() {
try {
Thread.sleep(time * 1000);
} catch (InterruptedException ignored) {
}
Command.sendChat(commandSender, writeStats(new TableFormatter(commandSender)).toString());
synchronized (PacketProfiler.class) {
size.clear();
count.clear();
profiling = false;
}
new Thread(() -> {
try {
Thread.sleep(time * 1000);
} catch (InterruptedException ignored) {
}
Command.sendChat(commandSender, writeStats(new TableFormatter(commandSender)).toString());
synchronized (PacketProfiler.class) {
size.clear();
count.clear();
profiling = false;
}
}.start();
return true;
}).start();
}

private static <T> List<T> sortedKeys(Map<T, ? extends Comparable<?>> map, int elements) {
Expand Down Expand Up @@ -96,8 +93,7 @@ private static String humanReadableName(String name) {
return name;
}

// called from packethook.xml
@SuppressWarnings("unused")
@UsedByPatch("packethook.xml")
public static void record(final Packet packet, PacketBuffer buffer) {
if (!profiling) {
return;
Expand All @@ -113,18 +109,14 @@ private static AtomicInteger getAtomicInteger(String key, Map<String, AtomicInte
AtomicInteger t = map.get(key);
if (t == null) {
synchronized (map) {
t = map.get(key);
if (t == null) {
t = new AtomicInteger();
map.put(key, t);
}
t = map.computeIfAbsent(key, k -> new AtomicInteger());
}
}
return t;
}

// http://stackoverflow.com/a/3758880/250076
public static String humanReadableByteCount(int bytes) {
private static String humanReadableByteCount(int bytes) {
int unit = 1024;
if (bytes < unit) {
return bytes + " B";
Expand Down

0 comments on commit d24e30d

Please sign in to comment.