-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ProfileCommand.java
169 lines (162 loc) · 5.38 KB
/
ProfileCommand.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
package nallar.tickthreading.minecraft.commands;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nallar.tickthreading.Log;
import nallar.tickthreading.minecraft.TickManager;
import nallar.tickthreading.minecraft.TickThreading;
import nallar.tickthreading.minecraft.profiling.ContentionProfiler;
import nallar.tickthreading.minecraft.profiling.EntityTickProfiler;
import nallar.tickthreading.minecraft.profiling.PacketProfiler;
import nallar.tickthreading.minecraft.profiling.Timings;
import nallar.tickthreading.minecraft.tickregion.TickRegion;
import nallar.tickthreading.util.TableFormatter;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
public class ProfileCommand extends Command {
public static String name = "profile";
@Override
public String getCommandName() {
return name;
}
@Override
public boolean requireOp() {
return TickThreading.instance.requireOpForProfileCommand;
}
@Override
public void processCommand(final ICommandSender commandSender, List<String> arguments) {
World world = null;
int time_ = 7;
boolean entity_;
boolean location_ = false;
Integer x = null;
Integer z = null;
try {
if (arguments.isEmpty()) {
throw new Exception();
}
if ("p".equals(arguments.get(0))) {
PacketProfiler.startProfiling(commandSender, 10);
return;
}
if ("l".equals(arguments.get(0))) {
new ContentionProfiler(commandSender, 30, 5);
return;
}
entity_ = "e".equals(arguments.get(0));
if ("c".equals(arguments.get(0))) {
entity_ = true;
location_ = true;
if (arguments.size() > 2) {
x = Integer.valueOf(arguments.remove(1));
z = Integer.valueOf(arguments.remove(1));
}
}
if (arguments.size() > 1) {
time_ = Integer.valueOf(arguments.get(1));
}
if (arguments.size() > 2) {
world = DimensionManager.getWorld(Integer.valueOf(arguments.get(2)));
if (world == null) {
throw new NullPointerException();
}
}
} catch (Exception e) {
sendChat(commandSender, "Usage: /profile [type=a/e/p/(c [chunk x] [chunk z])] [time=7] [dimensionid=current dimension]");
return;
}
final int time = time_;
final boolean entity = entity_;
final boolean location = location_;
if (location) {
if (commandSender instanceof Entity) {
Entity e = (Entity) commandSender;
x = e.chunkCoordX;
z = e.chunkCoordZ;
world = e.worldObj;
} else {
world = DimensionManager.getWorld(0);
}
}
final TickManager manager = TickThreading.instance.getManager(world == null ? DimensionManager.getWorld(0) : world);
final List<World> worlds = new ArrayList<World>();
if (world == null) {
Collections.addAll(worlds, DimensionManager.getWorlds());
} else {
worlds.add(world);
}
final int hashCode = x != null ? TickManager.getHashCode(x * 16, z * 16) : 0;
if (entity) {
final EntityTickProfiler entityTickProfiler = EntityTickProfiler.ENTITY_TICK_PROFILER;
if (location) {
if (!world.getChunkProvider().chunkExists(x, z)) {
sendChat(commandSender, "The chunk coords " + x + ',' + z + " are not loaded, can not profile.");
return;
}
}
if (!entityTickProfiler.startProfiling(new Runnable() {
@Override
public void run() {
if (location) {
TickRegion tickRegion = manager.getEntityRegion(hashCode);
if (tickRegion != null) {
tickRegion.profilingEnabled = false;
}
tickRegion = manager.getTileEntityRegion(hashCode);
if (tickRegion != null) {
tickRegion.profilingEnabled = false;
}
}
sendChat(commandSender, entityTickProfiler.writeStringData(new TableFormatter(commandSender)).toString());
}
}, location ? ProfilingState.CHUNK : ProfilingState.GLOBAL, time, worlds)) {
sendChat(commandSender, "Someone else is currently profiling.");
}
if (location) {
manager.profilingEnabled = false;
TickRegion tickRegion = manager.getEntityRegion(hashCode);
if (tickRegion != null) {
tickRegion.profilingEnabled = true;
}
tickRegion = manager.getTileEntityRegion(hashCode);
if (tickRegion != null) {
tickRegion.profilingEnabled = true;
}
}
sendChat(commandSender, "Profiling for " + time + " seconds in " + (world == null ? "all worlds " : Log.name(world)) + (location ? " at chunk coords " + x + ',' + z : ""));
return;
}
if (Timings.enabled) {
sendChat(commandSender, "Someone else is currently profiling, please wait and try again.");
return;
}
Timings.enabled = true;
Runnable profilingRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000 * time);
} catch (InterruptedException ignored) {
}
Timings.enabled = false;
try {
Thread.sleep(100 * time);
} catch (InterruptedException ignored) {
}
sendChat(commandSender, String.valueOf(Timings.writeData(new TableFormatter(commandSender))));
Timings.clear();
}
};
Thread profilingThread = new Thread(profilingRunnable);
profilingThread.setName("TT Profiler");
sendChat(commandSender, "Profiling for " + time + " seconds");
profilingThread.start();
}
public static enum ProfilingState {
NONE,
GLOBAL,
CHUNK
}
}