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

136 lines
5.5 KiB
Diff
Raw Normal View History

2019-07-30 20:50:28 +10:00
From 4ac407e3ee7e7a09c9744dab392ab3342e0562a0 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
2019-07-30 20:50:28 +10:00
index 1fbf893eb..5859c8209 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
2019-07-20 09:00:00 +10:00
@@ -163,6 +163,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
2019-04-23 12:00:00 +10:00
public MinecraftServer(OptionSet options, Proxy proxy, DataFixer datafixer, CommandDispatcher commanddispatcher, YggdrasilAuthenticationService yggdrasilauthenticationservice, MinecraftSessionService minecraftsessionservice, GameProfileRepository gameprofilerepository, UserCache usercache, WorldLoadListenerFactory worldloadlistenerfactory, String s) {
super("Server");
2019-07-30 20:50:28 +10:00
@@ -749,6 +755,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
+
2014-04-12 14:18:37 +10:00
public void run() {
try {
if (this.init()) {
2019-07-30 20:50:28 +10:00
@@ -757,8 +770,11 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2019-04-23 12:00:00 +10:00
this.serverPing.setServerInfo(new ServerPing.ServerData(SharedConstants.a().getName(), SharedConstants.a().getProtocolVersion()));
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;
2019-07-30 20:50:28 +10:00
@@ -769,6 +785,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;
2019-07-20 09:00:00 +10:00
if (this.T) {
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
2019-04-30 21:46:22 +10:00
index 38a5744a8..013fcc902 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
2018-12-28 10:22:10 +11:00
@@ -271,4 +271,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
2019-04-25 12:00:00 +10:00
index 000000000..be2e31dea
2014-04-12 14:18:37 +10:00
--- /dev/null
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
@@ -0,0 +1,45 @@
+package org.spigotmc;
+
+import com.google.common.base.Joiner;
+import net.minecraft.server.MinecraftServer;
+import com.google.common.collect.Iterables;
2014-04-12 14:18:37 +10:00
+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 ) );
+
+ 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 );
+ }
+}
--
2019-04-23 09:33:25 +10:00
2.20.1
2014-04-12 14:18:37 +10:00