-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TickManager.java
571 lines (527 loc) · 18.2 KB
/
TickManager.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package me.nallar.tickthreading.minecraft;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.collections.ConcurrentIterableArrayList;
import me.nallar.tickthreading.minecraft.profiling.EntityTickProfiler;
import me.nallar.tickthreading.minecraft.tickregion.EntityTickRegion;
import me.nallar.tickthreading.minecraft.tickregion.TickRegion;
import me.nallar.tickthreading.minecraft.tickregion.TileEntityTickRegion;
import me.nallar.tickthreading.util.TableFormatter;
import me.nallar.unsafe.UnsafeUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.ChunkProviderServer;
import org.omg.CORBA.IntHolder;
public final class TickManager {
public final int regionSize;
public boolean variableTickRate;
public boolean profilingEnabled = false;
public double averageTickLength = 0;
public long lastTickLength = 0;
public long lastStartTime = 0;
private static final int shuffleInterval = 800;
private int shuffleCount;
private final boolean waitForCompletion;
public final EntityTickProfiler entityTickProfiler = new EntityTickProfiler();
public final World world;
public final List<TileEntity> tileEntityList = new ArrayList<TileEntity>();
public final List<Entity> entityList = new ArrayList<Entity>();
public Object tileEntityLock = new Object();
public Object entityLock = new Object();
private final Map<Integer, TileEntityTickRegion> tileEntityCallables = new HashMap<Integer, TileEntityTickRegion>();
private final Map<Integer, EntityTickRegion> entityCallables = new HashMap<Integer, EntityTickRegion>();
private final ConcurrentIterableArrayList<TickRegion> tickRegions = new ConcurrentIterableArrayList<TickRegion>();
private final ThreadManager threadManager;
private final Map<Class<?>, Integer> entityClassToCountMap = new HashMap<Class<?>, Integer>();
private final boolean lock = TickThreading.instance.lockRegionBorders;
public TickManager(World world, int regionSize, int threads, boolean waitForCompletion) {
this.waitForCompletion = waitForCompletion;
threadManager = new ThreadManager(threads, "Tile/Entity Tick for " + Log.name(world));
this.world = world;
this.regionSize = regionSize;
shuffleCount = world.rand.nextInt(shuffleInterval);
}
public void setVariableTickRate(boolean variableTickRate) {
this.variableTickRate = variableTickRate;
}
public TickRegion getTileEntityRegion(int hashCode) {
return tileEntityCallables.get(hashCode);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private TileEntityTickRegion getOrCreateRegion(TileEntity tileEntity) {
int hashCode = getHashCode(tileEntity);
TileEntityTickRegion callable = tileEntityCallables.get(hashCode);
if (callable == null) {
synchronized (tickRegions) {
callable = tileEntityCallables.get(hashCode);
if (callable == null) {
callable = new TileEntityTickRegion(world, this, tileEntity.xCoord / regionSize, tileEntity.zCoord / regionSize);
tileEntityCallables.put(hashCode, callable);
tickRegions.add(callable);
}
}
}
return callable;
}
public TickRegion getEntityRegion(int hashCode) {
return entityCallables.get(hashCode);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
private EntityTickRegion getOrCreateRegion(Entity entity) {
int regionX = (int) entity.posX / regionSize;
int regionZ = (int) entity.posZ / regionSize;
int hashCode = getHashCodeFromRegionCoords(regionX, regionZ);
EntityTickRegion callable = entityCallables.get(hashCode);
if (callable == null) {
synchronized (tickRegions) {
callable = entityCallables.get(hashCode);
if (callable == null) {
callable = new EntityTickRegion(world, this, regionX, regionZ);
entityCallables.put(hashCode, callable);
tickRegions.add(callable);
}
}
}
return callable;
}
public int getHashCode(TileEntity tileEntity) {
return getHashCode(tileEntity.xCoord, tileEntity.zCoord);
}
@SuppressWarnings ("NumericCastThatLosesPrecision")
public int getHashCode(Entity entity) {
return getHashCode((int) entity.posX, (int) entity.posZ);
}
public int getHashCode(int x, int z) {
return getHashCodeFromRegionCoords(x / regionSize, z / regionSize);
}
public static int getHashCodeFromRegionCoords(int x, int z) {
return x + (z << 16);
}
private void processChanges() {
try {
synchronized (tickRegions) {
Iterator<TickRegion> iterator = tickRegions.iterator();
while (iterator.hasNext()) {
TickRegion tickRegion = iterator.next();
tickRegion.processChanges();
if (tickRegion.isEmpty()) {
iterator.remove();
if (tickRegion instanceof EntityTickRegion) {
entityCallables.remove(tickRegion.hashCode);
} else {
tileEntityCallables.remove(tickRegion.hashCode);
}
tickRegion.die();
}
}
if (shuffleCount++ % shuffleInterval == 0) {
Collections.shuffle(tickRegions);
}
}
} catch (Exception e) {
Log.severe("Exception occurred while processing entity changes: ", e);
}
}
public boolean add(TileEntity tileEntity) {
return add(tileEntity, true);
}
public boolean add(TileEntity tileEntity, boolean newEntity) {
TileEntityTickRegion tileEntityTickRegion = getOrCreateRegion(tileEntity);
if (tileEntityTickRegion.add(tileEntity)) {
tileEntity.tickRegion = tileEntityTickRegion;
if (newEntity) {
synchronized (tileEntityLock) {
tileEntityList.add(tileEntity);
}
}
return true;
}
return false;
}
public boolean add(Entity entity) {
return add(entity, true);
}
public boolean add(Entity entity, boolean newEntity) {
EntityTickRegion entityTickRegion = getOrCreateRegion(entity);
if (entityTickRegion.add(entity)) {
entity.tickRegion = entityTickRegion;
if (newEntity) {
synchronized (entityLock) {
entityList.add(entity);
}
synchronized (entityClassToCountMap) {
Class entityClass = entity.getClass();
Integer count = entityClassToCountMap.get(entityClass);
if (count == null) {
count = 0;
}
entityClassToCountMap.put(entityClass, count + 1);
}
}
return true;
}
return false;
}
public void batchRemoveEntities(Collection<Entity> entities) {
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) world.getChunkProvider();
synchronized (entityLock) {
entityList.removeAll(entities);
}
for (Entity entity : entities) {
int x = entity.chunkCoordX;
int z = entity.chunkCoordZ;
if (entity.addedToChunk) {
Chunk chunk = chunkProviderServer.getChunkIfExists(x, z);
if (chunk != null) {
chunk.removeEntity(entity);
}
}
world.releaseEntitySkin(entity);
if (entity.tickRegion != null) {
entity.tickRegion.remove(entity);
entity.tickRegion = null;
Class entityClass = entity.getClass();
synchronized (entityClassToCountMap) {
Integer count = entityClassToCountMap.get(entityClass);
if (count == null) {
throw new IllegalStateException("Removed an entity which should not have been in the entityList");
}
entityClassToCountMap.put(entityClass, count - 1);
}
}
}
}
public void batchRemoveTileEntities(Collection<TileEntity> tileEntities) {
for (TileEntity tileEntity : tileEntities) {
if (tileEntity.tickRegion != null) {
tileEntity.tickRegion.remove(tileEntity);
tileEntity.tickRegion = null;
tileEntity.onChunkUnload();
}
if (lock) {
unlock(tileEntity);
}
}
synchronized (tileEntityLock) {
tileEntityList.removeAll(tileEntities);
}
}
public void remove(TileEntity tileEntity) {
TileEntityTickRegion tileEntityTickRegion = tileEntity.tickRegion;
if (tileEntityTickRegion == null) {
tileEntityTickRegion = getOrCreateRegion(tileEntity);
}
tileEntityTickRegion.remove(tileEntity);
removed(tileEntity);
}
public void remove(Entity entity) {
EntityTickRegion entityTickRegion = entity.tickRegion;
if (entityTickRegion == null) {
entityTickRegion = getOrCreateRegion(entity);
}
entityTickRegion.remove(entity);
removed(entity);
}
public void removed(TileEntity tileEntity) {
tileEntity.tickRegion = null;
synchronized (tileEntityLock) {
tileEntityList.remove(tileEntity);
}
if (lock) {
unlock(tileEntity);
}
}
public void removed(Entity entity) {
boolean removed;
entity.tickRegion = null;
synchronized (entityLock) {
removed = entityList.remove(entity);
}
if (removed) {
Class entityClass = entity.getClass();
synchronized (entityClassToCountMap) {
Integer count = entityClassToCountMap.get(entityClass);
if (count == null) {
throw new IllegalStateException("Removed an entity which should not have been in the entityList");
}
entityClassToCountMap.put(entityClass, count - 1);
}
}
}
public void unlock(TileEntity tileEntity) {
int xPos = tileEntity.lastTTX;
int yPos = tileEntity.lastTTY;
int zPos = tileEntity.lastTTZ;
if (tileEntity.xMinusLock != null) {
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos - 1, yPos, zPos);
if (lockTileEntity != null) {
lockTileEntity.xPlusLock = tileEntity.xMinusLock = null;
}
}
if (tileEntity.xPlusLock != null) {
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos + 1, yPos, zPos);
if (lockTileEntity != null) {
lockTileEntity.xMinusLock = tileEntity.xPlusLock = null;
}
}
if (tileEntity.zMinusLock != null) {
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos, yPos, zPos - 1);
if (lockTileEntity != null) {
lockTileEntity.zPlusLock = tileEntity.zMinusLock = null;
}
}
if (tileEntity.zPlusLock != null) {
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos, yPos, zPos + 1);
if (lockTileEntity != null) {
lockTileEntity.zMinusLock = tileEntity.zPlusLock = null;
}
}
}
public final void lock(TileEntity tileEntity) {
unlock(tileEntity);
int maxPosition = (regionSize / 2) - 1;
int xPos = tileEntity.xCoord;
int yPos = tileEntity.yCoord;
int zPos = tileEntity.zCoord;
tileEntity.lastTTX = xPos;
tileEntity.lastTTY = yPos;
tileEntity.lastTTZ = zPos;
int relativeXPos = (xPos % regionSize) / 2;
int relativeZPos = (zPos % regionSize) / 2;
boolean onMinusX = relativeXPos == 0;
boolean onMinusZ = relativeZPos == 0;
boolean onPlusX = relativeXPos == maxPosition;
boolean onPlusZ = relativeZPos == maxPosition;
if (onMinusX || onMinusZ || onPlusZ) { // minus X needs locked
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos - 1, yPos, zPos);
if (lockTileEntity != null) {
tileEntity.xMinusLock = lockTileEntity.thisLock;
lockTileEntity.xPlusLock = tileEntity.thisLock;
}
}
if (onPlusX || onMinusZ || onPlusZ) { // plus X needs locked
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos + 1, yPos, zPos);
if (lockTileEntity != null) {
tileEntity.xPlusLock = lockTileEntity.thisLock;
lockTileEntity.xMinusLock = tileEntity.thisLock;
}
}
if (onMinusZ || onMinusX || onPlusX) { // minus Z needs locked
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos, yPos, zPos - 1);
if (lockTileEntity != null) {
tileEntity.zMinusLock = lockTileEntity.thisLock;
lockTileEntity.zPlusLock = tileEntity.thisLock;
}
}
if (onPlusZ || onMinusX || onPlusX) { // plus Z needs locked
TileEntity lockTileEntity = world.getTEWithoutLoad(xPos, yPos, zPos + 1);
if (lockTileEntity != null) {
tileEntity.zPlusLock = lockTileEntity.thisLock;
lockTileEntity.zMinusLock = tileEntity.thisLock;
}
}
}
public void doTick() {
boolean previousProfiling = world.theProfiler.profilingEnabled;
lastStartTime = DeadLockDetector.tick("TickManager.doTick: " + Log.name(world));
threadManager.waitForCompletion();
if (previousProfiling) {
world.theProfiler.profilingEnabled = false;
}
threadManager.runList(tickRegions);
if (previousProfiling || waitForCompletion) {
lastTickLength = (threadManager.waitForCompletion() - lastStartTime);
averageTickLength = ((averageTickLength * 127) + lastTickLength) / 128;
threadManager.run(new Runnable() {
@Override
public void run() {
processChanges();
}
});
}
if (previousProfiling) {
world.theProfiler.profilingEnabled = true;
}
if (profilingEnabled) {
entityTickProfiler.tick();
}
}
public void tickEnd() {
if (!waitForCompletion) {
lastTickLength = (threadManager.waitForCompletion() - lastStartTime);
averageTickLength = ((averageTickLength * 127) + lastTickLength) / 128;
threadManager.run(new Runnable() {
@Override
public void run() {
processChanges();
}
});
}
}
public void unload() {
threadManager.stop();
synchronized (tickRegions) {
for (TickRegion tickRegion : tickRegions) {
tickRegion.die();
}
}
tickRegions.clear();
entityList.clear();
entityClassToCountMap.clear();
UnsafeUtil.clean(this);
}
public void fixDiscrepancies(TableFormatter tableFormatter) {
final IntHolder intHolder = new IntHolder();
{
Set<Entity> contained = new HashSet<Entity>();
Set<Entity> toRemove = new HashSet<Entity>();
synchronized (entityList) {
for (Entity e : entityList) {
if (add(e, false)) {
intHolder.value++;
}
if (!contained.add(e)) {
toRemove.add(e);
intHolder.value++;
}
}
entityList.removeAll(toRemove);
}
}
{
Set<TileEntity> contained = new HashSet<TileEntity>();
Set<TileEntity> toRemove = new HashSet<TileEntity>();
synchronized (tileEntityList) {
for (TileEntity te : tileEntityList) {
if (add(te, false)) {
intHolder.value++;
}
if (!contained.add(te)) {
toRemove.add(te);
intHolder.value++;
}
}
tileEntityList.removeAll(toRemove);
}
}
if (intHolder.value != 0) {
tableFormatter.sb.append("Fixed ").append(intHolder.value).append(" discrepancies in tile/entity lists.");
}
}
public void writeStats(TableFormatter tf) {
long timeTotal = 0;
double time = Double.NaN;
try {
long[] tickTimes = MinecraftServer.getServer().worldTickTimes.get(world.provider.dimensionId);
for (long tick : tickTimes) {
timeTotal += tick;
}
time = (timeTotal) / (double) tickTimes.length;
if (time == 0) {
time = 0.1;
}
} catch (NullPointerException ignored) {
}
tf
.row(Log.name(world))
.row(Math.min(1000000000 / time, MinecraftServer.getTargetTPS()))
.row(String.valueOf(entityList.size()))
.row(String.valueOf(tileEntityList.size()))
.row(world instanceof WorldServer ? String.valueOf(((WorldServer) world).theChunkProviderServer.getLoadedChunkCount()) : "0")
.row(TableFormatter.formatDoubleWithPrecision((time * 100f) / MinecraftServer.getTargetTickTime(), 3) + '%');
}
public TableFormatter writeDetailedStats(TableFormatter tf) {
@SuppressWarnings ("MismatchedQueryAndUpdateOfStringBuilder")
StringBuilder stats = tf.sb;
stats.append("World: ").append(Log.name(world)).append('\n');
stats.append("---- Slowest tick regions ----").append('\n');
// TODO: Rewrite this
float averageAverageTickTime = 0;
float maxTickTime = 0;
SortedMap<Float, TickRegion> sortedTickCallables = new TreeMap<Float, TickRegion>();
synchronized (tickRegions) {
for (TickRegion tickRegion : tickRegions) {
float averageTickTime = tickRegion.getAverageTickTime();
averageAverageTickTime += averageTickTime;
sortedTickCallables.put(averageTickTime, tickRegion);
if (averageTickTime > maxTickTime) {
maxTickTime = averageTickTime;
}
}
Collection<TickRegion> var = sortedTickCallables.values();
TickRegion[] sortedTickCallablesArray = var.toArray(new TickRegion[var.size()]);
tf
.heading("")
.heading("X")
.heading("Z")
.heading("N")
.heading("Time");
for (int i = sortedTickCallablesArray.length - 1; i >= sortedTickCallablesArray.length - 7; i--) {
if (i >= 0 && sortedTickCallablesArray[i].getAverageTickTime() > 0.2) {
sortedTickCallablesArray[i].writeStats(tf);
}
}
tf.finishTable();
stats.append('\n');
averageAverageTickTime /= tickRegions.size();
stats.append("---- World stats ----");
stats.append("\nAverage tick time: ").append(averageAverageTickTime).append("ms");
stats.append("\nMax tick time: ").append(maxTickTime).append("ms");
stats.append("\nEffective tick time: ").append(lastTickLength / 1000000f).append("ms");
stats.append("\nAverage effective tick time: ").append((float) averageTickLength / 1000000).append("ms");
}
return tf;
}
public TableFormatter writeEntityStats(TableFormatter tf) {
tf
.heading("Main")
.heading("Map")
.heading("Region")
.heading("Player");
tf
.row(entityList.size())
.row(getTotalEntityCountFromMap())
.row(getTotalEntityCountFromRegions())
.row(getEntityCount(EntityPlayer.class));
tf.finishTable();
return tf;
}
private int getTotalEntityCountFromRegions() {
int count = 0;
for (EntityTickRegion entityTickRegion : entityCallables.values()) {
count += entityTickRegion.size();
}
return count;
}
private int getTotalEntityCountFromMap() {
int count = 0;
for (Map.Entry<Class<?>, Integer> entry : entityClassToCountMap.entrySet()) {
count += entry.getValue();
}
return count;
}
public int getEntityCount(Class<?> clazz) {
int count = 0;
for (Map.Entry<Class<?>, Integer> entry : entityClassToCountMap.entrySet()) {
if (clazz.isAssignableFrom(entry.getKey())) {
count += entry.getValue();
}
}
return count;
}
}