Fix synchronization issue with chunk

This commit is contained in:
themode 2020-09-07 19:14:42 +02:00
parent e2940c9aee
commit d3d3e73eef
2 changed files with 32 additions and 14 deletions

View File

@ -3,6 +3,8 @@ package net.minestom.server.benchmark;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.time.UpdateOption;
import net.minestom.server.utils.validate.Check;
@ -80,6 +82,11 @@ public class BenchmarkManager {
threads.add(threadName);
}
/**
* Get the memory used by the server in bytes
*
* @return the memory used by the server
*/
public long getUsedMemory() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
@ -88,6 +95,23 @@ public class BenchmarkManager {
return Collections.unmodifiableMap(resultMap);
}
public String getCpuMonitoringMessage() {
String benchmarkMessage = "";
for (Map.Entry<String, ThreadResult> resultEntry : resultMap.entrySet()) {
final String name = resultEntry.getKey();
final ThreadResult result = resultEntry.getValue();
benchmarkMessage += ChatColor.GRAY + name;
benchmarkMessage += ": ";
benchmarkMessage += ChatColor.YELLOW.toString() + MathUtils.round(result.getCpuPercentage(), 2) + "% CPU ";
benchmarkMessage += ChatColor.RED.toString() + MathUtils.round(result.getUserPercentage(), 2) + "% USER ";
benchmarkMessage += ChatColor.PINK.toString() + MathUtils.round(result.getBlockedPercentage(), 2) + "% BLOCKED ";
benchmarkMessage += ChatColor.BRIGHT_GREEN.toString() + MathUtils.round(result.getWaitedPercentage(), 2) + "% WAITED ";
benchmarkMessage += "\n";
}
return benchmarkMessage;
}
private void refreshData() {
ThreadInfo[] threadInfo = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds());
for (ThreadInfo threadInfo2 : threadInfo) {

View File

@ -1,7 +1,5 @@
package net.minestom.server.instance;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import lombok.Setter;
import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data;
@ -32,6 +30,7 @@ import net.minestom.server.world.DimensionType;
import net.minestom.server.world.biomes.Biome;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
@ -53,8 +52,7 @@ public class InstanceContainer extends Instance {
private List<SharedInstance> sharedInstances = new CopyOnWriteArrayList<>();
private ChunkGenerator chunkGenerator;
// WARNING: need to be synchronized properly
private final Long2ObjectMap<Chunk> chunks = new Long2ObjectOpenHashMap<>();
private final ConcurrentHashMap<Long, Chunk> chunks = new ConcurrentHashMap<>();
private final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
private ReadWriteLock changingBlockLock = new ReentrantReadWriteLock();
@ -407,13 +405,11 @@ public class InstanceContainer extends Instance {
e.printStackTrace();
}
} else {
synchronized (chunks) {
final Iterator<Chunk> chunkIterator = chunks.values().iterator();
while (chunkIterator.hasNext()) {
final Chunk chunk = chunkIterator.next();
final boolean isLast = !chunkIterator.hasNext();
saveChunkToStorage(chunk, isLast ? callback : null);
}
final Iterator<Chunk> chunkIterator = chunks.values().iterator();
while (chunkIterator.hasNext()) {
final Chunk chunk = chunkIterator.next();
final boolean isLast = !chunkIterator.hasNext();
saveChunkToStorage(chunk, isLast ? callback : null);
}
}
}
@ -524,9 +520,7 @@ public class InstanceContainer extends Instance {
*/
private void cacheChunk(Chunk chunk) {
final long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
synchronized (chunks) {
this.chunks.put(index, chunk);
}
this.chunks.put(index, chunk);
}
@Override