diff --git a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java index e606bc7ca..e159c7d0a 100644 --- a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java +++ b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java @@ -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 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) { diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 771da0e16..66816a463 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -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 sharedInstances = new CopyOnWriteArrayList<>(); private ChunkGenerator chunkGenerator; - // WARNING: need to be synchronized properly - private final Long2ObjectMap chunks = new Long2ObjectOpenHashMap<>(); + private final ConcurrentHashMap chunks = new ConcurrentHashMap<>(); private final Set scheduledChunksToRemove = new HashSet<>(); private ReadWriteLock changingBlockLock = new ReentrantReadWriteLock(); @@ -407,13 +405,11 @@ public class InstanceContainer extends Instance { e.printStackTrace(); } } else { - synchronized (chunks) { - final Iterator chunkIterator = chunks.values().iterator(); - while (chunkIterator.hasNext()) { - final Chunk chunk = chunkIterator.next(); - final boolean isLast = !chunkIterator.hasNext(); - saveChunkToStorage(chunk, isLast ? callback : null); - } + final Iterator 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