-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
EntityTickProfiler.java
354 lines (322 loc) · 11 KB
/
EntityTickProfiler.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package me.nallar.tickthreading.minecraft.profiling;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import com.google.common.base.Functions;
import com.google.common.collect.Ordering;
import me.nallar.tickthreading.minecraft.TickManager;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.minecraft.commands.ProfileCommand;
import me.nallar.tickthreading.util.MappingUtil;
import me.nallar.tickthreading.util.TableFormatter;
import me.nallar.tickthreading.util.stringfillers.StringFiller;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import org.cliffc.high_scale_lib.NonBlockingHashMap;
import org.codehaus.jackson.map.ObjectMapper;
public class EntityTickProfiler {
public static final EntityTickProfiler ENTITY_TICK_PROFILER = new EntityTickProfiler();
public static ProfileCommand.ProfilingState profilingState = ProfileCommand.ProfilingState.NONE;
private int ticks;
private final AtomicLong totalTime = new AtomicLong();
private volatile long startTime;
private EntityTickProfiler() {
}
public static synchronized boolean startProfiling(ProfileCommand.ProfilingState profilingState_) {
if (profilingState != ProfileCommand.ProfilingState.NONE) {
return false;
}
profilingState = profilingState_;
return true;
}
public static synchronized void endProfiling() {
profilingState = ProfileCommand.ProfilingState.NONE;
}
public boolean startProfiling(final Runnable runnable, ProfileCommand.ProfilingState state, final int time, final Collection<World> worlds_) {
if (time <= 0) {
throw new IllegalArgumentException("time must be > 0");
}
final Collection<World> worlds = new ArrayList<World>(worlds_);
synchronized (EntityTickProfiler.class) {
if (!startProfiling(state)) {
return false;
}
for (World world_ : worlds) {
TickThreading.instance.getManager(world_).profilingEnabled = true;
}
}
Runnable profilingRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000 * time);
} catch (InterruptedException ignored) {
}
synchronized (EntityTickProfiler.class) {
endProfiling();
runnable.run();
clear();
for (World world_ : worlds) {
TickManager tickManager = TickThreading.instance.getManager(world_);
if (tickManager != null) {
tickManager.profilingEnabled = false;
}
}
}
}
};
Thread profilingThread = new Thread(profilingRunnable);
profilingThread.setName("TickProfiler");
profilingThread.start();
startTime = System.currentTimeMillis();
return true;
}
public void record(Object o, long time) {
if (time < 0) {
time = 0;
}
getSingleTime(o).addAndGet(time);
getSingleInvocationCount(o).incrementAndGet();
Class<?> clazz = o.getClass();
getTime(clazz).addAndGet(time);
getInvocationCount(clazz).incrementAndGet();
totalTime.addAndGet(time);
}
public void clear() {
invocationCount.clear();
time.clear();
totalTime.set(0);
singleTime.clear();
singleInvocationCount.clear();
ticks = 0;
}
public void tick() {
if (profilingState != ProfileCommand.ProfilingState.NONE) {
ticks++;
}
}
public void writeJSONData(File file) throws IOException {
TableFormatter tf = new TableFormatter(StringFiller.FIXED_WIDTH);
tf.recordTables();
writeData(tf, 20);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(file, tf.getTables());
}
private static <T> List<T> sortedKeys(Map<T, ? extends Comparable<?>> map, int elements) {
List<T> list = Ordering.natural().reverse().onResultOf(Functions.forMap(map)).immutableSortedCopy(map.keySet());
return list.size() > elements ? list.subList(0, elements) : list;
}
public TableFormatter writeStringData(TableFormatter tf) {
return writeStringData(tf, 5);
}
public TableFormatter writeStringData(TableFormatter tf, int elements) {
long timeProfiled = System.currentTimeMillis() - startTime;
float tps = ticks * 1000f / timeProfiled;
tf.sb.append("TPS: ").append(tps).append('\n').append(tf.tableSeparator);
return writeData(tf, elements);
}
public TableFormatter writeData(TableFormatter tf, int elements) {
Map<Class<?>, Long> time = new HashMap<Class<?>, Long>();
for (Map.Entry<Class<?>, AtomicLong> entry : this.time.entrySet()) {
time.put(entry.getKey(), entry.getValue().get());
}
Map<Object, Long> singleTime = new HashMap<Object, Long>();
for (Map.Entry<Object, AtomicLong> entry : this.singleTime.entrySet()) {
singleTime.put(entry.getKey(), entry.getValue().get());
}
double totalTime = this.totalTime.get();
tf
.heading("Single Entity")
.heading("Time/Tick")
.heading("%");
final List<Object> sortedSingleKeysByTime = sortedKeys(singleTime, elements);
for (Object o : sortedSingleKeysByTime) {
tf
.row(niceName(o))
.row(singleTime.get(o) / (1000000d * singleInvocationCount.get(o).get()))
.row((singleTime.get(o) / totalTime) * 100);
}
tf.finishTable();
tf.sb.append('\n');
final Map<ChunkCoords, ComparableLongHolder> chunkTimeMap = new HashMap<ChunkCoords, ComparableLongHolder>() {
@Override
public ComparableLongHolder get(Object key_) {
ChunkCoords key = (ChunkCoords) key_;
ComparableLongHolder value = super.get(key);
if (value == null) {
value = new ComparableLongHolder();
put(key, value);
}
return value;
}
};
for (final Map.Entry<Object, Long> singleTimeEntry : singleTime.entrySet()) {
int x = Integer.MIN_VALUE;
int z = Integer.MIN_VALUE;
Object o = singleTimeEntry.getKey();
if (o instanceof Entity) {
x = ((Entity) o).chunkCoordX;
z = ((Entity) o).chunkCoordZ;
} else if (o instanceof TileEntity) {
x = ((TileEntity) o).xCoord >> 4;
z = ((TileEntity) o).zCoord >> 4;
}
if (x != Integer.MIN_VALUE) {
chunkTimeMap.get(new ChunkCoords(x, z)).value += singleTimeEntry.getValue();
}
}
tf
.heading("Chunk")
.heading("Time/Tick")
.heading("%");
for (ChunkCoords chunkCoords : sortedKeys(chunkTimeMap, elements)) {
long chunkTime = chunkTimeMap.get(chunkCoords).value;
tf
.row(chunkCoords.chunkXPos + ", " + chunkCoords.chunkZPos)
.row(chunkTime / (1000000d * ticks))
.row((chunkTime / totalTime) * 100);
}
tf.finishTable();
tf.sb.append('\n');
tf
.heading("All Entities of Type")
.heading("Time/Tick")
.heading("%");
for (Class c : sortedKeys(time, elements)) {
tf
.row(niceName(c))
.row(time.get(c) / (1000000d * ticks))
.row((time.get(c) / totalTime) * 100);
}
tf.finishTable();
tf.sb.append('\n');
Map<Class<?>, Long> timePerTick = new HashMap<Class<?>, Long>();
for (Map.Entry<Class<?>, AtomicLong> entry : this.time.entrySet()) {
timePerTick.put(entry.getKey(), entry.getValue().get() / invocationCount.get(entry.getKey()).get());
}
tf
.heading("Average Entity of Type")
.heading("Time/tick")
.heading("Calls");
for (Class c : sortedKeys(timePerTick, elements)) {
tf
.row(niceName(c))
.row(timePerTick.get(c) / 1000000d)
.row(invocationCount.get(c));
}
tf.finishTable();
return tf;
}
private static Object niceName(Object o) {
if (o instanceof TileEntity) {
return niceName(o.getClass()) + ' ' + ((TileEntity) o).xCoord + ',' + ((TileEntity) o).yCoord + ',' + ((TileEntity) o).zCoord;
} else if (o instanceof Entity) {
return niceName(o.getClass()) + ' ' + (int) ((Entity) o).posX + ',' + (int) ((Entity) o).posY + ',' + (int) ((Entity) o).posZ;
}
return o.toString().substring(0, 48);
}
private static String niceName(Class<?> clazz) {
String name = MappingUtil.debobfuscate(clazz.getName());
if (name.contains(".")) {
String cName = name.substring(name.lastIndexOf('.') + 1);
String pName = name.substring(0, name.lastIndexOf('.'));
if (pName.contains(".")) {
pName = pName.substring(pName.lastIndexOf('.') + 1);
}
return (cName.length() < 15 ? pName + '.' : "") + cName;
}
return name;
}
private final Map<Class<?>, AtomicInteger> invocationCount = new NonBlockingHashMap<Class<?>, AtomicInteger>();
private final Map<Class<?>, AtomicLong> time = new NonBlockingHashMap<Class<?>, AtomicLong>();
private final Map<Object, AtomicLong> singleTime = new NonBlockingHashMap<Object, AtomicLong>();
private final Map<Object, AtomicLong> singleInvocationCount = new NonBlockingHashMap<Object, AtomicLong>();
private AtomicLong getSingleInvocationCount(Object o) {
AtomicLong t = singleInvocationCount.get(o);
if (t == null) {
synchronized (o) {
t = singleInvocationCount.get(o);
if (t == null) {
t = new AtomicLong();
singleInvocationCount.put(o, t);
}
}
}
return t;
}
// We synchronize on the class name as it is always the same object
// We do not synchronize on the class object as that would also
// prevent any synchronized static methods on it from running
private AtomicInteger getInvocationCount(Class<?> clazz) {
AtomicInteger i = invocationCount.get(clazz);
if (i == null) {
synchronized (clazz.getName()) {
i = invocationCount.get(clazz);
if (i == null) {
i = new AtomicInteger();
invocationCount.put(clazz, i);
}
}
}
return i;
}
private AtomicLong getSingleTime(Object o) {
AtomicLong t = singleTime.get(o);
if (t == null) {
synchronized (o) {
t = singleTime.get(o);
if (t == null) {
t = new AtomicLong();
singleTime.put(o, t);
}
}
}
return t;
}
private AtomicLong getTime(Class<?> clazz) {
AtomicLong t = time.get(clazz);
if (t == null) {
synchronized (clazz.getName()) {
t = time.get(clazz);
if (t == null) {
t = new AtomicLong();
time.put(clazz, t);
}
}
}
return t;
}
private class ComparableLongHolder implements Comparable<ComparableLongHolder> {
public long value;
ComparableLongHolder() {
}
@Override
public int compareTo(final ComparableLongHolder comparableLongHolder) {
long otherValue = comparableLongHolder.value;
return (value < otherValue) ? -1 : ((value == otherValue) ? 0 : 1);
}
}
private static final class ChunkCoords {
public final int chunkXPos;
public final int chunkZPos;
ChunkCoords(final int chunkXPos, final int chunkZPos) {
this.chunkXPos = chunkXPos;
this.chunkZPos = chunkZPos;
}
@Override
public boolean equals(Object o) {
return o instanceof ChunkCoords && ((ChunkCoords) o).chunkXPos == this.chunkXPos && ((ChunkCoords) o).chunkZPos == this.chunkZPos;
}
@Override
public int hashCode() {
return (chunkXPos * 7907) + chunkXPos;
}
}
}