mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-02 14:37:45 +01:00
Added SystemUsage class
This class is collection of some methods in TPSCounter that were refactored out of it.
This commit is contained in:
parent
d6e3817c71
commit
8a059ced0b
@ -17,6 +17,7 @@
|
||||
package com.djrapitops.plan.gathering.timed;
|
||||
|
||||
import com.djrapitops.plan.Plan;
|
||||
import com.djrapitops.plan.gathering.SystemUsage;
|
||||
import com.djrapitops.plan.gathering.domain.TPS;
|
||||
import com.djrapitops.plan.gathering.domain.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
@ -75,7 +76,7 @@ public class BukkitTPSCounter extends TPSCounter {
|
||||
*/
|
||||
private TPS calculateTPS(long diff, long now) {
|
||||
double averageCPUUsage = getCPUUsage();
|
||||
long usedMemory = getUsedMemory();
|
||||
long usedMemory = SystemUsage.getUsedMemory();
|
||||
long freeDiskSpace = getFreeDiskSpace();
|
||||
|
||||
int playersOnline = serverProperties.getOnlinePlayers();
|
||||
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.gathering.timed;
|
||||
|
||||
import com.djrapitops.plan.gathering.SystemUsage;
|
||||
import com.djrapitops.plan.gathering.domain.TPS;
|
||||
import com.djrapitops.plan.gathering.domain.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
@ -51,7 +52,7 @@ public class BungeeTPSCounter extends TPSCounter {
|
||||
.date(now)
|
||||
.playersOnline(onlineCount)
|
||||
.usedCPU(getCPUUsage())
|
||||
.usedMemory(getUsedMemory())
|
||||
.usedMemory(SystemUsage.getUsedMemory())
|
||||
.entities(-1)
|
||||
.chunksLoaded(-1)
|
||||
.freeDiskSpace(getFreeDiskSpace())
|
||||
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.gathering;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.OperatingSystemMXBean;
|
||||
|
||||
/**
|
||||
* Utility class for obtaining System usage statistics.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class SystemUsage {
|
||||
|
||||
private SystemUsage() {
|
||||
/* Static method class */
|
||||
}
|
||||
|
||||
/**
|
||||
* Check how much memory (in Mb) is in use.
|
||||
*
|
||||
* @return used memory (megabytes) at the time of fetching
|
||||
*/
|
||||
public static long getUsedMemory() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long totalMemory = runtime.totalMemory();
|
||||
return (totalMemory - runtime.freeMemory()) / 1000000L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check how active the system is (CPU) or if not available, using system load average.
|
||||
* <p>
|
||||
* - On some OSes CPU usage information is not available, and system load average is used instead.
|
||||
* - On some OSes system load average is not available.
|
||||
*
|
||||
* @return 0.0 to 1.0 if CPU, or system load average, or -1 if nothing is available.
|
||||
*/
|
||||
public static double getAverageSystemLoad() {
|
||||
double averageUsage;
|
||||
|
||||
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
|
||||
if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
|
||||
com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
|
||||
averageUsage = nativeOsBean.getSystemCpuLoad();
|
||||
} else {
|
||||
int availableProcessors = osBean.getAvailableProcessors();
|
||||
averageUsage = osBean.getSystemLoadAverage() / availableProcessors;
|
||||
}
|
||||
if (averageUsage < 0) {
|
||||
averageUsage = -1; // If unavailable, getSystemLoadAverage() returns -1
|
||||
}
|
||||
return averageUsage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check how much disk space is available on the current partition.
|
||||
*
|
||||
* @return free disk space (megabytes) on the partition JVM working directory is in.
|
||||
* @throws SecurityException if permission is required to see disk space.
|
||||
*/
|
||||
public static long getFreeDiskSpace() {
|
||||
File file = new File(new File("").getAbsolutePath());
|
||||
return file.getFreeSpace() / 1000000L;
|
||||
}
|
||||
|
||||
}
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.gathering.timed;
|
||||
|
||||
import com.djrapitops.plan.gathering.SystemUsage;
|
||||
import com.djrapitops.plan.gathering.domain.TPS;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
import com.djrapitops.plan.storage.database.DBSystem;
|
||||
@ -25,9 +26,6 @@ import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import com.djrapitops.plugin.task.AbsRunnable;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.OperatingSystemMXBean;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -90,33 +88,13 @@ public abstract class TPSCounter extends AbsRunnable {
|
||||
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;
|
||||
return SystemUsage.getAverageSystemLoad() * 100.0;
|
||||
}
|
||||
|
||||
protected long getFreeDiskSpace() {
|
||||
try {
|
||||
File file = new File(new File("").getAbsolutePath());
|
||||
return file.getFreeSpace() / 1000000L;
|
||||
return SystemUsage.getFreeDiskSpace();
|
||||
} catch (SecurityException noPermission) {
|
||||
if (!diskErrored) {
|
||||
errorHandler.log(L.WARN, this.getClass(), noPermission);
|
||||
|
@ -18,6 +18,7 @@ package com.djrapitops.plan.gathering.timed;
|
||||
|
||||
import cn.nukkit.level.Level;
|
||||
import com.djrapitops.plan.PlanNukkit;
|
||||
import com.djrapitops.plan.gathering.SystemUsage;
|
||||
import com.djrapitops.plan.gathering.domain.TPS;
|
||||
import com.djrapitops.plan.gathering.domain.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
@ -75,7 +76,7 @@ public class NukkitTPSCounter extends TPSCounter {
|
||||
*/
|
||||
private TPS calculateTPS(long diff, long now) {
|
||||
double averageCPUUsage = getCPUUsage();
|
||||
long usedMemory = getUsedMemory();
|
||||
long usedMemory = SystemUsage.getUsedMemory();
|
||||
long freeDiskSpace = getFreeDiskSpace();
|
||||
|
||||
int playersOnline = serverProperties.getOnlinePlayers();
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.djrapitops.plan.gathering.timed;
|
||||
|
||||
import com.djrapitops.plan.PlanSponge;
|
||||
import com.djrapitops.plan.gathering.SystemUsage;
|
||||
import com.djrapitops.plan.gathering.domain.TPS;
|
||||
import com.djrapitops.plan.gathering.domain.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
@ -74,7 +75,7 @@ public class SpongeTPSCounter extends TPSCounter {
|
||||
private TPS calculateTPS(long now) {
|
||||
double averageCPUUsage = getCPUUsage();
|
||||
|
||||
long usedMemory = getUsedMemory();
|
||||
long usedMemory = SystemUsage.getUsedMemory();
|
||||
|
||||
double tps = plugin.getGame().getServer().getTicksPerSecond();
|
||||
int playersOnline = serverProperties.getOnlinePlayers();
|
||||
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.gathering.timed;
|
||||
|
||||
import com.djrapitops.plan.gathering.SystemUsage;
|
||||
import com.djrapitops.plan.gathering.domain.TPS;
|
||||
import com.djrapitops.plan.gathering.domain.builders.TPSBuilder;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
@ -51,7 +52,7 @@ public class VelocityTPSCounter extends TPSCounter {
|
||||
.date(now)
|
||||
.playersOnline(onlineCount)
|
||||
.usedCPU(getCPUUsage())
|
||||
.usedMemory(getUsedMemory())
|
||||
.usedMemory(SystemUsage.getUsedMemory())
|
||||
.entities(-1)
|
||||
.chunksLoaded(-1)
|
||||
.freeDiskSpace(getFreeDiskSpace())
|
||||
|
Loading…
Reference in New Issue
Block a user