-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
TickProfiler.java
executable file
·213 lines (193 loc) · 9.28 KB
/
TickProfiler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package me.nallar.tickprofiler.minecraft;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import cpw.mods.fml.common.IScheduledTickHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;
import me.nallar.tickprofiler.Log;
import me.nallar.tickprofiler.minecraft.commands.Command;
import me.nallar.tickprofiler.minecraft.commands.DumpCommand;
import me.nallar.tickprofiler.minecraft.commands.ProfileCommand;
import me.nallar.tickprofiler.minecraft.entitylist.EntityList;
import me.nallar.tickprofiler.minecraft.entitylist.LoadedEntityList;
import me.nallar.tickprofiler.minecraft.entitylist.LoadedTileEntityList;
import me.nallar.tickprofiler.minecraft.profiling.EntityTickProfiler;
import me.nallar.tickprofiler.reporting.Metrics;
import me.nallar.tickprofiler.util.ReflectUtil;
import me.nallar.tickprofiler.util.TableFormatter;
import me.nallar.tickprofiler.util.VersionUtil;
import net.minecraft.command.ServerCommandManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.PacketCount;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@SuppressWarnings ("WeakerAccess")
@Mod (modid = "TickProfiler", name = "TickProfiler", version = "@MOD_VERSION@")
@NetworkMod (clientSideRequired = false, serverSideRequired = false)
public class TickProfiler {
@Mod.Instance
public static TickProfiler instance;
private static final int loadedEntityFieldIndex = 0;
private static final int loadedTileEntityFieldIndex = 2;
public boolean requireOpForProfileCommand = true;
public boolean requireOpForDumpCommand = true;
private int profilingInterval = 0;
private String profilingFileName = "world/computer/<computer id>/profile.txt";
private boolean profilingJson = false;
static {
new Metrics("TickProfiler", VersionUtil.versionNumber());
}
public TickProfiler() {
Log.LOGGER.getLevel(); // Force log class to load
}
@Mod.Init
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
TickRegistry.registerScheduledTickHandler(new ProfilingScheduledTickHandler(profilingInterval, new File(".", profilingFileName), profilingJson), Side.SERVER);
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod")
@Mod.PreInit
public void preInit(FMLPreInitializationEvent event) {
Configuration config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
String GENERAL = Configuration.CATEGORY_GENERAL;
ProfileCommand.name = config.get(GENERAL, "profileCommandName", ProfileCommand.name, "Name of the command to be used for profiling reports.").value;
DumpCommand.name = config.get(GENERAL, "dumpCommandName", DumpCommand.name, "Name of the command to be used for profiling reports.").value;
requireOpForProfileCommand = config.get(GENERAL, "requireOpForProfileCommand", requireOpForProfileCommand, "If a player must be opped to use /profile").getBoolean(requireOpForProfileCommand);
requireOpForDumpCommand = config.get(GENERAL, "requireOpForProfileCommand", requireOpForDumpCommand, "If a player must be opped to use /dump").getBoolean(requireOpForDumpCommand);
profilingInterval = config.get(GENERAL, "profilingInterval", profilingInterval, "Interval, in minutes, to record profiling information to disk. 0 = never. Recommended >= 2.").getInt();
profilingFileName = config.get(GENERAL, "profilingFileName", profilingFileName, "Location to store profiling information to, relative to the server folder. For example, why not store it in a computercraft computer's folder?").value;
profilingJson = config.get(GENERAL, "profilingJson", profilingJson, "Whether to write periodic profiling in JSON format").getBoolean(profilingJson);
config.save();
PacketCount.allowCounting = false;
}
@Mod.ServerStarting
public void serverStarting(FMLServerStartingEvent event) {
ServerCommandManager serverCommandManager = (ServerCommandManager) event.getServer().getCommandManager();
serverCommandManager.registerCommand(new ProfileCommand());
serverCommandManager.registerCommand(new DumpCommand());
Command.checkForPermissions();
}
public synchronized void hookProfiler(World world) {
if (world.isRemote) {
Log.severe("World " + Log.name(world) + " seems to be a client world", new Throwable());
}
try {
Field loadedTileEntityField = ReflectUtil.getFields(World.class, List.class)[loadedTileEntityFieldIndex];
new LoadedTileEntityList(world, loadedTileEntityField);
Field loadedEntityField = ReflectUtil.getFields(World.class, List.class)[loadedEntityFieldIndex];
new LoadedEntityList(world, loadedEntityField);
Log.finer("Profiling hooked for world " + Log.name(world));
} catch (Exception e) {
Log.severe("Failed to initialise profiling for world " + Log.name(world), e);
}
}
public synchronized void unhookProfiler(World world) {
if (world.isRemote) {
Log.severe("World " + Log.name(world) + " seems to be a client world", new Throwable());
}
try {
Field loadedTileEntityField = ReflectUtil.getFields(World.class, List.class)[loadedTileEntityFieldIndex];
Object loadedTileEntityList = loadedTileEntityField.get(world);
if (loadedTileEntityList instanceof EntityList) {
((EntityList) loadedTileEntityList).unhook();
} else {
Log.severe("Looks like another mod broke TickProfiler's replacement tile entity list in world: " + Log.name(world));
}
Field loadedEntityField = ReflectUtil.getFields(World.class, List.class)[loadedEntityFieldIndex];
Object loadedEntityList = loadedEntityField.get(world);
if (loadedEntityList instanceof EntityList) {
((EntityList) loadedEntityList).unhook();
} else {
Log.severe("Looks like another mod broke TickProfiler's replacement entity list in world: " + Log.name(world));
}
Log.finer("Profiling unhooked for world " + Log.name(world));
} catch (Exception e) {
Log.severe("Failed to unload TickProfiler for world " + Log.name(world), e);
}
}
@ForgeSubscribe
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {
EntityPlayer entityPlayer = event.entityPlayer;
ItemStack usedItem = entityPlayer.getCurrentEquippedItem();
if (usedItem != null) {
Item usedItemType = usedItem.getItem();
if (usedItemType == Item.pocketSundial && (!requireOpForDumpCommand || entityPlayer.canCommandSenderUseCommand(4, "dump"))) {
Command.sendChat(entityPlayer, DumpCommand.dump(new TableFormatter(entityPlayer), entityPlayer.worldObj, event.x, event.y, event.z, 35).toString());
event.setCanceled(true);
}
}
}
}
private static class ProfilingScheduledTickHandler implements IScheduledTickHandler {
private static final EnumSet<TickType> TICKS = EnumSet.of(TickType.SERVER);
private final int profilingInterval;
private final File profilingFile;
private final boolean json;
private int counter = 0;
public ProfilingScheduledTickHandler(final int profilingInterval, final File profilingFile, final boolean json) {
this.profilingInterval = profilingInterval;
this.profilingFile = profilingFile;
this.json = json;
}
@Override
public int nextTickSpacing() {
return 1;
}
@Override
public void tickStart(final EnumSet<TickType> type, final Object... tickData) {
final EntityTickProfiler entityTickProfiler = EntityTickProfiler.ENTITY_TICK_PROFILER;
entityTickProfiler.tick();
int profilingInterval = this.profilingInterval;
if (profilingInterval <= 0 || counter++ % (profilingInterval * 60 * 20) != 0) {
return;
}
entityTickProfiler.startProfiling(new Runnable() {
@Override
public void run() {
try {
TableFormatter tf = new TableFormatter(MinecraftServer.getServer());
tf.tableSeparator = "\n";
if (json) {
entityTickProfiler.writeJSONData(profilingFile);
} else {
Files.write(entityTickProfiler.writeStringData(tf, 6).toString(), profilingFile, Charsets.UTF_8);
}
} catch (Throwable t) {
Log.severe("Failed to save periodic profiling data to " + profilingFile, t);
}
}
}, ProfileCommand.ProfilingState.GLOBAL, 10, Arrays.<World>asList(DimensionManager.getWorlds()));
}
@Override
public void tickEnd(final EnumSet<TickType> type, final Object... tickData) {
}
@Override
public EnumSet<TickType> ticks() {
return TICKS;
}
@Override
public String getLabel() {
return "TickProfiler scheduled profiling handler";
}
}
}