| 1 | package net.minecraft.command;
|
| 2 |
|
| 3 | import java.io.File;
|
| 4 | import java.io.FileWriter;
|
| 5 | import java.text.SimpleDateFormat;
|
| 6 | import java.util.Date;
|
| 7 | import java.util.List;
|
| 8 | import net.minecraft.profiler.Profiler;
|
| 9 | import net.minecraft.server.MinecraftServer;
|
| 10 | import net.minecraft.util.BlockPos;
|
| 11 | import org.apache.logging.log4j.LogManager;
|
| 12 | import org.apache.logging.log4j.Logger;
|
| 13 |
|
| 14 | public class CommandDebug extends CommandBase
|
| 15 | {
|
| 16 | private static final Logger logger = LogManager.getLogger();
|
| 17 |
|
| 18 | /** The time (in milliseconds) that profiling was started */
|
| 19 | private long profileStartTime;
|
| 20 |
|
| 21 | /** The tick number that profiling was started on */
|
| 22 | private int profileStartTick;
|
| 23 |
|
| 24 | /**
|
| 25 | * Gets the name of the command
|
| 26 | */
|
| 27 | public String getCommandName()
|
| 28 | {
|
| 29 | return "debug";
|
| 30 | }
|
| 31 |
|
| 32 | /**
|
| 33 | * Return the required permission level for this command.
|
| 34 | */
|
| 35 | public int getRequiredPermissionLevel()
|
| 36 | {
|
| 37 | return 3;
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Gets the usage string for the command.
|
| 42 | */
|
| 43 | public String getCommandUsage(ICommandSender sender)
|
| 44 | {
|
| 45 | return "commands.debug.usage";
|
| 46 | }
|
| 47 |
|
| 48 | /**
|
| 49 | * Callback when the command is invoked
|
| 50 | */
|
| 51 | public void processCommand(ICommandSender sender, String[] args) throws CommandException
|
| 52 | {
|
| 53 | if (args.length < 1)
|
| 54 | {
|
| 55 | throw new WrongUsageException("commands.debug.usage", new Object[0]);
|
| 56 | }
|
| 57 | else
|
| 58 | {
|
| 59 | if (args[0].equals("start"))
|
| 60 | {
|
| 61 | if (args.length != 1)
|
| 62 | {
|
| 63 | throw new WrongUsageException("commands.debug.usage", new Object[0]);
|
| 64 | }
|
| 65 |
|
| 66 | notifyOperators(sender, this, "commands.debug.start", new Object[0]);
|
| 67 | MinecraftServer.getServer().enableProfiling();
|
| 68 | this.profileStartTime = MinecraftServer.getCurrentTimeMillis();
|
| 69 | this.profileStartTick = MinecraftServer.getServer().getTickCounter();
|
| 70 | }
|
| 71 | else
|
| 72 | {
|
| 73 | if (!args[0].equals("stop"))
|
| 74 | {
|
| 75 | throw new WrongUsageException("commands.debug.usage", new Object[0]);
|
| 76 | }
|
| 77 |
|
| 78 | if (args.length != 1)
|
| 79 | {
|
| 80 | throw new WrongUsageException("commands.debug.usage", new Object[0]);
|
| 81 | }
|
| 82 |
|
| 83 | if (!MinecraftServer.getServer().theProfiler.profilingEnabled)
|
| 84 | {
|
| 85 | throw new CommandException("commands.debug.notStarted", new Object[0]);
|
| 86 | }
|
| 87 |
|
| 88 | long i = MinecraftServer.getCurrentTimeMillis();
|
| 89 | int j = MinecraftServer.getServer().getTickCounter();
|
| 90 | long k = i - this.profileStartTime;
|
| 91 | int l = j - this.profileStartTick;
|
| 92 | this.saveProfileResults(k, l);
|
| 93 | MinecraftServer.getServer().theProfiler.profilingEnabled = false;
|
| 94 | notifyOperators(sender, this, "commands.debug.stop", new Object[] {Float.valueOf((float)k / 1000.0F), Integer.valueOf(l)});
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | /**
|
| 100 | * Save the profiling results from the last profile
|
| 101 | */
|
| 102 | private void saveProfileResults(long timeSpan, int tickSpan)
|
| 103 | {
|
| 104 | File file1 = new File(MinecraftServer.getServer().getFile("debug"), "profile-results-" + (new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss")).format(new Date()) + ".txt");
|
| 105 | file1.getParentFile().mkdirs();
|
| 106 |
|
| 107 | try
|
| 108 | {
|
| 109 | FileWriter filewriter = new FileWriter(file1);
|
| 110 | filewriter.write(this.getProfileResults(timeSpan, tickSpan));
|
| 111 | filewriter.close();
|
| 112 | }
|
| 113 | catch (Throwable throwable)
|
| 114 | {
|
| 115 | logger.error("Could not save profiler results to " + file1, throwable);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | /**
|
| 120 | * Get the profiling results from the last profile
|
| 121 | */
|
| 122 | private String getProfileResults(long timeSpan, int tickSpan)
|
| 123 | {
|
| 124 | StringBuilder stringbuilder = new StringBuilder();
|
| 125 | stringbuilder.append("---- Minecraft Profiler Results ----\n");
|
| 126 | stringbuilder.append("// ");
|
| 127 | stringbuilder.append(getWittyComment());
|
| 128 | stringbuilder.append("\n\n");
|
| 129 | stringbuilder.append("Time span: ").append(timeSpan).append(" ms\n");
|
| 130 | stringbuilder.append("Tick span: ").append(tickSpan).append(" ticks\n");
|
| 131 | stringbuilder.append("// This is approximately ").append(String.format("%.2f", new Object[] {Float.valueOf((float)tickSpan / ((float)timeSpan / 1000.0F))})).append(" ticks per second. It should be ").append((int)20).append(" ticks per second\n\n");
|
| 132 | stringbuilder.append("--- BEGIN PROFILE DUMP ---\n\n");
|
| 133 | this.func_147202_a(0, "root", stringbuilder);
|
| 134 | stringbuilder.append("--- END PROFILE DUMP ---\n\n");
|
| 135 | return stringbuilder.toString();
|
| 136 | }
|
| 137 |
|
| 138 | private void func_147202_a(int p_147202_1_, String p_147202_2_, StringBuilder stringBuilder)
|
| 139 | {
|
| 140 | List<Profiler.Result> list = MinecraftServer.getServer().theProfiler.getProfilingData(p_147202_2_);
|
| 141 |
|
| 142 | if (list != null && list.size() >= 3)
|
| 143 | {
|
| 144 | for (int i = 1; i < list.size(); ++i)
|
| 145 | {
|
| 146 | Profiler.Result profiler$result = (Profiler.Result)list.get(i);
|
| 147 | stringBuilder.append(String.format("[%02d] ", new Object[] {Integer.valueOf(p_147202_1_)}));
|
| 148 |
|
| 149 | for (int j = 0; j < p_147202_1_; ++j)
|
| 150 | {
|
| 151 | stringBuilder.append(" ");
|
| 152 | }
|
| 153 |
|
| 154 | stringBuilder.append(profiler$result.field_76331_c).append(" - ").append(String.format("%.2f", new Object[] {Double.valueOf(profiler$result.field_76332_a)})).append("%/").append(String.format("%.2f", new Object[] {Double.valueOf(profiler$result.field_76330_b)})).append("%\n");
|
| 155 |
|
| 156 | if (!profiler$result.field_76331_c.equals("unspecified"))
|
| 157 | {
|
| 158 | try
|
| 159 | {
|
| 160 | this.func_147202_a(p_147202_1_ + 1, p_147202_2_ + "." + profiler$result.field_76331_c, stringBuilder);
|
| 161 | }
|
| 162 | catch (Exception exception)
|
| 163 | {
|
| 164 | stringBuilder.append("[[ EXCEPTION ").append((Object)exception).append(" ]]");
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | /**
|
| 172 | * Get a random witty comment
|
| 173 | */
|
| 174 | private static String getWittyComment()
|
| 175 | {
|
| 176 | String[] astring = new String[] {"Shiny numbers!", "Am I not running fast enough? :(", "I\'m working as hard as I can!", "Will I ever be good enough for you? :(", "Speedy. Zoooooom!", "Hello world", "40% better than a crash report.", "Now with extra numbers", "Now with less numbers", "Now with the same numbers", "You should add flames to things, it makes them go faster!", "Do you feel the need for... optimization?", "*cracks redstone whip*", "Maybe if you treated it better then it\'ll have more motivation to work faster! Poor server."};
|
| 177 |
|
| 178 | try
|
| 179 | {
|
| 180 | return astring[(int)(System.nanoTime() % (long)astring.length)];
|
| 181 | }
|
| 182 | catch (Throwable var2)
|
| 183 | {
|
| 184 | return "Witty comment unavailable :(";
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
|
| 189 | {
|
| 190 | return args.length == 1 ? getListOfStringsMatchingLastWord(args, new String[] {"start", "stop"}): null;
|
| 191 | }
|
| 192 | }
|
| 193 |
|