From 0ae51bbfb8d58d4d473a1fe162269176a0dcdf1a Mon Sep 17 00:00:00 2001 From: ProfMobius Date: Mon, 23 Dec 2013 04:34:09 +0100 Subject: [PATCH] Added skeleton for client side render tick timing. Fixed name lookup for ticks (will require an AT) --- .../opis/client/OpisClientTickHandler.java | 40 ++++++++-- .../client/CommandClientShowRenderTick.java | 38 ++++++++++ .../commands/client/CommandClientStart.java | 38 ++++++++++ .../data/client/TickHandlerClientManager.java | 57 ++++++++++++++ .../client/TickHandlerClientProfiler.java | 75 +++++++++++++++++++ .../opis/data/server/TickHandlerManager.java | 28 +++++-- .../opis/data/server/TickHandlerProfiler.java | 14 +++- mcp/mobius/opis/modOpis.java | 8 +- mcp/mobius/opis/network/ClientCommand.java | 5 ++ .../opis/network/ClientCommandHandler.java | 28 ++++++- 10 files changed, 311 insertions(+), 20 deletions(-) create mode 100644 mcp/mobius/opis/commands/client/CommandClientShowRenderTick.java create mode 100644 mcp/mobius/opis/commands/client/CommandClientStart.java create mode 100644 mcp/mobius/opis/data/client/TickHandlerClientManager.java create mode 100644 mcp/mobius/opis/data/client/TickHandlerClientProfiler.java diff --git a/mcp/mobius/opis/client/OpisClientTickHandler.java b/mcp/mobius/opis/client/OpisClientTickHandler.java index d6521e9..616af5b 100644 --- a/mcp/mobius/opis/client/OpisClientTickHandler.java +++ b/mcp/mobius/opis/client/OpisClientTickHandler.java @@ -1,28 +1,54 @@ package mcp.mobius.opis.client; import java.util.EnumSet; +import java.util.logging.Level; +import mcp.mobius.mobiuscore.profiler.DummyProfiler; +import mcp.mobius.mobiuscore.profiler.ProfilerRegistrar; +import mcp.mobius.opis.modOpis; import cpw.mods.fml.common.ITickHandler; import cpw.mods.fml.common.TickType; +import cpw.mods.fml.common.network.PacketDispatcher; public class OpisClientTickHandler implements ITickHandler { - @Override - public void tickStart(EnumSet type, Object... tickData) { - // TODO Auto-generated method stub + public long profilerUpdateTickCounter = 0; + public long profilerRunningTicks; - } + public static OpisClientTickHandler instance; + + public OpisClientTickHandler(){ + instance = this; + } + + @Override + public void tickStart(EnumSet type, Object... tickData) {} @Override public void tickEnd(EnumSet type, Object... tickData) { - if(type.contains(TickType.RENDER)){ + if(type.contains(TickType.CLIENT)){ + profilerUpdateTickCounter++; + + if (profilerRunningTicks < modOpis.profilerMaxTicks && modOpis.profilerRun){ + profilerRunningTicks++; + } + else if (profilerRunningTicks >= modOpis.profilerMaxTicks && modOpis.profilerRun){ + profilerRunningTicks = 0; + modOpis.profilerRun = false; + ProfilerRegistrar.registerProfilerTileEntity(new DummyProfiler()); + ProfilerRegistrar.registerProfilerEntity(new DummyProfiler()); + ProfilerRegistrar.registerProfilerTick(new DummyProfiler()); + + //TODO : Print something to the client chat + System.out.printf("Profiling done\n"); + //modOpis.log.log(Level.INFO, "Profiling done"); + } } - } @Override public EnumSet ticks() { - return EnumSet.of(TickType.RENDER); + return EnumSet.of(TickType.CLIENT); } @Override diff --git a/mcp/mobius/opis/commands/client/CommandClientShowRenderTick.java b/mcp/mobius/opis/commands/client/CommandClientShowRenderTick.java new file mode 100644 index 0000000..a0c51ee --- /dev/null +++ b/mcp/mobius/opis/commands/client/CommandClientShowRenderTick.java @@ -0,0 +1,38 @@ +package mcp.mobius.opis.commands.client; + +import mcp.mobius.opis.network.ClientCommand; +import mcp.mobius.opis.network.server.Packet_ClientCommand; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayerMP; + +public class CommandClientShowRenderTick extends CommandBase { + + @Override + public String getCommandName() { + return "opis_ctick"; + } + + @Override + public String getCommandUsage(ICommandSender icommandsender) { + return ""; + } + + @Override + public void processCommand(ICommandSender icommandsender, String[] astring) { + ((EntityPlayerMP)icommandsender).playerNetServerHandler.sendPacketToPlayer(Packet_ClientCommand.create(ClientCommand.SHOW_RENDER_TICK)); + } + + @Override + public int getRequiredPermissionLevel() + { + return 0; + } + + @Override + public boolean canCommandSenderUseCommand(ICommandSender sender) + { + return true; + } + +} diff --git a/mcp/mobius/opis/commands/client/CommandClientStart.java b/mcp/mobius/opis/commands/client/CommandClientStart.java new file mode 100644 index 0000000..5fe23ab --- /dev/null +++ b/mcp/mobius/opis/commands/client/CommandClientStart.java @@ -0,0 +1,38 @@ +package mcp.mobius.opis.commands.client; + +import mcp.mobius.opis.network.ClientCommand; +import mcp.mobius.opis.network.server.Packet_ClientCommand; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayerMP; + +public class CommandClientStart extends CommandBase { + + @Override + public String getCommandName() { + return "opis_cstart"; + } + + @Override + public String getCommandUsage(ICommandSender icommandsender) { + return ""; + } + + @Override + public void processCommand(ICommandSender icommandsender, String[] astring) { + ((EntityPlayerMP)icommandsender).playerNetServerHandler.sendPacketToPlayer(Packet_ClientCommand.create(ClientCommand.START_PROFILING)); + } + + @Override + public int getRequiredPermissionLevel() + { + return 0; + } + + @Override + public boolean canCommandSenderUseCommand(ICommandSender sender) + { + return true; + } + +} diff --git a/mcp/mobius/opis/data/client/TickHandlerClientManager.java b/mcp/mobius/opis/data/client/TickHandlerClientManager.java new file mode 100644 index 0000000..721c21d --- /dev/null +++ b/mcp/mobius/opis/data/client/TickHandlerClientManager.java @@ -0,0 +1,57 @@ +package mcp.mobius.opis.data.client; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +import cpw.mods.fml.common.IScheduledTickHandler; +import cpw.mods.fml.common.SingleIntervalHandler; +import mcp.mobius.opis.data.holders.TickHandlerStats; + +public class TickHandlerClientManager { + + public static HashMap startStats = new HashMap(); + public static HashMap endStats = new HashMap(); + + public static void addHandlerStart(IScheduledTickHandler handler, long timing){ + String name = getHandlerName(handler); + + if (!(startStats.containsKey(name))) + startStats.put(name, new TickHandlerStats(name)); + startStats.get(name).addMeasure(timing); + } + + public static void addHandlerEnd(IScheduledTickHandler handler, long timing){ + String name = getHandlerName(handler); + + if (!(endStats.containsKey(name))) + endStats.put(name, new TickHandlerStats(name)); + endStats.get(name).addMeasure(timing); + } + + public static ArrayList getCumulatedStats(){ + HashMap totalStats = new HashMap(); + + for (String key : startStats.keySet()){ + totalStats.put(key, new TickHandlerStats(key)); + totalStats.get(key).setGeometricMean(startStats.get(key).getGeometricMean() + endStats.get(key).getGeometricMean()); + } + + ArrayList sortedStats = new ArrayList(totalStats.values()); + Collections.sort(sortedStats); + + return sortedStats; + } + + public static String getHandlerName(IScheduledTickHandler handler){ + String name = handler.getLabel(); + if (name == null || name.equals("")) + if (handler instanceof SingleIntervalHandler) + name = ((SingleIntervalHandler)handler).wrapped.getClass().getName(); + else + name = handler.getClass().getName(); + + return name; + } + +} diff --git a/mcp/mobius/opis/data/client/TickHandlerClientProfiler.java b/mcp/mobius/opis/data/client/TickHandlerClientProfiler.java new file mode 100644 index 0000000..3798116 --- /dev/null +++ b/mcp/mobius/opis/data/client/TickHandlerClientProfiler.java @@ -0,0 +1,75 @@ +package mcp.mobius.opis.data.client; + +import java.util.EnumSet; + +import mcp.mobius.mobiuscore.profiler.IProfilerTick; +import mcp.mobius.opis.modOpis; +import mcp.mobius.opis.client.OpisClientTickHandler; +import mcp.mobius.opis.data.server.TickHandlerManager; +import mcp.mobius.opis.server.OpisServerTickHandler; +import cpw.mods.fml.common.IScheduledTickHandler; +import cpw.mods.fml.common.TickType; + +public class TickHandlerClientProfiler implements IProfilerTick { + + public class Clock{ + public long startTime = 0; + public long timeDelta = 0; + public void start(){this.startTime = System.nanoTime();} + public void stop(){this.timeDelta = System.nanoTime() - this.startTime;} + } + + private String currentHandler = null; + private Clock clock = new Clock(); + + @Override + public void StartTickStart(IScheduledTickHandler ticker, EnumSet ticksToRun) { + //if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) return; + if ((!modOpis.profilerRun) || (OpisClientTickHandler.instance.profilerUpdateTickCounter % modOpis.profilerDelay != 0)) return; + if (!ticksToRun.contains(TickType.RENDER)) return; + + String name = TickHandlerClientManager.getHandlerName(ticker); + this.currentHandler = name; + this.clock.start(); + } + + @Override + public void StopTickStart(IScheduledTickHandler ticker, EnumSet ticksToRun) { + if ((!modOpis.profilerRun) || (OpisClientTickHandler.instance.profilerUpdateTickCounter % modOpis.profilerDelay != 0)) return; + if (!ticksToRun.contains(TickType.RENDER)) return; + this.clock.stop(); + + String name = TickHandlerClientManager.getHandlerName(ticker); + if (!this.currentHandler.equals(name)) + throw new RuntimeException(String.format("Mismatched tick handler during the profiling ! %s %s", this.currentHandler, ticker.getLabel())); + + //EntityManager.addEntity(ent, this.clock.timeDelta); + TickHandlerClientManager.addHandlerStart(ticker, this.clock.timeDelta); + this.currentHandler = null; + } + + @Override + public void StartTickEnd(IScheduledTickHandler ticker, EnumSet ticksToRun) { + if ((!modOpis.profilerRun) || (OpisClientTickHandler.instance.profilerUpdateTickCounter % modOpis.profilerDelay != 0)) return; + if (!ticksToRun.contains(TickType.RENDER)) return; + + String name = TickHandlerClientManager.getHandlerName(ticker); + this.currentHandler = name; + this.clock.start(); + } + + @Override + public void StopTickEnd(IScheduledTickHandler ticker, EnumSet ticksToRun) { + if ((!modOpis.profilerRun) || (OpisClientTickHandler.instance.profilerUpdateTickCounter % modOpis.profilerDelay != 0)) return; + if (!ticksToRun.contains(TickType.RENDER)) return; + this.clock.stop(); + + String name = TickHandlerClientManager.getHandlerName(ticker); + if (!this.currentHandler.equals(name)) + throw new RuntimeException(String.format("Mismatched tick handler during the profiling ! %s %s", this.currentHandler, ticker.getLabel())); + + TickHandlerClientManager.addHandlerEnd(ticker, this.clock.timeDelta); + this.currentHandler = null; + } + +} diff --git a/mcp/mobius/opis/data/server/TickHandlerManager.java b/mcp/mobius/opis/data/server/TickHandlerManager.java index 4c5bfec..c9405aa 100644 --- a/mcp/mobius/opis/data/server/TickHandlerManager.java +++ b/mcp/mobius/opis/data/server/TickHandlerManager.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.HashMap; import cpw.mods.fml.common.IScheduledTickHandler; +import cpw.mods.fml.common.SingleIntervalHandler; import mcp.mobius.opis.data.holders.TickHandlerStats; public class TickHandlerManager { @@ -13,15 +14,19 @@ public class TickHandlerManager { public static HashMap endStats = new HashMap(); public static void addHandlerStart(IScheduledTickHandler handler, long timing){ - if (!(startStats.containsKey(handler.getLabel()))) - startStats.put(handler.getLabel(), new TickHandlerStats(handler.getLabel())); - startStats.get(handler.getLabel()).addMeasure(timing); + String name = getHandlerName(handler); + + if (!(startStats.containsKey(name))) + startStats.put(name, new TickHandlerStats(name)); + startStats.get(name).addMeasure(timing); } public static void addHandlerEnd(IScheduledTickHandler handler, long timing){ - if (!(endStats.containsKey(handler.getLabel()))) - endStats.put(handler.getLabel(), new TickHandlerStats(handler.getLabel())); - endStats.get(handler.getLabel()).addMeasure(timing); + String name = getHandlerName(handler); + + if (!(endStats.containsKey(name))) + endStats.put(name, new TickHandlerStats(name)); + endStats.get(name).addMeasure(timing); } public static ArrayList getCumulatedStats(){ @@ -38,5 +43,14 @@ public class TickHandlerManager { return sortedStats; } - + public static String getHandlerName(IScheduledTickHandler handler){ + String name = handler.getLabel(); + if (name == null || name.equals("")) + if (handler instanceof SingleIntervalHandler) + name = ((SingleIntervalHandler)handler).wrapped.getClass().getName(); + else + name = handler.getClass().getName(); + + return name; + } } diff --git a/mcp/mobius/opis/data/server/TickHandlerProfiler.java b/mcp/mobius/opis/data/server/TickHandlerProfiler.java index 24126c1..450b3b1 100644 --- a/mcp/mobius/opis/data/server/TickHandlerProfiler.java +++ b/mcp/mobius/opis/data/server/TickHandlerProfiler.java @@ -25,7 +25,9 @@ public class TickHandlerProfiler implements IProfilerTick { //if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) return; if ((!modOpis.profilerRun) || (OpisServerTickHandler.instance.profilerUpdateTickCounter % modOpis.profilerDelay != 0)) return; if (!ticksToRun.contains(TickType.SERVER)) return; - this.currentHandler = ticker.getLabel(); + + String name = TickHandlerManager.getHandlerName(ticker); + this.currentHandler = name; this.clock.start(); } @@ -35,7 +37,8 @@ public class TickHandlerProfiler implements IProfilerTick { if (!ticksToRun.contains(TickType.SERVER)) return; this.clock.stop(); - if (!this.currentHandler.equals(ticker.getLabel())) + String name = TickHandlerManager.getHandlerName(ticker); + if (!this.currentHandler.equals(name)) throw new RuntimeException(String.format("Mismatched tick handler during the profiling ! %s %s", this.currentHandler, ticker.getLabel())); //EntityManager.addEntity(ent, this.clock.timeDelta); @@ -47,7 +50,9 @@ public class TickHandlerProfiler implements IProfilerTick { public void StartTickEnd(IScheduledTickHandler ticker, EnumSet ticksToRun) { if ((!modOpis.profilerRun) || (OpisServerTickHandler.instance.profilerUpdateTickCounter % modOpis.profilerDelay != 0)) return; if (!ticksToRun.contains(TickType.SERVER)) return; - this.currentHandler = ticker.getLabel(); + + String name = TickHandlerManager.getHandlerName(ticker); + this.currentHandler = name; this.clock.start(); } @@ -57,7 +62,8 @@ public class TickHandlerProfiler implements IProfilerTick { if (!ticksToRun.contains(TickType.SERVER)) return; this.clock.stop(); - if (!this.currentHandler.equals(ticker.getLabel())) + String name = TickHandlerManager.getHandlerName(ticker); + if (!this.currentHandler.equals(name)) throw new RuntimeException(String.format("Mismatched tick handler during the profiling ! %s %s", this.currentHandler, ticker.getLabel())); TickHandlerManager.addHandlerEnd(ticker, this.clock.timeDelta); diff --git a/mcp/mobius/opis/modOpis.java b/mcp/mobius/opis/modOpis.java index b732d0e..20312b5 100644 --- a/mcp/mobius/opis/modOpis.java +++ b/mcp/mobius/opis/modOpis.java @@ -8,6 +8,8 @@ import net.minecraftforge.common.MinecraftForge; import mcp.mobius.mobiuscore.profiler.ProfilerRegistrar; import mcp.mobius.opis.client.OpisClientEventHandler; import mcp.mobius.opis.client.OpisClientTickHandler; +import mcp.mobius.opis.commands.client.CommandClientShowRenderTick; +import mcp.mobius.opis.commands.client.CommandClientStart; import mcp.mobius.opis.commands.client.CommandClientTest; import mcp.mobius.opis.commands.server.CommandAmountEntities; import mcp.mobius.opis.commands.server.CommandChunkDump; @@ -89,6 +91,7 @@ public class modOpis { @EventHandler public void load(FMLInitializationEvent event) { TickRegistry.registerTickHandler(new OpisServerTickHandler(), Side.SERVER); + TickRegistry.registerTickHandler(new OpisClientTickHandler(), Side.CLIENT); } @EventHandler @@ -115,8 +118,11 @@ public class modOpis { event.registerServerCommand(new CommandHandler()); event.registerServerCommand(new CommandClientTest()); + event.registerServerCommand(new CommandClientStart()); + event.registerServerCommand(new CommandClientShowRenderTick()); + + event.registerServerCommand(new CommandHelp()); - //event.registerServerCommand(new CommandHelp()); //event.registerServerCommand(new CommandTPS()); GameRegistry.registerPlayerTracker(new OpisPlayerTracker()); diff --git a/mcp/mobius/opis/network/ClientCommand.java b/mcp/mobius/opis/network/ClientCommand.java index d7dec0c..3be8845 100644 --- a/mcp/mobius/opis/network/ClientCommand.java +++ b/mcp/mobius/opis/network/ClientCommand.java @@ -2,4 +2,9 @@ package mcp.mobius.opis.network; public class ClientCommand { public static byte TEST_CMD = 0x00; + public static byte START_PROFILING = 0x01; + public static byte STOP_PROFILING = 0x02; + public static byte RESET_PROFILING = 0x03; + + public static byte SHOW_RENDER_TICK = 0x04; } diff --git a/mcp/mobius/opis/network/ClientCommandHandler.java b/mcp/mobius/opis/network/ClientCommandHandler.java index e13361d..4e13d2d 100644 --- a/mcp/mobius/opis/network/ClientCommandHandler.java +++ b/mcp/mobius/opis/network/ClientCommandHandler.java @@ -1,5 +1,15 @@ package mcp.mobius.opis.network; +import java.util.ArrayList; +import java.util.logging.Level; + +import mcp.mobius.mobiuscore.profiler.ProfilerRegistrar; +import mcp.mobius.opis.modOpis; +import mcp.mobius.opis.client.OpisClientTickHandler; +import mcp.mobius.opis.data.client.TickHandlerClientManager; +import mcp.mobius.opis.data.client.TickHandlerClientProfiler; +import mcp.mobius.opis.data.holders.TickHandlerStats; + public class ClientCommandHandler { private static ClientCommandHandler _instance; @@ -12,6 +22,22 @@ public class ClientCommandHandler { } public void handle(byte cmd){ - System.out.printf("Received client cmd with id %s\n", cmd); + if (cmd == ClientCommand.START_PROFILING){ + modOpis.log.log(Level.INFO, "Started profiling"); + + modOpis.profilerRun = true; + OpisClientTickHandler.instance.profilerRunningTicks = 0; + TickHandlerClientManager.startStats.clear(); + TickHandlerClientManager.endStats.clear(); + + ProfilerRegistrar.registerProfilerTick(new TickHandlerClientProfiler()); + } + else if (cmd == ClientCommand.SHOW_RENDER_TICK){ + modOpis.log.log(Level.INFO, "=== RENDER TICK ==="); + ArrayList stats = TickHandlerClientManager.getCumulatedStats(); + for (TickHandlerStats stat : stats){ + System.out.printf("%s \n", stat); + } + } } }