mirror of
https://hub.spigotmc.org/stash/scm/spigot/spigot.git
synced 2025-09-18 21:33:01 +00:00
135 lines
5.6 KiB
Diff
135 lines
5.6 KiB
Diff
From 743f9db049a31020f67ed6ad942431a5b3d92de7 Mon Sep 17 00:00:00 2001
|
|
From: md_5 <git@md-5.net>
|
|
Date: Thu, 26 Jan 2017 21:50:51 +0000
|
|
Subject: [PATCH] Highly Optimized Tick Loop
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index de8da6de0..91f274832 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -288,6 +288,12 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
public CommandDispatcher vanillaCommandDispatcher;
|
|
private boolean forceTicks;
|
|
// CraftBukkit end
|
|
+ // Spigot start
|
|
+ public static final int TPS = 20;
|
|
+ public static final int TICK_TIME = 1000000000 / TPS;
|
|
+ private static final int SAMPLE_INTERVAL = 100;
|
|
+ public final double[] recentTps = new double[ 3 ];
|
|
+ // Spigot end
|
|
|
|
public static <S extends MinecraftServer> S a(Function<Thread, S> function) {
|
|
AtomicReference<S> atomicreference = new AtomicReference();
|
|
@@ -969,6 +975,13 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
|
|
}
|
|
|
|
+ // Spigot Start
|
|
+ private static double calcTps(double avg, double exp, double tps)
|
|
+ {
|
|
+ return ( avg * exp ) + ( tps * ( 1 - exp ) );
|
|
+ }
|
|
+ // Spigot End
|
|
+
|
|
protected void x() {
|
|
try {
|
|
if (this.init()) {
|
|
@@ -977,8 +990,11 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
this.status.setServerInfo(new ServerPing.ServerData(SharedConstants.getGameVersion().getName(), SharedConstants.getGameVersion().getProtocolVersion()));
|
|
this.a(this.status);
|
|
|
|
+ // Spigot start
|
|
+ Arrays.fill( recentTps, 20 );
|
|
+ long curTime, tickSection = SystemUtils.getMonotonicMillis(), tickCount = 1;
|
|
while (this.running) {
|
|
- long i = SystemUtils.getMonotonicMillis() - this.nextTickTime;
|
|
+ long i = (curTime = SystemUtils.getMonotonicMillis()) - this.nextTickTime;
|
|
|
|
if (i > 5000L && this.nextTickTime - this.lastOverloadWarning >= 30000L) { // CraftBukkit
|
|
long j = i / 50L;
|
|
@@ -989,6 +1005,16 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
|
this.lastOverloadWarning = this.nextTickTime;
|
|
}
|
|
|
|
+ if ( tickCount++ % SAMPLE_INTERVAL == 0 )
|
|
+ {
|
|
+ double currentTps = 1E3 / ( curTime - tickSection ) * SAMPLE_INTERVAL;
|
|
+ recentTps[0] = calcTps( recentTps[0], 0.92, currentTps ); // 1/exp(5sec/1min)
|
|
+ recentTps[1] = calcTps( recentTps[1], 0.9835, currentTps ); // 1/exp(5sec/5min)
|
|
+ recentTps[2] = calcTps( recentTps[2], 0.9945, currentTps ); // 1/exp(5sec/15min)
|
|
+ tickSection = curTime;
|
|
+ }
|
|
+ // Spigot end
|
|
+
|
|
if (this.debugCommandProfilerDelayStart) {
|
|
this.debugCommandProfilerDelayStart = false;
|
|
this.debugCommandProfiler = new MinecraftServer.a(SystemUtils.getMonotonicNanos(), this.tickCount);
|
|
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
|
|
index 584953b9f..2e80766f8 100644
|
|
--- a/src/main/java/org/spigotmc/SpigotConfig.java
|
|
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
|
|
@@ -266,4 +266,9 @@ public class SpigotConfig
|
|
}
|
|
}
|
|
}
|
|
+
|
|
+ private static void tpsCommand()
|
|
+ {
|
|
+ commands.put( "tps", new TicksPerSecondCommand( "tps" ) );
|
|
+ }
|
|
}
|
|
diff --git a/src/main/java/org/spigotmc/TicksPerSecondCommand.java b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
|
|
new file mode 100644
|
|
index 000000000..f5b6dec1c
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
|
|
@@ -0,0 +1,45 @@
|
|
+package org.spigotmc;
|
|
+
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+import org.bukkit.ChatColor;
|
|
+import org.bukkit.command.Command;
|
|
+import org.bukkit.command.CommandSender;
|
|
+
|
|
+public class TicksPerSecondCommand extends Command
|
|
+{
|
|
+
|
|
+ public TicksPerSecondCommand(String name)
|
|
+ {
|
|
+ super( name );
|
|
+ this.description = "Gets the current ticks per second for the server";
|
|
+ this.usageMessage = "/tps";
|
|
+ this.setPermission( "bukkit.command.tps" );
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean execute(CommandSender sender, String currentAlias, String[] args)
|
|
+ {
|
|
+ if ( !testPermission( sender ) )
|
|
+ {
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ StringBuilder sb = new StringBuilder( ChatColor.GOLD + "TPS from last 1m, 5m, 15m: " );
|
|
+ for ( double tps : MinecraftServer.getServer().recentTps )
|
|
+ {
|
|
+ sb.append( format( tps ) );
|
|
+ sb.append( ", " );
|
|
+ }
|
|
+ sender.sendMessage( sb.substring( 0, sb.length() - 2 ) );
|
|
+ sender.sendMessage(ChatColor.GOLD + "Current Memory Usage: " + ChatColor.GREEN + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "/" + (Runtime.getRuntime().totalMemory() / (1024 * 1024)) + " mb (Max: "
|
|
+ + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " mb)");
|
|
+
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ private String format(double tps)
|
|
+ {
|
|
+ return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString()
|
|
+ + ( ( tps > 20.0 ) ? "*" : "" ) + Math.min( Math.round( tps * 100.0 ) / 100.0, 20.0 );
|
|
+ }
|
|
+}
|
|
--
|
|
2.25.1
|
|
|