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

156 lines
6.2 KiB
Diff
Raw Normal View History

2018-07-22 12:00:00 +10:00
From 7316080501bd346405991b9dc4d68c8dfa363267 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
2018-07-22 12:00:00 +10:00
index 7aa788871..540079fcb 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
2018-07-22 12:00:00 +10:00
@@ -147,6 +147,12 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
2018-07-15 10:00:00 +10:00
public File bukkitDataPackFolder;
2018-07-21 11:24:31 +10:00
public CommandDispatcher vanillaCommandDispatcher;
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
2018-07-15 10:00:00 +10:00
public MinecraftServer(OptionSet options, Proxy proxy, DataFixer datafixer, CommandDispatcher commanddispatcher, YggdrasilAuthenticationService yggdrasilauthenticationservice, MinecraftSessionService minecraftsessionservice, GameProfileRepository gameprofilerepository, UserCache usercache) {
2018-07-21 11:24:31 +10:00
this.ac = new ResourceManager(EnumResourcePackType.SERVER_DATA);
2018-07-22 12:00:00 +10:00
@@ -660,6 +666,13 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
2014-04-12 14:18:37 +10:00
this.isRunning = false;
}
+ // 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()) {
2018-07-22 12:00:00 +10:00
@@ -668,28 +681,36 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
this.n.setServerInfo(new ServerPing.ServerData("1.13", 393));
2018-07-15 10:00:00 +10:00
this.a(this.n);
2014-04-12 14:18:37 +10:00
+ // Spigot start
+ Arrays.fill( recentTps, 20 );
+ long lastTick = System.nanoTime(), catchupTime = 0, curTime, wait, tickSection = lastTick;
while (this.isRunning) {
2018-07-15 10:00:00 +10:00
- long i = SystemUtils.b() - this.aa;
2014-04-12 14:18:37 +10:00
-
2018-07-15 10:00:00 +10:00
- if (i > 2000L && this.aa - this.Q >= 15000L) {
- long j = i / 50L;
2014-04-12 14:18:37 +10:00
+ curTime = System.nanoTime();
+ wait = TICK_TIME - (curTime - lastTick) - catchupTime;
+ if (wait > 0) {
+ Thread.sleep(wait / 1000000);
+ catchupTime = 0;
+ continue;
+ } else {
+ catchupTime = Math.min(1000000000, Math.abs(wait));
2018-07-15 10:00:00 +10:00
+ }
2014-04-12 14:18:37 +10:00
2018-07-15 10:00:00 +10:00
- if (server.getWarnOnOverload()) // CraftBukkit
- MinecraftServer.LOGGER.warn("Can\'t keep up! Is the server overloaded? Running {}ms or {} ticks behind", Long.valueOf(i), Long.valueOf(j));
- this.aa += j * 50L;
- this.Q = this.aa;
2014-04-12 14:18:37 +10:00
+ if ( MinecraftServer.currentTick++ % SAMPLE_INTERVAL == 0 )
+ {
+ double currentTps = 1E9 / ( 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;
}
+ lastTick = curTime;
2018-07-15 10:00:00 +10:00
MinecraftServer.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
this.v();
this.aa += 50L;
-
- while (SystemUtils.b() < this.aa) {
- Thread.sleep(1L);
- }
-
this.P = true;
2014-04-12 14:18:37 +10:00
}
+ // Spigot end
} else {
this.a((CrashReport) null);
}
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
2018-07-21 11:24:31 +10:00
index 5d3060c01..6096cccc1 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
@@ -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
2018-07-21 11:24:31 +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 );
+ }
+}
--
2018-07-15 10:00:00 +10:00
2.17.1
2014-04-12 14:18:37 +10:00