spigot/CraftBukkit-Patches/0043-Highly-Optimized-Tick-Loop.patch

136 lines
5.6 KiB
Diff
Raw Normal View History

2021-04-28 08:10:22 +10:00
From dbfe9a7c13f20e04705cff9864ab29fb3e3e07e8 Mon Sep 17 00:00:00 2001
2014-04-12 14:18:37 +10:00
From: md_5 <git@md-5.net>
Date: Thu, 26 Jan 2017 21:50:51 +0000
2014-04-12 14:18:37 +10:00
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
2021-04-28 08:10:22 +10:00
index cf3a033dc..4f0ae31f4 100644
2014-04-12 14:18:37 +10:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-03-16 09:00:00 +11:00
@@ -258,6 +258,12 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2018-07-21 11:24:31 +10:00
public CommandDispatcher vanillaCommandDispatcher;
2019-04-25 15:38:41 +10:00
private boolean forceTicks;
2014-04-12 14:18:37 +10:00
// CraftBukkit end
+ // Spigot start
+ public static final int TPS = 20;
+ public static final int TICK_TIME = 1000000000 / TPS;
2014-04-12 14:18:37 +10:00
+ private static final int SAMPLE_INTERVAL = 100;
+ public final double[] recentTps = new double[ 3 ];
+ // Spigot end
2020-06-25 10:00:00 +10:00
public static <S extends MinecraftServer> S a(Function<Thread, S> function) {
AtomicReference<S> atomicreference = new AtomicReference();
2021-04-28 08:10:22 +10:00
@@ -902,6 +908,13 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2019-04-23 12:00:00 +10:00
2014-04-12 14:18:37 +10:00
}
+ // Spigot Start
+ private static double calcTps(double avg, double exp, double tps)
+ {
+ return ( avg * exp ) + ( tps * ( 1 - exp ) );
+ }
+ // Spigot End
+
2020-08-12 07:00:00 +10:00
protected void w() {
2014-04-12 14:18:37 +10:00
try {
if (this.init()) {
2021-04-28 08:10:22 +10:00
@@ -910,8 +923,11 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2019-12-11 09:00:00 +11:00
this.serverPing.setServerInfo(new ServerPing.ServerData(SharedConstants.getGameVersion().getName(), SharedConstants.getGameVersion().getProtocolVersion()));
2019-04-23 12:00:00 +10:00
this.a(this.serverPing);
2014-04-12 14:18:37 +10:00
+ // Spigot start
+ Arrays.fill( recentTps, 20 );
2019-04-23 12:00:00 +10:00
+ long curTime, tickSection = SystemUtils.getMonotonicMillis(), tickCount = 1;
2014-04-12 14:18:37 +10:00
while (this.isRunning) {
2018-12-13 11:00:00 +11:00
- long i = SystemUtils.getMonotonicMillis() - this.nextTick;
2019-04-23 12:00:00 +10:00
+ long i = (curTime = SystemUtils.getMonotonicMillis()) - this.nextTick;
2019-05-19 12:21:25 +10:00
if (i > 5000L && this.nextTick - this.lastOverloadTime >= 30000L) { // CraftBukkit
2019-04-23 12:00:00 +10:00
long j = i / 50L;
2021-04-28 08:10:22 +10:00
@@ -922,6 +938,16 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2019-04-23 12:00:00 +10:00
this.lastOverloadTime = this.nextTick;
}
2014-04-12 14:18:37 +10:00
2018-08-09 21:02:45 +10:00
+ if ( tickCount++ % SAMPLE_INTERVAL == 0 )
2014-04-12 14:18:37 +10:00
+ {
2019-04-23 12:00:00 +10:00
+ double currentTps = 1E3 / ( curTime - tickSection ) * SAMPLE_INTERVAL;
2014-04-12 14:18:37 +10:00
+ 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;
2019-04-23 12:00:00 +10:00
+ }
+ // Spigot end
+
2018-07-15 10:00:00 +10:00
MinecraftServer.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
2018-12-13 11:00:00 +11:00
this.nextTick += 50L;
2020-06-25 10:00:00 +10:00
GameProfilerTick gameprofilertick = GameProfilerTick.a("Server");
2014-04-12 14:18:37 +10:00
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
2021-03-16 09:00:00 +11:00
index 584953b9f..2e80766f8 100644
2014-04-12 14:18:37 +10:00
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
2019-07-30 20:52:01 +10:00
@@ -266,4 +266,9 @@ public class SpigotConfig
}
2014-04-12 14:18:37 +10:00
}
}
+
+ 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
2021-01-31 10:08:46 +11:00
index 000000000..f5b6dec1c
2014-04-12 14:18:37 +10:00
--- /dev/null
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
2021-01-31 10:08:46 +11:00
@@ -0,0 +1,45 @@
2014-04-12 14:18:37 +10:00
+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)");
2014-04-12 14:18:37 +10:00
+
+ 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
2014-04-12 14:18:37 +10:00