0
0
mirror of https://hub.spigotmc.org/stash/scm/spigot/spigot.git synced 2024-11-21 20:56:14 +00:00
spigot/CraftBukkit-Patches/0032-Highly-Optimized-Tick-Loop.patch
2024-10-23 02:15:00 +11:00

134 lines
5.2 KiB
Diff

From 7c0b9bdeac66a6983356307bffb7d5bf6ef936db Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Thu, 26 Jan 2017 21:50:51 +0000
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
index 3ac965c79b..f79019f774 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -316,6 +316,12 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
public CommandDispatcher vanillaCommandDispatcher;
private boolean forceTicks;
// CraftBukkit end
+ // Spigot start
+ public static final int TPS = 20;
+ public static final int TICK_TIME = 1000000000 / TPS;
+ private static final int SAMPLE_INTERVAL = 100;
+ public final double[] recentTps = new double[ 3 ];
+ // Spigot end
public static <S extends MinecraftServer> S spin(Function<Thread, S> function) {
AtomicReference<S> atomicreference = new AtomicReference();
@@ -1008,6 +1014,13 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
}
+ // Spigot Start
+ private static double calcTps(double avg, double exp, double tps)
+ {
+ return ( avg * exp ) + ( tps * ( 1 - exp ) );
+ }
+ // Spigot End
+
protected void runServer() {
try {
if (!this.initServer()) {
@@ -1018,6 +1031,9 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
this.statusIcon = (ServerPing.a) this.loadStatusIcon().orElse(null); // CraftBukkit - decompile error
this.status = this.buildServerStatus();
+ // Spigot start
+ Arrays.fill( recentTps, 20 );
+ long tickSection = SystemUtils.getMillis(), tickCount = 1;
while (this.running) {
long i;
@@ -1038,6 +1054,17 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
this.lastOverloadWarningNanos = this.nextTickTimeNanos;
}
}
+ // Spigot start
+ if ( tickCount++ % SAMPLE_INTERVAL == 0 )
+ {
+ long curTime = SystemUtils.getMillis();
+ double currentTps = 1E3 / ( 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;
+ }
+ // Spigot end
boolean flag = i == 0L;
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
index db04e7418d..cf09bd207b 100644
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
@@ -266,4 +266,9 @@ public class SpigotConfig
}
}
}
+
+ 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
index 0000000000..f5b6dec1cb
--- /dev/null
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
@@ -0,0 +1,45 @@
+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)");
+
+ 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.47.0