2014-03-09 22:37:28 +01:00
From 1724d1265735b9e88a66f636fe59f56c2c19f0b4 Mon Sep 17 00:00:00 2001
2014-01-22 09:16:12 +01:00
From: md_5 <git@md-5.net>
2014-01-25 04:09:21 +01:00
Date: Sat, 25 Jan 2014 14:08:35 +1100
2013-06-11 06:11:48 +02:00
Subject: [PATCH] Highly Optimized Tick Loop
2013-02-03 02:28:39 +01:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2014-02-13 07:34:01 +01:00
index 588ce0a..5a83c45 100644
2013-02-03 02:28:39 +01:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2014-02-13 07:27:00 +01:00
@@ -101,6 +101,12 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
2013-02-03 02:28:39 +01:00
public java.util.Queue<Runnable> processQueue = new java.util.concurrent.ConcurrentLinkedQueue<Runnable>();
public int autosavePeriod;
// CraftBukkit end
+ // Spigot start
+ private static final int TPS = 20;
+ private static final int TICK_TIME = 1000000000 / TPS;
2014-02-13 07:27:00 +01:00
+ private static final int SAMPLE_INTERVAL = 100;
2014-01-25 04:09:21 +01:00
+ public final double[] recentTps = new double[ 3 ];
2013-02-03 02:28:39 +01:00
+ // Spigot end
2013-12-01 04:40:53 +01:00
public MinecraftServer(OptionSet options, Proxy proxy) { // CraftBukkit - signature file -> OptionSet
i = this;
2014-02-13 07:27:00 +01:00
@@ -419,6 +425,13 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
2014-01-25 04:09:21 +01:00
this.isRunning = false;
}
+ // Spigot Start
+ private static double calcTps(double avg, double exp, double tps)
+ {
+ return ( avg * exp ) + ( tps * ( 1 - exp ) );
+ }
+ // Spigot End
+
public void run() {
try {
if (this.init()) {
2014-02-13 07:27:00 +01:00
@@ -429,38 +442,34 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
2014-01-22 09:16:12 +01:00
this.p.setServerInfo(new ServerPingServerData("1.7.2", 4));
this.a(this.p);
+ // Spigot start
2014-01-26 02:17:55 +01:00
+ Arrays.fill( recentTps, 20 );
2014-02-13 07:34:01 +01:00
+ long lastTick = System.nanoTime(), catchupTime = 0, curTime, wait, tickSection = lastTick;
2014-01-22 09:16:12 +01:00
while (this.isRunning) {
2013-12-01 04:40:53 +01:00
- long k = ap();
2013-02-03 02:28:39 +01:00
- long l = k - i;
-
2013-12-01 04:40:53 +01:00
- if (l > 2000L && i - this.O >= 15000L) {
2013-02-03 02:28:39 +01:00
- if (this.server.getWarnOnOverload()) // CraftBukkit - Added option to suppress warning messages
2013-12-01 04:40:53 +01:00
- h.warn("Can\'t keep up! Did the system time change, or is the server overloaded? Running {}ms behind, skipping {} tick(s)", new Object[] { Long.valueOf(l), Long.valueOf(l / 50L)});
2013-02-03 02:28:39 +01:00
- l = 2000L;
2013-12-01 04:40:53 +01:00
- this.O = i;
2013-02-03 02:28:39 +01:00
- }
-
- if (l < 0L) {
2013-12-01 04:40:53 +01:00
- h.warn("Time ran backwards! Did the system time change?");
2013-02-03 02:28:39 +01:00
- l = 0L;
2014-01-22 09:16:12 +01:00
+ curTime = System.nanoTime();
+ wait = TICK_TIME - (curTime - lastTick) - catchupTime;
2013-02-03 02:28:39 +01:00
+ if (wait > 0) {
+ Thread.sleep(wait / 1000000);
+ catchupTime = 0;
+ continue;
2014-01-25 04:09:21 +01:00
+ } else {
+ catchupTime = Math.min(1000000000, Math.abs(wait));
}
- j += l;
- i = k;
- if (this.worlds.get(0).everyoneDeeplySleeping()) { // CraftBukkit
- this.t();
- j = 0L;
- } else {
2013-02-03 02:28:39 +01:00
- while (j > 50L) {
- MinecraftServer.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
- j -= 50L;
2013-12-01 04:40:53 +01:00
- this.t();
2013-02-03 02:28:39 +01:00
- }
2014-02-13 07:27:00 +01:00
+ if ( MinecraftServer.currentTick++ % SAMPLE_INTERVAL == 0 )
2014-01-25 04:09:21 +01:00
+ {
2014-02-13 07:27:00 +01:00
+ double currentTps = 1E9 / ( curTime - tickSection ) * SAMPLE_INTERVAL;
2014-01-25 04:09:21 +01: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)
2014-02-13 07:27:00 +01:00
+ tickSection = curTime;
2013-02-03 02:28:39 +01:00
}
+ lastTick = curTime;
2014-01-25 04:09:21 +01:00
- Thread.sleep(1L);
2013-12-01 04:40:53 +01:00
+ this.t();
2014-01-22 09:16:12 +01:00
this.N = true;
2013-02-03 02:28:39 +01:00
}
+ // Spigot end
} else {
this.a((CrashReport) null);
}
2014-01-25 04:09:21 +01:00
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
2014-02-13 07:34:01 +01:00
index eaafc2d..713b351 100644
2014-01-25 04:09:21 +01:00
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
2014-02-12 14:48:26 +01:00
@@ -233,4 +233,9 @@ public class SpigotConfig
2014-01-25 04:09:21 +01:00
"screen." );
}
}
+
+ 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
2013-02-03 02:28:39 +01:00
new file mode 100644
2014-01-26 02:17:55 +01:00
index 0000000..2b8343d
2013-02-03 02:28:39 +01:00
--- /dev/null
2014-01-25 04:09:21 +01:00
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
2014-01-26 02:17:55 +01:00
@@ -0,0 +1,45 @@
2014-01-25 04:09:21 +01:00
+package org.spigotmc;
2013-02-03 02:28:39 +01:00
+
2014-01-25 04:09:21 +01:00
+import com.google.common.base.Joiner;
2013-02-03 02:28:39 +01:00
+import net.minecraft.server.MinecraftServer;
2014-01-25 04:09:21 +01:00
+import net.minecraft.util.com.google.common.collect.Iterables;
2013-02-03 02:28:39 +01:00
+import org.bukkit.ChatColor;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+
2014-01-25 04:09:21 +01:00
+public class TicksPerSecondCommand extends Command
+{
2013-02-03 02:28:39 +01:00
+
2014-01-25 04:09:21 +01:00
+ public TicksPerSecondCommand(String name)
+ {
+ super( name );
2013-02-03 02:28:39 +01:00
+ this.description = "Gets the current ticks per second for the server";
+ this.usageMessage = "/tps";
2014-01-25 04:09:21 +01:00
+ this.setPermission( "bukkit.command.tps" );
2013-02-03 02:28:39 +01:00
+ }
+
+ @Override
2014-01-25 04:09:21 +01:00
+ public boolean execute(CommandSender sender, String currentAlias, String[] args)
+ {
+ if ( !testPermission( sender ) )
+ {
+ return true;
2013-02-03 02:28:39 +01:00
+ }
+
2014-01-25 04:36:32 +01:00
+ StringBuilder sb = new StringBuilder( ChatColor.GOLD + "TPS from last 1m, 5m, 15m: " );
2014-01-25 04:09:21 +01:00
+ for ( double tps : MinecraftServer.getServer().recentTps )
+ {
2014-01-26 02:17:55 +01:00
+ sb.append( format( tps ) );
2014-01-25 04:09:21 +01:00
+ sb.append( ", " );
+ }
+ sender.sendMessage( sb.substring( 0, sb.length() - 2 ) );
2013-02-03 02:28:39 +01:00
+
+ return true;
+ }
2013-06-21 10:25:45 +02:00
+
2014-01-25 04:09:21 +01:00
+ private String format(double tps)
2013-06-21 10:25:45 +02:00
+ {
2014-01-26 02:17:55 +01:00
+ 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 );
2013-06-21 10:25:45 +02:00
+ }
2014-01-25 04:09:21 +01:00
+}
2013-02-03 02:28:39 +01:00
--
2014-02-13 07:34:01 +01:00
1.8.3.2
2013-02-03 02:28:39 +01:00