2014-12-21 02:09:49 +01:00
From 700c8673e39c0155d20b504981d779e4ce48fd06 Mon Sep 17 00:00:00 2001
2014-07-21 22:46:54 +02:00
From: md_5 <git@md-5.net>
Date: Sat, 25 Jan 2014 14:08:35 +1100
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
2014-12-06 04:33:40 +01:00
index 4a2f084..d213ae4 100644
2014-07-21 22:46:54 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2014-11-28 02:17:45 +01:00
@@ -118,6 +118,12 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IAs
2014-07-21 22:46:54 +02: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;
+ private static final int SAMPLE_INTERVAL = 100;
+ public final double[] recentTps = new double[ 3 ];
+ // Spigot end
2014-11-28 02:17:45 +01:00
public MinecraftServer(OptionSet options, Proxy proxy, File file1) {
this.d = proxy;
2014-12-06 04:33:40 +01:00
@@ -469,6 +475,13 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IAs
2014-07-21 22:46:54 +02: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-12-06 04:33:40 +01:00
@@ -479,38 +492,34 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IAs
2014-11-28 02:17:45 +01:00
this.r.setServerInfo(new ServerPingServerData("1.8", 47));
this.a(this.r);
2014-07-21 22:46:54 +02:00
+ // Spigot start
+ Arrays.fill( recentTps, 20 );
+ long lastTick = System.nanoTime(), catchupTime = 0, curTime, wait, tickSection = lastTick;
while (this.isRunning) {
2014-11-28 02:17:45 +01:00
- long j = ax();
- long k = j - this.ab;
2014-07-21 22:46:54 +02:00
-
2014-11-28 02:17:45 +01:00
- if (k > 2000L && this.ab - this.R >= 15000L) {
- if (server.getWarnOnOverload()) // CraftBukkit
- MinecraftServer.LOGGER.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(k), Long.valueOf(k / 50L)});
- k = 2000L;
- this.R = this.ab;
2014-07-21 22:46:54 +02:00
- }
-
2014-11-28 02:17:45 +01:00
- if (k < 0L) {
- MinecraftServer.LOGGER.warn("Time ran backwards! Did the system time change?");
- k = 0L;
2014-07-21 22:46:54 +02: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));
}
2014-11-28 02:17:45 +01:00
- i += k;
- this.ab = j;
2014-07-21 22:46:54 +02:00
- if (this.worlds.get(0).everyoneDeeplySleeping()) { // CraftBukkit
2014-11-28 02:17:45 +01:00
- this.y();
- i = 0L;
2014-07-21 22:46:54 +02:00
- } else {
2014-11-28 02:17:45 +01:00
- while (i > 50L) {
2014-07-21 22:46:54 +02:00
- MinecraftServer.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
2014-11-28 02:17:45 +01:00
- i -= 50L;
- this.y();
2014-07-21 22:46:54 +02: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;
2014-11-28 02:17:45 +01:00
- Thread.sleep(Math.max(1L, 50L - i));
+ this.y();
this.Q = true;
2014-07-21 22:46:54 +02: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
2014-11-28 02:17:45 +01:00
index 9566cfa..345a49c 100644
2014-07-21 22:46:54 +02:00
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
2014-07-30 10:35:19 +02:00
@@ -261,4 +261,9 @@ public class SpigotConfig
2014-07-21 22:46:54 +02: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
new file mode 100644
2014-11-28 02:17:45 +01:00
index 0000000..be2e31d
2014-07-21 22:46:54 +02: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;
2014-11-28 02:17:45 +01:00
+import com.google.common.collect.Iterables;
2014-07-21 22:46:54 +02: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 );
+ }
+}
--
2014-11-28 02:17:45 +01:00
2.1.0
2014-07-21 22:46:54 +02:00