From 86f99cbe1beed83e2b17c537c0bbd222cd0daa5a Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 27 Aug 2018 12:02:00 +0300 Subject: [PATCH] Fixed Sponge CPU usage gathering --- .../plan/system/tasks/TPSCountTimer.java | 25 +++++++++++++++++++ .../tasks/bungee/BungeeTPSCountTimer.java | 4 +++ .../tasks/server/BukkitTPSCountTimer.java | 24 +----------------- .../tasks/server/SpongeTPSCountTimer.java | 16 ++---------- 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/TPSCountTimer.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/TPSCountTimer.java index ba94523c3..93fe4cd32 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/TPSCountTimer.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/TPSCountTimer.java @@ -7,6 +7,8 @@ import com.djrapitops.plan.system.processing.processors.TPSInsertProcessor; import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.task.AbsRunnable; +import java.lang.management.ManagementFactory; +import java.lang.management.OperatingSystemMXBean; import java.util.ArrayList; import java.util.List; @@ -52,4 +54,27 @@ public abstract class TPSCountTimer extends AbsRunnable { public int getLatestPlayersOnline() { return latestPlayersOnline; } + + protected long getUsedMemory() { + Runtime runtime = Runtime.getRuntime(); + long totalMemory = runtime.totalMemory(); + return (totalMemory - runtime.freeMemory()) / 1000000; + } + + protected double getCPUUsage() { + double averageCPUUsage; + + OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean(); + if (osBean instanceof com.sun.management.OperatingSystemMXBean) { + com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean; + averageCPUUsage = nativeOsBean.getSystemCpuLoad(); + } else { + int availableProcessors = osBean.getAvailableProcessors(); + averageCPUUsage = osBean.getSystemLoadAverage() / availableProcessors; + } + if (averageCPUUsage < 0) { // If unavailable, getSystemLoadAverage() returns -1 + averageCPUUsage = -1; + } + return averageCPUUsage * 100.0; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/BungeeTPSCountTimer.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/BungeeTPSCountTimer.java index 2d5edb328..dda2d8233 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/BungeeTPSCountTimer.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/BungeeTPSCountTimer.java @@ -19,6 +19,10 @@ public class BungeeTPSCountTimer extends TPSCountTimer { .date(now) .skipTPS() .playersOnline(onlineCount) + .usedCPU(getCPUUsage()) + .usedMemory(getUsedMemory()) + .entities(-1) + .chunksLoaded(-1) .toTPS(); history.add(tps); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BukkitTPSCountTimer.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BukkitTPSCountTimer.java index 7ace91d0a..1017dea99 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BukkitTPSCountTimer.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BukkitTPSCountTimer.java @@ -7,9 +7,6 @@ import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.utility.log.Log; import org.bukkit.World; -import java.lang.management.ManagementFactory; -import java.lang.management.OperatingSystemMXBean; - public class BukkitTPSCountTimer extends TPSCountTimer { private long lastCheckNano; @@ -43,10 +40,7 @@ public class BukkitTPSCountTimer extends TPSCountTimer { private TPS calculateTPS(long diff, long now) { double averageCPUUsage = getCPUUsage(); - Runtime runtime = Runtime.getRuntime(); - - long totalMemory = runtime.totalMemory(); - long usedMemory = (totalMemory - runtime.freeMemory()) / 1000000; + long usedMemory = getUsedMemory(); int playersOnline = plugin.getServer().getOnlinePlayers().size(); latestPlayersOnline = playersOnline; @@ -58,22 +52,6 @@ public class BukkitTPSCountTimer extends TPSCountTimer { return getTPS(diff, now, averageCPUUsage, usedMemory, entityCount, loadedChunks, playersOnline); } - private double getCPUUsage() { - double averageCPUUsage; - - OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean(); - if (osBean instanceof com.sun.management.OperatingSystemMXBean) { - com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean; - averageCPUUsage = nativeOsBean.getSystemCpuLoad(); - } else { - int availableProcessors = osBean.getAvailableProcessors(); - averageCPUUsage = osBean.getSystemLoadAverage() / availableProcessors; - } - if (averageCPUUsage < 0) { // If unavailable, getSystemLoadAverage() returns -1 - averageCPUUsage = -1; - } - return averageCPUUsage * 100.0; - } /** * Gets the TPS for Spigot / Bukkit diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/SpongeTPSCountTimer.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/SpongeTPSCountTimer.java index 2e3356bab..a3ce43333 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/SpongeTPSCountTimer.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/SpongeTPSCountTimer.java @@ -9,9 +9,6 @@ import com.djrapitops.plugin.api.utility.log.Log; import org.spongepowered.api.Sponge; import org.spongepowered.api.world.World; -import java.lang.management.ManagementFactory; -import java.lang.management.OperatingSystemMXBean; - public class SpongeTPSCountTimer extends TPSCountTimer { private long lastCheckNano; @@ -42,18 +39,9 @@ public class SpongeTPSCountTimer extends TPSCountTimer { * @return the TPS */ private TPS calculateTPS(long now) { - OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); - int availableProcessors = operatingSystemMXBean.getAvailableProcessors(); - double averageCPUUsage = operatingSystemMXBean.getSystemLoadAverage() / availableProcessors * 100.0; + double averageCPUUsage = getCPUUsage(); - if (averageCPUUsage < 0) { // If unavailable, getSystemLoadAverage() returns -1 - averageCPUUsage = -1; - } - - Runtime runtime = Runtime.getRuntime(); - - long totalMemory = runtime.totalMemory(); - long usedMemory = (totalMemory - runtime.freeMemory()) / 1000000; + long usedMemory = getUsedMemory(); double tps = Sponge.getGame().getServer().getTicksPerSecond(); int playersOnline = ServerInfo.getServerProperties().getOnlinePlayers();