2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2012-12-27 23:27:59 +00:00
|
|
|
|
2013-08-09 13:40:08 +00:00
|
|
|
import java.text.DecimalFormat;
|
2013-08-10 16:39:50 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2013-08-10 16:39:50 +00:00
|
|
|
import org.apache.commons.lang.math.DoubleRange;
|
2013-08-09 13:40:08 +00:00
|
|
|
import org.bukkit.ChatColor;
|
2012-12-27 23:27:59 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
2013-08-09 13:40:08 +00:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2013-08-10 16:39:50 +00:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2012-12-27 23:27:59 +00:00
|
|
|
|
2021-01-10 21:23:20 +00:00
|
|
|
@CommandPermissions(level = Rank.OP, source = SourceType.BOTH)
|
2020-03-30 23:43:57 +00:00
|
|
|
@CommandParameters(description = "View server health, such as ticks-per-second, memory, etc.", usage = "/<command>")
|
2015-10-19 17:43:46 +00:00
|
|
|
public class Command_health extends FreedomCommand
|
2012-12-27 23:27:59 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-08-10 16:39:50 +00:00
|
|
|
private static final int BYTES_PER_MB = 1024 * 1024;
|
|
|
|
private static final DoubleRange TPS_RANGE = new DoubleRange(20.0 - 0.1, 20.0 + 0.1);
|
|
|
|
|
2012-12-27 23:27:59 +00:00
|
|
|
@Override
|
2015-11-22 18:26:47 +00:00
|
|
|
public boolean run(final CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
2012-12-27 23:27:59 +00:00
|
|
|
{
|
2013-08-10 16:39:50 +00:00
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
|
long usedMem = runtime.totalMemory() - runtime.freeMemory();
|
|
|
|
|
2018-07-31 07:01:29 +00:00
|
|
|
msg("Reserved Memory: " + (double)runtime.totalMemory() / (double)BYTES_PER_MB + "mb");
|
|
|
|
msg("Used Memory: " + new DecimalFormat("#").format((double)usedMem / (double)BYTES_PER_MB)
|
|
|
|
+ "mb (" + new DecimalFormat("#").format(((double)usedMem / (double)runtime.totalMemory()) * 100.0) + "%)");
|
|
|
|
msg("Max Memory: " + (double)runtime.maxMemory() / (double)BYTES_PER_MB + "mb");
|
2016-03-02 19:28:01 +00:00
|
|
|
msg("Calculating ticks per second, please wait...");
|
2013-08-10 00:11:17 +00:00
|
|
|
|
2013-08-09 13:40:08 +00:00
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-08-10 16:39:50 +00:00
|
|
|
TFM_TickMeter tickMeter = new TFM_TickMeter(plugin);
|
|
|
|
tickMeter.startTicking();
|
|
|
|
Thread.sleep(2500);
|
|
|
|
final double ticksPerSecond = tickMeter.stopTicking();
|
2013-08-10 00:11:17 +00:00
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
// Plugin was disabled during async task
|
|
|
|
if (!plugin.isEnabled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-09 13:40:08 +00:00
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
2013-08-10 00:11:17 +00:00
|
|
|
public void run()
|
|
|
|
{
|
2016-03-02 19:28:01 +00:00
|
|
|
msg("Ticks per second: " + (TPS_RANGE.containsDouble(ticksPerSecond) ? ChatColor.GREEN : ChatColor.RED) + ticksPerSecond);
|
2013-08-09 13:40:08 +00:00
|
|
|
}
|
2013-08-10 16:39:50 +00:00
|
|
|
}.runTask(plugin);
|
2013-08-09 13:40:08 +00:00
|
|
|
}
|
2013-08-14 13:28:19 +00:00
|
|
|
catch (Exception ex)
|
2013-08-09 13:40:08 +00:00
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-09 13:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-10 16:39:50 +00:00
|
|
|
}.runTaskAsynchronously(plugin);
|
2013-08-09 13:40:08 +00:00
|
|
|
|
2012-12-27 23:27:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-10 16:39:50 +00:00
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
private static class TFM_TickMeter
|
2013-08-10 16:39:50 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-08-10 16:39:50 +00:00
|
|
|
private final AtomicInteger ticks = new AtomicInteger();
|
|
|
|
private final TotalFreedomMod plugin;
|
|
|
|
private long startTime;
|
|
|
|
private BukkitTask task;
|
|
|
|
|
|
|
|
public TFM_TickMeter(TotalFreedomMod plugin)
|
|
|
|
{
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startTicking()
|
|
|
|
{
|
|
|
|
startTime = System.currentTimeMillis();
|
|
|
|
ticks.set(0);
|
|
|
|
|
|
|
|
task = new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
ticks.incrementAndGet();
|
|
|
|
}
|
|
|
|
}.runTaskTimer(plugin, 0L, 1L);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double stopTicking()
|
|
|
|
{
|
|
|
|
task.cancel();
|
|
|
|
long elapsed = System.currentTimeMillis() - startTime;
|
|
|
|
int tickCount = ticks.get();
|
|
|
|
|
2018-07-31 07:01:29 +00:00
|
|
|
return (double)tickCount / ((double)elapsed / 1000.0);
|
2013-08-10 16:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 00:28:53 +00:00
|
|
|
}
|